Prechádzať zdrojové kódy

converse-mam: Don't fetch MAM messages on each page load.

JC Brand 8 rokov pred
rodič
commit
2a81d2e6f3
3 zmenil súbory, kde vykonal 11 pridanie a 6 odobranie
  1. 1 0
      docs/CHANGES.md
  2. 4 1
      src/converse-mam.js
  3. 6 5
      src/converse-muc.js

+ 1 - 0
docs/CHANGES.md

@@ -3,6 +3,7 @@
 ## 2.0.4 (Unreleased)
 - Bugfix. Switching from bookmarks form to config form shows only the spinner. [jcbrand]
 - Bugfix. Other room occupants sometimes not shown when reloading the page. [jcbrand]
+- Optimize fetching of MAM messages (in some cases happened on each page load). [jcbrand]
 
 ## 2.0.3 (2016-11-30)
 - #735 Room configuration button not visible. [jcbrand]

+ 4 - 1
src/converse-mam.js

@@ -69,12 +69,15 @@
                     if (this.disable_mam || !converse.features.findWhere({'var': Strophe.NS.MAM})) {
                         return this.__super__.afterMessagesFetched.apply(this, arguments);
                     }
-                    if (this.model.messages.length < converse.archived_messages_page_size) {
+                    if (!this.model.get('mam_initialized') &&
+                            this.model.messages.length < converse.archived_messages_page_size) {
+
                         this.fetchArchivedMessages({
                             'before': '', // Page backwards from the most recent message
                             'with': this.model.get('jid'),
                             'max': converse.archived_messages_page_size
                         });
+                        this.model.save({'mam_initialized': true});
                     }
                     return this.__super__.afterMessagesFetched.apply(this, arguments);
                 },

+ 6 - 5
src/converse-muc.js

@@ -1583,6 +1583,7 @@
                      * Then, upon receiving them, call onChatRoomMessage
                      * so that they are displayed inside it.
                      */
+                    var that = this;
                     if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
                         converse.log("Attempted to fetch archived messages but this user's server doesn't support XEP-0313");
                         return;
@@ -1590,15 +1591,15 @@
                     this.addSpinner();
                     converse_api.archive.query(_.extend(options, {'groupchat': true}),
                         function (messages) {
-                            this.clearSpinner();
+                            that.clearSpinner();
                             if (messages.length) {
-                                _.map(messages, this.onChatRoomMessage.bind(this));
+                                _.map(messages, that.onChatRoomMessage.bind(that));
                             }
-                        }.bind(this),
+                        },
                         function () {
-                            this.clearSpinner();
+                            that.clearSpinner();
                             converse.log("Error while trying to fetch archived messages", "error");
-                        }.bind(this)
+                        }
                     );
                 }
             });