diff options
Diffstat (limited to 'addons/mail/static/src/models/thread_cache/thread_cache.js')
| -rw-r--r-- | addons/mail/static/src/models/thread_cache/thread_cache.js | 71 |
1 files changed, 71 insertions, 0 deletions
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 @@ -308,6 +308,44 @@ function factory(dependencies) { } /** + * 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. * @@ -414,6 +452,16 @@ function factory(dependencies) { 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. * * This DOES NOT necessarily mean the last message linked to this thread @@ -459,6 +507,23 @@ function factory(dependencies) { ], }), /** + * 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 * the load of messages at the right time. @@ -521,6 +586,12 @@ function factory(dependencies) { related: 'thread.messages', }), /** + * Serves as compute dependency. + */ + threadModel: attr({ + related: 'thread.model', + }), + /** * States the 'mail.thread_view' that are currently displaying `this`. */ threadViews: one2many('mail.thread_view', { |
