diff --git a/addons/mail/models/mail_channel.py b/addons/mail/models/mail_channel.py index 17d11c89401..2c02be5997b 100644 --- a/addons/mail/models/mail_channel.py +++ b/addons/mail/models/mail_channel.py @@ -806,7 +806,7 @@ class Channel(models.Model): if self.moderation_guidelines: self._send_guidelines(self.env.user.partner_id) - channel_info = self.channel_info()[0] + channel_info = self.channel_info('join')[0] self.env['bus.bus'].sendone((self._cr.dbname, 'res.partner', self.env.user.partner_id.id), channel_info) return channel_info diff --git a/addons/mail/static/src/js/services/mail_notification_manager.js b/addons/mail/static/src/js/services/mail_notification_manager.js index 77639c74a8e..8b698b3e04e 100644 --- a/addons/mail/static/src/js/services/mail_notification_manager.js +++ b/addons/mail/static/src/js/services/mail_notification_manager.js @@ -179,6 +179,7 @@ MailManager.include({ * @private * @param {Object} channelData * @param {integer} channelData.id + * @param {string} [channelData.info] * @param {boolean} channelData.is_minimized * @param {string} channelData.state */ @@ -199,7 +200,7 @@ MailManager.include({ } } var channel = this.getChannel(channelData.id); - if (channel) { + if (channel && channelData.info !== 'join') { channel.updateWindowState({ folded: channelData.state === 'folded' ? true : false, detached: channelData.is_minimized, diff --git a/addons/mail/static/tests/thread_window/basic_thread_window_tests.js b/addons/mail/static/tests/thread_window/basic_thread_window_tests.js index 8e454dced18..b749ae34963 100644 --- a/addons/mail/static/tests/thread_window/basic_thread_window_tests.js +++ b/addons/mail/static/tests/thread_window/basic_thread_window_tests.js @@ -522,6 +522,116 @@ QUnit.test('do not autofocus chat window on receiving new direct message', funct form.destroy(); }); +QUnit.test('do not auto-focus chat window on receiving new message from new DM', function (assert) { + assert.expect(10); + + var self = this; + this.data['mail.channel'] = { + fields: { + name: { + string: "Name", + type: "char", + required: true, + }, + channel_type: { + string: "Channel Type", + type: "selection", + }, + channel_message_ids: { + string: "Messages", + type: "many2many", + relation: 'mail.message' + }, + message_unread_counter: { + string: "Amount of Unread Messages", + type: "integer" + }, + }, + records: [{ + id: 2, + name: "DM", + channel_type: "chat", + message_unread_counter: 1, + direct_partner: [{ id: 666, name: 'DemoUser1', im_status: '' }], + is_minimized: false, + state: 'open', + }], + }; + + var parent = this.createParent({ + data: this.data, + services: this.services, + session: { partner_id: 3 }, + mockRPC: function (route, args) { + if (args.method === 'channel_join_and_get_info') { + return $.when(_.extend({}, self.data['mail.channel'].records[0], { info: 'join' })); + } + if (args.method === 'channel_minimize') { + _.extend(self.data['mail.channel'].records[0], { + is_minimized: true, + state: 'open', + }); + } + if (args.method === 'channel_seen') { + throw new Error('should not mark channel as seen'); + } + return this._super.apply(this, arguments); + } + }); + + assert.strictEqual($('.o_thread_window').length, 0, + "should not have any DM window open"); + + // simulate receiving message from someone else + var messageData = { + author_id: [5, "Someone else"], + body: "

Test message

", + id: 2, + model: 'mail.channel', + res_id: 2, + channel_ids: [2], + }; + this.data['mail.message'].records.push(messageData); + var notification = [[false, 'mail.channel', 2], messageData]; + parent.call('bus_service', 'trigger', 'notification', [notification]); + + assert.strictEqual($('.o_thread_window').length, 1, + "should have DM window open"); + assert.strictEqual($('.o_thread_window .o_input:focus').length, 0, + "thread window should not have the focus on auto-open"); + assert.ok($('.o_thread_window .o_thread_window_title').text().indexOf('(1)') !== -1, + "DM should display one unread message"); + + // simulate receiving join DM notification (cross-tab synchronization) + var dmInfo = _.extend({}, self.data['mail.channel'].records[0], { + info: 'join', + is_minimized: false, + state: 'open', + }); + notification = [[false, 'res.partner', 3], dmInfo]; + parent.call('bus_service', 'trigger', 'notification', [notification]); + + assert.strictEqual($('.o_thread_window').length, 1, + "should still have DM window open after receiving DM info from polling"); + assert.strictEqual($('.o_thread_window .o_input:focus').length, 0, + "thread window should still not have the focus after receiving DM info from polling"); + assert.ok($('.o_thread_window .o_thread_window_title').text().indexOf('(1)') !== -1, + "DM should still display one unread message after receiving DM info from polling"); + + // simulate receiving detached DM notification (cross-tab synchronization) + notification = [[false, 'res.partner', 3], self.data['mail.channel'].records[0]]; + parent.call('bus_service', 'trigger', 'notification', [notification]); + + assert.strictEqual($('.o_thread_window').length, 1, + "should still have DM open after receiving detached info from polling"); + assert.strictEqual($('.o_thread_window .o_input:focus').length, 0, + "thread window should still not have the focus after receiving detached info from polling"); + assert.ok($('.o_thread_window .o_thread_window_title').text().indexOf('(1)') !== -1, + "DM should not still have one unread message after receiving detached info from polling"); + + parent.destroy(); +}); + }); }); });