Commit 46fef286 authored by JC Brand's avatar JC Brand

Clear messages when closing a chat

parent 3085c5d4
......@@ -2265,11 +2265,13 @@
id="5f3dbc5e-e1d3-4077-a492-693f3769c7ad"
by="room@muc.example.com"/>
</message>`);
spyOn(view.model, 'updateMessage');
_converse.connection._dataRecv(test_utils.createRequest(stanza));
await test_utils.waitUntil(() => view.model.findDuplicateFromStanzaID.calls.count() === 2);
result = await view.model.findDuplicateFromStanzaID.calls.all()[1].returnValue;
expect(result instanceof _converse.Message).toBe(true);
expect(view.model.messages.length).toBe(1);
await test_utils.waitUntil(() => view.model.updateMessage.calls.count());
done();
}));
......@@ -2689,7 +2691,10 @@
<acknowledged xmlns="urn:xmpp:chat-markers:0" id="${msg_obj.get('msgid')}"/>
</message>`);
_converse.connection._dataRecv(test_utils.createRequest(stanza));
await test_utils.waitUntil(() => _converse.api.trigger.calls.count() === 3);
spyOn(view.model, "isChatMarker").and.callThrough();
await test_utils.waitUntil(() => view.model.isChatMarker.calls.count() === 1);
expect(view.el.querySelectorAll('.chat-msg').length).toBe(1);
expect(view.el.querySelectorAll('.chat-msg__receipt').length).toBe(0);
expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
......@@ -2701,7 +2706,7 @@
<markable xmlns="urn:xmpp:chat-markers:0"/>
</message>`);
_converse.connection._dataRecv(test_utils.createRequest(stanza));
await test_utils.waitUntil(() => _converse.api.trigger.calls.count() === 5);
await test_utils.waitUntil(() => view.model.isChatMarker.calls.count() === 2);
expect(view.el.querySelectorAll('.chat-msg').length).toBe(2);
expect(view.el.querySelectorAll('.chat-msg__receipt').length).toBe(0);
expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
......
......@@ -54,8 +54,8 @@
test_utils.createContacts(_converse, 'current');
await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length, 300);
await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy');
let jid = 'lounge@localhost';
await test_utils.openAndEnterChatRoom(_converse, 'chillout', 'localhost', 'dummy');
let jid = 'chillout@localhost';
let room = _converse.api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy();
......@@ -87,7 +87,7 @@
chatroomview.close();
// Non-existing room
jid = 'lounge2@localhost';
jid = 'chillout2@localhost';
room = _converse.api.rooms.get(jid);
expect(typeof room === 'undefined').toBeTruthy();
done();
......@@ -392,6 +392,33 @@
describe("A Groupchat", function () {
it("clears cached messages when it gets closed",
mock.initConverse(
null, ['rosterGroupsFetched'], {},
async function (done, _converse) {
await test_utils.waitUntil(() => test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy'));
const view = _converse.chatboxviews.get('lounge@localhost');
const message = 'Hello world',
nick = mock.chatroom_names[0],
msg = $msg({
'from': 'lounge@localhost/'+nick,
'id': (new Date()).getTime(),
'to': 'dummy@localhost',
'type': 'groupchat'
}).c('body').t(message).tree();
await view.model.onMessage(msg);
await new Promise((resolve, reject) => view.once('messageInserted', resolve));
spyOn(view.model, 'clearMessages').and.callThrough();
view.model.close();
await test_utils.waitUntil(() => view.model.clearMessages.calls.count());
expect(view.model.messages.length).toBe(0);
expect(view.content.innerHTML).toBe('');
done()
}));
it("is opened when an xmpp: URI is clicked inside another groupchat",
mock.initConverse(
null, ['rosterGroupsFetched'], {},
......
......@@ -339,6 +339,7 @@ converse.plugins.add('converse-chatview', {
this.initDebounced();
this.model.messages.on('add', this.onMessageAdded, this);
this.model.messages.on('rendered', this.scrollDown, this);
this.model.messages.on('reset', () => (this.content.innerHTML = ''));
this.model.on('show', this.show, this);
this.model.on('destroy', this.remove, this);
......@@ -1071,9 +1072,7 @@ converse.plugins.add('converse-chatview', {
if (ev && ev.preventDefault) { ev.preventDefault(); }
const result = confirm(__("Are you sure you want to clear the messages from this conversation?"));
if (result === true) {
this.content.innerHTML = '';
this.model.messages.reset();
this.model.messages.browserStorage._clear();
this.model.clearMessages();
}
return this;
},
......
......@@ -81,7 +81,13 @@ converse.plugins.add('converse-message-view', {
},
initialize () {
this.debouncedRender = _.debounce(this.render, 50);
this.debouncedRender = _.debounce(() => {
// If the model gets destroyed in the meantime,
// it no longer has a collection
if (this.model.collection) {
this.render();
}
}, 50);
if (this.model.vcard) {
this.model.vcard.on('change', this.debouncedRender, this);
}
......@@ -197,7 +203,11 @@ converse.plugins.add('converse-message-view', {
}
await promise;
this.replaceElement(msg);
if (this.model.collection) {
// If the model gets destroyed in the meantime, it no
// longer has a collection.
this.model.collection.trigger('rendered', this);
}
},
renderErrorMessage () {
......
......@@ -541,6 +541,7 @@ converse.plugins.add('converse-muc-views', {
this.model.messages.on('add', this.onMessageAdded, this);
this.model.messages.on('rendered', this.scrollDown, this);
this.model.messages.on('reset', () => (this.content.innerHTML = ''));
this.model.on('change:affiliation', this.renderHeading, this);
this.model.on('change:connection_status', this.afterConnected, this);
......@@ -1350,7 +1351,7 @@ converse.plugins.add('converse-muc-views', {
this.nickname_form.model.set('validation_message', message);
}
u.showElement(this.nickname_form.el);
this.model.save('connection_status', converse.ROOMSTATUS.NICKNAME_REQUIRED);
u.safeSave(this.model, {'connection_status': converse.ROOMSTATUS.NICKNAME_REQUIRED});
},
renderPasswordForm (message='') {
......
......@@ -331,11 +331,24 @@ converse.plugins.add('converse-chatboxes', {
});
},
clearMessages () {
try {
this.messages.reset();
} catch (e) {
this.messages.trigger('reset');
_converse.log(e, Strophe.LogLevel.ERROR);
} finally {
this.messages.browserStorage._clear();
}
},
close () {
try {
this.destroy();
} catch (e) {
_converse.log(e, Strophe.LogLevel.ERROR);
} finally {
this.clearMessages();
}
},
......
......@@ -243,12 +243,11 @@ converse.plugins.add('converse-muc', {
async onConnectionStatusChanged () {
if (this.get('connection_status') === converse.ROOMSTATUS.ENTERED &&
_converse.auto_register_muc_nickname &&
!this.get('reserved_nick')) {
!this.get('reserved_nick') &&
await _converse.api.disco.supports(Strophe.NS.MUC_REGISTER, this.get('jid'))) {
if (await _converse.api.disco.supports(Strophe.NS.MUC_REGISTER, this.get('jid'))) {
this.registerNickname()
}
}
},
registerHandlers () {
......
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