(function (root, factory) {
define([
"jasmine",
"mock",
"test-utils"
], factory);
} (this, function (jasmine, mock, test_utils) {
"use strict";
const { Strophe, $iq } = converse.env;
const u = converse.env.utils;
async function sendAndThenRetractMessage (_converse, view) {
view.model.sendMessage('hello world');
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg').length === 1);
const msg_obj = view.model.messages.at(0);
const reflection_stanza = u.toStanza(`
${msg_obj.get('message')}
`);
await view.model.onMessage(reflection_stanza);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg__body.chat-msg__body--received').length, 500);
const retract_button = await u.waitUntil(() => view.el.querySelector('.chat-msg__content .chat-msg__action-retract'));
retract_button.click();
await u.waitUntil(() => u.isVisible(document.querySelector('#converse-modals .modal')));
const submit_button = document.querySelector('#converse-modals .modal button[type="submit"]');
submit_button.click();
const sent_stanzas = _converse.connection.sent_stanzas;
return u.waitUntil(() => sent_stanzas.filter(s => s.querySelector('message apply-to[xmlns="urn:xmpp:fasten:0"]')).pop());
}
describe("Message Retractions", function () {
describe("A groupchat message retraction", function () {
it("is not applied if it's not from the right author",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const received_stanza = u.toStanza(`
Hello world
`);
const view = _converse.api.chatviews.get(muc_jid);
await view.model.onMessage(received_stanza);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg').length === 1);
expect(view.model.messages.at(0).get('retracted')).toBeFalsy();
expect(view.model.messages.at(0).get('is_ephemeral')).toBeFalsy();
const retraction_stanza = u.toStanza(`
`);
spyOn(view.model, 'handleRetraction').and.callThrough();
_converse.connection._dataRecv(test_utils.createRequest(retraction_stanza));
await u.waitUntil(() => view.model.handleRetraction.calls.count() === 1);
expect(view.model.handleRetraction.calls.first().returnValue).toBe(true);
expect(view.el.querySelectorAll('.chat-msg').length).toBe(1);
expect(view.model.messages.length).toBe(2);
expect(view.model.messages.at(1).get('retracted')).toBeTruthy();
expect(view.model.messages.at(1).get('is_ephemeral')).toBeFalsy();
expect(view.model.messages.at(1).get('dangling_retraction')).toBe(true);
expect(view.model.messages.at(0).get('retracted')).toBeFalsy();
expect(view.model.messages.at(0).get('is_ephemeral')).toBeFalsy();
done();
}));
it("can be received before the message it pertains to",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const date = (new Date()).toISOString();
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const retraction_stanza = u.toStanza(`
`);
const view = _converse.api.chatviews.get(muc_jid);
spyOn(converse.env.log, 'warn');
spyOn(view.model, 'handleRetraction').and.callThrough();
_converse.connection._dataRecv(test_utils.createRequest(retraction_stanza));
await u.waitUntil(() => view.model.handleRetraction.calls.count() === 1);
await u.waitUntil(() => view.model.messages.length === 1);
expect(view.model.handleRetraction.calls.first().returnValue).toBe(true);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('retracted')).toBeTruthy();
expect(view.model.messages.at(0).get('dangling_retraction')).toBe(true);
const received_stanza = u.toStanza(`
Hello world
`);
_converse.connection._dataRecv(test_utils.createRequest(received_stanza));
await u.waitUntil(() => view.model.handleRetraction.calls.count() === 2);
expect(view.el.querySelectorAll('.chat-msg').length).toBe(0);
expect(view.model.messages.length).toBe(1);
const message = view.model.messages.at(0)
expect(message.get('retracted')).toBeTruthy();
expect(message.get('dangling_retraction')).toBe(false);
expect(message.get('origin_id')).toBe('origin-id-1');
expect(message.get(`stanza_id ${muc_jid}`)).toBe('stanza-id-1');
expect(message.get('time')).toBe(date);
expect(message.get('type')).toBe('groupchat');
expect(view.model.handleRetraction.calls.all().pop().returnValue).toBe(true);
done();
}));
});
describe("A message retraction", function () {
it("can be received before the message it pertains to",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const date = (new Date()).toISOString();
await test_utils.waitForRoster(_converse, 'current', 1);
await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [Strophe.NS.SID]);
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
const view = await test_utils.openChatBoxFor(_converse, contact_jid);
spyOn(view.model, 'handleRetraction').and.callThrough();
const retraction_stanza = u.toStanza(`
`);
const promise = new Promise(resolve => _converse.api.listen.on('messageAdded', resolve));
_converse.connection._dataRecv(test_utils.createRequest(retraction_stanza));
await u.waitUntil(() => view.model.messages.length === 1);
await promise;
const message = view.model.messages.at(0);
expect(message.get('dangling_retraction')).toBe(true);
expect(message.get('is_ephemeral')).toBe(false);
expect(message.get('retracted')).toBeTruthy();
expect(view.el.querySelectorAll('.chat-msg').length).toBe(0);
const stanza = u.toStanza(`
Hello world
`);
_converse.connection._dataRecv(test_utils.createRequest(stanza));
await u.waitUntil(() => view.model.handleRetraction.calls.count() === 2);
expect(view.model.messages.length).toBe(1);
expect(message.get('retracted')).toBeTruthy();
expect(message.get('dangling_retraction')).toBe(false);
expect(message.get('origin_id')).toBe('2e972ea0-0050-44b7-a830-f6638a2595b3');
expect(message.get('time')).toBe(date);
expect(message.get('type')).toBe('chat');
done();
}));
});
describe("A Received Chat Message", function () {
it("can be followed up by a retraction",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
await test_utils.waitForRoster(_converse, 'current', 1);
await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, [], [Strophe.NS.SID]);
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
const view = await test_utils.openChatBoxFor(_converse, contact_jid);
let stanza = u.toStanza(`
😊
`);
_converse.connection._dataRecv(test_utils.createRequest(stanza));
await u.waitUntil(() => view.model.messages.length === 1);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg').length === 1);
stanza = u.toStanza(`
This message will be retracted
`);
_converse.connection._dataRecv(test_utils.createRequest(stanza));
await u.waitUntil(() => view.model.messages.length === 2);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg').length === 2);
const retraction_stanza = u.toStanza(`
`);
_converse.connection._dataRecv(test_utils.createRequest(retraction_stanza));
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 1);
expect(view.model.messages.length).toBe(2);
const message = view.model.messages.at(1);
expect(message.get('retracted')).toBeTruthy();
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const msg_el = view.el.querySelector('.chat-msg--retracted .chat-msg__message');
expect(msg_el.textContent.trim()).toBe('Mercutio has retracted this message');
expect(u.hasClass('chat-msg--followup', view.el.querySelector('.chat-msg--retracted'))).toBe(true);
done();
}));
});
describe("A Sent Chat Message", function () {
it("can be retracted by its author",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
await test_utils.waitForRoster(_converse, 'current', 1);
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
const view = await test_utils.openChatBoxFor(_converse, contact_jid);
view.model.sendMessage('hello world');
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg').length === 1);
const retract_button = await u.waitUntil(() => view.el.querySelector('.chat-msg__content .chat-msg__action-retract'));
retract_button.click();
await u.waitUntil(() => u.isVisible(document.querySelector('#converse-modals .modal')));
const submit_button = document.querySelector('#converse-modals .modal button[type="submit"]');
submit_button.click();
const sent_stanzas = _converse.connection.sent_stanzas;
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 1);
const msg_obj = view.model.messages.at(0);
const retraction_stanza = await u.waitUntil(() => sent_stanzas.filter(s => s.querySelector('message apply-to[xmlns="urn:xmpp:fasten:0"]')).pop());
expect(Strophe.serialize(retraction_stanza)).toBe(
``+
``+
``+
``+
``+
``);
const message = view.model.messages.at(0);
expect(view.model.messages.length).toBe(1);
expect(message.get('retracted')).toBeTruthy();
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message');
expect(el.textContent.trim()).toBe('Romeo Montague has retracted this message');
done();
}));
});
describe("A Received Groupchat Message", function () {
it("can be followed up by a retraction by the author",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const received_stanza = u.toStanza(`
Hello world
`);
const view = _converse.api.chatviews.get(muc_jid);
await view.model.onMessage(received_stanza);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg').length === 1);
expect(view.model.messages.at(0).get('retracted')).toBeFalsy();
expect(view.model.messages.at(0).get('is_ephemeral')).toBeFalsy();
const retraction_stanza = u.toStanza(`
`);
_converse.connection._dataRecv(test_utils.createRequest(retraction_stanza));
// We opportunistically save the message as retracted, even before receiving the retraction message
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 1);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('retracted')).toBeTruthy();
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const msg_el = view.el.querySelector('.chat-msg--retracted .chat-msg__message');
expect(msg_el.textContent.trim()).toBe('eve has retracted this message');
expect(msg_el.querySelector('.chat-msg--retracted q')).toBe(null);
done();
}));
it("can be retracted by a moderator, with the IQ response received before the retraction message",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.api.chatviews.get(muc_jid);
const occupant = view.model.getOwnOccupant();
expect(occupant.get('role')).toBe('moderator');
const received_stanza = u.toStanza(`
Visit this site to get free Bitcoin!
`);
await view.model.onMessage(received_stanza);
await u.waitUntil(() => view.model.messages.length === 1);
expect(view.model.messages.at(0).get('retracted')).toBeFalsy();
const reason = "This content is inappropriate for this forum!"
const retract_button = await u.waitUntil(() => view.el.querySelector('.chat-msg__content .chat-msg__action-retract'));
retract_button.click();
await u.waitUntil(() => u.isVisible(document.querySelector('#converse-modals .modal')));
const reason_input = document.querySelector('#converse-modals .modal input[name="reason"]');
reason_input.value = 'This content is inappropriate for this forum!';
const submit_button = document.querySelector('#converse-modals .modal button[type="submit"]');
submit_button.click();
const sent_IQs = _converse.connection.IQ_stanzas;
const stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector('iq apply-to[xmlns="urn:xmpp:fasten:0"]')).pop());
const message = view.model.messages.at(0);
const stanza_id = message.get(`stanza_id ${view.model.get('jid')}`);
expect(Strophe.serialize(stanza)).toBe(
``+
``+
``+
``+
`This content is inappropriate for this forum!`+
``+
``+
``);
const result_iq = $iq({'from': muc_jid, 'id': stanza.getAttribute('id'), 'to': _converse.bare_jid, 'type': 'result'});
_converse.connection._dataRecv(test_utils.createRequest(result_iq));
// We opportunistically save the message as retracted, even before receiving the retraction message
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 1);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('moderated')).toBe('retracted');
expect(view.model.messages.at(0).get('moderation_reason')).toBe(reason);
expect(view.model.messages.at(0).get('is_ephemeral')).toBe(false);
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const msg_el = view.el.querySelector('.chat-msg--retracted .chat-msg__message');
expect(msg_el.firstElementChild.textContent.trim()).toBe('romeo has retracted this message from mallory');
const qel = msg_el.querySelector('q');
expect(qel.textContent.trim()).toBe('This content is inappropriate for this forum!');
// The server responds with a retraction message
const retraction = u.toStanza(`
${reason}
`);
await view.model.onMessage(retraction);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('moderated')).toBe('retracted');
expect(view.model.messages.at(0).get('moderation_reason')).toBe(reason);
expect(view.model.messages.at(0).get('is_ephemeral')).toBe(false);
done();
}));
it("can be retracted by a moderator, with the retraction message received before the IQ response",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.api.chatviews.get(muc_jid);
const occupant = view.model.getOwnOccupant();
expect(occupant.get('role')).toBe('moderator');
const received_stanza = u.toStanza(`
Visit this site to get free Bitcoin!
`);
await view.model.onMessage(received_stanza);
await u.waitUntil(() => view.model.messages.length === 1);
const retract_button = await u.waitUntil(() => view.el.querySelector('.chat-msg__content .chat-msg__action-retract'));
retract_button.click();
await u.waitUntil(() => u.isVisible(document.querySelector('#converse-modals .modal')));
const reason_input = document.querySelector('#converse-modals .modal input[name="reason"]');
const reason = "This content is inappropriate for this forum!"
reason_input.value = reason;
const submit_button = document.querySelector('#converse-modals .modal button[type="submit"]');
submit_button.click();
const sent_IQs = _converse.connection.IQ_stanzas;
const stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector('iq apply-to[xmlns="urn:xmpp:fasten:0"]')).pop());
const message = view.model.messages.at(0);
const stanza_id = message.get(`stanza_id ${view.model.get('jid')}`);
// The server responds with a retraction message
const retraction = u.toStanza(`
${reason}
`);
await view.model.onMessage(retraction);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 1);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('moderated')).toBe('retracted');
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const msg_el = view.el.querySelector('.chat-msg--retracted .chat-msg__message div');
expect(msg_el.textContent).toBe('romeo has retracted this message from mallory');
const qel = view.el.querySelector('.chat-msg--retracted .chat-msg__message q');
expect(qel.textContent).toBe('This content is inappropriate for this forum!');
const result_iq = $iq({'from': muc_jid, 'id': stanza.getAttribute('id'), 'to': _converse.bare_jid, 'type': 'result'});
_converse.connection._dataRecv(test_utils.createRequest(result_iq));
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('moderated')).toBe('retracted');
expect(view.model.messages.at(0).get('moderated_by')).toBe(_converse.bare_jid);
expect(view.model.messages.at(0).get('moderation_reason')).toBe(reason);
done();
}));
});
describe("A Sent Groupchat Message", function () {
it("can be retracted by its author",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.api.chatviews.get(muc_jid);
const occupant = view.model.getOwnOccupant();
expect(occupant.get('role')).toBe('moderator');
occupant.save('role', 'member');
const retraction_stanza = await sendAndThenRetractMessage(_converse, view);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 1);
const msg_obj = view.model.messages.at(0);
expect(Strophe.serialize(retraction_stanza)).toBe(
``+
``+
``+
``+
``+
``);
const message = view.model.messages.at(0);
expect(message.get('retracted')).toBeTruthy();
expect(message.get('is_ephemeral')).toBe(false);
const stanza_id = message.get(`stanza_id ${muc_jid}`);
// The server responds with a retraction message
const reflection = u.toStanza(`
`);
spyOn(view.model, 'handleRetraction').and.callThrough();
_converse.connection._dataRecv(test_utils.createRequest(reflection));
await u.waitUntil(() => view.model.handleRetraction.calls.count() === 1);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('retracted')).toBeTruthy();
expect(view.model.messages.at(0).get('is_ephemeral')).toBe(false);
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message div');
expect(el.textContent).toBe('romeo has retracted this message');
done();
}));
it("can be retracted by its author, causing an error message in response",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.api.chatviews.get(muc_jid);
const occupant = view.model.getOwnOccupant();
expect(occupant.get('role')).toBe('moderator');
occupant.save('role', 'member');
const retraction_stanza = await sendAndThenRetractMessage(_converse, view);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 1);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('retracted')).toBeTruthy();
const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message div');
expect(el.textContent.trim()).toBe('romeo has retracted this message');
const message = view.model.messages.at(0);
const stanza_id = message.get(`stanza_id ${view.model.get('jid')}`);
// The server responds with an error message
const error = u.toStanza(`
`);
_converse.connection._dataRecv(test_utils.createRequest(error));
await u.waitUntil(() => view.el.querySelectorAll('.chat-error').length === 1);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 0);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('retracted')).toBeFalsy();
expect(view.model.messages.at(0).get('is_ephemeral')).toBeFalsy();
expect(view.el.querySelectorAll('.chat-error').length).toBe(1);
const errmsg = view.el.querySelector('.chat-error');
expect(errmsg.textContent).toBe("Sorry, something went wrong while trying to retract your message.");
done();
}));
it("can be retracted by its author, causing an timeout error in response",
mock.initConverse(
['rosterGroupsFetched', 'chatBoxesFetched'], {},
async function (done, _converse) {
_converse.STANZA_TIMEOUT = 1;
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.api.chatviews.get(muc_jid);
const occupant = view.model.getOwnOccupant();
expect(occupant.get('role')).toBe('moderator');
occupant.save('role', 'member');
await sendAndThenRetractMessage(_converse, view);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 1);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('retracted')).toBeTruthy();
const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message div');
expect(el.textContent.trim()).toBe('romeo has retracted this message');
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg').length === 1);
await u.waitUntil(() => view.el.querySelectorAll('.chat-msg--retracted').length === 0);
expect(view.model.messages.length).toBe(1);
expect(view.model.messages.at(0).get('retracted')).toBeFalsy();
expect(view.model.messages.at(0).get('is_ephemeral')).toBeFalsy();
const error_messages = view.el.querySelectorAll('.chat-error');
expect(error_messages.length).toBe(2);
expect(error_messages[0].textContent).toBe("Sorry, something went wrong while trying to retract your message.");
expect(error_messages[1].textContent).toBe("Timeout Error: No response from server");
done();
}));
});
describe("when archived", function () {
it("may be returned as a tombstone message",
mock.initConverse(
['discoInitialized'], {},
async function (done, _converse) {
await test_utils.waitForRoster(_converse, 'current', 1);
const contact_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit';
await test_utils.openChatBoxFor(_converse, contact_jid);
await test_utils.waitUntilDiscoConfirmed(_converse, _converse.bare_jid, null, [Strophe.NS.MAM]);
const sent_IQs = _converse.connection.IQ_stanzas;
const stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq[type="set"] query[xmlns="${Strophe.NS.MAM}"]`)).pop());
const queryid = stanza.querySelector('query').getAttribute('queryid');
const view = _converse.chatboxviews.get(contact_jid);
const first_id = u.getUniqueId();
spyOn(view.model, 'handleRetraction').and.callThrough();
const first_message = u.toStanza(`
😊
`);
_converse.connection._dataRecv(test_utils.createRequest(first_message));
const tombstone = u.toStanza(`
`);
_converse.connection._dataRecv(test_utils.createRequest(tombstone));
const last_id = u.getUniqueId();
const retraction = u.toStanza(`
`);
_converse.connection._dataRecv(test_utils.createRequest(retraction));
const iq_result = $iq({'type': 'result', 'id': stanza.getAttribute('id')})
.c('fin', {'xmlns': 'urn:xmpp:mam:2'})
.c('set', {'xmlns': 'http://jabber.org/protocol/rsm'})
.c('first', {'index': '0'}).t(first_id).up()
.c('last').t(last_id).up()
.c('count').t('2');
_converse.connection._dataRecv(test_utils.createRequest(iq_result));
await u.waitUntil(() => view.model.handleRetraction.calls.count() === 3);
expect(view.model.messages.length).toBe(2);
const message = view.model.messages.at(1);
expect(message.get('retracted')).toBeTruthy();
expect(message.get('is_tombstone')).toBe(true);
expect(view.model.handleRetraction.calls.first().returnValue).toBe(false);
expect(view.model.handleRetraction.calls.all()[1].returnValue).toBe(false);
expect(view.model.handleRetraction.calls.all()[2].returnValue).toBe(true);
expect(view.el.querySelectorAll('.chat-msg').length).toBe(2);
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message div');
expect(el.textContent.trim()).toBe('Mercutio has retracted this message');
expect(u.hasClass('chat-msg--followup', el.parentElement)).toBe(false);
done();
}));
it("may be returned as a tombstone groupchat message",
mock.initConverse(
['discoInitialized'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.chatboxviews.get(muc_jid);
const sent_IQs = _converse.connection.IQ_stanzas;
const stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq[type="set"] query[xmlns="${Strophe.NS.MAM}"]`)).pop());
const queryid = stanza.querySelector('query').getAttribute('queryid');
const first_id = u.getUniqueId();
const tombstone = u.toStanza(`
`);
spyOn(view.model, 'handleRetraction').and.callThrough();
const promise = new Promise(resolve => _converse.api.listen.once('messageAdded', resolve));
_converse.connection._dataRecv(test_utils.createRequest(tombstone));
const last_id = u.getUniqueId();
const retraction = u.toStanza(`
`);
_converse.connection._dataRecv(test_utils.createRequest(retraction));
const iq_result = $iq({'type': 'result', 'id': stanza.getAttribute('id')})
.c('fin', {'xmlns': 'urn:xmpp:mam:2'})
.c('set', {'xmlns': 'http://jabber.org/protocol/rsm'})
.c('first', {'index': '0'}).t(first_id).up()
.c('last').t(last_id).up()
.c('count').t('2');
_converse.connection._dataRecv(test_utils.createRequest(iq_result));
await promise;
expect(view.model.messages.length).toBe(1);
let message = view.model.messages.at(0);
expect(message.get('retracted')).toBeTruthy();
expect(message.get('is_tombstone')).toBe(true);
await u.waitUntil(() => view.model.handleRetraction.calls.count() === 2);
expect(view.model.handleRetraction.calls.first().returnValue).toBe(false);
expect(view.model.handleRetraction.calls.all()[1].returnValue).toBe(true);
expect(view.model.messages.length).toBe(1);
message = view.model.messages.at(0);
expect(message.get('retracted')).toBeTruthy();
expect(message.get('is_tombstone')).toBe(true);
expect(view.el.querySelectorAll('.chat-msg').length).toBe(1);
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message div');
expect(el.textContent.trim()).toBe('eve has retracted this message');
done();
}));
it("may be returned as a tombstone moderated groupchat message",
mock.initConverse(
['discoInitialized', 'chatBoxesFetched'], {},
async function (done, _converse) {
const muc_jid = 'lounge@montague.lit';
await test_utils.openAndEnterChatRoom(_converse, muc_jid, 'romeo');
const view = _converse.chatboxviews.get(muc_jid);
const sent_IQs = _converse.connection.IQ_stanzas;
const stanza = await u.waitUntil(() => sent_IQs.filter(iq => iq.querySelector(`iq[type="set"] query[xmlns="${Strophe.NS.MAM}"]`)).pop());
const queryid = stanza.querySelector('query').getAttribute('queryid');
const first_id = u.getUniqueId();
const tombstone = u.toStanza(`
This message contains inappropriate content
`);
spyOn(view.model, 'handleModeration').and.callThrough();
const promise = new Promise(resolve => _converse.api.listen.once('messageAdded', resolve));
_converse.connection._dataRecv(test_utils.createRequest(tombstone));
const last_id = u.getUniqueId();
const retraction = u.toStanza(`
This message contains inappropriate content
`);
_converse.connection._dataRecv(test_utils.createRequest(retraction));
const iq_result = $iq({'type': 'result', 'id': stanza.getAttribute('id')})
.c('fin', {'xmlns': 'urn:xmpp:mam:2'})
.c('set', {'xmlns': 'http://jabber.org/protocol/rsm'})
.c('first', {'index': '0'}).t(first_id).up()
.c('last').t(last_id).up()
.c('count').t('2');
_converse.connection._dataRecv(test_utils.createRequest(iq_result));
await promise;
expect(view.model.messages.length).toBe(1);
let message = view.model.messages.at(0);
expect(message.get('retracted')).toBeTruthy();
expect(message.get('is_tombstone')).toBe(true);
await u.waitUntil(() => view.model.handleModeration.calls.count() === 2);
expect(view.model.handleModeration.calls.first().returnValue).toBe(false);
expect(view.model.handleModeration.calls.all()[1].returnValue).toBe(true);
expect(view.model.messages.length).toBe(1);
message = view.model.messages.at(0);
expect(message.get('retracted')).toBeTruthy();
expect(message.get('is_tombstone')).toBe(true);
expect(message.get('moderation_reason')).toBe("This message contains inappropriate content");
expect(view.el.querySelectorAll('.chat-msg').length).toBe(1);
expect(view.el.querySelectorAll('.chat-msg--retracted').length).toBe(1);
const el = view.el.querySelector('.chat-msg--retracted .chat-msg__message div');
expect(el.textContent.trim()).toBe('A moderator has retracted this message from eve');
const qel = view.el.querySelector('.chat-msg--retracted .chat-msg__message q');
expect(qel.textContent.trim()).toBe('This message contains inappropriate content');
done();
}));
});
})
}));