A platform for community discussion. Free, open, simple.
DEV: Plugin API to add desktop notification handlers (#15280)
Mark VanLandingham53475cf5be0c
diff --git a/app/assets/javascripts/discourse/app/lib/desktop-notifications.js b/app/assets/javascripts/discourse/app/lib/desktop-notifications.jsindex 4fb5c91dc72..7667e44b196 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 secondsconst 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 initializerfunction 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.jsindex 12f1b900c20..5c9b68eb5f4 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 registerCustomPostMessageCallback1import { 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.jsindex ed745f5dee6..245f318e2b6 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);