Explorar el Código

`onMessage` improvements.

Don't check for older messages with same msgid
we can't rely on a message's `id` attribute being unique.

Also, remove `createMessage` in favour of calling `messages.create` directly.
JC Brand hace 6 años
padre
commit
2c9c11e971
Se han modificado 2 ficheros con 53 adiciones y 22 borrados
  1. 7 14
      src/headless/converse-chatboxes.js
  2. 46 8
      src/headless/converse-muc.js

+ 7 - 14
src/headless/converse-chatboxes.js

@@ -637,19 +637,6 @@ converse.plugins.add('converse-chatboxes', {
                 return attrs;
             },
 
-            async createMessage (stanza, original_stanza) {
-                const msgid = stanza.getAttribute('id'),
-                      message = msgid && this.messages.findWhere({msgid});
-                if (!message) {
-                    // Only create the message when we're sure it's not a duplicate
-                    const attrs = await this.getMessageAttributesFromStanza(stanza, original_stanza);
-                    if (attrs['chat_state'] || !u.isEmptyMessage(attrs)) {
-                        const msg = this.messages.create(attrs);
-                        this.incrementUnreadMsgCounter(msg);
-                    }
-                }
-            },
-
             isHidden () {
                 /* Returns a boolean to indicate whether a newly received
                  * message will be visible to the user or not.
@@ -855,11 +842,17 @@ converse.plugins.add('converse-chatboxes', {
                 const has_body = sizzle(`body, encrypted[xmlns="${Strophe.NS.OMEMO}"]`, stanza).length > 0,
                       chatbox_attrs = {'fullname': _.get(_converse.api.contacts.get(contact_jid), 'attributes.fullname')},
                       chatbox = this.getChatBox(contact_jid, chatbox_attrs, has_body);
+
                 if (chatbox &&
                         !chatbox.handleMessageCorrection(stanza) &&
                         !chatbox.handleReceipt (stanza, from_jid, is_carbon, is_me) &&
                         !chatbox.handleChatMarker(stanza, from_jid, is_carbon)) {
-                    await chatbox.createMessage(stanza, original_stanza);
+
+                    const attrs = await chatbox.getMessageAttributesFromStanza(stanza, original_stanza);
+                    if (attrs['chat_state'] || !u.isEmptyMessage(attrs)) {
+                        const msg = chatbox.messages.create(attrs);
+                        chatbox.incrementUnreadMsgCounter(msg);
+                    }
                 }
                 _converse.emit('message', {'stanza': original_stanza, 'chatbox': chatbox});
             },

+ 46 - 8
src/headless/converse-muc.js

@@ -989,7 +989,35 @@ converse.plugins.add('converse-muc', {
                      acknowledged[xmlns="${Strophe.NS.MARKERS}"]`, stanza).length > 0;
             },
 
+            reflectionHandled (stanza) {
+                /* Handle a MUC reflected message and return true if so.
+                 *
+                 * Parameters:
+                 *  (XMLElement) stanza: The message stanza
+                 */
+                const from = stanza.getAttribute('from');
+                const own_message = Strophe.getResourceFromJid(from) == this.get('nick');
+                if (own_message) {
+                    const msgid = stanza.getAttribute('id'),
+                          jid = stanza.getAttribute('from');
+
+                    // TODO: use stanza-id?
+                    if (msgid) {
+                        const msg = this.messages.findWhere({'msgid': msgid, 'from': jid});
+                        if (msg && msg.get('sender') === 'me' && !msg.get('received')) {
+                            msg.save({'received': moment().format()});
+                            return true;
+                        }
+                    }
+                }
+            },
+
             subjectChangeHandled (attrs) {
+                /* Handle a subject change and return `true` if so.
+                 *
+                 * Parameters:
+                 *  (Object) attrs: The message attributes
+                 */
                 if (attrs.subject && !attrs.thread && !attrs.message) {
                     // https://xmpp.org/extensions/xep-0045.html#subject-mod
                     // -----------------------------------------------------
@@ -1002,6 +1030,18 @@ converse.plugins.add('converse-muc', {
                 return false;
             },
 
+            ignorableCSN (attrs) {
+                /* Is this a chat state notification that can be ignored,
+                 * because it's old or because it's from us.
+                 *
+                 * Parameters:
+                 *  (Object) attrs: The message attributes
+                 */
+                const is_csn = u.isOnlyChatStateNotification(attrs),
+                        own_message = Strophe.getResourceFromJid(attrs.from) == this.get('nick');
+                return is_csn && (attrs.is_delayed || own_message);
+            },
+
             async onMessage (stanza) {
                 /* Handler for all MUC messages sent to this groupchat.
                  *
@@ -1025,15 +1065,13 @@ converse.plugins.add('converse-muc', {
                 if (!this.handleMessageCorrection(stanza) &&
                     !this.isReceipt(stanza) &&
                     !this.isChatMarker(stanza) &&
-                    !this.subjectChangeHandled(attrs)) {
+                    !this.reflectionHandled(stanza) &&
+                    !this.subjectChangeHandled(attrs) &&
+                    !this.ignorableCSN(attrs) &&
+                    (attrs['chat_state'] || !u.isEmptyMessage(attrs))) {
 
-                    const is_csn = u.isOnlyChatStateNotification(attrs),
-                          own_message = Strophe.getResourceFromJid(attrs.from) == this.get('nick');
-                    if (is_csn && (attrs.is_delayed || own_message)) {
-                        // No need showing delayed or our own CSN messages
-                        return;
-                    }
-                    const msg = await this.createMessage(stanza, original_stanza);
+                    const msg = this.messages.create(attrs);
+                    this.incrementUnreadMsgCounter(msg);
                     if (forwarded && msg && msg.get('sender')  === 'me') {
                         msg.save({'received': moment().format()});
                     }