Commit 16db6195 authored by JC Brand's avatar JC Brand

Move MAM methods to the model

parent 94bea16c
...@@ -25,16 +25,8 @@ converse.plugins.add('converse-mam-views', { ...@@ -25,16 +25,8 @@ converse.plugins.add('converse-mam-views', {
// //
// New functions which don't exist yet can also be added. // New functions which don't exist yet can also be added.
ChatBoxView: {
render () {
const result = this.__super__.render.apply(this, arguments);
if (!this.disable_mam) {
this.content.addEventListener('scroll', _.debounce(this.onScroll.bind(this), 100));
}
return result;
},
ChatBox: {
fetchNewestMessages () { fetchNewestMessages () {
/* Fetches messages that might have been archived *after* /* Fetches messages that might have been archived *after*
* the last archived message in our local cache. * the last archived message in our local cache.
...@@ -43,12 +35,12 @@ converse.plugins.add('converse-mam-views', { ...@@ -43,12 +35,12 @@ converse.plugins.add('converse-mam-views', {
return; return;
} }
const { _converse } = this.__super__; const { _converse } = this.__super__;
const most_recent_msg = u.getMostRecentMessage(this.model); const most_recent_msg = u.getMostRecentMessage(this);
if (_.isNil(most_recent_msg)) { if (_.isNil(most_recent_msg)) {
this.fetchArchivedMessages(); this.fetchArchivedMessages();
} else { } else {
const stanza_id = most_recent_msg.get(`stanza_id ${this.model.get('jid')}`); const stanza_id = most_recent_msg.get(`stanza_id ${this.get('jid')}`);
if (stanza_id) { if (stanza_id) {
this.fetchArchivedMessages({'after': stanza_id}); this.fetchArchivedMessages({'after': stanza_id});
} else { } else {
...@@ -62,18 +54,17 @@ converse.plugins.add('converse-mam-views', { ...@@ -62,18 +54,17 @@ converse.plugins.add('converse-mam-views', {
return; return;
} }
const { _converse } = this.__super__; const { _converse } = this.__super__;
const is_groupchat = this.model.get('type') === CHATROOMS_TYPE; const is_groupchat = this.get('type') === CHATROOMS_TYPE;
const mam_jid = is_groupchat ? this.model.get('jid') : _converse.bare_jid; const mam_jid = is_groupchat ? this.get('jid') : _converse.bare_jid;
if (!(await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid))) { if (!(await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid))) {
return; return;
} }
let message_handler; let message_handler;
if (is_groupchat) { if (is_groupchat) {
message_handler = this.model.onMessage.bind(this.model); message_handler = this.onMessage.bind(this);
} else { } else {
message_handler = _converse.chatboxes.onMessage.bind(_converse.chatboxes) message_handler = _converse.chatboxes.onMessage.bind(_converse.chatboxes)
} }
this.addSpinner();
let result; let result;
try { try {
result = await _converse.api.archive.query( result = await _converse.api.archive.query(
...@@ -81,19 +72,28 @@ converse.plugins.add('converse-mam-views', { ...@@ -81,19 +72,28 @@ converse.plugins.add('converse-mam-views', {
'groupchat': is_groupchat, 'groupchat': is_groupchat,
'before': '', // Page backwards from the most recent message 'before': '', // Page backwards from the most recent message
'max': _converse.archived_messages_page_size, 'max': _converse.archived_messages_page_size,
'with': this.model.get('jid'), 'with': this.get('jid'),
}, options)); }, options));
} catch (e) { } catch (e) {
_converse.log( _converse.log(
"Error or timeout while trying to fetch "+ "Error or timeout while trying to fetch "+
"archived messages", Strophe.LogLevel.ERROR); "archived messages", Strophe.LogLevel.ERROR);
_converse.log(e, Strophe.LogLevel.ERROR); _converse.log(e, Strophe.LogLevel.ERROR);
} finally {
this.clearSpinner();
} }
if (result.messages) { if (result.messages) {
result.messages.forEach(message_handler); result.messages.forEach(message_handler);
} }
}
},
ChatBoxView: {
render () {
const result = this.__super__.render.apply(this, arguments);
if (!this.disable_mam) {
this.content.addEventListener('scroll', _.debounce(this.onScroll.bind(this), 100));
}
return result;
}, },
onScroll (ev) { onScroll (ev) {
...@@ -103,42 +103,45 @@ converse.plugins.add('converse-mam-views', { ...@@ -103,42 +103,45 @@ converse.plugins.add('converse-mam-views', {
const by_jid = this.model.get('jid'); const by_jid = this.model.get('jid');
const stanza_id = oldest_message.get(`stanza_id ${by_jid}`); const stanza_id = oldest_message.get(`stanza_id ${by_jid}`);
if (stanza_id) { if (stanza_id) {
this.fetchArchivedMessages({'before': stanza_id}); this.model.fetchArchivedMessages({
'before': stanza_id
});
} else { } else {
this.fetchArchivedMessages({ this.model.fetchArchivedMessages({
'end': oldest_message.get('time') 'end': oldest_message.get('time')
}); });
} }
} }
}, }
}, },
ChatRoomView: { ChatRoom: {
initialize () { initialize () {
const { _converse } = this.__super__;
this.__super__.initialize.apply(this, arguments); this.__super__.initialize.apply(this, arguments);
this.model.on('change:mam_enabled', this.fetchArchivedMessagesIfNecessary, this); this.on('change:mam_enabled', this.fetchArchivedMessagesIfNecessary, this);
this.model.on('change:connection_status', this.fetchArchivedMessagesIfNecessary, this); this.on('change:connection_status', this.fetchArchivedMessagesIfNecessary, this);
}, },
fetchArchivedMessagesIfNecessary () {
if (this.get('connection_status') !== converse.ROOMSTATUS.ENTERED ||
!this.get('mam_enabled') ||
this.get('mam_initialized')) {
return;
}
this.fetchArchivedMessages();
this.save({'mam_initialized': true});
}
},
ChatRoomView: {
renderChatArea () { renderChatArea () {
const result = this.__super__.renderChatArea.apply(this, arguments); const result = this.__super__.renderChatArea.apply(this, arguments);
if (!this.disable_mam) { if (!this.disable_mam) {
this.content.addEventListener('scroll', _.debounce(this.onScroll.bind(this), 100)); this.content.addEventListener('scroll', _.debounce(this.onScroll.bind(this), 100));
} }
return result; return result;
},
fetchArchivedMessagesIfNecessary () {
if (this.model.get('connection_status') !== converse.ROOMSTATUS.ENTERED ||
!this.model.get('mam_enabled') ||
this.model.get('mam_initialized')) {
return;
}
this.fetchArchivedMessages();
this.model.save({'mam_initialized': true});
} }
} }
}, },
...@@ -150,13 +153,13 @@ converse.plugins.add('converse-mam-views', { ...@@ -150,13 +153,13 @@ converse.plugins.add('converse-mam-views', {
const { _converse } = this; const { _converse } = this;
/* Event handlers */ /* Event handlers */
_converse.api.listen.on('afterMessagesFetched', view => view.fetchNewestMessages()); _converse.api.listen.on('afterMessagesFetched', view => view.model.fetchNewestMessages());
_converse.api.listen.on('reconnected', () => { _converse.api.listen.on('reconnected', () => {
const private_chats = _converse.chatboxviews.filter( const private_chats = _converse.chatboxviews.filter(
view => _.at(view, 'model.attributes.type')[0] === 'chatbox' view => _.at(view, 'model.attributes.type')[0] === 'chatbox'
); );
_.each(private_chats, (view) => view.fetchNewestMessages()) _.each(private_chats, view => view.model.fetchNewestMessages())
}); });
} }
}); });
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