summaryrefslogtreecommitdiff
path: root/addons/mail/static/src/models/thread_cache
diff options
context:
space:
mode:
authorSébastien Theys <seb@odoo.com>2021-01-22 17:10:29 +0000
committerSébastien Theys <seb@odoo.com>2021-01-22 17:10:29 +0000
commit12f11e0daf7e459bfee518b312e144d78e5a5759 (patch)
tree6d1a65755c2ee8e9df43d6da5ded087efb030cde /addons/mail/static/src/models/thread_cache
parent86333160f92c16bc61f36b413caed325f8ac59a0 (diff)
[FIX] mail: fix inbox mark all as read on thread view
task-2426357 task-2426366 closes odoo/odoo#64009 Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
Diffstat (limited to 'addons/mail/static/src/models/thread_cache')
-rw-r--r--addons/mail/static/src/models/thread_cache/thread_cache.js71
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', {