Commit 1c375d58 authored by JC Brand's avatar JC Brand

Cleaner separation between converse-mam and converse-chatview

Also add the ability to disable MAM for certain chats (such as the headlines chat).
parent ea9665df
...@@ -106,9 +106,7 @@ ...@@ -106,9 +106,7 @@
this.setWidth(); this.setWidth();
this.$content = this.$el.find('.chat-content'); this.$content = this.$el.find('.chat-content');
this.renderToolbar().renderAvatar(); this.renderToolbar().renderAvatar();
this.$content.on('scroll', _.debounce(this.onScroll.bind(this), 100));
converse.emit('chatBoxOpened', this); converse.emit('chatBoxOpened', this);
converse.emit('chatRoomOpened', this);
window.setTimeout(utils.refreshWebkit, 50); window.setTimeout(utils.refreshWebkit, 50);
return this.showStatusMessage(); return this.showStatusMessage();
}, },
...@@ -121,67 +119,19 @@ ...@@ -121,67 +119,19 @@
} }
}, },
onScroll: function (ev) { afterMessagesFetched: function () {
// XXX: This should go into converse-mam.js // Provides a hook for plugins, such as converse-mam.
if ($(ev.target).scrollTop() === 0 && this.model.messages.length) { return;
this.fetchArchivedMessages({
'before': this.model.messages.at(0).get('archive_id'),
'with': this.model.get('jid'),
'max': converse.archived_messages_page_size
});
}
}, },
fetchMessages: function () { 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({ this.model.messages.fetch({
'add': true, 'add': true,
'success': function () { 'success': this.afterMessagesFetched.bind(this)
// 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)
}); });
return 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 () { insertIntoPage: function () {
/* This method gets overridden in src/converse-controlbox.js if /* This method gets overridden in src/converse-controlbox.js if
* the controlbox plugin is active. * the controlbox plugin is active.
......
...@@ -76,6 +76,7 @@ ...@@ -76,6 +76,7 @@
}, },
initialize: function () { initialize: function () {
this.disable_mam = true; // Don't do MAM queries for this box
$(window).on('resize', _.debounce(this.setDimensions.bind(this), 100)); $(window).on('resize', _.debounce(this.setDimensions.bind(this), 100));
this.model.messages.on('add', this.onMessageAdded, this); this.model.messages.on('add', this.onMessageAdded, this);
this.model.on('show', this.show, this); this.model.on('show', this.show, this);
...@@ -104,7 +105,7 @@ ...@@ -104,7 +105,7 @@
converse.emit('chatBoxOpened', this); converse.emit('chatBoxOpened', this);
window.setTimeout(utils.refreshWebkit, 50); window.setTimeout(utils.refreshWebkit, 50);
return this; return this;
}, }
}); });
var registerHeadlineHandler = function () { var registerHeadlineHandler = function () {
......
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
define("converse-mam", [ define("converse-mam", [
"converse-core", "converse-core",
"converse-api", "converse-api",
"converse-chatview", // Could be made a soft dependency
"strophe.rsm" "strophe.rsm"
], factory); ], factory);
}(this, function (converse, converse_api) { }(this, function (converse, converse_api) {
...@@ -53,6 +54,68 @@ ...@@ -53,6 +54,68 @@
archive_id: $message.find('result[xmlns="'+Strophe.NS.MAM+'"]').attr('id') 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
});
}
},
} }
}, },
......
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