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";
|
2020-05-18 10:54:37 +02:00
|
|
|
import { _converse, api, converse } from "@converse/headless/converse-core";
|
2020-06-08 16:08:50 +02:00
|
|
|
import { intersection, pick } from 'lodash-es'
|
2019-11-06 11:01:34 +01:00
|
|
|
import log from "./log";
|
2018-10-23 03:41:38 +02:00
|
|
|
import sizzle from "sizzle";
|
2020-04-24 17:33:32 +02:00
|
|
|
import st from "./utils/stanza";
|
2018-01-09 23:05:23 +01:00
|
|
|
|
2019-10-09 16:01:38 +02:00
|
|
|
const { Strophe, $iq, dayjs } = converse.env;
|
2020-04-24 17:33:32 +02:00
|
|
|
const { NS } = Strophe;
|
2018-10-23 03:41:38 +02:00
|
|
|
const u = converse.env.utils;
|
|
|
|
|
|
|
|
// XEP-0313 Message Archive Management
|
|
|
|
const MAM_ATTRIBUTES = ['with', 'start', 'end'];
|
|
|
|
|
|
|
|
|
2020-04-23 10:04:14 +02:00
|
|
|
/**
|
|
|
|
* The MUC utils object. Contains utility functions related to multi-user chat.
|
|
|
|
* @mixin MAMEnabledChat
|
|
|
|
*/
|
|
|
|
const MAMEnabledChat = {
|
|
|
|
/**
|
|
|
|
* Fetches messages that might have been archived *after*
|
|
|
|
* the last archived message in our local cache.
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
fetchNewestMessages () {
|
|
|
|
if (this.disable_mam) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const most_recent_msg = this.getMostRecentMessage();
|
2020-07-14 22:41:26 +02:00
|
|
|
// if clear_messages_on_reconnection is true, than any recent messages
|
|
|
|
// must have been received *after* connection and we instead must query
|
|
|
|
// for earlier messages
|
|
|
|
if (most_recent_msg && !api.settings.get('clear_messages_on_reconnection')) {
|
2020-04-23 10:04:14 +02:00
|
|
|
const stanza_id = most_recent_msg.get(`stanza_id ${this.get('jid')}`);
|
|
|
|
if (stanza_id) {
|
|
|
|
this.fetchArchivedMessages({'after': stanza_id}, 'forwards');
|
|
|
|
} else {
|
|
|
|
this.fetchArchivedMessages({'start': most_recent_msg.get('time')}, 'forwards');
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.fetchArchivedMessages({'before': ''});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-04-24 17:33:32 +02:00
|
|
|
async handleMAMResult (result, query, options, page_direction) {
|
2020-07-08 14:08:08 +02:00
|
|
|
await api.emojis.initialize();
|
2020-04-24 17:33:32 +02:00
|
|
|
const is_muc = this.get('type') === _converse.CHATROOMS_TYPE;
|
|
|
|
result.messages = result.messages.map(
|
|
|
|
s => (is_muc ? st.parseMUCMessage(s, this, _converse) : st.parseMessage(s, _converse))
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Synchronous event which allows listeners to first do some
|
|
|
|
* work based on the MAM result before calling the handlers here.
|
|
|
|
* @event _converse#MAMResult
|
|
|
|
*/
|
2020-05-05 14:06:47 +02:00
|
|
|
const data = { query, 'chatbox': this, 'messages': result.messages };
|
|
|
|
await api.trigger('MAMResult', data, {'synchronous': true});
|
2020-04-24 17:33:32 +02:00
|
|
|
|
|
|
|
result.messages.forEach(m => this.queueMessage(m));
|
|
|
|
if (result.error) {
|
2020-06-11 16:42:48 +02:00
|
|
|
const event_id = result.error.retry_event_id = u.getUniqueId();
|
|
|
|
api.listen.once(event_id, () => this.fetchArchivedMessages(options, page_direction));
|
2020-04-24 17:33:32 +02:00
|
|
|
this.createMessageFromError(result.error);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2020-04-23 10:04:14 +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.
|
|
|
|
* @param { boolean } [options.groupchat] - True if archive in groupchat.
|
2020-04-24 17:33:32 +02:00
|
|
|
* @param { ('forwards'|'backwards')} [page_direction] - Determines whether this function should
|
|
|
|
* recursively page through the entire result set if a limited number of results were returned.
|
2020-04-23 10:04:14 +02:00
|
|
|
*/
|
2020-04-24 17:33:32 +02:00
|
|
|
async fetchArchivedMessages (options={}, page_direction) {
|
2020-04-23 10:04:14 +02:00
|
|
|
if (this.disable_mam) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-24 17:33:32 +02:00
|
|
|
const is_muc = this.get('type') === _converse.CHATROOMS_TYPE;
|
|
|
|
const mam_jid = is_muc ? this.get('jid') : _converse.bare_jid;
|
|
|
|
if (!(await api.disco.supports(NS.MAM, mam_jid))) {
|
2020-04-23 10:04:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const query = Object.assign({
|
2020-04-24 17:33:32 +02:00
|
|
|
'groupchat': is_muc,
|
|
|
|
'max': api.settings.get('archived_messages_page_size'),
|
|
|
|
'with': this.get('jid'),
|
|
|
|
}, options);
|
2020-04-23 10:04:14 +02:00
|
|
|
|
|
|
|
const result = await api.archive.query(query);
|
2020-04-24 17:33:32 +02:00
|
|
|
await this.handleMAMResult(result, query, options, page_direction);
|
2020-04-23 10:04:14 +02:00
|
|
|
|
2020-04-24 17:33:32 +02:00
|
|
|
if (page_direction && result.rsm) {
|
|
|
|
if (page_direction === 'forwards') {
|
2020-04-23 10:04:14 +02:00
|
|
|
options = result.rsm.next(api.settings.get('archived_messages_page_size'), options.before);
|
2020-04-24 17:33:32 +02:00
|
|
|
} else if (page_direction === 'backwards') {
|
2020-04-23 10:04:14 +02:00
|
|
|
options = result.rsm.previous(api.settings.get('archived_messages_page_size'), options.after);
|
|
|
|
}
|
2020-04-24 17:33:32 +02:00
|
|
|
return this.fetchArchivedMessages(options, page_direction);
|
2020-04-23 10:04:14 +02:00
|
|
|
} 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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-10-23 03:41:38 +02:00
|
|
|
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.
|
|
|
|
*/
|
2020-06-03 09:17:13 +02:00
|
|
|
api.settings.extend({
|
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
|
|
|
|
});
|
|
|
|
|
|
|
|
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) {
|
2020-06-03 17:59:41 +02:00
|
|
|
if (iq?.querySelectorAll('feature-not-implemented').length) {
|
|
|
|
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.
|
|
|
|
*/
|
2020-04-24 17:33:32 +02:00
|
|
|
const preference = sizzle(`prefs[xmlns="${NS.MAM}"]`, iq).pop();
|
2018-10-23 03:41:38 +02:00
|
|
|
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', {
|
2020-04-24 17:33:32 +02:00
|
|
|
'xmlns':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-04-24 17:33:32 +02:00
|
|
|
if (feature.get('var') !== 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')) {
|
2020-04-24 17:33:32 +02:00
|
|
|
api.sendIQ($iq({'type': 'get'}).c('prefs', {'xmlns': 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) {
|
2020-09-10 11:08:27 +02:00
|
|
|
if (!api.settings.get('muc_show_logs_before_join') ||
|
2019-11-29 15:01:41 +01:00
|
|
|
!room.features.get('mam_enabled') ||
|
|
|
|
room.get('prejoin_mam_fetched')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
room.fetchNewestMessages();
|
|
|
|
room.save({'prejoin_mam_fetched': true});
|
|
|
|
}
|
|
|
|
|
|
|
|
/************************ BEGIN Event Handlers ************************/
|
2020-04-24 17:33:32 +02:00
|
|
|
api.listen.on('addClientFeatures', () => api.disco.own.features.add(NS.MAM));
|
2020-03-31 13:15:57 +02:00
|
|
|
api.listen.on('serviceDiscovered', getMAMPrefsFromFeature);
|
|
|
|
api.listen.on('chatRoomViewInitialized', view => {
|
2020-09-10 11:08:27 +02:00
|
|
|
if (api.settings.get('muc_show_logs_before_join')) {
|
|
|
|
preMUCJoinMAMFetch(view.model);
|
2019-11-29 15:01:41 +01:00
|
|
|
// 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.
|
2020-09-10 11:08:27 +02:00
|
|
|
view.model.features.on('change:mam_enabled', () => preMUCJoinMAMFetch(view.model));
|
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('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-04-24 17:33:32 +02:00
|
|
|
const supported = await api.disco.supports(NS.MAM, jid);
|
2019-05-07 09:53:36 +02:00
|
|
|
if (!supported) {
|
2020-04-24 17:33:32 +02:00
|
|
|
log.warn(`Did not fetch MAM archive for ${jid} because it doesn't support ${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();
|
2020-04-24 17:33:32 +02:00
|
|
|
const stanza = $iq(attrs).c('query', {'xmlns':NS.MAM, 'queryid':queryid});
|
2019-05-03 19:47:49 +02:00
|
|
|
if (options) {
|
2020-04-24 17:33:32 +02:00
|
|
|
stanza.c('x', {'xmlns':NS.XFORM, 'type': 'submit'})
|
2019-05-03 19:47:49 +02:00
|
|
|
.c('field', {'var':'FORM_TYPE', 'type': 'hidden'})
|
2020-04-24 17:33:32 +02:00
|
|
|
.c('value').t(NS.MAM).up().up();
|
2019-05-03 19:47:49 +02:00
|
|
|
|
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 => {
|
2020-04-24 17:33:32 +02:00
|
|
|
const result = sizzle(`message > result[xmlns="${NS.MAM}"]`, stanza).pop();
|
2019-09-11 14:16:48 +02:00
|
|
|
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;
|
2020-04-24 17:33:32 +02:00
|
|
|
}, NS.MAM);
|
2019-05-03 19:47:49 +02:00
|
|
|
|
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;
|
2020-04-24 17:33:32 +02:00
|
|
|
const fin = iq_result && sizzle(`fin[xmlns="${NS.MAM}"]`, iq_result).pop();
|
2019-08-03 18:26:57 +02:00
|
|
|
if (fin && [null, 'false'].includes(fin.getAttribute('complete'))) {
|
2020-04-24 17:33:32 +02:00
|
|
|
const set = sizzle(`set[xmlns="${NS.RSM}"]`, fin).pop();
|
2019-08-03 18:26:57 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|