Skip to content

Commit

Permalink
Only create new message models once messages have been fetched
Browse files Browse the repository at this point in the history
Fixes #2241
  • Loading branch information
jcbrand committed Oct 28, 2020
1 parent c08ee00 commit 32cbb9e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
9 changes: 5 additions & 4 deletions spec/emojis.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe("Emojis", function () {
const muc_jid = 'lounge@montague.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.chatboxviews.get(muc_jid);

await u.waitUntil(() => view.el.querySelector('converse-emoji-dropdown'));
const textarea = view.el.querySelector('textarea.chat-textarea');
textarea.value = ':gri';

Expand Down Expand Up @@ -107,7 +107,7 @@ describe("Emojis", function () {
const muc_jid = 'lounge@montague.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.chatboxviews.get(muc_jid);

await u.waitUntil(() => view.el.querySelector('converse-emoji-dropdown'));
const textarea = view.el.querySelector('textarea.chat-textarea');
textarea.value = ':';
// Press tab
Expand Down Expand Up @@ -157,7 +157,7 @@ describe("Emojis", function () {
const muc_jid = 'lounge@montague.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.chatboxviews.get(muc_jid);

await u.waitUntil(() => view.el.querySelector('converse-emoji-dropdown'));
const textarea = view.el.querySelector('textarea.chat-textarea');
textarea.value = ':gri';

Expand All @@ -183,6 +183,7 @@ describe("Emojis", function () {
textarea.value = ':';
view.onKeyDown(tab_event);
await u.waitUntil(() => u.isVisible(view.el.querySelector('.emoji-picker__lists')));
await u.waitUntil(() => input.value === ':');
input.dispatchEvent(new KeyboardEvent('keydown', tab_event));
await u.waitUntil(() => input.value === ':100:');
await u.waitUntil(() => sizzle('.emojis-lists__container--search .insert-emoji:not(.hidden)', view.el).length === 1, 1000);
Expand All @@ -200,8 +201,8 @@ describe("Emojis", function () {

const muc_jid = 'lounge@montague.lit';
await mock.openAndEnterChatRoom(_converse, muc_jid, 'romeo');

const view = _converse.chatboxviews.get(muc_jid);
await u.waitUntil(() => view.el.querySelector('converse-emoji-dropdown'));
const toolbar = view.el.querySelector('converse-chat-toolbar');
toolbar.querySelector('.toggle-emojis').click();
await u.waitUntil(() => u.isVisible(view.el.querySelector('.emoji-picker__lists')));
Expand Down
24 changes: 17 additions & 7 deletions src/headless/converse-chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,7 @@ converse.plugins.add('converse-chat', {

initMessages () {
this.messages = new this.messagesCollection();
this.messages.fetched = u.getResolveablePromise();
this.messages.chatbox = this;
this.messages.browserStorage = _converse.createStore(this.getMessagesCacheKey());
this.listenTo(this.messages, 'change:upload', message => {
Expand All @@ -380,11 +381,11 @@ converse.plugins.add('converse-chat', {
},

fetchMessages () {
if (this.messages.fetched) {
if (this.messages.fetched_flag) {
log.info(`Not re-fetching messages for ${this.get('jid')}`);
return;
}
this.messages.fetched = u.getResolveablePromise();
this.messages.fetched_flag = true;
const resolve = this.messages.fetched.resolve;
this.messages.fetch({
'add': true,
Expand Down Expand Up @@ -439,8 +440,9 @@ converse.plugins.add('converse-chat', {
* @param { Promise<MessageAttributes> } attrs - A promise which resolves to the message attributes
*/
queueMessage (attrs) {
this.msg_chain = (this.msg_chain || this.messages.fetched);
this.msg_chain = this.msg_chain.then(() => this.onMessage(attrs));
this.msg_chain = (this.msg_chain || this.messages.fetched)
.then(() => this.onMessage(attrs))
.catch(e => log.error(e));
return this.msg_chain;
},

Expand Down Expand Up @@ -486,6 +488,7 @@ converse.plugins.add('converse-chat', {
} finally {
delete this.msg_chain;
delete this.messages.fetched;
delete this.messages.fetched_flag;
}
},

Expand Down Expand Up @@ -1009,12 +1012,19 @@ converse.plugins.add('converse-chat', {
},

/**
* Queue the creation of a message, to make sure that we don't run
* into a race condition whereby we're creating a new message
* before the collection has been fetched.
* @async
* @private
* @method _converse.ChatBox#createMessage
* @method _converse.ChatRoom#queueMessageCreation
* @param { Object } attrs
*/
createMessage (attrs, options) {
return this.messages.create(attrs, Object.assign({'wait': true, 'promise':true}, options)).catch(e => log.error(e));
async createMessage (attrs, options) {
attrs.time = attrs.time || (new Date()).toISOString();
await this.messages.fetched;
const p = this.messages.create(attrs, Object.assign({'wait': true, 'promise':true}, options));
return p;
},

/**
Expand Down
2 changes: 0 additions & 2 deletions src/headless/converse-chatboxes.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ converse.plugins.add('converse-chatboxes', {
}
});


async function createChatBox (jid, attrs, Model) {
jid = Strophe.getBareJidFromJid(jid.toLowerCase());
Object.assign(attrs, {'jid': jid, 'id': jid});
Expand All @@ -106,7 +105,6 @@ converse.plugins.add('converse-chatboxes', {
return null;
}
_converse.chatboxes.add(chatbox);
await chatbox.messages.fetched;
return chatbox;
}

Expand Down

0 comments on commit 32cbb9e

Please sign in to comment.