From 53475cf5be0cd57570264b82aafa6ce4a6132407 Mon Sep 17 00:00:00 2001 From: Mark VanLandingham Date: Mon, 13 Dec 2021 11:54:46 -0600 Subject: [PATCH] DEV: Plugin API to add desktop notification handlers (#15280) --- .../discourse/app/lib/desktop-notifications.js | 13 ++++++++++++- .../javascripts/discourse/app/lib/plugin-api.js | 14 ++++++++++++++ .../discourse/tests/helpers/qunit-helpers.js | 2 ++ 3 files changed, 28 insertions(+), 1 deletion(-) diff --git a/app/assets/javascripts/discourse/app/lib/desktop-notifications.js b/app/assets/javascripts/discourse/app/lib/desktop-notifications.js index 4fb5c91dc7..7667e44b19 100644 --- a/app/assets/javascripts/discourse/app/lib/desktop-notifications.js +++ b/app/assets/javascripts/discourse/app/lib/desktop-notifications.js @@ -19,6 +19,14 @@ const idleThresholdTime = 1000 * 10; // 10 seconds const context = "discourse_desktop_notifications_"; const keyValueStore = new KeyValueStore(context); +let desktopNotificationHandlers = []; +export function registerDesktopNotificationHandler(handler) { + desktopNotificationHandlers.push(handler); +} +export function clearDesktopNotificationHandlers() { + desktopNotificationHandlers = []; +} + // Called from an initializer function init(messageBus, appEvents) { liveEnabled = false; @@ -176,11 +184,14 @@ function onNotification(data, siteSettings, user) { icon: notificationIcon, tag: notificationTag, }); - notification.onclick = () => { DiscourseURL.routeTo(data.post_url); notification.close(); }; + + desktopNotificationHandlers.forEach((handler) => + handler(data, siteSettings, user) + ); }); } diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js index 12f1b900c2..5c9b68eb5f 100644 --- a/app/assets/javascripts/discourse/app/lib/plugin-api.js +++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js @@ -81,6 +81,7 @@ import { registerCustomPostMessageCallback as registerCustomPostMessageCallback1 import { registerHighlightJSLanguage } from "discourse/lib/highlight-syntax"; import { registerTopicFooterButton } from "discourse/lib/register-topic-footer-button"; import { registerTopicFooterDropdown } from "discourse/lib/register-topic-footer-dropdown"; +import { registerDesktopNotificationHandler } from "discourse/lib/desktop-notifications"; import { replaceFormatter } from "discourse/lib/utilities"; import { replaceTagRenderer } from "discourse/lib/render-tag"; import { setNewCategoryDefaultColors } from "discourse/routes/new-category"; @@ -775,6 +776,19 @@ class PluginApi { registerTopicFooterDropdown(dropdownOptions); } + /** + * Register a desktop notificaiton handler + * + * ```javascript + * api.registerDesktopNotificationHandler((data, siteSettings, user) => { + * // Do something! + * }); + * ``` + **/ + registerDesktopNotificationHandler(handler) { + registerDesktopNotificationHandler(handler); + } + /** * Register a small icon to be used for custom small post actions * diff --git a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js index ed745f5dee..245f318e2b 100644 --- a/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js +++ b/app/assets/javascripts/discourse/tests/helpers/qunit-helpers.js @@ -53,6 +53,7 @@ import { resetLastEditNotificationClick } from "discourse/models/post-stream"; import { clearAuthMethods } from "discourse/models/login-method"; import { clearTopicFooterDropdowns } from "discourse/lib/register-topic-footer-dropdown"; import { clearTopicFooterButtons } from "discourse/lib/register-topic-footer-button"; +import { clearDesktopNotificationHandlers } from "discourse/lib/desktop-notifications"; import { clearPresenceCallbacks, setTestPresence, @@ -297,6 +298,7 @@ export function acceptance(name, optionsOrCallback) { cleanUpComposerUploadPreProcessor(); clearTopicFooterDropdowns(); clearTopicFooterButtons(); + clearDesktopNotificationHandlers(); resetLastEditNotificationClick(); clearAuthMethods(); setTestPresence(true);