From e3ff7a49ae261c22336e6fdfcc01c33b7e9b2229 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= <tde@odoo.com>
Date: Tue, 21 Jan 2020 12:35:37 +0000
Subject: [PATCH] [IMP] mail: make followers API explicit
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Purpose of this commit is to always call followers API with explicit parameters
about existing followers management (check_existing and existing_policy
parameters). It makes code easier to read and maintain.

LINKS

Task 1933771
Task 2078313

Co-Authored-By: Thibault Delavallée <tde@odoo.com>
Co-Authored-By: Rémy Voet <ryv@odoo.com>
---
 addons/mail/models/mail_followers.py | 25 +++++++++++++++++++------
 addons/mail/models/mail_thread.py    |  6 ++++--
 2 files changed, 23 insertions(+), 8 deletions(-)

diff --git a/addons/mail/models/mail_followers.py b/addons/mail/models/mail_followers.py
index 14e38e314d4b..b9b3f53e8b94 100644
--- a/addons/mail/models/mail_followers.py
+++ b/addons/mail/models/mail_followers.py
@@ -228,7 +228,7 @@ GROUP BY fol.id%s""" % (
     # --------------------------------------------------
 
     def _insert_followers(self, res_model, res_ids, partner_ids, partner_subtypes, channel_ids, channel_subtypes,
-                          customer_ids=None, check_existing=False, existing_policy='skip'):
+                          customer_ids=None, check_existing=True, existing_policy='skip'):
         """ Main internal method allowing to create or update followers for documents, given a
         res_model and the document res_ids. This method does not handle access rights. This is the
         role of the caller to ensure there is no security breach.
@@ -243,9 +243,19 @@ GROUP BY fol.id%s""" % (
         """
         sudo_self = self.sudo().with_context(default_partner_id=False, default_channel_id=False)
         if not partner_subtypes and not channel_subtypes:  # no subtypes -> default computation, no force, skip existing
-            new, upd = self._add_default_followers(res_model, res_ids, partner_ids, channel_ids, customer_ids=customer_ids)
+            new, upd = self._add_default_followers(
+                res_model, res_ids,
+                partner_ids, channel_ids,
+                customer_ids=customer_ids,
+                check_existing=check_existing,
+                existing_policy=existing_policy)
         else:
-            new, upd = self._add_followers(res_model, res_ids, partner_ids, partner_subtypes, channel_ids, channel_subtypes, check_existing=check_existing, existing_policy=existing_policy)
+            new, upd = self._add_followers(
+                res_model, res_ids,
+                partner_ids, partner_subtypes,
+                channel_ids, channel_subtypes,
+                check_existing=check_existing,
+                existing_policy=existing_policy)
         if new:
             sudo_self.create([
                 dict(values, res_id=res_id)
@@ -255,7 +265,8 @@ GROUP BY fol.id%s""" % (
         for fol_id, values in upd.items():
             sudo_self.browse(fol_id).write(values)
 
-    def _add_default_followers(self, res_model, res_ids, partner_ids, channel_ids=None, customer_ids=None):
+    def _add_default_followers(self, res_model, res_ids, partner_ids, channel_ids=None, customer_ids=None,
+                               check_existing=True, existing_policy='skip'):
         """ Shortcut to ``_add_followers`` that computes default subtypes. Existing
         followers are skipped as their subscription is considered as more important
         compared to new default subscription.
@@ -263,6 +274,8 @@ GROUP BY fol.id%s""" % (
         :param customer_ids: optional list of partner ids that are customers. It is used if computing
          default subtype is necessary and allow to avoid the check of partners being customers (no
          user or share user). It is just a matter of saving queries if the info is already known;
+        :param check_existing: see ``_add_followers``;
+        :param existing_policy: see ``_add_followers``;
 
         :return: see ``_add_followers``
         """
@@ -276,7 +289,7 @@ GROUP BY fol.id%s""" % (
         c_stypes = dict.fromkeys(channel_ids or [], default.ids)
         p_stypes = dict((pid, external.ids if pid in customer_ids else default.ids) for pid in partner_ids)
 
-        return self._add_followers(res_model, res_ids, partner_ids, p_stypes, channel_ids, c_stypes, check_existing=True, existing_policy='skip')
+        return self._add_followers(res_model, res_ids, partner_ids, p_stypes, channel_ids, c_stypes, check_existing=check_existing, existing_policy=existing_policy)
 
     def _add_followers(self, res_model, res_ids, partner_ids, partner_subtypes, channel_ids, channel_subtypes,
                        check_existing=False, existing_policy='skip'):
@@ -296,7 +309,7 @@ GROUP BY fol.id%s""" % (
 
           * skip: simply skip existing followers, do not touch them;
           * force: update existing with given subtypes only;
-          * replace: replace existing with nex subtypes (like force without old / new follower);
+          * replace: replace existing with new subtypes (like force without old / new follower);
           * update: gives an update dict allowing to add missing subtypes (no subtype removal);
         """
         _res_ids = res_ids or [0]
diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py
index e560c7c2fb35..4672cd4d5697 100644
--- a/addons/mail/models/mail_thread.py
+++ b/addons/mail/models/mail_thread.py
@@ -260,7 +260,9 @@ class MailThread(models.AbstractModel):
 
         # subscribe uid unless asked not to
         if not self._context.get('mail_create_nosubscribe'):
-            default_followers = self.env['mail.followers']._add_default_followers(self._name, [], self.env.user.partner_id.ids, customer_ids=[])[0][0]
+            default_followers = self.env['mail.followers']._add_default_followers(
+                self._name, [], self.env.user.partner_id.ids, customer_ids=[],
+                check_existing=False, existing_policy='skip')[0][0]
             for values in vals_list:
                 message_follower_ids = values.get('message_follower_ids') or []
                 message_follower_ids += [(0, 0, fol_vals) for fol_vals in default_followers]
@@ -2726,7 +2728,7 @@ class MailThread(models.AbstractModel):
         if not subtype_ids:
             self.env['mail.followers']._insert_followers(
                 self._name, self.ids, partner_ids, None, channel_ids, None,
-                customer_ids=customer_ids)
+                customer_ids=customer_ids, check_existing=True, existing_policy='skip')
         else:
             self.env['mail.followers']._insert_followers(
                 self._name, self.ids,
-- 
GitLab