Commit a0f1a5cf authored by JC Brand's avatar JC Brand

Fetch chat messages in the model, not the view

parent e7ceb22a
...@@ -307,8 +307,8 @@ ...@@ -307,8 +307,8 @@
expect(box instanceof Object).toBeTruthy(); expect(box instanceof Object).toBeTruthy();
expect(box.get('box_id')).toBe(`box-${btoa(jid)}`); expect(box.get('box_id')).toBe(`box-${btoa(jid)}`);
const chatboxview = _converse.chatboxviews.get(jid); const view = _converse.chatboxviews.get(jid);
expect(u.isVisible(chatboxview.el)).toBeTruthy(); await test_utils.waitUntil(() => u.isVisible(view.el));
// Test for multiple JIDs // Test for multiple JIDs
test_utils.openChatBoxFor(_converse, jid2); test_utils.openChatBoxFor(_converse, jid2);
await test_utils.waitUntil(() => _converse.chatboxes.length == 2); await test_utils.waitUntil(() => _converse.chatboxes.length == 2);
...@@ -339,8 +339,8 @@ ...@@ -339,8 +339,8 @@
_.keys(box), _.keys(box),
['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set'] ['close', 'endOTR', 'focus', 'get', 'initiateOTR', 'is_chatroom', 'maximize', 'minimize', 'open', 'set']
); );
const chatboxview = _converse.chatboxviews.get(jid); const view = _converse.chatboxviews.get(jid);
expect(u.isVisible(chatboxview.el)).toBeTruthy(); await test_utils.waitUntil(() => u.isVisible(view.el));
// Test for multiple JIDs // Test for multiple JIDs
const list = await _converse.api.chats.open([jid, jid2]); const list = await _converse.api.chats.open([jid, jid2]);
expect(Array.isArray(list)).toBeTruthy(); expect(Array.isArray(list)).toBeTruthy();
......
...@@ -337,7 +337,6 @@ converse.plugins.add('converse-chatview', { ...@@ -337,7 +337,6 @@ converse.plugins.add('converse-chatview', {
initialize () { initialize () {
this.initDebounced(); this.initDebounced();
this.model.messages.on('add', this.onMessageAdded, this); this.model.messages.on('add', this.onMessageAdded, this);
this.model.messages.on('rendered', this.scrollDown, this); this.model.messages.on('rendered', this.scrollDown, this);
...@@ -347,8 +346,7 @@ converse.plugins.add('converse-chatview', { ...@@ -347,8 +346,7 @@ converse.plugins.add('converse-chatview', {
this.model.presence.on('change:show', this.onPresenceChanged, this); this.model.presence.on('change:show', this.onPresenceChanged, this);
this.model.on('showHelpMessages', this.showHelpMessages, this); this.model.on('showHelpMessages', this.showHelpMessages, this);
this.render(); this.render();
this.updateAfterMessagesFetched();
this.fetchMessages();
/** /**
* Triggered once the _converse.ChatBoxView has been initialized * Triggered once the _converse.ChatBoxView has been initialized
* @event _converse#chatBoxInitialized * @event _converse#chatBoxInitialized
...@@ -530,27 +528,12 @@ converse.plugins.add('converse-chatview', { ...@@ -530,27 +528,12 @@ converse.plugins.add('converse-chatview', {
}); });
}, },
afterMessagesFetched () { async updateAfterMessagesFetched () {
await this.model.messages.fetched;
await Promise.all(this.model.messages.map(m => this.onMessageAdded(m)));
this.insertIntoDOM(); this.insertIntoDOM();
this.scrollDown(); this.scrollDown();
this.content.addEventListener('scroll', this.markScrolled.bind(this)); this.content.addEventListener('scroll', this.markScrolled.bind(this));
/**
* Triggered whenever a `_converse.ChatBox` instance has fetched its messages from
* `sessionStorage` but **NOT** from the server.
* @event _converse#afterMessagesFetched
* @type {_converse.ChatBoxView | _converse.ChatRoomView}
* @example _converse.api.listen.on('afterMessagesFetched', view => { ... });
*/
_converse.api.trigger('afterMessagesFetched', this);
},
fetchMessages () {
this.model.messages.fetch({
'add': true,
'success': this.afterMessagesFetched.bind(this),
'error': this.afterMessagesFetched.bind(this),
});
return this;
}, },
insertIntoDOM () { insertIntoDOM () {
......
...@@ -88,7 +88,9 @@ converse.plugins.add('converse-headline', { ...@@ -88,7 +88,9 @@ converse.plugins.add('converse-headline', {
this.model.on('destroy', this.hide, this); this.model.on('destroy', this.hide, this);
this.model.on('change:minimized', this.onMinimizedChanged, this); this.model.on('change:minimized', this.onMinimizedChanged, this);
this.render().insertHeading().fetchMessages().insertIntoDOM().hide(); this.render().insertHeading()
this.updateAfterMessagesFetched();
this.insertIntoDOM().hide();
_converse.api.trigger('chatBoxInitialized', this); _converse.api.trigger('chatBoxInitialized', this);
}, },
......
...@@ -153,7 +153,7 @@ converse.plugins.add('converse-mam-views', { ...@@ -153,7 +153,7 @@ converse.plugins.add('converse-mam-views', {
const { _converse } = this; const { _converse } = this;
/* Event handlers */ /* Event handlers */
_converse.api.listen.on('afterMessagesFetched', view => view.model.fetchNewestMessages()); _converse.api.listen.on('afterMessagesFetched', chatbox => chatbox.fetchNewestMessages());
_converse.api.listen.on('reconnected', () => { _converse.api.listen.on('reconnected', () => {
const private_chats = _converse.chatboxviews.filter( const private_chats = _converse.chatboxviews.filter(
......
...@@ -564,6 +564,7 @@ converse.plugins.add('converse-muc-views', { ...@@ -564,6 +564,7 @@ converse.plugins.add('converse-muc-views', {
this.createEmojiPicker(); this.createEmojiPicker();
this.render(); this.render();
this.updateAfterMessagesFetched();
this.createOccupantsView(); this.createOccupantsView();
this.insertIntoDOM(); this.insertIntoDOM();
this.registerHandlers(); this.registerHandlers();
...@@ -581,7 +582,7 @@ converse.plugins.add('converse-muc-views', { ...@@ -581,7 +582,7 @@ converse.plugins.add('converse-muc-views', {
} }
this.populateAndJoin(); this.populateAndJoin();
} else { } else {
this.fetchMessages(); this.model.fetchMessages();
} }
/** /**
* Triggered once a groupchat has been opened * Triggered once a groupchat has been opened
...@@ -1195,7 +1196,7 @@ converse.plugins.add('converse-muc-views', { ...@@ -1195,7 +1196,7 @@ converse.plugins.add('converse-muc-views', {
populateAndJoin () { populateAndJoin () {
this.model.occupants.fetchMembers(); this.model.occupants.fetchMembers();
this.join(); this.join();
this.fetchMessages(); this.model.fetchMessages();
}, },
/** /**
......
...@@ -272,7 +272,6 @@ converse.plugins.add('converse-chatboxes', { ...@@ -272,7 +272,6 @@ converse.plugins.add('converse-chatboxes', {
// from being persisted if there's no jid, but that gets // from being persisted if there's no jid, but that gets
// called after model instantiation, so we have to deal // called after model instantiation, so we have to deal
// with invalid models here also. // with invalid models here also.
//
// This happens when the controlbox is in browser storage, // This happens when the controlbox is in browser storage,
// but we're in embedded mode. // but we're in embedded mode.
return; return;
...@@ -288,6 +287,7 @@ converse.plugins.add('converse-chatboxes', { ...@@ -288,6 +287,7 @@ converse.plugins.add('converse-chatboxes', {
} }
this.on('change:chat_state', this.sendChatState, this); this.on('change:chat_state', this.sendChatState, this);
this.initMessages(); this.initMessages();
this.fetchMessages();
}, },
initMessages () { initMessages () {
...@@ -304,6 +304,27 @@ converse.plugins.add('converse-chatboxes', { ...@@ -304,6 +304,27 @@ converse.plugins.add('converse-chatboxes', {
}); });
}, },
afterMessagesFetched () {
/**
* Triggered whenever a `_converse.ChatBox` instance has fetched its messages from
* `sessionStorage` but **NOT** from the server.
* @event _converse#afterMessagesFetched
* @type {_converse.ChatBox | _converse.ChatRoom}
* @example _converse.api.listen.on('afterMessagesFetched', view => { ... });
*/
_converse.api.trigger('afterMessagesFetched', this);
},
fetchMessages () {
this.messages.fetched = new Promise(resolve => {
this.messages.fetch({
'add': true,
'success': _.flow(this.afterMessagesFetched.bind(this), resolve),
'error': _.flow(this.afterMessagesFetched.bind(this), resolve)
});
});
},
validate (attrs, options) { validate (attrs, options) {
const { _converse } = this.__super__; const { _converse } = this.__super__;
if (!attrs.jid) { if (!attrs.jid) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment