Clear messages when closing a chat

This commit is contained in:
JC Brand 2019-05-16 08:24:25 +02:00
parent 3085c5d408
commit 46fef28601
7 changed files with 69 additions and 15 deletions

View File

@ -2265,11 +2265,13 @@
id="5f3dbc5e-e1d3-4077-a492-693f3769c7ad" id="5f3dbc5e-e1d3-4077-a492-693f3769c7ad"
by="room@muc.example.com"/> by="room@muc.example.com"/>
</message>`); </message>`);
spyOn(view.model, 'updateMessage');
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _converse.connection._dataRecv(test_utils.createRequest(stanza));
await test_utils.waitUntil(() => view.model.findDuplicateFromStanzaID.calls.count() === 2); await test_utils.waitUntil(() => view.model.findDuplicateFromStanzaID.calls.count() === 2);
result = await view.model.findDuplicateFromStanzaID.calls.all()[1].returnValue; result = await view.model.findDuplicateFromStanzaID.calls.all()[1].returnValue;
expect(result instanceof _converse.Message).toBe(true); expect(result instanceof _converse.Message).toBe(true);
expect(view.model.messages.length).toBe(1); expect(view.model.messages.length).toBe(1);
await test_utils.waitUntil(() => view.model.updateMessage.calls.count());
done(); done();
})); }));
@ -2689,7 +2691,10 @@
<acknowledged xmlns="urn:xmpp:chat-markers:0" id="${msg_obj.get('msgid')}"/> <acknowledged xmlns="urn:xmpp:chat-markers:0" id="${msg_obj.get('msgid')}"/>
</message>`); </message>`);
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _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').length).toBe(1);
expect(view.el.querySelectorAll('.chat-msg__receipt').length).toBe(0); expect(view.el.querySelectorAll('.chat-msg__receipt').length).toBe(0);
expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object)); expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));
@ -2701,7 +2706,7 @@
<markable xmlns="urn:xmpp:chat-markers:0"/> <markable xmlns="urn:xmpp:chat-markers:0"/>
</message>`); </message>`);
_converse.connection._dataRecv(test_utils.createRequest(stanza)); _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').length).toBe(2);
expect(view.el.querySelectorAll('.chat-msg__receipt').length).toBe(0); expect(view.el.querySelectorAll('.chat-msg__receipt').length).toBe(0);
expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object)); expect(_converse.api.trigger).toHaveBeenCalledWith('message', jasmine.any(Object));

View File

@ -54,8 +54,8 @@
test_utils.createContacts(_converse, 'current'); test_utils.createContacts(_converse, 'current');
await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length, 300); await test_utils.waitUntil(() => _converse.rosterview.el.querySelectorAll('.roster-group .group-toggle').length, 300);
await test_utils.openAndEnterChatRoom(_converse, 'lounge', 'localhost', 'dummy'); await test_utils.openAndEnterChatRoom(_converse, 'chillout', 'localhost', 'dummy');
let jid = 'lounge@localhost'; let jid = 'chillout@localhost';
let room = _converse.api.rooms.get(jid); let room = _converse.api.rooms.get(jid);
expect(room instanceof Object).toBeTruthy(); expect(room instanceof Object).toBeTruthy();
@ -87,7 +87,7 @@
chatroomview.close(); chatroomview.close();
// Non-existing room // Non-existing room
jid = 'lounge2@localhost'; jid = 'chillout2@localhost';
room = _converse.api.rooms.get(jid); room = _converse.api.rooms.get(jid);
expect(typeof room === 'undefined').toBeTruthy(); expect(typeof room === 'undefined').toBeTruthy();
done(); done();
@ -392,6 +392,33 @@
describe("A Groupchat", function () { 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", it("is opened when an xmpp: URI is clicked inside another groupchat",
mock.initConverse( mock.initConverse(
null, ['rosterGroupsFetched'], {}, null, ['rosterGroupsFetched'], {},

View File

@ -339,6 +339,7 @@ converse.plugins.add('converse-chatview', {
this.initDebounced(); this.initDebounced();
this.model.messages.on('add', this.onMessageAdded, this); this.model.messages.on('add', this.onMessageAdded, this);
this.model.messages.on('rendered', this.scrollDown, 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('show', this.show, this);
this.model.on('destroy', this.remove, this); this.model.on('destroy', this.remove, this);
@ -1071,9 +1072,7 @@ converse.plugins.add('converse-chatview', {
if (ev && ev.preventDefault) { ev.preventDefault(); } if (ev && ev.preventDefault) { ev.preventDefault(); }
const result = confirm(__("Are you sure you want to clear the messages from this conversation?")); const result = confirm(__("Are you sure you want to clear the messages from this conversation?"));
if (result === true) { if (result === true) {
this.content.innerHTML = ''; this.model.clearMessages();
this.model.messages.reset();
this.model.messages.browserStorage._clear();
} }
return this; return this;
}, },

View File

@ -81,7 +81,13 @@ converse.plugins.add('converse-message-view', {
}, },
initialize () { 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) { if (this.model.vcard) {
this.model.vcard.on('change', this.debouncedRender, this); this.model.vcard.on('change', this.debouncedRender, this);
} }
@ -197,7 +203,11 @@ converse.plugins.add('converse-message-view', {
} }
await promise; await promise;
this.replaceElement(msg); 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); this.model.collection.trigger('rendered', this);
}
}, },
renderErrorMessage () { renderErrorMessage () {

View File

@ -541,6 +541,7 @@ converse.plugins.add('converse-muc-views', {
this.model.messages.on('add', this.onMessageAdded, this); this.model.messages.on('add', this.onMessageAdded, this);
this.model.messages.on('rendered', this.scrollDown, 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:affiliation', this.renderHeading, this);
this.model.on('change:connection_status', this.afterConnected, 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); this.nickname_form.model.set('validation_message', message);
} }
u.showElement(this.nickname_form.el); 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='') { renderPasswordForm (message='') {

View File

@ -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 () { close () {
try { try {
this.destroy(); this.destroy();
} catch (e) { } catch (e) {
_converse.log(e, Strophe.LogLevel.ERROR); _converse.log(e, Strophe.LogLevel.ERROR);
} finally {
this.clearMessages();
} }
}, },

View File

@ -243,12 +243,11 @@ converse.plugins.add('converse-muc', {
async onConnectionStatusChanged () { async onConnectionStatusChanged () {
if (this.get('connection_status') === converse.ROOMSTATUS.ENTERED && if (this.get('connection_status') === converse.ROOMSTATUS.ENTERED &&
_converse.auto_register_muc_nickname && _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() this.registerNickname()
} }
}
}, },
registerHandlers () { registerHandlers () {