From f81292e9558c7aa906e9d3f193b6c005637dcd0d Mon Sep 17 00:00:00 2001 From: JC Brand Date: Tue, 9 Feb 2021 10:00:23 +0100 Subject: [PATCH] Bugfix in window state change handler --- spec/notification.js | 25 ++++++++++++++----------- src/plugins/chatview/view.js | 2 +- src/plugins/headlines-view/view.js | 2 +- src/plugins/muc-views/muc.js | 2 +- src/shared/chatview.js | 6 +++--- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/spec/notification.js b/spec/notification.js index d7928bb85..3d9934ed0 100644 --- a/spec/notification.js +++ b/spec/notification.js @@ -297,17 +297,15 @@ describe("Notifications", function () { spyOn(converse.env, 'Favico').and.returnValue(favico); const message = 'This message will always increment the message counter from zero'; const sender_jid = mock.cur_names[0].replace(/ /g,'.').toLowerCase() + '@montague.lit'; - const msgFactory = function () { - return $msg({ - from: sender_jid, - to: _converse.connection.jid, - type: 'chat', - id: u.getUniqueId() - }) - .c('body').t(message).up() - .c('active', {'xmlns': Strophe.NS.CHATSTATES}) - .tree(); - }; + const msgFactory = () => $msg({ + from: sender_jid, + to: _converse.connection.jid, + type: 'chat', + id: u.getUniqueId() + }) + .c('body').t(message).up() + .c('active', {'xmlns': Strophe.NS.CHATSTATES}) + .tree(); // leave converse-chat page _converse.windowState = 'hidden'; @@ -315,10 +313,15 @@ describe("Notifications", function () { let view = _converse.chatboxviews.get(sender_jid); await u.waitUntil(() => favico.badge.calls.count() === 1, 1000); expect(favico.badge.calls.mostRecent().args.pop()).toBe(1); + expect(view.model.get('num_unread')).toBe(1); // come back to converse-chat page _converse.saveWindowState({'type': 'focus'}); + + await u.waitUntil(() => u.isVisible(view)); + expect(view.model.get('num_unread')).toBe(0); + await u.waitUntil(() => favico.badge.calls.count() === 2); expect(favico.badge.calls.mostRecent().args.pop()).toBe(0); diff --git a/src/plugins/chatview/view.js b/src/plugins/chatview/view.js index e87d7af46..e24a5dd4c 100644 --- a/src/plugins/chatview/view.js +++ b/src/plugins/chatview/view.js @@ -39,7 +39,7 @@ export default class ChatView extends BaseChatView { this.model = _converse.chatboxes.get(jid); this.initDebounced(); - api.listen.on('windowStateChanged', this.onWindowStateChanged); + api.listen.on('windowStateChanged', d => this.onWindowStateChanged(d)); this.listenTo(this.model, 'change:composing_spoiler', this.renderMessageForm); this.listenTo(this.model, 'change:hidden', () => !this.model.get('hidden') && this.afterShown()); diff --git a/src/plugins/headlines-view/view.js b/src/plugins/headlines-view/view.js index 4eec7fca7..123492b76 100644 --- a/src/plugins/headlines-view/view.js +++ b/src/plugins/headlines-view/view.js @@ -20,7 +20,7 @@ class HeadlinesView extends BaseChatView { this.model = _converse.chatboxes.get(jid); this.initDebounced(); - api.listen.on('windowStateChanged', this.onWindowStateChanged); + api.listen.on('windowStateChanged', d => this.onWindowStateChanged(d)); this.model.disable_mam = true; // Don't do MAM queries for this box this.listenTo(this.model, 'change:hidden', () => this.afterShown()); diff --git a/src/plugins/muc-views/muc.js b/src/plugins/muc-views/muc.js index 3df11d086..57cf4c09e 100644 --- a/src/plugins/muc-views/muc.js +++ b/src/plugins/muc-views/muc.js @@ -81,7 +81,7 @@ export default class MUCView extends BaseChatView { this.model = _converse.chatboxes.get(jid); this.initDebounced(); - api.listen.on('windowStateChanged', this.onWindowStateChanged); + api.listen.on('windowStateChanged', d => this.onWindowStateChanged(d)); this.listenTo( this.model, diff --git a/src/shared/chatview.js b/src/shared/chatview.js index 32e3e3d13..e9b02959d 100644 --- a/src/shared/chatview.js +++ b/src/shared/chatview.js @@ -443,12 +443,12 @@ export default class BaseChatView extends ElementView { api.trigger('chatBoxScrolledDown', { 'chatbox': this.model }); // TODO: clean up } - onWindowStateChanged (state) { - if (state === 'visible') { + onWindowStateChanged (data) { + if (data.state === 'visible') { if (!this.model.isHidden() && this.model.get('num_unread', 0)) { this.model.clearUnreadMsgCounter(); } - } else if (state === 'hidden') { + } else if (data.state === 'hidden') { this.model.setChatState(_converse.INACTIVE, { 'silent': true }); this.model.sendChatState(); }