From 4022d2e7625c4a205a15b5cbf1593f874216e36b Mon Sep 17 00:00:00 2001 From: Christopher Poile Date: Fri, 8 Sep 2023 08:16:26 -0400 Subject: [PATCH] MM-54443 - Fix: registerDesktopNotificationHook is missing (#23884) (#24497) (cherry picked from commit 46a659e06de17551948b1319fe7013cc489f0e05) --- webapp/channels/src/plugins/registry.ts | 48 +++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/webapp/channels/src/plugins/registry.ts b/webapp/channels/src/plugins/registry.ts index 842036e225..e135b7c85e 100644 --- a/webapp/channels/src/plugins/registry.ts +++ b/webapp/channels/src/plugins/registry.ts @@ -1177,4 +1177,52 @@ export default class PluginRegistry { data, }); }); + + // Register a hook to intercept desktop notifications before they occur. + // Accepts a function to run before the desktop notification is triggered. + // The function has the following signature: + // (post: Post, msgProps: NewPostMessageProps, channel: Channel, + // teamId: string, args: DesktopNotificationArgs) => Promise<{ + // error?: string; + // args?: DesktopNotificationArgs; + // }>) + // + // DesktopNotificationArgs is the following type: + // export type DesktopNotificationArgs = { + // title: string; + // body: string; + // silent: boolean; + // soundName: string; + // url: string; + // notify: boolean; + // }; + // + // To stop a desktop notification and allow subsequent hooks to process the notification, return: + // {args: {...args, notify: false}} + // To enable a desktop notification and allow subsequent hooks to process the notification, return: + // {args: {...args, notify: true}} + // To stop a desktop notification and prevent subsequent hooks from processing the notification, return either: + // {error: 'log this error'}, or {} + // To allow subsequent hooks to process the notification, return: + // {args}, or null or undefined (thanks js) + // + // The args returned by the hook will be used as the args for the next hook, until all hooks are + // completed. The resulting args will be used as the arguments for the `notifyMe` function. + // + // Returns a unique identifier. + registerDesktopNotificationHook = reArg(['hook'], ({hook}) => { + const id = generateId(); + + store.dispatch({ + type: ActionTypes.RECEIVED_PLUGIN_COMPONENT, + name: 'DesktopNotificationHooks', + data: { + id, + pluginId: this.id, + hook, + }, + }); + + return id; + }); }