Browse Source

Remove jQuery as dependency for `converse-mam`.

Also, if a message has no `archive_id` attribute, then do a time-based MAM query.
JC Brand 7 years ago
parent
commit
c26ccf5e01
3 changed files with 32 additions and 15 deletions
  1. 2 0
      CHANGES.md
  2. 1 0
      src/converse-core.js
  3. 29 15
      src/converse-mam.js

+ 2 - 0
CHANGES.md

@@ -13,6 +13,8 @@
   and therefore as per [RFC-6121](https://tools.ietf.org/html/rfc6121#section-2.1.6)
   and therefore as per [RFC-6121](https://tools.ietf.org/html/rfc6121#section-2.1.6)
   the roster SHOULD be queried, making the client an "interested resource".
   the roster SHOULD be queried, making the client an "interested resource".
   Otherwise connected contacts might not get your presence updates.
   Otherwise connected contacts might not get your presence updates.
+- The way the archive ID of a MAM message is specified, has changed.
+  See https://xmpp.org/extensions/xep-0313.html#archives_id
 
 
 ### New Features
 ### New Features
 - #314 Add support for opening chat rooms with a URL fragment such as `#converse/room?jid=room@domain`
 - #314 Add support for opening chat rooms with a URL fragment such as `#converse/room?jid=room@domain`

+ 1 - 0
src/converse-core.js

@@ -38,6 +38,7 @@
     Strophe.addNamespace('DELAY', 'urn:xmpp:delay');
     Strophe.addNamespace('DELAY', 'urn:xmpp:delay');
     Strophe.addNamespace('HINTS', 'urn:xmpp:hints');
     Strophe.addNamespace('HINTS', 'urn:xmpp:hints');
     Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
     Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
+    Strophe.addNamespace('SID', 'urn:xmpp:sid:0');
     Strophe.addNamespace('NICK', 'http://jabber.org/protocol/nick');
     Strophe.addNamespace('NICK', 'http://jabber.org/protocol/nick');
     Strophe.addNamespace('PUBSUB', 'http://jabber.org/protocol/pubsub');
     Strophe.addNamespace('PUBSUB', 'http://jabber.org/protocol/pubsub');
     Strophe.addNamespace('ROSTERX', 'http://jabber.org/protocol/rosterx');
     Strophe.addNamespace('ROSTERX', 'http://jabber.org/protocol/rosterx');

+ 29 - 15
src/converse-mam.js

@@ -9,14 +9,14 @@
 // XEP-0059 Result Set Management
 // XEP-0059 Result Set Management
 
 
 (function (root, factory) {
 (function (root, factory) {
-    define(["jquery.noconflict",
+    define(["sizzle",
             "converse-core",
             "converse-core",
             "converse-disco",
             "converse-disco",
             "converse-chatview", // Could be made a soft dependency
             "converse-chatview", // Could be made a soft dependency
             "converse-muc", // Could be made a soft dependency
             "converse-muc", // Could be made a soft dependency
             "strophe.rsm"
             "strophe.rsm"
     ], factory);
     ], factory);
-}(this, function ($, converse) {
+}(this, function (sizzle, converse, utils) {
     "use strict";
     "use strict";
     const { Promise, Strophe, $iq, _, moment } = converse.env;
     const { Promise, Strophe, $iq, _, moment } = converse.env;
 
 
@@ -24,6 +24,7 @@
     // XEP-0313 Message Archive Management
     // XEP-0313 Message Archive Management
     const MAM_ATTRIBUTES = ['with', 'start', 'end'];
     const MAM_ATTRIBUTES = ['with', 'start', 'end'];
 
 
+
     converse.plugins.add('converse-mam', {
     converse.plugins.add('converse-mam', {
 
 
         overrides: {
         overrides: {
@@ -35,7 +36,10 @@
             ChatBox: {
             ChatBox: {
                 getMessageAttributes ($message, $delay, original_stanza) {
                 getMessageAttributes ($message, $delay, original_stanza) {
                     const attrs = this.__super__.getMessageAttributes.apply(this, arguments);
                     const attrs = this.__super__.getMessageAttributes.apply(this, arguments);
-                    attrs.archive_id = $(original_stanza).find(`result[xmlns="${Strophe.NS.MAM}"]`).attr('id');
+                    const result = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, original_stanza).pop();
+                    if (!_.isUndefined(result)) {
+                        attrs.archive_id =  result.getAttribute('id');
+                    }
                     return attrs;
                     return attrs;
                 }
                 }
             },
             },
@@ -122,10 +126,18 @@
 
 
                 onScroll (ev) {
                 onScroll (ev) {
                     const { _converse } = this.__super__;
                     const { _converse } = this.__super__;
-                    if ($(ev.target).scrollTop() === 0 && this.model.messages.length) {
-                        this.fetchArchivedMessages({
-                            'before': this.model.messages.at(0).get('archive_id')
-                        });
+                    if (ev.target.scrollTop === 0 && this.model.messages.length) {
+                        const oldest_message = this.model.messages.at(0);
+                        const archive_id = oldest_message.get('archive_id');
+                        if (archive_id) {
+                            this.fetchArchivedMessages({
+                                'before': archive_id
+                            });
+                        } else {
+                            this.fetchArchivedMessages({
+                                'end': oldest_message.get('time')
+                            });
+                        }
                     }
                     }
                 },
                 },
             },
             },
@@ -151,8 +163,7 @@
                     /* MAM (message archive management XEP-0313) messages are
                     /* MAM (message archive management XEP-0313) messages are
                      * ignored, since they're handled separately.
                      * ignored, since they're handled separately.
                      */
                      */
-                    const is_mam = $(stanza).find(`[xmlns="${Strophe.NS.MAM}"]`).length > 0;
-                    if (is_mam) {
+                    if (sizzle(`[xmlns="${Strophe.NS.MAM}"]`, stanza).length > 0) {
                         return true;
                         return true;
                     }
                     }
                     return this.__super__.handleMUCMessage.apply(this, arguments);
                     return this.__super__.handleMUCMessage.apply(this, arguments);
@@ -304,7 +315,7 @@
             };
             };
 
 
             _converse.onMAMError = function (iq) {
             _converse.onMAMError = function (iq) {
-                if ($(iq).find('feature-not-implemented').length) {
+                if (iq.querySelectorAll('feature-not-implemented').length) {
                     _converse.log(
                     _converse.log(
                         "Message Archive Management (XEP-0313) not supported by this server",
                         "Message Archive Management (XEP-0313) not supported by this server",
                         Strophe.LogLevel.WARN);
                         Strophe.LogLevel.WARN);
@@ -327,12 +338,15 @@
                  * Per JID preferences will be set in chat boxes, so it'll
                  * Per JID preferences will be set in chat boxes, so it'll
                  * probbaly be handled elsewhere in any case.
                  * probbaly be handled elsewhere in any case.
                  */
                  */
-                const $prefs = $(iq).find(`prefs[xmlns="${Strophe.NS.MAM}"]`);
-                const default_pref = $prefs.attr('default');
-                let stanza;
+                const preference = sizzle(`prefs[xmlns="${Strophe.NS.MAM}"]`, iq).pop();
+                const default_pref = preference.getAttribute('default');
                 if (default_pref !== _converse.message_archiving) {
                 if (default_pref !== _converse.message_archiving) {
-                    stanza = $iq({'type': 'set'}).c('prefs', {'xmlns':Strophe.NS.MAM, 'default':_converse.message_archiving});
-                    $prefs.children().each(function (idx, child) {
+                    const stanza = $iq({'type': 'set'})
+                        .c('prefs', {
+                            'xmlns':Strophe.NS.MAM,
+                            'default':_converse.message_archiving
+                        });
+                    _.each(preference.children, function (child) {
                         stanza.cnode(child).up();
                         stanza.cnode(child).up();
                     });
                     });
                     _converse.connection.sendIQ(stanza, _.partial(function (feature, iq) {
                     _converse.connection.sendIQ(stanza, _.partial(function (feature, iq) {