ソースを参照

Notifiations: Add new hook `shouldNotifyOfGroupMessage`

JC Brand 4 年 前
コミット
5d1b5ac6c9
2 ファイル変更22 行追加6 行削除
  1. 1 0
      CHANGES.md
  2. 21 6
      src/plugins/notifications/utils.js

+ 1 - 0
CHANGES.md

@@ -9,6 +9,7 @@
 - Bugfix: Connection protocol not updated based on XEP-0156 connection methods
 - Bugfix: `null` inserted by emoji picker and can't switch between skintones
 - New hook: [getMessageActionButtons](https://conversejs.org/docs/html/api/-_converse.html#event:getMessageActionButtons)
+- New hook: [shouldNotifyOfGroupMessage](https://conversejs.org/docs/html/api/-_converse.html#event:shouldNotifyOfGroupMessage)
 - File structure reordering: All plugins are now in `./plugins` folders.
 - New configuration setting: [show_tab_notifications](https://conversejs.org/docs/html/configuration.html#show-tab-notifications)
 - New configuration setting: [muc_clear_messages_on_leave](https://conversejs.org/docs/html/configuration.html#muc-clear-messages-on-leave)

+ 21 - 6
src/plugins/notifications/utils.js

@@ -41,7 +41,7 @@ export function updateUnreadFavicon () {
  * @private
  * @param { MUCMessageAttributes } attrs
  */
-export function shouldNotifyOfGroupMessage (attrs) {
+export async function shouldNotifyOfGroupMessage (attrs) {
     if (!attrs?.body) {
         return false;
     }
@@ -69,7 +69,21 @@ export function shouldNotifyOfGroupMessage (attrs) {
         (Array.isArray(notify_all) && notify_all.includes(muc_jid)) ||
         is_referenced ||
         is_mentioned;
-    return is_not_mine && !!should_notify_user;
+
+    if (is_not_mine && !!should_notify_user) {
+        /**
+         * *Hook* which allows plugins to run further logic to determine
+         * whether a notification should be sent out for this message.
+         * @event _converse#shouldNotifyOfGroupMessage
+         * @example
+         *  api.listen.on('shouldNotifyOfGroupMessage', (should_notify) => {
+         *      return should_notify && flurb === floob;
+         *  });
+         */
+        const should_notify = await api.hook('shouldNotifyOfGroupMessage', null, true);
+        return should_notify;
+    }
+    return false;
 }
 
 /**
@@ -77,13 +91,14 @@ export function shouldNotifyOfGroupMessage (attrs) {
  * @method shouldNotifyOfMessage
  * @param { MessageData|MUCMessageData } data
  */
-function shouldNotifyOfMessage (data) {
+async function shouldNotifyOfMessage (data) {
     const { attrs, stanza } = data;
     if (!attrs || stanza.querySelector('forwarded') !== null) {
         return false;
     }
     if (attrs['type'] === 'groupchat') {
-        return shouldNotifyOfGroupMessage(attrs);
+        const result = await shouldNotifyOfGroupMessage(attrs);
+        return result;
     } else if (attrs.is_headline) {
         // We want to show notifications for headline messages.
         return isMessageToHiddenChat(attrs);
@@ -229,8 +244,8 @@ function playSoundNotification () {
  * Event handler for the on('message') event. Will call methods
  * to play sounds and show HTML5 notifications.
  */
-export function handleMessageNotification (data) {
-    if (!shouldNotifyOfMessage(data)) {
+export async function handleMessageNotification (data) {
+    if (!await shouldNotifyOfMessage(data)) {
         return false;
     }
     /**