瀏覽代碼

Combine two versions of isOnlyChatStateNotification into one

Also fix a bug in the code dermining whether an XMLElement stanza is only a chat state notification
JC Brand 6 年之前
父節點
當前提交
d90bcd5274
共有 2 個文件被更改,包括 15 次插入18 次删除
  1. 2 14
      src/converse-notification.js
  2. 13 4
      src/headless/utils/core.js

+ 2 - 14
src/converse-notification.js

@@ -38,17 +38,6 @@ converse.plugins.add('converse-notification', {
             notification_delay: 5000
         });
 
-        _converse.isOnlyChatStateNotification = (msg) =>
-            // See XEP-0085 Chat State Notification
-            _.isNull(msg.querySelector('body')) && (
-                    _.isNull(msg.querySelector(_converse.ACTIVE)) ||
-                    _.isNull(msg.querySelector(_converse.COMPOSING)) ||
-                    _.isNull(msg.querySelector(_converse.INACTIVE)) ||
-                    _.isNull(msg.querySelector(_converse.PAUSED)) ||
-                    _.isNull(msg.querySelector(_converse.GONE))
-                )
-        ;
-
         _converse.shouldNotifyOfGroupMessage = function (message) {
             /* Is this a group message worthy of notification?
              */
@@ -96,9 +85,8 @@ converse.plugins.add('converse-notification', {
                 // We want to show notifications for headline messages.
                 return _converse.isMessageToHiddenChat(message);
             }
-            const is_me = Strophe.getBareJidFromJid(
-                    message.getAttribute('from')) === _converse.bare_jid;
-            return !_converse.isOnlyChatStateNotification(message) &&
+            const is_me = Strophe.getBareJidFromJid(message.getAttribute('from')) === _converse.bare_jid;
+            return !u.isOnlyChatStateNotification(message) &&
                 !is_me &&
                 (_converse.show_desktop_notifications === 'all' || _converse.isMessageToHiddenChat(message));
         };

+ 13 - 4
src/headless/utils/core.js

@@ -128,11 +128,20 @@ u.isEmptyMessage = function (attrs) {
         !attrs['message'];
 };
 
-u.isOnlyChatStateNotification = function (attrs) {
-    if (attrs instanceof Backbone.Model) {
-        attrs = attrs.attributes;
+u.isOnlyChatStateNotification = function (msg) {
+    if (msg instanceof Element) {
+        // See XEP-0085 Chat State Notification
+        return (msg.querySelector('body') === null) && (
+                    (msg.querySelector('active') !== null) ||
+                    (msg.querySelector('composing') !== null) ||
+                    (msg.querySelector('inactive') !== null) ||
+                    (msg.querySelector('paused') !== null) ||
+                    (msg.querySelector('gone') !== null));
+    }
+    if (msg instanceof Backbone.Model) {
+        msg = msg.attributes;
     }
-    return attrs['chat_state'] && u.isEmptyMessage(attrs);
+    return msg['chat_state'] && u.isEmptyMessage(msg);
 };
 
 u.isHeadlineMessage = function (_converse, message) {