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