浏览代码

converse-muc: Avoid refetching messages from cache

We want to fetch messages from cache only once. For one-on-one chats
this is the case because messages are fetched only in the `initialize`
method.

This used to be the case for MUCs as well, until we added the option
`clear_messages_on_reconnection`.

Now we only fetch messages after the user has entered the MUC.
JC Brand 6 年之前
父节点
当前提交
c490ba212d
共有 2 个文件被更改,包括 9 次插入2 次删除
  1. 6 0
      src/headless/converse-chatboxes.js
  2. 3 2
      src/headless/converse-muc.js

+ 6 - 0
src/headless/converse-chatboxes.js

@@ -323,6 +323,10 @@ converse.plugins.add('converse-chatboxes', {
             },
 
             fetchMessages () {
+                if (this.messages.fetched) {
+                    _converse.log(`Not re-fetching messages for ${this.get('jid')}`, Strophe.LogLevel.INFO);
+                    return;
+                }
                 this.messages.fetched = u.getResolveablePromise();
                 const resolve = this.messages.fetched.resolve;
                 this.messages.fetch({
@@ -334,11 +338,13 @@ converse.plugins.add('converse-chatboxes', {
 
             clearMessages () {
                 try {
+                    this.messages.models.forEach(m => m.destroy());
                     this.messages.reset();
                 } catch (e) {
                     this.messages.trigger('reset');
                     _converse.log(e, Strophe.LogLevel.ERROR);
                 } finally {
+                    delete this.messages.fetched;
                     this.messages.browserStorage._clear();
                 }
             },

+ 3 - 2
src/headless/converse-muc.js

@@ -397,8 +397,7 @@ converse.plugins.add('converse-muc', {
                     Strophe.LogLevel.DEBUG
                 );
                 if (conn_status !==  converse.ROOMSTATUS.ENTERED) {
-                    // We're not restoring a room from cache, so let's clear
-                    // the cache (which might be stale).
+                    // We're not restoring a room from cache, so let's clear the potentially stale cache.
                     this.removeNonMembers();
                     await this.refreshRoomFeatures();
                     if (_converse.clear_messages_on_reconnection) {
@@ -411,6 +410,7 @@ converse.plugins.add('converse-muc', {
                     }
                     this.join();
                 } else if (!(await this.rejoinIfNecessary())) {
+                    // We've restored the room from cache and we're still joined.
                     this.features.fetch();
                     this.fetchMessages();
                 }
@@ -1519,6 +1519,7 @@ converse.plugins.add('converse-muc', {
                 if (forwarded) {
                     stanza = forwarded.querySelector('message');
                 }
+
                 const message = await this.getDuplicateMessage(original_stanza);
                 if (message) {
                     this.updateMessage(message, original_stanza);