shrub/pkg/npm/api/hark/lib.ts

155 lines
3.0 KiB
TypeScript
Raw Normal View History

import { BigInteger } from 'big-integer';
2021-02-25 06:54:00 +03:00
import { Poke } from '../lib/types';
import {
HarkBin,
HarkBinId,
2021-09-17 03:49:31 +03:00
HarkLid,
2021-09-06 07:29:43 +03:00
HarkPlace
} from './types';
2021-02-25 06:54:00 +03:00
import { decToUd } from '../lib';
export const harkAction = <T>(data: T): Poke<T> => ({
app: 'hark-store',
mark: 'hark-action',
json: data
});
const graphHookAction = <T>(data: T): Poke<T> => ({
app: 'hark-graph-hook',
mark: 'hark-graph-hook-action',
json: data
});
export { graphHookAction as harkGraphHookAction };
const groupHookAction = <T>(data: T): Poke<T> => ({
app: 'hark-group-hook',
mark: 'hark-group-hook-action',
json: data
});
export { groupHookAction as harkGroupHookAction };
export const actOnNotification = (
frond: string,
intTime: BigInteger,
2021-09-06 07:29:43 +03:00
bin: HarkBin
): Poke<unknown> =>
harkAction({
[frond]: {
time: decToUd(intTime.toString()),
2021-09-06 07:29:43 +03:00
bin
}
});
2021-02-25 06:54:00 +03:00
export const setMentions = (mentions: boolean): Poke<unknown> =>
graphHookAction({
'set-mentions': mentions
});
2021-02-25 06:54:00 +03:00
export const setWatchOnSelf = (watchSelf: boolean): Poke<unknown> =>
graphHookAction({
'set-watch-on-self': watchSelf
});
2021-02-25 06:54:00 +03:00
export const setDoNotDisturb = (dnd: boolean): Poke<unknown> =>
harkAction({
'set-dnd': dnd
});
2021-02-25 06:54:00 +03:00
2021-09-17 03:49:31 +03:00
export const archive = (bin: HarkBin, lid: HarkLid): Poke<unknown> =>
harkAction({
archive: {
2021-09-17 03:49:31 +03:00
lid,
bin
}
});
2021-02-25 06:54:00 +03:00
2021-09-17 03:49:31 +03:00
export const opened = harkAction({
opened: null
});
export const markCountAsRead = (place: HarkPlace): Poke<unknown> =>
harkAction({
'read-count': place
});
2021-02-25 06:54:00 +03:00
export const markEachAsRead = (
place: HarkPlace,
path: string
): Poke<unknown> =>
harkAction({
'read-each': {
place,
path
}
});
2021-02-25 06:54:00 +03:00
export const seen = () => harkAction({ seen: null });
export const readAll = harkAction({ 'read-all': null });
export const archiveAll = harkAction({ 'archive-all': null });
2021-02-25 06:54:00 +03:00
export const ignoreGroup = (group: string): Poke<unknown> =>
groupHookAction({
ignore: group
});
2021-02-25 06:54:00 +03:00
export const ignoreGraph = (graph: string, index: string): Poke<unknown> =>
graphHookAction({
ignore: {
graph,
index
2021-02-25 06:54:00 +03:00
}
});
2021-02-25 06:54:00 +03:00
export const listenGroup = (group: string): Poke<unknown> =>
groupHookAction({
listen: group
});
2021-02-25 06:54:00 +03:00
export const listenGraph = (graph: string, index: string): Poke<unknown> =>
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];
}