Commit 77e58bff authored by JC Brand's avatar JC Brand

Test that MAM messages are fetched when a chatbox is opened

This is a precursor to also testing (and implementing) that a chat pages
through MAM results (see #1549)

Remove unused method.
parent 2526d804
......@@ -44990,55 +44990,27 @@ _converse_headless_converse_core__WEBPACK_IMPORTED_MODULE_0__["default"].plugins
}
},
fetchArchivedMessagesIfNecessary() {
/* Check if archived messages should be fetched, and if so, do so. */
if (this.disable_mam || this.model.get('mam_initialized')) {
async fetchArchivedMessages(options) {
if (this.disable_mam) {
return;
}
const _converse = this.__super__._converse;
const is_groupchat = this.model.get('type') === CHATROOMS_TYPE;
const mam_jid = is_groupchat ? this.model.get('jid') : _converse.bare_jid;
_converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then(result => {
// Success
if (result.length) {
this.fetchArchivedMessages();
}
this.model.save({
'mam_initialized': true
});
}, () => {
// Error
_converse.log("Error or timeout while checking for MAM support", Strophe.LogLevel.ERROR);
}).catch(msg => {
this.clearSpinner();
_converse.log(msg, Strophe.LogLevel.FATAL);
});
},
async fetchArchivedMessages(options) {
const _converse = this.__super__._converse;
if (this.disable_mam) {
if (!(await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid))) {
return;
}
const is_groupchat = this.model.get('type') === CHATROOMS_TYPE;
let mam_jid, message_handler;
let message_handler;
if (is_groupchat) {
mam_jid = this.model.get('jid');
message_handler = this.model.onMessage.bind(this.model);
} else {
mam_jid = _converse.bare_jid;
message_handler = _converse.chatboxes.onMessage.bind(_converse.chatboxes);
}
if (!(await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid))) {
return;
}
this.addSpinner();
let result;
......@@ -719,4 +719,72 @@
}));
});
});
describe("Chatboxes", function () {
describe("A Chatbox", function () {
it("will fetch archived messages once it's opened",
mock.initConverse(
null, ['discoInitialized'], {},
async function (done, _converse) {
await test_utils.waitForRoster(_converse, 'current', 1);
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@localhost';
await test_utils.openChatBoxFor(_converse, contact_jid);
await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, null, [Strophe.NS.MAM]);
let sent_stanza, IQ_id;
const sendIQ = _converse.connection.sendIQ;
spyOn(_converse.connection, 'sendIQ').and.callFake(function (iq, callback, errback) {
sent_stanza = iq;
IQ_id = sendIQ.bind(this)(iq, callback, errback);
});
const view = _converse.chatboxviews.get(contact_jid);
await test_utils.waitUntil(() => sent_stanza);
const stanza_el = sent_stanza.root().nodeTree;
const queryid = stanza_el.querySelector('query').getAttribute('queryid');
expect(sent_stanza.toString()).toBe(
`<iq id="${stanza_el.getAttribute('id')}" type="set" xmlns="jabber:client">`+
`<query queryid="${queryid}" xmlns="urn:xmpp:mam:2">`+
`<x type="submit" xmlns="jabber:x:data">`+
`<field type="hidden" var="FORM_TYPE"><value>urn:xmpp:mam:2</value></field>`+
`<field var="with"><value>max.frankfurter@localhost</value></field>`+
`</x>`+
`<set xmlns="http://jabber.org/protocol/rsm"><max>50</max><before></before></set>`+
`</query>`+
`</iq>`
);
const msg1 = $msg({'id':'aeb213', 'to': contact_jid})
.c('result', {'xmlns': 'urn:xmpp:mam:2', 'queryid':queryid, 'id':'28482-98726-73623'})
.c('forwarded', {'xmlns':'urn:xmpp:forward:0'})
.c('delay', {'xmlns':'urn:xmpp:delay', 'stamp':'2010-07-10T23:08:25Z'}).up()
.c('message', {
'xmlns':'jabber:client',
'to': contact_jid,
'from': _converse.bare_jid,
'type':'chat' })
.c('body').t("Call me but love, and I'll be new baptized;");
_converse.connection._dataRecv(test_utils.createRequest(msg1));
const msg2 = $msg({'id':'aeb213', 'to': contact_jid})
.c('result', {'xmlns': 'urn:xmpp:mam:2', 'queryid':queryid, 'id':'28482-98726-73624'})
.c('forwarded', {'xmlns':'urn:xmpp:forward:0'})
.c('delay', {'xmlns':'urn:xmpp:delay', 'stamp':'2010-07-10T23:08:25Z'}).up()
.c('message', {
'xmlns':'jabber:client',
'to': contact_jid,
'from': _converse.bare_jid,
'type':'chat' })
.c('body').t("Henceforth I never will be Romeo.");
_converse.connection._dataRecv(test_utils.createRequest(msg2));
const stanza = $iq({'type': 'result', 'id': IQ_id})
.c('fin', {'xmlns': 'urn:xmpp:mam:2'})
.c('set', {'xmlns': 'http://jabber.org/protocol/rsm'})
.c('first', {'index': '0'}).t('23452-4534-1').up()
.c('last').t('09af3-cc343-b409f').up()
.c('count').t('16');
_converse.connection._dataRecv(test_utils.createRequest(stanza));
done();
}));
});
});
}));
......@@ -57,48 +57,22 @@ converse.plugins.add('converse-mam-views', {
}
},
fetchArchivedMessagesIfNecessary () {
/* Check if archived messages should be fetched, and if so, do so. */
if (this.disable_mam || this.model.get('mam_initialized')) {
return;
}
const { _converse } = this.__super__;
_converse.api.disco.supports(Strophe.NS.MAM, _converse.bare_jid).then(
(result) => { // Success
if (result.length) {
this.fetchArchivedMessages();
}
this.model.save({'mam_initialized': true});
},
() => { // Error
_converse.log(
"Error or timeout while checking for MAM support",
Strophe.LogLevel.ERROR
);
}
).catch((msg) => {
this.clearSpinner();
_converse.log(msg, Strophe.LogLevel.FATAL);
});
},
async fetchArchivedMessages (options) {
const { _converse } = this.__super__;
if (this.disable_mam) {
return;
}
const { _converse } = this.__super__;
const is_groupchat = this.model.get('type') === CHATROOMS_TYPE;
let mam_jid, message_handler;
const mam_jid = is_groupchat ? this.model.get('jid') : _converse.bare_jid;
if (!(await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid))) {
return;
}
let message_handler;
if (is_groupchat) {
mam_jid = this.model.get('jid');
message_handler = this.model.onMessage.bind(this.model);
} else {
mam_jid = _converse.bare_jid;
message_handler = _converse.chatboxes.onMessage.bind(_converse.chatboxes)
}
if (!(await _converse.api.disco.supports(Strophe.NS.MAM, mam_jid))) {
return;
}
this.addSpinner();
let result;
try {
......
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