From 17bb0ca443f54bc231b03794e776bbc6992ffdfa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Wed, 24 Oct 2018 12:58:47 +0000 Subject: [PATCH] [FIX] mail: mark as read only when accessing to chatter Before this commit, the user could not receive any notification from the chatter of a document when having Notification Management set to "Handle with Odoo". This problem occurs when the user opens (or has opened) the document. Any messages received from the document are automatically marked as read, so it removes the notification right after receiving it. In order to no longer have this issue, the user had to reload all pages that have opened this document. This commit slightly changes the behaviour of the chatter, so that it automatically marks it as read only when accessing the document, but not when the document is open (or has been opened). Consequently, if the user has opened the document and then receives a notification from this document, it won't automatically mark it as read. Task-ID 1895359 opw-1890556 closes odoo/odoo#28114 --- addons/mail/static/src/js/thread_field.js | 9 ++- addons/mail/static/tests/chatter_tests.js | 98 +++++++++++++++++++++++ 2 files changed, 106 insertions(+), 1 deletion(-) 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);