DEV: Plugin API to add to document title count (#14449)

DEV: Plugin API to add to document title count (#14449)

diff --git a/app/assets/javascripts/discourse/app/components/d-document.js b/app/assets/javascripts/discourse/app/components/d-document.js
index 8cb0675..76b111a 100644
--- a/app/assets/javascripts/discourse/app/components/d-document.js
+++ b/app/assets/javascripts/discourse/app/components/d-document.js
@@ -6,6 +6,11 @@ import logout from "discourse/lib/logout";
 import { inject as service } from "@ember/service";
 import { setLogoffCallback } from "discourse/lib/ajax";
 
+let pluginCounterFunctions = [];
+export function addPluginDocumentTitleCounter(counterFunction) {
+  pluginCounterFunctions.push(counterFunction);
+}
+
 export default Component.extend({
   tagName: "",
   documentTitle: service(),
@@ -44,10 +49,11 @@ export default Component.extend({
       return;
     }
 
-    this.documentTitle.updateNotificationCount(
+    const count =
+      pluginCounterFunctions.reduce((sum, fn) => sum + fn(), 0) +
       this.currentUser.unread_notifications +
-        this.currentUser.unread_high_priority_notifications
-    );
+      this.currentUser.unread_high_priority_notifications;
+    this.documentTitle.updateNotificationCount(count);
   },
 
   @bind
diff --git a/app/assets/javascripts/discourse/app/lib/plugin-api.js b/app/assets/javascripts/discourse/app/lib/plugin-api.js
index 783b14e..2caaf9b 100644
--- a/app/assets/javascripts/discourse/app/lib/plugin-api.js
+++ b/app/assets/javascripts/discourse/app/lib/plugin-api.js
@@ -50,6 +50,7 @@ import { addFeaturedLinkMetaDecorator } from "discourse/lib/render-topic-feature
 import { addGTMPageChangedCallback } from "discourse/lib/page-tracker";
 import { addGlobalNotice } from "discourse/components/global-notice";
 import { addNavItem } from "discourse/models/nav-item";
+import { addPluginDocumentTitleCounter } from "discourse/components/d-document";
 import { addPluginOutletDecorator } from "discourse/components/plugin-connector";
 import { addPluginReviewableParam } from "discourse/components/reviewable-item";
 import { addPopupMenuOptionsCallback } from "discourse/controllers/composer";
@@ -1242,6 +1243,18 @@ class PluginApi {
   }
 
   /**
+   * Used for modifying the document title count. The core count is unread notifications, and
+   * the returned value from calling the passed in function will be added to this number.
+   *
+   * For example, to add a count
+   * api.addDocumentTitleCounter(() => {
+   *   return currentUser.somePluginValue;
+   * })
+   **/
+  addDocumentTitleCounter(counterFunction) {
+    addPluginDocumentTitleCounter(counterFunction);
+  }
+  /**
    * Used for decorating the rendered HTML content of a plugin-outlet after it's been rendered
    *
    * `callback` will be called when it is time to decorate it.

GitHub sha: ba49eaccfe28bf6db1a963b045a1bcf66a45f99b

This commit appears in #14449 which was approved by pmusaraj. It was merged by markvanlan.