Cleaner separation between converse-mam and converse-chatview

Also add the ability to disable MAM for certain chats (such as the headlines chat).
This commit is contained in:
JC Brand 2016-03-31 09:17:39 +00:00
parent ea9665df4b
commit 1c375d5821
3 changed files with 69 additions and 55 deletions

View File

@ -106,9 +106,7 @@
this.setWidth();
this.$content = this.$el.find('.chat-content');
this.renderToolbar().renderAvatar();
this.$content.on('scroll', _.debounce(this.onScroll.bind(this), 100));
converse.emit('chatBoxOpened', this);
converse.emit('chatRoomOpened', this);
window.setTimeout(utils.refreshWebkit, 50);
return this.showStatusMessage();
},
@ -121,67 +119,19 @@
}
},
onScroll: function (ev) {
// XXX: This should go into converse-mam.js
if ($(ev.target).scrollTop() === 0 && this.model.messages.length) {
this.fetchArchivedMessages({
'before': this.model.messages.at(0).get('archive_id'),
'with': this.model.get('jid'),
'max': converse.archived_messages_page_size
});
}
afterMessagesFetched: function () {
// Provides a hook for plugins, such as converse-mam.
return;
},
fetchMessages: function () {
/* Responsible for fetching previously sent messages, first
* from session storage, and then once that's done by calling
* fetchArchivedMessages, which fetches from the XMPP server if
* applicable.
*/
this.model.messages.fetch({
'add': true,
'success': function () {
// XXX: This should go into converse-mam.js
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
return;
}
if (this.model.messages.length < converse.archived_messages_page_size) {
this.fetchArchivedMessages({
'before': '', // Page backwards from the most recent message
'with': this.model.get('jid'),
'max': converse.archived_messages_page_size
});
}
}.bind(this)
'success': this.afterMessagesFetched.bind(this)
});
return this;
},
fetchArchivedMessages: function (options) {
/* Fetch archived chat messages from the XMPP server.
*
* Then, upon receiving them, call onMessage on the chat box,
* so that they are displayed inside it.
*/
// XXX: This should go into converse-mam.js
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.log("Attempted to fetch archived messages but this user's server doesn't support XEP-0313");
return;
}
this.addSpinner();
converse.queryForArchivedMessages(options, function (messages) {
this.clearSpinner();
if (messages.length) {
_.map(messages, converse.chatboxes.onMessage.bind(converse.chatboxes));
}
}.bind(this),
function () {
this.clearSpinner();
converse.log("Error or timeout while trying to fetch archived messages", "error");
}.bind(this)
);
},
insertIntoPage: function () {
/* This method gets overridden in src/converse-controlbox.js if
* the controlbox plugin is active.

View File

@ -76,6 +76,7 @@
},
initialize: function () {
this.disable_mam = true; // Don't do MAM queries for this box
$(window).on('resize', _.debounce(this.setDimensions.bind(this), 100));
this.model.messages.on('add', this.onMessageAdded, this);
this.model.on('show', this.show, this);
@ -104,7 +105,7 @@
converse.emit('chatBoxOpened', this);
window.setTimeout(utils.refreshWebkit, 50);
return this;
},
}
});
var registerHeadlineHandler = function () {

View File

@ -12,6 +12,7 @@
define("converse-mam", [
"converse-core",
"converse-api",
"converse-chatview", // Could be made a soft dependency
"strophe.rsm"
], factory);
}(this, function (converse, converse_api) {
@ -53,6 +54,68 @@
archive_id: $message.find('result[xmlns="'+Strophe.NS.MAM+'"]').attr('id')
});
}
},
ChatBoxView: {
render: function () {
var result = this._super.render.apply(this, arguments);
if (!this.disable_mam) {
this.$content.on('scroll', _.debounce(this.onScroll.bind(this), 100));
}
return result;
},
afterMessagesFetched: function () {
if (this.disable_mam || !converse.features.findWhere({'var': Strophe.NS.MAM})) {
return this._super.afterMessagesFetched.apply(this, arguments);
}
if (this.model.messages.length < converse.archived_messages_page_size) {
this.fetchArchivedMessages({
'before': '', // Page backwards from the most recent message
'with': this.model.get('jid'),
'max': converse.archived_messages_page_size
});
}
return this._super.afterMessagesFetched.apply(this, arguments);
},
fetchArchivedMessages: function (options) {
/* Fetch archived chat messages from the XMPP server.
*
* Then, upon receiving them, call onMessage on the chat box,
* so that they are displayed inside it.
*/
if (!converse.features.findWhere({'var': Strophe.NS.MAM})) {
converse.log("Attempted to fetch archived messages but this user's server doesn't support XEP-0313");
return;
}
if (this.disable_mam) {
return;
}
this.addSpinner();
converse.queryForArchivedMessages(options, function (messages) {
this.clearSpinner();
if (messages.length) {
_.map(messages, converse.chatboxes.onMessage.bind(converse.chatboxes));
}
}.bind(this),
function () {
this.clearSpinner();
converse.log("Error or timeout while trying to fetch archived messages", "error");
}.bind(this)
);
},
onScroll: function (ev) {
if ($(ev.target).scrollTop() === 0 && this.model.messages.length) {
this.fetchArchivedMessages({
'before': this.model.messages.at(0).get('archive_id'),
'with': this.model.get('jid'),
'max': converse.archived_messages_page_size
});
}
},
}
},