import { BigInteger } from 'big-integer'; import { Poke } from '../lib/types'; import { HarkBin, HarkBinId, HarkBody, HarkLid, HarkPlace } from './types'; import { decToUd } from '../lib'; export const harkAction = (data: T): Poke => ({ app: 'hark-store', mark: 'hark-action', json: data }); const graphHookAction = (data: T): Poke => ({ app: 'hark-graph-hook', mark: 'hark-graph-hook-action', json: data }); export { graphHookAction as harkGraphHookAction }; const groupHookAction = (data: T): Poke => ({ app: 'hark-group-hook', mark: 'hark-group-hook-action', json: data }); export { groupHookAction as harkGroupHookAction }; export const actOnNotification = ( frond: string, intTime: BigInteger, bin: HarkBin ): Poke => harkAction({ [frond]: { time: decToUd(intTime.toString()), bin } }); export const setMentions = (mentions: boolean): Poke => graphHookAction({ 'set-mentions': mentions }); export const setWatchOnSelf = (watchSelf: boolean): Poke => graphHookAction({ 'set-watch-on-self': watchSelf }); export const setDoNotDisturb = (dnd: boolean): Poke => harkAction({ 'set-dnd': dnd }); export const addNote = (bin: HarkBin, body: HarkBody) => harkAction({ 'add-note': { bin, body } }); export const archive = (bin: HarkBin, lid: HarkLid): Poke => harkAction({ archive: { lid, bin } }); export const opened = harkAction({ opened: null }); export const markCountAsRead = (place: HarkPlace): Poke => harkAction({ 'read-count': place }); export const markEachAsRead = ( place: HarkPlace, path: string ): Poke => harkAction({ 'read-each': { place, path } }); export const seen = () => harkAction({ seen: null }); export const readAll = harkAction({ 'read-all': null }); export const archiveAll = harkAction({ 'archive-all': null }); export const ignoreGroup = (group: string): Poke => groupHookAction({ ignore: group }); export const ignoreGraph = (graph: string, index: string): Poke => graphHookAction({ ignore: { graph, index } }); export const listenGroup = (group: string): Poke => groupHookAction({ listen: group }); export const listenGraph = (graph: string, index: string): Poke => graphHookAction({ listen: { graph, index } }); /** * Read all graphs belonging to a particular group */ export const readGroup = (group: string) => harkAction({ 'read-group': group }); /** * Read all unreads in a graph */ export const readGraph = (graph: string) => harkAction({ 'read-graph': graph }); export function harkBinToId(bin: HarkBin): HarkBinId { const { place, path } = bin; return `${place.desk}${place.path}${path}`; } export function harkBinEq(a: HarkBin, b: HarkBin): boolean { return ( a.place.path === b.place.path && a.place.desk === b.place.desk && a.path === b.path ); } export function harkLidToId(lid: HarkLid): string { if('time' in lid) { return `archive-${lid.time}`; } return Object.keys(lid)[0]; }