From 16102407f3e27cca7431dccf1844966208cd566d Mon Sep 17 00:00:00 2001 From: Renaud Thiry Date: Thu, 22 Feb 2024 14:33:23 +0000 Subject: [PATCH] [FIX] mail: let auto_subscribe use up-to-date values `_message_auto_subscribe` relies on `_get_subscription_data` to know who is subscribed to what. This method uses a raw SQL query but doesn't flush the relevant records beforehand. This is not an issue in most normal cases, but if followers are edited manually for example in order to merge existing records. It may cause issues when assigning a user to the record after merging, as the this will attempt to add them to the list of followers automatically, when they already were on it. task-3713472 original issue in opw-3634410 closes odoo/odoo#155139 X-original-commit: d1b0e46f7d3034c400ba3c60f343c21c68aa011e Signed-off-by: Thibault Delavallee (tde) Signed-off-by: Renaud Thiry (reth) --- addons/crm/tests/test_crm_lead_convert_mass.py | 2 +- addons/mail/models/mail_followers.py | 2 ++ addons/test_mail/tests/test_mail_followers.py | 15 +++++++++++++++ 3 files changed, 18 insertions(+), 1 deletion(-) diff --git a/addons/crm/tests/test_crm_lead_convert_mass.py b/addons/crm/tests/test_crm_lead_convert_mass.py index b153a053198..acdfc5ac3ab 100644 --- a/addons/crm/tests/test_crm_lead_convert_mass.py +++ b/addons/crm/tests/test_crm_lead_convert_mass.py @@ -167,7 +167,7 @@ class TestLeadConvertMass(crm_common.TestLeadConvertMassCommon): user_ids = self.assign_users.ids # randomness: at least 1 query - with self.assertQueryCount(user_sales_manager=1801): # crm 1503 / com 1790 / ent 1800 + with self.assertQueryCount(user_sales_manager=1802): # crm 1481 / com 1794 / ent 1802 mass_convert = self.env['crm.lead2opportunity.partner.mass'].with_context({ 'active_model': 'crm.lead', 'active_ids': test_leads.ids, diff --git a/addons/mail/models/mail_followers.py b/addons/mail/models/mail_followers.py index 9199ccb0c44..f5fd1a669bb 100644 --- a/addons/mail/models/mail_followers.py +++ b/addons/mail/models/mail_followers.py @@ -349,6 +349,8 @@ class Followers(models.Model): share status of partner (returned only if include_pshare is True) active flag status of partner (returned only if include_active is True) """ + self.env['mail.followers'].flush_model(['partner_id', 'res_id', 'res_model', 'subtype_ids']) + self.env['res.partner'].flush_model(['active', 'partner_share']) # base query: fetch followers of given documents where_clause = ' OR '.join(['fol.res_model = %s AND fol.res_id IN %s'] * len(doc_data)) where_params = list(itertools.chain.from_iterable((rm, tuple(rids)) for rm, rids in doc_data)) diff --git a/addons/test_mail/tests/test_mail_followers.py b/addons/test_mail/tests/test_mail_followers.py index aefd1454c38..82b5217edbd 100644 --- a/addons/test_mail/tests/test_mail_followers.py +++ b/addons/test_mail/tests/test_mail_followers.py @@ -248,6 +248,21 @@ class BaseFollowersTest(MailCommon): self.assertEqual(document.message_follower_ids.partner_id, self.env.user.partner_id) self.assertEqual(document.message_follower_ids.subtype_ids, self.default_group_subtypes) + @users('employee') + def test_subscriptions_data_fetch(self): + """ Test that _get_subscription_data gives correct values when modifying followers manually.""" + test_record = self.test_record + test_record_copy = self.test_record.copy() + test_records = test_record + test_record_copy + test_record.message_subscribe([self.user_employee.partner_id.id]) + subscription_data = self.env['mail.followers']._get_subscription_data([(test_records._name, test_records.ids)], None) + self.assertEqual(len(subscription_data), 1) + self.assertEqual(subscription_data[0][1], test_record.id) + self.env['mail.followers'].browse(subscription_data[0][0]).sudo().res_id = test_record_copy + subscription_data = self.env['mail.followers']._get_subscription_data([(test_records._name, test_records.ids)], None) + self.assertEqual(len(subscription_data), 1) + self.assertEqual(subscription_data[0][1], test_record_copy.id) + @tagged('mail_followers') class AdvancedFollowersTest(MailCommon):