From a55c78836f172dba1cfa6db3df0e927a9c7e6471 Mon Sep 17 00:00:00 2001
From: Xavier-Do <xdo@odoo.com>
Date: Mon, 29 Jul 2019 15:48:56 +0000
Subject: [PATCH] [REF] mail: remove notified_partner_ids from mark_all_as_read

mark_all_as_read is using notified_partner_ids to filter
the messages to use. But anyway, the notification will
be filtered on partner right after that.

Since if we have a domain, the message set will be quite small,
(scope of a thread) we search all those messages.

If not, we actually avoid to search on message and go right
to the notifications.

Reading the message_ids will garantee to have the minimal
set of message that where actually passed from unread to read.
Since data should not be in cache at this time and mark_all_as_read
is usually called alone, we prefer to use cache and perform a read
immediatly.

Task-ID 402597
---
 addons/mail/models/mail_message.py | 18 +++++++++++-------
 1 file changed, 11 insertions(+), 7 deletions(-)

diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py
index d7bc521ded6d..be3db85d137e 100644
--- a/addons/mail/models/mail_message.py
+++ b/addons/mail/models/mail_message.py
@@ -197,16 +197,20 @@ class Message(models.Model):
         # search, and one for each message in the result set is_read to True in the
         # current notifications from the relation.
         partner_id = self.env.user.partner_id.id
-        msg_domain = [('notified_partner_ids', 'in', partner_id)]
-        unread_messages = self.search(expression.AND([msg_domain, domain]))
-        ids = unread_messages.ids
-        notifications = self.env['mail.notification'].sudo().search([
-            ('mail_message_id', 'in', ids),
+        notif_domain = [
             ('res_partner_id', '=', partner_id),
-            ('is_read', '=', False)])
+            ('is_read', '=', False)]
+
+        if domain:
+            messages_ids = self.search(domain).ids  # need sudo?
+            notif_domain = expression.AND([notif_domain, [('mail_message_id', 'in', messages_ids)]])
+
+        notifications = self.env['mail.notification'].sudo().search(notif_domain)
         notifications.write({'is_read': True})
 
-        notification = {'type': 'mark_as_read', 'message_ids': ids}
+        ids = [n['mail_message_id'] for n in notifications.read(['mail_message_id'])]
+
+        notification = {'type': 'mark_as_read', 'message_ids': notifications}
         self.env['bus.bus'].sendone((self._cr.dbname, 'res.partner', partner_id), notification)
 
         return ids
-- 
GitLab