diff --git a/addons/mail/static/src/js/thread_field.js b/addons/mail/static/src/js/thread_field.js index 5484fb1aa61..5c85a2adb37 100644 --- a/addons/mail/static/src/js/thread_field.js +++ b/addons/mail/static/src/js/thread_field.js @@ -20,6 +20,9 @@ var ThreadField = AbstractField.extend({ */ init: function () { this._super.apply(this, arguments); + // Used to automatically mark document thread as read at the moment we + // access the document and render the thread. + this._markAsReadOnNextRender = false; this._setDocumentThread(); }, /** @@ -115,7 +118,10 @@ var ThreadField = AbstractField.extend({ self._threadWidget.render(self._documentThread, { displayLoadMore: self._documentThread.getMessages().length < self._documentThread.getMessageIDs().length, }); - return self._documentThread.markAsRead(); + if (self._markAsReadOnNextRender) { + self._markAsReadOnNextRender = false; + return self._documentThread.markAsRead(); + } }); } }, @@ -157,6 +163,7 @@ var ThreadField = AbstractField.extend({ this._documentThread = null; } else { this._documentThread = this.call('mail_service', 'getOrAddDocumentThread', params); + this._markAsReadOnNextRender = true; } }, diff --git a/addons/mail/static/tests/chatter_tests.js b/addons/mail/static/tests/chatter_tests.js index 73bfcbb83f2..aff69b58197 100644 --- a/addons/mail/static/tests/chatter_tests.js +++ b/addons/mail/static/tests/chatter_tests.js @@ -786,6 +786,104 @@ QUnit.test('chatter: post, receive and star messages', function (assert) { }); }); +QUnit.test('chatter: receive notif when document is open', function (assert) { + assert.expect(2); + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + services: this.services, + arch: '
' + + '' + + '' + + '' + + '
' + + '' + + '
' + + '
', + res_id: 2, + session: { + partner_id: 3, + }, + }); + + var thread = form.call('mail_service', 'getDocumentThread', 'partner', 2); + assert.strictEqual(thread.getUnreadCounter(), 0, + "document thread should have no unread messages initially"); + + // simulate receiving a needaction message on this document thread + var needactionMessageData = { + id: 5, + author_id: [42, "Someone"], + body: 'important message', + channel_ids: [], + res_id: 2, + model: 'partner', + needaction: true, + needaction_partner_ids: [3], + }; + this.data['mail.message'].records.push(needactionMessageData); + var notification = [[false, 'mail.channel', 1], needactionMessageData]; + form.call('bus_service', 'trigger', 'notification', [notification]); + + assert.strictEqual(thread.getUnreadCounter(), 1, + "document thread should now have one unread message"); + + form.destroy(); +}); + +QUnit.test('chatter: access document with some notifs', function (assert) { + assert.expect(3); + + // simulate received needaction message on this document thread + var needactionMessageData = { + id: 5, + author_id: [42, "Someone"], + body: 'important message', + channel_ids: [], + res_id: 2, + model: 'partner', + needaction: true, + needaction_partner_ids: [3], + }; + this.data['mail.message'].records.push(needactionMessageData); + this.data['partner'].records[0].message_ids = [5]; + + var form = createView({ + View: FormView, + model: 'partner', + data: this.data, + services: this.services, + arch: '
' + + '' + + '' + + '' + + '
' + + '' + + '
' + + '
', + res_id: 2, + session: { + partner_id: 3, + }, + mockRPC: function (route, args) { + if (args.method === 'set_message_done') { + assert.step('set_message_done'); + } + return this._super.apply(this, arguments); + }, + }); + + assert.verifySteps(['set_message_done']); + + var thread = form.call('mail_service', 'getDocumentThread', 'partner', 2); + assert.strictEqual(thread.getUnreadCounter(), 0, + "document thread should have no unread messages (marked as read)"); + + form.destroy(); +}); + QUnit.test('chatter: post a message and switch in edit mode', function (assert) { assert.expect(5);