DEV: Plugin API to add desktop notification handlers (#15280)

This commit is contained in:
Mark VanLandingham 2021-12-13 11:54:46 -06:00 committed by GitHub
parent 08f4edc032
commit 53475cf5be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 1 deletions

View File

@ -19,6 +19,14 @@ const idleThresholdTime = 1000 * 10; // 10 seconds
const context = "discourse_desktop_notifications_"; const context = "discourse_desktop_notifications_";
const keyValueStore = new KeyValueStore(context); const keyValueStore = new KeyValueStore(context);
let desktopNotificationHandlers = [];
export function registerDesktopNotificationHandler(handler) {
desktopNotificationHandlers.push(handler);
}
export function clearDesktopNotificationHandlers() {
desktopNotificationHandlers = [];
}
// Called from an initializer // Called from an initializer
function init(messageBus, appEvents) { function init(messageBus, appEvents) {
liveEnabled = false; liveEnabled = false;
@ -176,11 +184,14 @@ function onNotification(data, siteSettings, user) {
icon: notificationIcon, icon: notificationIcon,
tag: notificationTag, tag: notificationTag,
}); });
notification.onclick = () => { notification.onclick = () => {
DiscourseURL.routeTo(data.post_url); DiscourseURL.routeTo(data.post_url);
notification.close(); notification.close();
}; };
desktopNotificationHandlers.forEach((handler) =>
handler(data, siteSettings, user)
);
}); });
} }

View File

@ -81,6 +81,7 @@ import { registerCustomPostMessageCallback as registerCustomPostMessageCallback1
import { registerHighlightJSLanguage } from "discourse/lib/highlight-syntax"; import { registerHighlightJSLanguage } from "discourse/lib/highlight-syntax";
import { registerTopicFooterButton } from "discourse/lib/register-topic-footer-button"; import { registerTopicFooterButton } from "discourse/lib/register-topic-footer-button";
import { registerTopicFooterDropdown } from "discourse/lib/register-topic-footer-dropdown"; import { registerTopicFooterDropdown } from "discourse/lib/register-topic-footer-dropdown";
import { registerDesktopNotificationHandler } from "discourse/lib/desktop-notifications";
import { replaceFormatter } from "discourse/lib/utilities"; import { replaceFormatter } from "discourse/lib/utilities";
import { replaceTagRenderer } from "discourse/lib/render-tag"; import { replaceTagRenderer } from "discourse/lib/render-tag";
import { setNewCategoryDefaultColors } from "discourse/routes/new-category"; import { setNewCategoryDefaultColors } from "discourse/routes/new-category";
@ -775,6 +776,19 @@ class PluginApi {
registerTopicFooterDropdown(dropdownOptions); 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 * Register a small icon to be used for custom small post actions
* *

View File

@ -53,6 +53,7 @@ import { resetLastEditNotificationClick } from "discourse/models/post-stream";
import { clearAuthMethods } from "discourse/models/login-method"; import { clearAuthMethods } from "discourse/models/login-method";
import { clearTopicFooterDropdowns } from "discourse/lib/register-topic-footer-dropdown"; import { clearTopicFooterDropdowns } from "discourse/lib/register-topic-footer-dropdown";
import { clearTopicFooterButtons } from "discourse/lib/register-topic-footer-button"; import { clearTopicFooterButtons } from "discourse/lib/register-topic-footer-button";
import { clearDesktopNotificationHandlers } from "discourse/lib/desktop-notifications";
import { import {
clearPresenceCallbacks, clearPresenceCallbacks,
setTestPresence, setTestPresence,
@ -297,6 +298,7 @@ export function acceptance(name, optionsOrCallback) {
cleanUpComposerUploadPreProcessor(); cleanUpComposerUploadPreProcessor();
clearTopicFooterDropdowns(); clearTopicFooterDropdowns();
clearTopicFooterButtons(); clearTopicFooterButtons();
clearDesktopNotificationHandlers();
resetLastEditNotificationClick(); resetLastEditNotificationClick();
clearAuthMethods(); clearAuthMethods();
setTestPresence(true); setTestPresence(true);