2019-07-11 10:48:52 +02:00
|
|
|
/**
|
|
|
|
* @module converse-mam
|
2020-01-26 16:21:20 +01:00
|
|
|
* @description XEP-0313 Message Archive Management
|
|
|
|
* @copyright 2020, the Converse.js contributors
|
|
|
|
* @license Mozilla Public License (MPLv2)
|
2019-07-11 10:48:52 +02:00
|
|
|
*/
|
2018-10-23 03:41:38 +02:00
|
|
|
import "./converse-disco";
|
2019-06-20 16:51:50 +02:00
|
|
|
import "./converse-rsm";
|
2019-08-03 14:16:46 +02:00
|
|
|
import { intersection, pick } from 'lodash'
|
2018-10-23 03:41:38 +02:00
|
|
|
import converse from "./converse-core";
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "./log";
|
2018-10-23 03:41:38 +02:00
|
|
|
import sizzle from "sizzle";
|
2018-01-09 23:05:23 +01:00
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
const { Strophe, $iq, dayjs } = converse.env;
|
2018-10-23 03:41:38 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
|
|
|
|
// XEP-0313 Message Archive Management
|
|
|
|
const MAM_ATTRIBUTES = ['with', 'start', 'end'];
|
|
|
|
|
|
|
|
|
|
|
|
converse.plugins.add('converse-mam', {
|
2016-03-13 18:41:16 +01:00
|
|
|
|
2019-05-29 17:40:16 +02:00
|
|
|
dependencies: ['converse-rsm', 'converse-disco', 'converse-muc'],
|
2017-12-22 22:23:08 +01:00
|
|
|
|
2019-05-24 13:52:15 +02:00
|
|
|
|
|
|
|
initialize () {
|
|
|
|
/* The initialize function gets called as soon as the plugin is
|
|
|
|
* loaded by Converse.js's plugin machinery.
|
|
|
|
*/
|
|
|
|
const { _converse } = this;
|
2020-03-31 13:15:57 +02:00
|
|
|
const { api } = _converse;
|
2019-05-24 13:52:15 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.settings.update({
|
2019-05-24 13:52:15 +02:00
|
|
|
archived_messages_page_size: '50',
|
|
|
|
message_archiving: undefined, // Supported values are 'always', 'never', 'roster' (https://xmpp.org/extensions/xep-0313.html#prefs)
|
|
|
|
message_archiving_timeout: 20000, // Time (in milliseconds) to wait before aborting MAM request
|
|
|
|
});
|
|
|
|
|
|
|
|
const MAMEnabledChat = {
|
2019-08-03 14:16:46 +02:00
|
|
|
/**
|
|
|
|
* Fetches messages that might have been archived *after*
|
|
|
|
* the last archived message in our local cache.
|
2019-11-28 17:01:08 +01:00
|
|
|
* @private
|
2019-08-03 14:16:46 +02:00
|
|
|
*/
|
2019-05-22 14:03:13 +02:00
|
|
|
fetchNewestMessages () {
|
|
|
|
if (this.disable_mam) {
|
|
|
|
return;
|
|
|
|
}
|
2019-08-06 23:29:56 +02:00
|
|
|
const most_recent_msg = this.getMostRecentMessage();
|
|
|
|
if (most_recent_msg) {
|
2019-05-22 14:03:13 +02:00
|
|
|
const stanza_id = most_recent_msg.get(`stanza_id ${this.get('jid')}`);
|
|
|
|
if (stanza_id) {
|
2019-08-06 22:54:51 +02:00
|
|
|
this.fetchArchivedMessages({'after': stanza_id}, 'forwards');
|
2019-05-22 14:03:13 +02:00
|
|
|
} else {
|
2019-08-06 22:54:51 +02:00
|
|
|
this.fetchArchivedMessages({'start': most_recent_msg.get('time')}, 'forwards');
|
2019-05-22 14:03:13 +02:00
|
|
|
}
|
2019-08-06 23:29:56 +02:00
|
|
|
} else {
|
|
|
|
this.fetchArchivedMessages({'before': ''});
|
2019-05-22 14:03:13 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-08-06 22:54:51 +02:00
|
|
|
/**
|
|
|
|
* Fetch XEP-0313 archived messages based on the passed in criteria.
|
|
|
|
* @private
|
|
|
|
* @param { Object } options
|
|
|
|
* @param { integer } [options.max] - The maxinum number of items to return.
|
|
|
|
* Defaults to "archived_messages_page_size"
|
|
|
|
* @param { string } [options.after] - The XEP-0359 stanza ID of a message
|
|
|
|
* after which messages should be returned. Implies forward paging.
|
|
|
|
* @param { string } [options.before] - The XEP-0359 stanza ID of a message
|
|
|
|
* before which messages should be returned. Implies backward paging.
|
|
|
|
* @param { string } [options.end] - A date string in ISO-8601 format,
|
|
|
|
* before which messages should be returned. Implies backward paging.
|
|
|
|
* @param { string } [options.start] - A date string in ISO-8601 format,
|
|
|
|
* after which messages should be returned. Implies forward paging.
|
|
|
|
* @param { string } [options.with] - The JID of the entity with
|
|
|
|
* which messages were exchanged.
|
2019-12-26 12:56:58 +01:00
|
|
|
* @param { boolean } [options.groupchat] - True if archive in groupchat.
|
2019-08-06 22:54:51 +02:00
|
|
|
* @param { boolean } [page] - Whether this function should recursively
|
|
|
|
* page through the entire result set if a limited number of results
|
|
|
|
* were returned.
|
|
|
|
*/
|
|
|
|
async fetchArchivedMessages (options={}, page) {
|
2019-05-22 14:03:13 +02:00
|
|
|
if (this.disable_mam) {
|
|
|
|
return;
|
|
|
|
}
|
2019-05-29 13:00:45 +02:00
|
|
|
const is_groupchat = this.get('type') === _converse.CHATROOMS_TYPE;
|
2019-05-22 14:03:13 +02:00
|
|
|
const mam_jid = is_groupchat ? this.get('jid') : _converse.bare_jid;
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!(await api.disco.supports(Strophe.NS.MAM, mam_jid))) {
|
2019-05-22 14:03:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-03-02 16:59:18 +01:00
|
|
|
const msg_handler = is_groupchat ? s => this.queueMessage(s) : s => _converse.handleMessageStanza(s);
|
2019-08-06 22:54:51 +02:00
|
|
|
|
2019-08-03 18:26:57 +02:00
|
|
|
const query = Object.assign({
|
2019-08-03 14:16:46 +02:00
|
|
|
'groupchat': is_groupchat,
|
2020-03-31 13:15:57 +02:00
|
|
|
'max': api.settings.get('archived_messages_page_size'),
|
2019-08-03 14:16:46 +02:00
|
|
|
'with': this.get('jid'),
|
2019-08-03 18:26:57 +02:00
|
|
|
}, options);
|
2019-08-06 22:54:51 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
const result = await api.archive.query(query);
|
2019-09-19 16:25:16 +02:00
|
|
|
/* eslint-disable no-await-in-loop */
|
|
|
|
for (const message of result.messages) {
|
|
|
|
try {
|
2020-03-02 16:59:18 +01:00
|
|
|
await msg_handler(message);
|
2019-09-19 16:25:16 +02:00
|
|
|
} catch (e) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error(e);
|
2019-09-19 16:25:16 +02:00
|
|
|
}
|
|
|
|
}
|
2019-08-03 18:26:57 +02:00
|
|
|
|
2019-08-12 20:16:34 +02:00
|
|
|
if (result.error) {
|
|
|
|
result.error.retry = () => this.fetchArchivedMessages(options, page);
|
|
|
|
this.createMessageFromError(result.error);
|
|
|
|
}
|
|
|
|
|
2019-08-06 22:54:51 +02:00
|
|
|
if (page && result.rsm) {
|
|
|
|
if (page === 'forwards') {
|
2020-03-31 13:15:57 +02:00
|
|
|
options = result.rsm.next(api.settings.get('archived_messages_page_size'), options.before);
|
2019-08-06 22:54:51 +02:00
|
|
|
} else if (page === 'backwards') {
|
2020-03-31 13:15:57 +02:00
|
|
|
options = result.rsm.previous(api.settings.get('archived_messages_page_size'), options.after);
|
2019-08-03 18:26:57 +02:00
|
|
|
}
|
2019-08-06 22:54:51 +02:00
|
|
|
return this.fetchArchivedMessages(options, page);
|
|
|
|
} else {
|
|
|
|
// TODO: Add a special kind of message which will
|
|
|
|
// render as a link to fetch further messages, either
|
|
|
|
// to fetch older messages or to fill in a gap.
|
2019-08-03 18:26:57 +02:00
|
|
|
}
|
2020-02-02 17:20:05 +01:00
|
|
|
}
|
2019-05-24 13:52:15 +02:00
|
|
|
}
|
|
|
|
Object.assign(_converse.ChatBox.prototype, MAMEnabledChat);
|
2019-05-22 14:03:13 +02:00
|
|
|
|
|
|
|
|
2018-10-25 07:32:44 +02:00
|
|
|
_converse.onMAMError = function (iq) {
|
2019-11-28 17:01:08 +01:00
|
|
|
if (iq && iq.querySelectorAll('feature-not-implemented').length) {
|
2020-04-10 16:04:46 +02:00
|
|
|
log.warn(`Message Archive Management (XEP-0313) not supported by ${iq.getAttribute('from')}`);
|
2018-10-23 03:41:38 +02:00
|
|
|
} else {
|
2020-04-10 16:04:46 +02:00
|
|
|
log.error(`Error while trying to set archiving preferences for ${iq.getAttribute('from')}.`);
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error(iq);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-08-03 14:16:46 +02:00
|
|
|
_converse.onMAMPreferences = function (iq, feature) {
|
2018-10-23 03:41:38 +02: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.
|
|
|
|
*/
|
|
|
|
const preference = sizzle(`prefs[xmlns="${Strophe.NS.MAM}"]`, iq).pop();
|
|
|
|
const default_pref = preference.getAttribute('default');
|
2020-03-31 13:15:57 +02:00
|
|
|
if (default_pref !== api.settings.get('message_archiving')) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const stanza = $iq({'type': 'set'})
|
|
|
|
.c('prefs', {
|
|
|
|
'xmlns':Strophe.NS.MAM,
|
2020-03-31 13:15:57 +02:00
|
|
|
'default':api.settings.get('message_archiving')
|
2018-10-23 03:41:38 +02:00
|
|
|
});
|
2019-08-03 14:16:46 +02:00
|
|
|
Array.from(preference.children).forEach(child => stanza.cnode(child).up());
|
2018-10-25 07:32:44 +02: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.
|
2020-03-31 13:15:57 +02:00
|
|
|
api.sendIQ(stanza)
|
|
|
|
.then(() => feature.save({'preferences': {'default':api.settings.get('message_archiving')}}))
|
2018-10-25 07:32:44 +02:00
|
|
|
.catch(_converse.onMAMError);
|
2018-10-23 03:41:38 +02:00
|
|
|
} else {
|
2020-03-31 13:15:57 +02:00
|
|
|
feature.save({'preferences': {'default':api.settings.get('message_archiving')}});
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-05-22 19:45:00 +02:00
|
|
|
function getMAMPrefsFromFeature (feature) {
|
2018-10-23 03:41:38 +02:00
|
|
|
const prefs = feature.get('preferences') || {};
|
2020-03-31 13:15:57 +02:00
|
|
|
if (feature.get('var') !== Strophe.NS.MAM || api.settings.get('message_archiving') === undefined) {
|
2019-05-22 19:45:00 +02:00
|
|
|
return;
|
|
|
|
}
|
2020-03-31 13:15:57 +02:00
|
|
|
if (prefs['default'] !== api.settings.get('message_archiving')) {
|
|
|
|
api.sendIQ($iq({'type': 'get'}).c('prefs', {'xmlns': Strophe.NS.MAM}))
|
2019-08-03 14:16:46 +02:00
|
|
|
.then(iq => _converse.onMAMPreferences(iq, feature))
|
2018-10-25 07:32:44 +02:00
|
|
|
.catch(_converse.onMAMError);
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
2019-05-22 19:45:00 +02:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
|
2019-11-29 15:01:41 +01:00
|
|
|
function preMUCJoinMAMFetch (room) {
|
|
|
|
if (!_converse.muc_show_logs_before_join ||
|
|
|
|
!room.features.get('mam_enabled') ||
|
2019-12-10 15:40:33 +01:00
|
|
|
room.session.get('connection_status') !== converse.ROOMSTATUS.ENTERED ||
|
2019-11-29 15:01:41 +01:00
|
|
|
room.get('prejoin_mam_fetched')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
room.fetchNewestMessages();
|
|
|
|
room.save({'prejoin_mam_fetched': true});
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************ BEGIN Event Handlers ************************/
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('addClientFeatures', () => api.disco.own.features.add(Strophe.NS.MAM));
|
|
|
|
api.listen.on('serviceDiscovered', getMAMPrefsFromFeature);
|
|
|
|
api.listen.on('chatRoomViewInitialized', view => {
|
2019-11-29 15:01:41 +01:00
|
|
|
if (_converse.muc_show_logs_before_join) {
|
|
|
|
// If we want to show MAM logs before entering the MUC, we need
|
|
|
|
// to be informed once it's clear that this MUC supports MAM.
|
|
|
|
view.model.features.on('change:mam_enabled', preMUCJoinMAMFetch(view.model));
|
|
|
|
}
|
2019-11-28 17:01:08 +01:00
|
|
|
});
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('enteredNewRoom', room => room.features.get('mam_enabled') && room.fetchNewestMessages());
|
2019-11-29 15:01:41 +01:00
|
|
|
|
2019-11-28 17:01:08 +01:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('chatReconnected', chat => {
|
2019-08-10 13:46:01 +02:00
|
|
|
// XXX: For MUCs, we listen to enteredNewRoom instead
|
|
|
|
if (chat.get('type') === _converse.PRIVATE_CHAT_TYPE) {
|
|
|
|
chat.fetchNewestMessages();
|
|
|
|
}
|
|
|
|
});
|
2019-10-24 14:29:15 +02:00
|
|
|
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('afterMessagesFetched', chat => {
|
2019-07-29 15:41:03 +02:00
|
|
|
// XXX: We don't want to query MAM every time this is triggered
|
|
|
|
// since it's not necessary when the chat is restored from cache.
|
|
|
|
// (given that BOSH or SMACKS will ensure that you get messages
|
|
|
|
// sent during the reload).
|
2019-10-24 14:29:15 +02:00
|
|
|
// With MUCs we can listen for `enteredNewRoom`.
|
|
|
|
if (chat.get('type') === _converse.PRIVATE_CHAT_TYPE && !_converse.connection.restored) {
|
2019-07-29 15:41:03 +02:00
|
|
|
chat.fetchNewestMessages();
|
|
|
|
}
|
|
|
|
});
|
2019-05-22 19:45:00 +02:00
|
|
|
/************************ END Event Handlers **************************/
|
2018-10-23 03:41:38 +02:00
|
|
|
|
|
|
|
|
2019-03-15 10:00:08 +01:00
|
|
|
/************************ BEGIN API ************************/
|
2020-03-31 13:15:57 +02:00
|
|
|
Object.assign(api, {
|
2018-10-23 03:41:38 +02:00
|
|
|
/**
|
|
|
|
* The [XEP-0313](https://xmpp.org/extensions/xep-0313.html) Message Archive Management API
|
|
|
|
*
|
|
|
|
* Enables you to query an XMPP server for archived messages.
|
|
|
|
*
|
|
|
|
* See also the [message-archiving](/docs/html/configuration.html#message-archiving)
|
|
|
|
* option in the configuration settings section, which you'll
|
|
|
|
* usually want to use in conjunction with this API.
|
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @namespace api.archive
|
|
|
|
* @memberOf api
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
|
|
|
'archive': {
|
|
|
|
/**
|
|
|
|
* Query for archived messages.
|
|
|
|
*
|
|
|
|
* The options parameter can also be an instance of
|
2019-05-29 17:40:16 +02:00
|
|
|
* _converse.RSM to enable easy querying between results pages.
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
2020-03-31 13:15:57 +02:00
|
|
|
* @method api.archive.query
|
2019-05-29 17:40:16 +02:00
|
|
|
* @param {(Object|_converse.RSM)} options Query parameters, either
|
2018-10-23 03:41:38 +02:00
|
|
|
* MAM-specific or also for Result Set Management.
|
2019-05-29 17:40:16 +02:00
|
|
|
* Can be either an object or an instance of _converse.RSM.
|
2018-10-23 03:41:38 +02:00
|
|
|
* Valid query parameters are:
|
|
|
|
* * `with`
|
|
|
|
* * `start`
|
|
|
|
* * `end`
|
|
|
|
* * `first`
|
|
|
|
* * `last`
|
|
|
|
* * `after`
|
|
|
|
* * `before`
|
|
|
|
* * `index`
|
|
|
|
* * `count`
|
2019-12-26 12:56:58 +01:00
|
|
|
* * `groupchat`
|
2019-05-03 19:47:49 +02:00
|
|
|
* @throws {Error} An error is thrown if the XMPP server responds with an error.
|
2019-08-12 20:16:34 +02:00
|
|
|
* @returns { (Promise<Object> | _converse.TimeoutError) } A promise which resolves
|
|
|
|
* to an object which will have keys `messages` and `rsm` which contains a _converse.RSM
|
|
|
|
* object on which "next" or "previous" can be called before passing it in again
|
2019-05-03 19:47:49 +02:00
|
|
|
* to this method, to get the next or previous page in the result set.
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Requesting all archived messages
|
|
|
|
* // ================================
|
|
|
|
* //
|
|
|
|
* // The simplest query that can be made is to simply not pass in any parameters.
|
|
|
|
* // Such a query will return all archived messages for the current user.
|
|
|
|
*
|
2019-05-03 19:47:49 +02:00
|
|
|
* let result;
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query();
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful, perhaps inform the user?
|
|
|
|
* // The IQ stanza returned by the XMPP server is passed in, so that you
|
|
|
|
* // may inspect it and determine what the problem was.
|
|
|
|
* }
|
|
|
|
* // Do something with the messages, like showing them in your webpage.
|
|
|
|
* result.messages.forEach(m => this.showMessage(m));
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Requesting all archived messages for a particular contact or room
|
|
|
|
* // =================================================================
|
|
|
|
* //
|
|
|
|
* // To query for messages sent between the current user and another user or room,
|
|
|
|
* // the query options need to contain the the JID (Jabber ID) of the user or
|
|
|
|
* // room under the `with` key.
|
|
|
|
*
|
|
|
|
* // For a particular user
|
2019-05-03 19:47:49 +02:00
|
|
|
* let result;
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query({'with': 'john@doe.net'});
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful
|
|
|
|
* }
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
|
|
|
* // For a particular room
|
2019-05-03 19:47:49 +02:00
|
|
|
* let result;
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query({'with': 'discuss@conference.doglovers.net', 'groupchat': true});
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful
|
|
|
|
* }
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Requesting all archived messages before or after a certain date
|
|
|
|
* // ===============================================================
|
|
|
|
* //
|
|
|
|
* // The `start` and `end` parameters are used to query for messages
|
|
|
|
* // within a certain timeframe. The passed in date values may either be ISO8601
|
|
|
|
* // formatted date strings, or JavaScript Date objects.
|
|
|
|
*
|
|
|
|
* const options = {
|
|
|
|
* 'with': 'john@doe.net',
|
|
|
|
* 'start': '2010-06-07T00:00:00Z',
|
|
|
|
* 'end': '2010-07-07T13:23:54Z'
|
|
|
|
* };
|
2019-05-03 19:47:49 +02:00
|
|
|
* let result;
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query(options);
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful
|
|
|
|
* }
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Limiting the amount of messages returned
|
|
|
|
* // ========================================
|
|
|
|
* //
|
|
|
|
* // The amount of returned messages may be limited with the `max` parameter.
|
|
|
|
* // By default, the messages are returned from oldest to newest.
|
|
|
|
*
|
|
|
|
* // Return maximum 10 archived messages
|
2019-05-03 19:47:49 +02:00
|
|
|
* let result;
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query({'with': 'john@doe.net', 'max':10});
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful
|
|
|
|
* }
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Paging forwards through a set of archived messages
|
|
|
|
* // ==================================================
|
|
|
|
* //
|
|
|
|
* // When limiting the amount of messages returned per query, you might want to
|
|
|
|
* // repeatedly make a further query to fetch the next batch of messages.
|
|
|
|
* //
|
|
|
|
* // To simplify this usecase for you, the callback method receives not only an array
|
2019-05-29 17:40:16 +02:00
|
|
|
* // with the returned archived messages, but also a special _converse.RSM (*Result Set Management*)
|
2019-05-03 19:47:49 +02:00
|
|
|
* // object which contains the query parameters you passed in, as well
|
2018-10-23 03:41:38 +02:00
|
|
|
* // as two utility methods `next`, and `previous`.
|
|
|
|
* //
|
|
|
|
* // When you call one of these utility methods on the returned RSM object, and then
|
|
|
|
* // pass the result into a new query, you'll receive the next or previous batch of
|
|
|
|
* // archived messages. Please note, when calling these methods, pass in an integer
|
|
|
|
* // to limit your results.
|
|
|
|
*
|
2019-05-03 19:47:49 +02:00
|
|
|
* let result;
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query({'with': 'john@doe.net', 'max':10});
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful
|
|
|
|
* }
|
|
|
|
* // Do something with the messages, like showing them in your webpage.
|
|
|
|
* result.messages.forEach(m => this.showMessage(m));
|
2018-10-23 03:41:38 +02:00
|
|
|
*
|
2019-05-03 19:47:49 +02:00
|
|
|
* while (result.rsm) {
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query(rsm.next(10));
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful
|
|
|
|
* }
|
|
|
|
* // Do something with the messages, like showing them in your webpage.
|
|
|
|
* result.messages.forEach(m => this.showMessage(m));
|
2018-10-23 03:41:38 +02:00
|
|
|
* }
|
|
|
|
*
|
|
|
|
* @example
|
|
|
|
* // Paging backwards through a set of archived messages
|
|
|
|
* // ===================================================
|
|
|
|
* //
|
|
|
|
* // To page backwards through the archive, you need to know the UID of the message
|
|
|
|
* // which you'd like to page backwards from and then pass that as value for the
|
|
|
|
* // `before` parameter. If you simply want to page backwards from the most recent
|
|
|
|
* // message, pass in the `before` parameter with an empty string value `''`.
|
|
|
|
*
|
2019-05-03 19:47:49 +02:00
|
|
|
* let result;
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query({'before': '', 'max':5});
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful
|
|
|
|
* }
|
|
|
|
* // Do something with the messages, like showing them in your webpage.
|
|
|
|
* result.messages.forEach(m => this.showMessage(m));
|
|
|
|
*
|
|
|
|
* // Now we query again, to get the previous batch.
|
|
|
|
* try {
|
2020-03-31 13:15:57 +02:00
|
|
|
* result = await api.archive.query(rsm.previous(5););
|
2019-05-03 19:47:49 +02:00
|
|
|
* } catch (e) {
|
|
|
|
* // The query was not successful
|
2018-10-23 03:41:38 +02:00
|
|
|
* }
|
2019-05-03 19:47:49 +02:00
|
|
|
* // Do something with the messages, like showing them in your webpage.
|
|
|
|
* result.messages.forEach(m => this.showMessage(m));
|
|
|
|
*
|
2018-10-23 03:41:38 +02:00
|
|
|
*/
|
2019-08-03 14:16:46 +02:00
|
|
|
async query (options) {
|
2020-03-31 13:15:57 +02:00
|
|
|
if (!api.connection.connected()) {
|
2018-10-23 03:41:38 +02:00
|
|
|
throw new Error('Can\'t call `api.archive.query` before having established an XMPP session');
|
2017-11-02 23:23:01 +01:00
|
|
|
}
|
2019-05-03 19:47:49 +02:00
|
|
|
const attrs = {'type':'set'};
|
|
|
|
if (options && options.groupchat) {
|
2019-05-22 19:45:00 +02:00
|
|
|
if (!options['with']) {
|
2019-05-03 19:47:49 +02:00
|
|
|
throw new Error(
|
|
|
|
'You need to specify a "with" value containing '+
|
|
|
|
'the chat room JID, when querying groupchat messages.');
|
|
|
|
}
|
2019-05-22 19:45:00 +02:00
|
|
|
attrs.to = options['with'];
|
2019-05-03 19:47:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const jid = attrs.to || _converse.bare_jid;
|
2020-03-31 13:15:57 +02:00
|
|
|
const supported = await api.disco.supports(Strophe.NS.MAM, jid);
|
2019-05-07 09:53:36 +02:00
|
|
|
if (!supported) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(`Did not fetch MAM archive for ${jid} because it doesn't support ${Strophe.NS.MAM}`);
|
2019-05-07 09:53:36 +02:00
|
|
|
return {'messages': []};
|
2019-05-03 19:47:49 +02:00
|
|
|
}
|
|
|
|
|
2019-10-11 15:32:38 +02:00
|
|
|
const queryid = u.getUniqueId();
|
2019-05-03 19:47:49 +02:00
|
|
|
const stanza = $iq(attrs).c('query', {'xmlns':Strophe.NS.MAM, 'queryid':queryid});
|
|
|
|
if (options) {
|
|
|
|
stanza.c('x', {'xmlns':Strophe.NS.XFORM, 'type': 'submit'})
|
|
|
|
.c('field', {'var':'FORM_TYPE', 'type': 'hidden'})
|
|
|
|
.c('value').t(Strophe.NS.MAM).up().up();
|
|
|
|
|
2019-05-22 19:45:00 +02:00
|
|
|
if (options['with'] && !options.groupchat) {
|
2019-05-03 19:47:49 +02:00
|
|
|
stanza.c('field', {'var':'with'}).c('value')
|
2019-05-22 19:45:00 +02:00
|
|
|
.t(options['with']).up().up();
|
2019-05-03 19:47:49 +02:00
|
|
|
}
|
|
|
|
['start', 'end'].forEach(t => {
|
|
|
|
if (options[t]) {
|
2019-05-06 11:16:56 +02:00
|
|
|
const date = dayjs(options[t]);
|
2019-05-03 19:47:49 +02:00
|
|
|
if (date.isValid()) {
|
2019-05-05 19:05:45 +02:00
|
|
|
stanza.c('field', {'var':t}).c('value').t(date.toISOString()).up().up();
|
2019-05-03 19:47:49 +02:00
|
|
|
} else {
|
|
|
|
throw new TypeError(`archive.query: invalid date provided for: ${t}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
stanza.up();
|
2019-05-29 17:40:16 +02:00
|
|
|
if (options instanceof _converse.RSM) {
|
2019-05-03 19:47:49 +02:00
|
|
|
stanza.cnode(options.toXML());
|
2019-08-03 18:26:57 +02:00
|
|
|
} else if (intersection(_converse.RSM_ATTRIBUTES, Object.keys(options)).length) {
|
2019-05-29 17:40:16 +02:00
|
|
|
stanza.cnode(new _converse.RSM(options).toXML());
|
2019-05-03 19:47:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const messages = [];
|
2019-09-11 14:16:48 +02:00
|
|
|
const message_handler = _converse.connection.addHandler(stanza => {
|
|
|
|
const result = sizzle(`message > result[xmlns="${Strophe.NS.MAM}"]`, stanza).pop();
|
|
|
|
if (result === undefined || result.getAttribute('queryid') !== queryid) {
|
2019-05-03 19:47:49 +02:00
|
|
|
return true;
|
|
|
|
}
|
2019-09-11 14:16:48 +02:00
|
|
|
const from = stanza.getAttribute('from') || _converse.bare_jid;
|
|
|
|
if (options.groupchat) {
|
|
|
|
if (from !== options['with']) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(`Ignoring alleged groupchat MAM message from ${stanza.getAttribute('from')}`);
|
2019-09-11 14:16:48 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
} else if (from !== _converse.bare_jid) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.warn(`Ignoring alleged MAM message from ${stanza.getAttribute('from')}`);
|
2019-09-11 14:16:48 +02:00
|
|
|
return true;
|
2019-05-03 19:47:49 +02:00
|
|
|
}
|
2019-09-11 14:16:48 +02:00
|
|
|
messages.push(stanza);
|
2019-05-03 19:47:49 +02:00
|
|
|
return true;
|
|
|
|
}, Strophe.NS.MAM);
|
|
|
|
|
2019-08-12 20:16:34 +02:00
|
|
|
let error;
|
2020-03-31 13:15:57 +02:00
|
|
|
const iq_result = await api.sendIQ(stanza, api.settings.get('message_archiving_timeout'), false)
|
2019-08-12 20:16:34 +02:00
|
|
|
if (iq_result === null) {
|
|
|
|
const err_msg = "Timeout while trying to fetch archived messages.";
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error(err_msg);
|
2019-08-12 20:16:34 +02:00
|
|
|
error = new _converse.TimeoutError(err_msg);
|
|
|
|
return { messages, error };
|
|
|
|
|
|
|
|
} else if (u.isErrorStanza(iq_result)) {
|
2019-11-06 11:01:34 +01:00
|
|
|
log.error("Error stanza received while trying to fetch archived messages");
|
|
|
|
log.error(iq_result);
|
2019-08-12 20:16:34 +02:00
|
|
|
return { messages };
|
2019-05-03 19:47:49 +02:00
|
|
|
}
|
|
|
|
_converse.connection.deleteHandler(message_handler);
|
2019-08-03 14:16:46 +02:00
|
|
|
|
2019-08-12 20:16:34 +02:00
|
|
|
let rsm;
|
2019-08-03 18:26:57 +02:00
|
|
|
const fin = iq_result && sizzle(`fin[xmlns="${Strophe.NS.MAM}"]`, iq_result).pop();
|
|
|
|
if (fin && [null, 'false'].includes(fin.getAttribute('complete'))) {
|
|
|
|
const set = sizzle(`set[xmlns="${Strophe.NS.RSM}"]`, fin).pop();
|
|
|
|
if (set) {
|
|
|
|
rsm = new _converse.RSM({'xml': set});
|
2019-08-06 22:54:51 +02:00
|
|
|
Object.assign(rsm, Object.assign(pick(options, [...MAM_ATTRIBUTES, ..._converse.RSM_ATTRIBUTES]), rsm));
|
2019-08-03 18:26:57 +02:00
|
|
|
}
|
2019-05-03 19:47:49 +02:00
|
|
|
}
|
2019-08-12 20:16:34 +02:00
|
|
|
return { messages, rsm, error };
|
2017-11-02 23:23:01 +01:00
|
|
|
}
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|
2019-03-15 10:00:08 +01:00
|
|
|
/************************ END API ************************/
|
2018-10-23 03:41:38 +02:00
|
|
|
}
|
|
|
|
});
|