2016-03-13 18:41:16 +01:00
|
|
|
// Converse.js (A browser based XMPP chat client)
|
|
|
|
// http://conversejs.org
|
|
|
|
//
|
2017-02-13 15:37:17 +01:00
|
|
|
// Copyright (c) 2012-2017, Jan-Carel Brand <jc@opkode.com>
|
2016-03-13 18:41:16 +01:00
|
|
|
// Licensed under the Mozilla Public License (MPLv2)
|
|
|
|
//
|
|
|
|
/*global define */
|
|
|
|
|
|
|
|
// XEP-0059 Result Set Management
|
|
|
|
|
|
|
|
(function (root, factory) {
|
2017-12-02 14:11:10 +01:00
|
|
|
define(["sizzle",
|
2017-02-14 15:08:39 +01:00
|
|
|
"converse-core",
|
2017-12-02 14:12:17 +01:00
|
|
|
"utils",
|
2017-07-21 12:41:16 +02:00
|
|
|
"converse-disco",
|
2016-03-16 10:00:17 +01:00
|
|
|
"strophe.rsm"
|
2016-03-13 18:41:16 +01:00
|
|
|
], factory);
|
2017-12-02 14:11:10 +01:00
|
|
|
}(this, function (sizzle, converse, utils) {
|
2016-03-13 18:41:16 +01:00
|
|
|
"use strict";
|
2017-12-02 14:12:17 +01:00
|
|
|
const CHATROOMS_TYPE = 'chatroom';
|
2017-08-08 17:35:17 +02:00
|
|
|
const { Promise, Strophe, $iq, _, moment } = converse.env;
|
2016-03-13 18:41:16 +01:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const RSM_ATTRIBUTES = ['max', 'first', 'last', 'after', 'before', 'index', 'count'];
|
2016-03-13 18:41:16 +01:00
|
|
|
// XEP-0313 Message Archive Management
|
2017-07-10 17:46:22 +02:00
|
|
|
const MAM_ATTRIBUTES = ['with', 'start', 'end'];
|
2016-03-13 18:41:16 +01:00
|
|
|
|
2017-12-02 14:11:10 +01:00
|
|
|
|
2016-12-20 11:42:20 +01:00
|
|
|
converse.plugins.add('converse-mam', {
|
2016-03-13 18:41:16 +01:00
|
|
|
|
2017-12-22 22:23:08 +01:00
|
|
|
optional_dependencies: ['converse-chatview', 'converse-muc'],
|
|
|
|
|
2016-03-13 18:41:16 +01:00
|
|
|
overrides: {
|
|
|
|
// Overrides mentioned here will be picked up by converse.js's
|
|
|
|
// plugin architecture they will replace existing methods on the
|
|
|
|
// relevant objects or classes.
|
|
|
|
//
|
|
|
|
// New functions which don't exist yet can also be added.
|
2016-05-30 18:17:26 +02:00
|
|
|
ChatBox: {
|
2018-01-02 21:44:46 +01:00
|
|
|
getMessageAttributes (message, delay, original_stanza) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const attrs = this.__super__.getMessageAttributes.apply(this, arguments);
|
2017-12-02 14:11:10 +01:00
|
|
|
const result = sizzle(`stanza-id[xmlns="${Strophe.NS.SID}"]`, original_stanza).pop();
|
|
|
|
if (!_.isUndefined(result)) {
|
|
|
|
attrs.archive_id = result.getAttribute('id');
|
|
|
|
}
|
2016-05-30 19:07:59 +02:00
|
|
|
return attrs;
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
2016-03-31 11:17:39 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ChatBoxView: {
|
2017-07-10 17:46:22 +02:00
|
|
|
render () {
|
|
|
|
const result = this.__super__.render.apply(this, arguments);
|
2016-03-31 11:17:39 +02:00
|
|
|
if (!this.disable_mam) {
|
2018-01-02 21:44:46 +01:00
|
|
|
this.content.addEventListener('scroll', _.debounce(this.onScroll.bind(this), 100));
|
2016-03-31 11:17:39 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
|
|
|
|
2017-12-02 14:12:17 +01:00
|
|
|
fetchNewestMessages () {
|
|
|
|
/* Fetches messages that might have been archived *after*
|
|
|
|
* the last archived message in our local cache.
|
|
|
|
*/
|
|
|
|
if (this.disable_mam) { return; }
|
|
|
|
const { _converse } = this.__super__;
|
2017-12-02 14:57:10 +01:00
|
|
|
this.addSpinner(true);
|
2017-12-02 14:12:17 +01:00
|
|
|
_converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then(
|
|
|
|
(result) => { // Success
|
|
|
|
if (result.supported) {
|
|
|
|
const most_recent_msg = utils.getMostRecentMessage(this.model);
|
|
|
|
const archive_id = most_recent_msg.get('archive_id');
|
|
|
|
if (archive_id) {
|
|
|
|
this.fetchArchivedMessages({
|
|
|
|
'after': most_recent_msg.get('archive_id')
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.fetchArchivedMessages({
|
|
|
|
'start': most_recent_msg.get('time')
|
|
|
|
});
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.clearSpinner();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
() => { // Error
|
|
|
|
this.clearSpinner();
|
|
|
|
_converse.log(
|
|
|
|
"Error or timeout while checking for MAM support",
|
|
|
|
Strophe.LogLevel.ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
).catch((msg) => {
|
|
|
|
this.clearSpinner();
|
|
|
|
_converse.log(msg, Strophe.LogLevel.FATAL);
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2017-07-21 15:05:22 +02:00
|
|
|
fetchArchivedMessagesIfNecessary () {
|
|
|
|
/* Check if archived messages should be fetched, and if so, do so. */
|
2017-08-08 17:35:17 +02:00
|
|
|
if (this.disable_mam || this.model.get('mam_initialized')) {
|
2017-07-21 15:05:22 +02:00
|
|
|
return;
|
2016-03-31 11:17:39 +02:00
|
|
|
}
|
2017-08-08 17:35:17 +02:00
|
|
|
const { _converse } = this.__super__;
|
|
|
|
this.addSpinner();
|
|
|
|
|
2017-11-10 15:53:59 +01:00
|
|
|
_converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then(
|
|
|
|
(result) => { // Success
|
|
|
|
if (result.supported) {
|
2017-08-08 17:35:17 +02:00
|
|
|
this.fetchArchivedMessages();
|
|
|
|
} else {
|
|
|
|
this.clearSpinner();
|
|
|
|
}
|
|
|
|
this.model.save({'mam_initialized': true});
|
|
|
|
},
|
|
|
|
() => { // Error
|
|
|
|
this.clearSpinner();
|
|
|
|
_converse.log(
|
|
|
|
"Error or timeout while checking for MAM support",
|
|
|
|
Strophe.LogLevel.ERROR
|
|
|
|
);
|
|
|
|
}
|
|
|
|
).catch((msg) => {
|
|
|
|
this.clearSpinner();
|
|
|
|
_converse.log(msg, Strophe.LogLevel.FATAL);
|
|
|
|
});
|
2016-03-31 11:17:39 +02:00
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
fetchArchivedMessages (options) {
|
2016-03-31 11:17:39 +02:00
|
|
|
/* Fetch archived chat messages from the XMPP server.
|
|
|
|
*
|
2017-02-24 21:52:23 +01:00
|
|
|
* Then, upon receiving them, call onMessage on the chat
|
|
|
|
* box, so that they are displayed inside it.
|
2016-03-31 11:17:39 +02:00
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
2017-08-08 17:35:17 +02:00
|
|
|
if (!_converse.disco_entities.get(_converse.bare_jid)
|
2017-07-21 12:41:16 +02:00
|
|
|
.features.findWhere({'var': Strophe.NS.MAM})) {
|
|
|
|
|
2017-02-24 21:52:23 +01:00
|
|
|
_converse.log(
|
|
|
|
"Attempted to fetch archived messages but this "+
|
2017-07-05 11:33:55 +02:00
|
|
|
"user's server doesn't support XEP-0313",
|
|
|
|
Strophe.LogLevel.WARN);
|
2016-03-31 11:17:39 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this.disable_mam) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.addSpinner();
|
2017-07-10 17:46:22 +02:00
|
|
|
_converse.queryForArchivedMessages(
|
2017-07-21 15:05:22 +02:00
|
|
|
_.extend({
|
|
|
|
'before': '', // Page backwards from the most recent message
|
|
|
|
'max': _converse.archived_messages_page_size,
|
|
|
|
'with': this.model.get('jid'),
|
|
|
|
}, options),
|
2017-07-10 17:46:22 +02:00
|
|
|
(messages) => { // Success
|
2016-03-31 11:17:39 +02:00
|
|
|
this.clearSpinner();
|
|
|
|
if (messages.length) {
|
2016-12-20 10:30:20 +01:00
|
|
|
_.each(messages, _converse.chatboxes.onMessage.bind(_converse.chatboxes));
|
2016-03-31 11:17:39 +02:00
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
},
|
|
|
|
() => { // Error
|
2016-03-31 11:17:39 +02:00
|
|
|
this.clearSpinner();
|
2017-02-24 21:52:23 +01:00
|
|
|
_converse.log(
|
|
|
|
"Error or timeout while trying to fetch "+
|
2017-07-05 11:33:55 +02:00
|
|
|
"archived messages", Strophe.LogLevel.ERROR);
|
2017-07-10 17:46:22 +02:00
|
|
|
}
|
2016-03-31 11:17:39 +02:00
|
|
|
);
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
onScroll (ev) {
|
|
|
|
const { _converse } = this.__super__;
|
2017-12-02 14:11:10 +01:00
|
|
|
if (ev.target.scrollTop === 0 && this.model.messages.length) {
|
|
|
|
const oldest_message = this.model.messages.at(0);
|
|
|
|
const archive_id = oldest_message.get('archive_id');
|
|
|
|
if (archive_id) {
|
|
|
|
this.fetchArchivedMessages({
|
|
|
|
'before': archive_id
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
this.fetchArchivedMessages({
|
|
|
|
'end': oldest_message.get('time')
|
|
|
|
});
|
|
|
|
}
|
2016-03-31 11:17:39 +02:00
|
|
|
}
|
|
|
|
},
|
2016-04-01 14:46:19 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
ChatRoomView: {
|
2017-06-23 22:53:11 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
|
|
|
const { _converse } = this.__super__;
|
2017-06-23 22:53:11 +02:00
|
|
|
this.__super__.initialize.apply(this, arguments);
|
2017-07-21 15:05:22 +02:00
|
|
|
this.model.on('change:mam_enabled', this.fetchArchivedMessagesIfNecessary, this);
|
|
|
|
this.model.on('change:connection_status', this.fetchArchivedMessagesIfNecessary, this);
|
2017-06-23 22:53:11 +02:00
|
|
|
},
|
|
|
|
|
2017-12-20 11:02:01 +01:00
|
|
|
renderChatArea () {
|
|
|
|
const result = this.__super__.renderChatArea.apply(this, arguments);
|
2016-04-01 14:46:19 +02:00
|
|
|
if (!this.disable_mam) {
|
2018-01-02 21:44:46 +01:00
|
|
|
this.content.addEventListener('scroll', _.debounce(this.onScroll.bind(this), 100));
|
2016-04-01 14:46:19 +02:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
},
|
2016-03-31 11:17:39 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
handleMUCMessage (stanza) {
|
2017-02-28 22:17:28 +01:00
|
|
|
/* MAM (message archive management XEP-0313) messages are
|
|
|
|
* ignored, since they're handled separately.
|
|
|
|
*/
|
2017-12-02 14:11:10 +01:00
|
|
|
if (sizzle(`[xmlns="${Strophe.NS.MAM}"]`, stanza).length > 0) {
|
2017-02-28 22:17:28 +01:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return this.__super__.handleMUCMessage.apply(this, arguments);
|
|
|
|
},
|
|
|
|
|
2017-07-21 15:05:22 +02:00
|
|
|
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});
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
fetchArchivedMessages (options) {
|
2017-07-21 15:05:22 +02:00
|
|
|
/* Fetch archived chat messages for this Chat Room
|
2017-02-24 21:52:23 +01:00
|
|
|
*
|
|
|
|
* Then, upon receiving them, call onChatRoomMessage
|
|
|
|
* so that they are displayed inside it.
|
|
|
|
*/
|
|
|
|
this.addSpinner();
|
2017-07-10 17:46:22 +02:00
|
|
|
const that = this;
|
2017-07-21 15:05:22 +02:00
|
|
|
const { _converse } = this.__super__;
|
|
|
|
_converse.api.archive.query(
|
|
|
|
_.extend({
|
|
|
|
'groupchat': true,
|
|
|
|
'before': '', // Page backwards from the most recent message
|
|
|
|
'with': this.model.get('jid'),
|
|
|
|
'max': _converse.archived_messages_page_size
|
|
|
|
}, options),
|
2017-02-24 21:52:23 +01:00
|
|
|
function (messages) {
|
|
|
|
that.clearSpinner();
|
|
|
|
if (messages.length) {
|
|
|
|
_.each(messages, that.onChatRoomMessage.bind(that));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
function () {
|
|
|
|
that.clearSpinner();
|
|
|
|
_converse.log(
|
|
|
|
"Error while trying to fetch archived messages",
|
2017-07-05 11:33:55 +02:00
|
|
|
Strophe.LogLevel.WARN);
|
2017-02-24 21:52:23 +01:00
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
initialize () {
|
2016-03-13 18:41:16 +01:00
|
|
|
/* The initialize function gets called as soon as the plugin is
|
2016-12-20 10:30:20 +01:00
|
|
|
* loaded by Converse.js's plugin machinery.
|
2016-03-13 18:41:16 +01:00
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
const { _converse } = this;
|
2016-12-20 11:42:20 +01:00
|
|
|
|
2017-07-05 11:03:13 +02:00
|
|
|
_converse.api.settings.update({
|
2017-02-27 22:12:15 +01:00
|
|
|
archived_messages_page_size: '50',
|
2017-02-11 23:08:26 +01:00
|
|
|
message_archiving: undefined, // Supported values are 'always', 'never', 'roster' (https://xmpp.org/extensions/xep-0313.html#prefs)
|
2016-03-13 18:41:16 +01:00
|
|
|
message_archiving_timeout: 8000, // Time (in milliseconds) to wait before aborting MAM request
|
|
|
|
});
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.queryForArchivedMessages = function (options, callback, errback) {
|
2016-03-13 18:41:16 +01:00
|
|
|
/* Do a MAM (XEP-0313) query for archived messages.
|
2016-03-19 23:16:00 +01:00
|
|
|
*
|
|
|
|
* Parameters:
|
|
|
|
* (Object) options - Query parameters, either MAM-specific or also for Result Set Management.
|
|
|
|
* (Function) callback - A function to call whenever we receive query-relevant stanza.
|
|
|
|
* (Function) errback - A function to call when an error stanza is received.
|
|
|
|
*
|
|
|
|
* The options parameter can also be an instance of
|
|
|
|
* Strophe.RSM to enable easy querying between results pages.
|
|
|
|
*
|
|
|
|
* The callback function may be called multiple times, first
|
|
|
|
* for the initial IQ result and then for each message
|
|
|
|
* returned. The last time the callback is called, a
|
|
|
|
* Strophe.RSM object is returned on which "next" or "previous"
|
|
|
|
* can be called before passing it in again to this method, to
|
|
|
|
* get the next or previous page in the result set.
|
|
|
|
*/
|
2017-07-10 17:46:22 +02:00
|
|
|
let date;
|
2017-01-26 15:49:02 +01:00
|
|
|
if (_.isFunction(options)) {
|
2016-03-13 18:41:16 +01:00
|
|
|
callback = options;
|
|
|
|
errback = callback;
|
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
const queryid = _converse.connection.getUniqueId();
|
|
|
|
const attrs = {'type':'set'};
|
2017-01-26 15:49:02 +01:00
|
|
|
if (!_.isUndefined(options) && options.groupchat) {
|
2017-07-21 12:41:16 +02:00
|
|
|
if (!options['with']) { // eslint-disable-line dot-notation
|
2017-06-23 22:53:11 +02:00
|
|
|
throw new Error(
|
|
|
|
'You need to specify a "with" value containing '+
|
|
|
|
'the chat room JID, when querying groupchat messages.');
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
2017-07-21 12:41:16 +02:00
|
|
|
attrs.to = options['with']; // eslint-disable-line dot-notation
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
2017-07-10 17:46:22 +02:00
|
|
|
const stanza = $iq(attrs).c('query', {'xmlns':Strophe.NS.MAM, 'queryid':queryid});
|
2017-01-26 15:49:02 +01:00
|
|
|
if (!_.isUndefined(options)) {
|
2016-03-13 18:41:16 +01:00
|
|
|
stanza.c('x', {'xmlns':Strophe.NS.XFORM, 'type': 'submit'})
|
|
|
|
.c('field', {'var':'FORM_TYPE', 'type': 'hidden'})
|
|
|
|
.c('value').t(Strophe.NS.MAM).up().up();
|
|
|
|
|
2017-07-21 12:41:16 +02:00
|
|
|
if (options['with'] && !options.groupchat) { // eslint-disable-line dot-notation
|
|
|
|
stanza.c('field', {'var':'with'}).c('value')
|
|
|
|
.t(options['with']).up().up(); // eslint-disable-line dot-notation
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
|
|
|
_.each(['start', 'end'], function (t) {
|
|
|
|
if (options[t]) {
|
|
|
|
date = moment(options[t]);
|
|
|
|
if (date.isValid()) {
|
|
|
|
stanza.c('field', {'var':t}).c('value').t(date.format()).up().up();
|
|
|
|
} else {
|
2017-07-10 17:46:22 +02:00
|
|
|
throw new TypeError(`archive.query: invalid date provided for: ${t}`);
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
stanza.up();
|
|
|
|
if (options instanceof Strophe.RSM) {
|
|
|
|
stanza.cnode(options.toXML());
|
|
|
|
} else if (_.intersection(RSM_ATTRIBUTES, _.keys(options)).length) {
|
|
|
|
stanza.cnode(new Strophe.RSM(options).toXML());
|
|
|
|
}
|
|
|
|
}
|
2016-07-28 18:01:37 +02:00
|
|
|
|
2017-07-10 17:46:22 +02:00
|
|
|
const messages = [];
|
|
|
|
const message_handler = _converse.connection.addHandler(function (message) {
|
|
|
|
const result = message.querySelector('result');
|
2017-06-23 22:53:11 +02:00
|
|
|
if (!_.isNull(result) && result.getAttribute('queryid') === queryid) {
|
|
|
|
messages.push(message);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}, Strophe.NS.MAM);
|
|
|
|
|
|
|
|
_converse.connection.sendIQ(
|
|
|
|
stanza,
|
|
|
|
function (iq) {
|
|
|
|
_converse.connection.deleteHandler(message_handler);
|
|
|
|
if (_.isFunction(callback)) {
|
2017-07-10 17:46:22 +02:00
|
|
|
const set = iq.querySelector('set');
|
2017-12-19 15:32:23 +01:00
|
|
|
let rsm;
|
|
|
|
if (!_.isUndefined(set)) {
|
|
|
|
rsm = new Strophe.RSM({xml: set});
|
|
|
|
_.extend(rsm, _.pick(options, _.concat(MAM_ATTRIBUTES, ['max'])));
|
|
|
|
}
|
2016-03-13 18:41:16 +01:00
|
|
|
callback(messages, rsm);
|
|
|
|
}
|
2017-06-23 22:53:11 +02:00
|
|
|
},
|
|
|
|
function () {
|
|
|
|
_converse.connection.deleteHandler(message_handler);
|
|
|
|
if (_.isFunction(errback)) { errback.apply(this, arguments); }
|
|
|
|
},
|
|
|
|
_converse.message_archiving_timeout
|
|
|
|
);
|
2016-03-13 18:41:16 +01:00
|
|
|
};
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.onMAMError = function (iq) {
|
2017-12-02 14:11:10 +01:00
|
|
|
if (iq.querySelectorAll('feature-not-implemented').length) {
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log(
|
|
|
|
"Message Archive Management (XEP-0313) not supported by this server",
|
|
|
|
Strophe.LogLevel.WARN);
|
2016-03-13 18:41:16 +01:00
|
|
|
} else {
|
2017-07-05 11:33:55 +02:00
|
|
|
_converse.log(
|
|
|
|
"An error occured while trying to set archiving preferences.",
|
|
|
|
Strophe.LogLevel.ERROR);
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.log(iq);
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.onMAMPreferences = function (feature, iq) {
|
2016-03-13 18:41:16 +01:00
|
|
|
/* Handle returned IQ stanza containing Message Archive
|
|
|
|
* Management (XEP-0313) preferences.
|
|
|
|
*
|
|
|
|
* XXX: For now we only handle the global default preference.
|
|
|
|
* The XEP also provides for per-JID preferences, which is
|
|
|
|
* currently not supported in converse.js.
|
|
|
|
*
|
|
|
|
* Per JID preferences will be set in chat boxes, so it'll
|
|
|
|
* probbaly be handled elsewhere in any case.
|
|
|
|
*/
|
2017-12-02 14:11:10 +01:00
|
|
|
const preference = sizzle(`prefs[xmlns="${Strophe.NS.MAM}"]`, iq).pop();
|
|
|
|
const default_pref = preference.getAttribute('default');
|
2016-12-20 10:30:20 +01:00
|
|
|
if (default_pref !== _converse.message_archiving) {
|
2017-12-02 14:11:10 +01:00
|
|
|
const stanza = $iq({'type': 'set'})
|
|
|
|
.c('prefs', {
|
|
|
|
'xmlns':Strophe.NS.MAM,
|
|
|
|
'default':_converse.message_archiving
|
|
|
|
});
|
|
|
|
_.each(preference.children, function (child) {
|
2016-03-13 18:41:16 +01:00
|
|
|
stanza.cnode(child).up();
|
|
|
|
});
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.sendIQ(stanza, _.partial(function (feature, iq) {
|
2016-03-13 18:41:16 +01:00
|
|
|
// XXX: Strictly speaking, the server should respond with the updated prefs
|
|
|
|
// (see example 18: https://xmpp.org/extensions/xep-0313.html#config)
|
|
|
|
// but Prosody doesn't do this, so we don't rely on it.
|
2016-12-20 10:30:20 +01:00
|
|
|
feature.save({'preferences': {'default':_converse.message_archiving}});
|
2016-03-13 18:41:16 +01:00
|
|
|
}, feature),
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.onMAMError
|
2016-03-13 18:41:16 +01:00
|
|
|
);
|
|
|
|
} else {
|
2016-12-20 10:30:20 +01:00
|
|
|
feature.save({'preferences': {'default':_converse.message_archiving}});
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-07-21 12:41:16 +02:00
|
|
|
/* Event handlers */
|
|
|
|
_converse.on('serviceDiscovered', (feature) => {
|
2017-07-10 17:46:22 +02:00
|
|
|
const prefs = feature.get('preferences') || {};
|
2017-02-11 23:08:26 +01:00
|
|
|
if (feature.get('var') === Strophe.NS.MAM &&
|
2017-07-21 12:41:16 +02:00
|
|
|
prefs['default'] !== _converse.message_archiving && // eslint-disable-line dot-notation
|
2017-02-12 20:01:07 +01:00
|
|
|
!_.isUndefined(_converse.message_archiving) ) {
|
2016-03-13 18:41:16 +01:00
|
|
|
// Ask the server for archiving preferences
|
2016-12-20 10:30:20 +01:00
|
|
|
_converse.connection.sendIQ(
|
2016-03-13 18:41:16 +01:00
|
|
|
$iq({'type': 'get'}).c('prefs', {'xmlns': Strophe.NS.MAM}),
|
2016-12-20 10:30:20 +01:00
|
|
|
_.partial(_converse.onMAMPreferences, feature),
|
|
|
|
_.partial(_converse.onMAMError, feature)
|
2016-03-13 18:41:16 +01:00
|
|
|
);
|
|
|
|
}
|
2017-07-21 12:41:16 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
_converse.on('addClientFeatures', () => {
|
|
|
|
_converse.connection.disco.addFeature(Strophe.NS.MAM);
|
|
|
|
});
|
2017-07-21 15:05:22 +02:00
|
|
|
|
|
|
|
_converse.on('afterMessagesFetched', (chatboxview) => {
|
2017-08-08 17:35:17 +02:00
|
|
|
chatboxview.fetchArchivedMessagesIfNecessary();
|
2017-07-21 15:05:22 +02:00
|
|
|
});
|
2017-11-02 23:23:01 +01:00
|
|
|
|
2017-12-02 14:12:17 +01:00
|
|
|
_converse.on('reconnected', () => {
|
|
|
|
const private_chats = _converse.chatboxviews.filter(
|
|
|
|
(view) => _.at(view, 'model.attributes.type')[0] === 'chatbox'
|
|
|
|
);
|
|
|
|
_.each(private_chats, (view) => view.fetchNewestMessages())
|
|
|
|
});
|
|
|
|
|
2017-11-02 23:23:01 +01:00
|
|
|
_.extend(_converse.api, {
|
|
|
|
/* Extend default converse.js API to add methods specific to MAM
|
|
|
|
*/
|
|
|
|
'archive': {
|
|
|
|
'query': function () {
|
|
|
|
if (!_converse.api.connection.connected()) {
|
|
|
|
throw new Error('Can\'t call `api.archive.query` before having established an XMPP session');
|
|
|
|
}
|
|
|
|
return _converse.queryForArchivedMessages.apply(this, arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2016-03-13 18:41:16 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}));
|