|
@@ -76,18 +76,38 @@ converse.plugins.add('converse-mam', {
|
|
|
const most_recent_msg = u.getMostRecentMessage(this);
|
|
|
|
|
|
if (!most_recent_msg) {
|
|
|
- this.fetchArchivedMessages();
|
|
|
+ this.fetchArchivedMessages({'before': ''});
|
|
|
} else {
|
|
|
const stanza_id = most_recent_msg.get(`stanza_id ${this.get('jid')}`);
|
|
|
if (stanza_id) {
|
|
|
- this.fetchArchivedMessages({'after': stanza_id});
|
|
|
+ this.fetchArchivedMessages({'after': stanza_id}, 'forwards');
|
|
|
} else {
|
|
|
- this.fetchArchivedMessages({'start': most_recent_msg.get('time')});
|
|
|
+ this.fetchArchivedMessages({'start': most_recent_msg.get('time')}, 'forwards');
|
|
|
}
|
|
|
}
|
|
|
},
|
|
|
|
|
|
- async fetchArchivedMessages (options) {
|
|
|
+ /**
|
|
|
+ * 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 } [page] - Whether this function should recursively
|
|
|
+ * page through the entire result set if a limited number of results
|
|
|
+ * were returned.
|
|
|
+ */
|
|
|
+ async fetchArchivedMessages (options={}, page) {
|
|
|
if (this.disable_mam) {
|
|
|
return;
|
|
|
}
|
|
@@ -96,30 +116,30 @@ converse.plugins.add('converse-mam', {
|
|
|
if (!(await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid))) {
|
|
|
return;
|
|
|
}
|
|
|
- let message_handler;
|
|
|
- if (is_groupchat) {
|
|
|
- message_handler = this.onMessage.bind(this);
|
|
|
- } else {
|
|
|
- message_handler = _converse.chatboxes.onMessage.bind(_converse.chatboxes)
|
|
|
- }
|
|
|
+ const message_handler = is_groupchat ?
|
|
|
+ this.onMessage.bind(this) :
|
|
|
+ _converse.chatboxes.onMessage.bind(_converse.chatboxes);
|
|
|
+
|
|
|
const query = Object.assign({
|
|
|
'groupchat': is_groupchat,
|
|
|
- 'before': '', // Page backwards from the most recent message
|
|
|
'max': _converse.archived_messages_page_size,
|
|
|
'with': this.get('jid'),
|
|
|
}, options);
|
|
|
+
|
|
|
const result = await _converse.api.archive.query(query);
|
|
|
result.messages.forEach(message_handler);
|
|
|
|
|
|
- const catching_up = query.before || query.after;
|
|
|
- if (result.rsm) {
|
|
|
- if (catching_up) {
|
|
|
- return this.fetchArchivedMessages(result.rsm.previous(_converse.archived_messages_page_size));
|
|
|
- } 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.
|
|
|
+ if (page && result.rsm) {
|
|
|
+ if (page === 'forwards') {
|
|
|
+ options = result.rsm.next(_converse.archived_messages_page_size, options.before);
|
|
|
+ } else if (page === 'backwards') {
|
|
|
+ options = result.rsm.previous(_converse.archived_messages_page_size, options.after);
|
|
|
}
|
|
|
+ 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.
|
|
|
}
|
|
|
},
|
|
|
|
|
@@ -149,7 +169,7 @@ converse.plugins.add('converse-mam', {
|
|
|
this.get('mam_initialized')) {
|
|
|
return;
|
|
|
}
|
|
|
- this.fetchArchivedMessages();
|
|
|
+ this.fetchNewestMessages();
|
|
|
this.save({'mam_initialized': true});
|
|
|
}
|
|
|
});
|
|
@@ -499,7 +519,7 @@ converse.plugins.add('converse-mam', {
|
|
|
const set = sizzle(`set[xmlns="${Strophe.NS.RSM}"]`, fin).pop();
|
|
|
if (set) {
|
|
|
rsm = new _converse.RSM({'xml': set});
|
|
|
- Object.assign(rsm, pick(options, [...MAM_ATTRIBUTES, ..._converse.RSM_ATTRIBUTES]));
|
|
|
+ Object.assign(rsm, Object.assign(pick(options, [...MAM_ATTRIBUTES, ..._converse.RSM_ATTRIBUTES]), rsm));
|
|
|
}
|
|
|
}
|
|
|
return { messages, rsm }
|