diff --git a/pkg/interface/src/logic/api/hark.ts b/pkg/interface/src/logic/api/hark.ts index c84b31f40..a4b5455bf 100644 --- a/pkg/interface/src/logic/api/hark.ts +++ b/pkg/interface/src/logic/api/hark.ts @@ -2,6 +2,7 @@ import BaseApi from "./base"; import { StoreState } from "../store/type"; import { dateToDa, decToUd } from "../lib/util"; import {NotifIndex} from "~/types"; +import { BigInteger } from 'big-integer'; export class HarkApi extends BaseApi { private harkAction(action: any): Promise { @@ -15,6 +16,10 @@ export class HarkApi extends BaseApi { private groupHookAction(action: any) { return this.action("hark-group-hook", "hark-group-hook-action", action); } + + private chatHookAction(action: any) { + return this.action("hark-chat-hook", "hark-chat-hook-action", action); + } private actOnNotification(frond: string, intTime: BigInteger, index: NotifIndex) { const time = decToUd(intTime.toString()); @@ -26,10 +31,6 @@ export class HarkApi extends BaseApi { }); } - private graphHookAction(action: any) { - return this.action("hark-graph-hook", "hark-graph-hook-action", action); - } - setMentions(mentions: boolean) { return this.graphHookAction({ 'set-mentions': mentions @@ -73,6 +74,9 @@ export class HarkApi extends BaseApi { const { group } = index.group; return this.ignoreGroup(group); } + if('chat' in index) { + return this.ignoreChat(index.chat); + } return Promise.resolve(); } @@ -83,6 +87,9 @@ export class HarkApi extends BaseApi { if('group' in index) { return this.listenGroup(index.group.group); } + if('chat' in index) { + return this.listenChat(index.chat); + } return Promise.resolve(); } @@ -98,6 +105,12 @@ export class HarkApi extends BaseApi { }) } + ignoreChat(chat: string) { + return this.chatHookAction({ + ignore: chat + }); + } + listenGroup(group: string) { return this.groupHookAction({ @@ -111,6 +124,12 @@ export class HarkApi extends BaseApi { }) } + listenChat(chat: string) { + return this.chatHookAction({ + listen: chat + }); + } + async getTimeSubset(start?: Date, end?: Date) { const s = start ? dateToDa(start) : "-"; const e = end ? dateToDa(end) : "-"; diff --git a/pkg/interface/src/logic/reducers/hark-update.ts b/pkg/interface/src/logic/reducers/hark-update.ts index 162752216..2aba37ad9 100644 --- a/pkg/interface/src/logic/reducers/hark-update.ts +++ b/pkg/interface/src/logic/reducers/hark-update.ts @@ -1,6 +1,5 @@ import { Notifications, - Notification, NotifIndex, NotificationGraphConfig, GroupNotificationsConfig, @@ -14,6 +13,7 @@ type HarkState = { notificationsCount: number; notificationsGraphConfig: NotificationGraphConfig; notificationsGroupConfig: GroupNotificationsConfig; + notificationsChatConfig: string[]; }; export const HarkReducer = (json: any, state: HarkState) => { @@ -36,8 +36,39 @@ export const HarkReducer = (json: any, state: HarkState) => { groupListen(groupHookData, state); groupIgnore(groupHookData, state); } + + const chatHookData = _.get(json, "hark-chat-hook-update", false); + if(chatHookData) { + + chatInitial(chatHookData, state); + chatListen(chatHookData, state); + chatIgnore(chatHookData, state); + + } }; +function chatInitial(json: any, state: HarkState) { + const data = _.get(json, "initial", false); + if (data) { + state.notificationsChatConfig = data; + } +} + + +function chatListen(json: any, state: HarkState) { + const data = _.get(json, "listen", false); + if (data) { + state.notificationsChatConfig = [...state.notificationsChatConfig, data]; + } +} + +function chatIgnore(json: any, state: HarkState) { + const data = _.get(json, "ignore", false); + if (data) { + state.notificationsChatConfig = state.notificationsChatConfig.filter(x => x !== data); + } +} + function groupInitial(json: any, state: HarkState) { const data = _.get(json, "initial", false); if (data) { @@ -177,6 +208,8 @@ function notifIdxEqual(a: NotifIndex, b: NotifIndex) { a.group.group === b.group.group && a.group.description === b.group.description ); + } else if ("chat" in a && "chat" in b) { + return a.chat === b.chat; } return false; } diff --git a/pkg/interface/src/logic/store/store.ts b/pkg/interface/src/logic/store/store.ts index 2d042c4b1..90c963709 100644 --- a/pkg/interface/src/logic/store/store.ts +++ b/pkg/interface/src/logic/store/store.ts @@ -100,6 +100,7 @@ export default class GlobalStore extends BaseStore { notifications: new BigIntOrderedMap(), archivedNotifications: new BigIntOrderedMap(), notificationsGroupConfig: [], + notificationsChatConfig: [], notificationsGraphConfig: { watchOnSelf: false, mentions: false, diff --git a/pkg/interface/src/logic/store/type.ts b/pkg/interface/src/logic/store/type.ts index 568bdaef2..75a5c5a7d 100644 --- a/pkg/interface/src/logic/store/type.ts +++ b/pkg/interface/src/logic/store/type.ts @@ -62,6 +62,7 @@ export interface StoreState { notifications: Notifications; notificationsGraphConfig: NotificationGraphConfig; notificationsGroupConfig: GroupNotificationsConfig; + notificationsChatConfig: string[]; notificationsCount: number, doNotDisturb: boolean; } diff --git a/pkg/interface/src/logic/subscription/global.ts b/pkg/interface/src/logic/subscription/global.ts index 0192d8c73..4e6d6e1e9 100644 --- a/pkg/interface/src/logic/subscription/global.ts +++ b/pkg/interface/src/logic/subscription/global.ts @@ -54,6 +54,7 @@ export default class GlobalSubscription extends BaseSubscription { this.subscribe('/updates', 'hark-store'); this.subscribe('/updates', 'hark-graph-hook'); this.subscribe('/updates', 'hark-group-hook'); + this.subscribe('/updates', 'hark-chat-hook'); } restart() { diff --git a/pkg/interface/src/types/hark-update.ts b/pkg/interface/src/types/hark-update.ts index 66f02f570..5549a0538 100644 --- a/pkg/interface/src/types/hark-update.ts +++ b/pkg/interface/src/types/hark-update.ts @@ -2,6 +2,7 @@ import _ from "lodash"; import { Post } from "./graph-update"; import { GroupUpdate } from "./group-update"; import { BigIntOrderedMap } from "~/logic/lib/BigIntOrderedMap"; +import { Envelope } from './chat-update'; type GraphNotifDescription = "link" | "comment"; @@ -17,17 +18,23 @@ export interface GroupNotifIndex { description: string; } +export type ChatNotifIndex = string; + export type NotifIndex = | { graph: GraphNotifIndex } - | { group: GroupNotifIndex }; + | { group: GroupNotifIndex } + | { chat: ChatNotifIndex }; export type GraphNotificationContents = Post[]; export type GroupNotificationContents = GroupUpdate[]; +export type ChatNotificationContents = Envelope[]; + export type NotificationContents = | { graph: GraphNotificationContents } - | { group: GroupNotificationContents }; + | { group: GroupNotificationContents } + | { chat: ChatNotificationContents }; interface Notification { read: boolean;