From 12f11e0daf7e459bfee518b312e144d78e5a5759 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Fri, 22 Jan 2021 17:10:29 +0000 Subject: [FIX] mail: fix inbox mark all as read on thread view MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit task-2426357 task-2426366 closes odoo/odoo#64009 Signed-off-by: Alexandre Kühn (aku) --- .../static/src/models/thread_cache/thread_cache.js | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) (limited to 'addons/mail/static/src/models/thread_cache') diff --git a/addons/mail/static/src/models/thread_cache/thread_cache.js b/addons/mail/static/src/models/thread_cache/thread_cache.js index 8383936a3f1..1760a509fba 100644 --- a/addons/mail/static/src/models/thread_cache/thread_cache.js +++ b/addons/mail/static/src/models/thread_cache/thread_cache.js @@ -307,6 +307,44 @@ function factory(dependencies) { return messages; } + /** + * Calls "mark all as read" when this thread becomes displayed in a + * view (which is notified by `isMarkAllAsReadRequested` being `true`), + * but delays the call until some other conditions are met, such as the + * messages being loaded. + * The reason to wait until messages are loaded is to avoid a race + * condition because "mark all as read" will change the state of the + * messages in parallel to fetch reading them. + * + * @private + */ + _onChangeMarkAllAsRead() { + if ( + !this.isMarkAllAsReadRequested || + !this.thread || + !this.thread.mainCache || + !this.isLoaded || + this.isLoading + ) { + // wait for change of state before deciding what to do + return; + } + this.update({ isMarkAllAsReadRequested: false }); + if ( + this.thread.isTemporary || + this.thread.model === 'mail.box' || + this.thread.mainCache !== this || + this.threadViews.length === 0 + ) { + // ignore the request + return; + } + this.env.models['mail.message'].markAllAsRead([ + ['model', '=', this.thread.model], + ['res_id', '=', this.thread.id], + ]); + } + /** * Loads this thread cache, by fetching the most recent messages in this * conversation. @@ -413,6 +451,16 @@ function factory(dependencies) { isCacheRefreshRequested: attr({ default: false, }), + /** + * Determines whether this cache should consider calling "mark all as + * read" on this thread. + * + * This field is a hint that may or may not lead to an actual call. + * @see `_onChangeMarkAllAsRead` + */ + isMarkAllAsReadRequested: attr({ + default: false, + }), /** * Last message that has been fetched by this thread cache. * @@ -458,6 +506,23 @@ function factory(dependencies) { 'messagesAreEmpty', ], }), + /** + * Not a real field, used to trigger its compute method when one of the + * dependencies changes. + */ + onChangeMarkAllAsRead: attr({ + compute: '_onChangeMarkAllAsRead', + dependencies: [ + 'isLoaded', + 'isLoading', + 'isMarkAllAsReadRequested', + 'thread', + 'threadIsTemporary', + 'threadMainCache', + 'threadModel', + 'threadViews', + ], + }), /** * Loads initial messages from `this`. * This is not a "real" field, its compute function is used to trigger @@ -520,6 +585,12 @@ function factory(dependencies) { threadMessages: many2many('mail.message', { related: 'thread.messages', }), + /** + * Serves as compute dependency. + */ + threadModel: attr({ + related: 'thread.model', + }), /** * States the 'mail.thread_view' that are currently displaying `this`. */ -- cgit v1.2.3