Procházet zdrojové kódy

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 před 7 roky
rodič
revize
c26ccf5e01
3 změnil soubory, kde provedl 32 přidání a 15 odebrání
  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)
   the roster SHOULD be queried, making the client an "interested resource".
   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
 - #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('HINTS', 'urn:xmpp:hints');
     Strophe.addNamespace('MAM', 'urn:xmpp:mam:2');
+    Strophe.addNamespace('SID', 'urn:xmpp:sid:0');
     Strophe.addNamespace('NICK', 'http://jabber.org/protocol/nick');
     Strophe.addNamespace('PUBSUB', 'http://jabber.org/protocol/pubsub');
     Strophe.addNamespace('ROSTERX', 'http://jabber.org/protocol/rosterx');

+ 29 - 15
src/converse-mam.js

@@ -9,14 +9,14 @@
 // XEP-0059 Result Set Management
 
 (function (root, factory) {
-    define(["jquery.noconflict",
+    define(["sizzle",
             "converse-core",
             "converse-disco",
             "converse-chatview", // Could be made a soft dependency
             "converse-muc", // Could be made a soft dependency
             "strophe.rsm"
     ], factory);
-}(this, function ($, converse) {
+}(this, function (sizzle, converse, utils) {
     "use strict";
     const { Promise, Strophe, $iq, _, moment } = converse.env;
 
@@ -24,6 +24,7 @@
     // XEP-0313 Message Archive Management
     const MAM_ATTRIBUTES = ['with', 'start', 'end'];
 
+
     converse.plugins.add('converse-mam', {
 
         overrides: {
@@ -35,7 +36,10 @@
             ChatBox: {
                 getMessageAttributes ($message, $delay, original_stanza) {
                     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;
                 }
             },
@@ -122,10 +126,18 @@
 
                 onScroll (ev) {
                     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
                      * 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 this.__super__.handleMUCMessage.apply(this, arguments);
@@ -304,7 +315,7 @@
             };
 
             _converse.onMAMError = function (iq) {
-                if ($(iq).find('feature-not-implemented').length) {
+                if (iq.querySelectorAll('feature-not-implemented').length) {
                     _converse.log(
                         "Message Archive Management (XEP-0313) not supported by this server",
                         Strophe.LogLevel.WARN);
@@ -327,12 +338,15 @@
                  * Per JID preferences will be set in chat boxes, so it'll
                  * 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) {
-                    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();
                     });
                     _converse.connection.sendIQ(stanza, _.partial(function (feature, iq) {