diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index 2decfac6c44..0ba6214de90 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -1006,6 +1006,7 @@ class MailThread(models.AbstractModel): thread_id = False for model, thread_id, custom_values, user_id, alias in routes or (): subtype_id = False + related_user = self.env['res.users'].browse(user_id) Model = self.env[model].with_context(mail_create_nosubscribe=True, mail_create_nolog=True) if not (thread_id and hasattr(Model, 'message_update') or hasattr(Model, 'message_new')): raise ValueError( @@ -1015,7 +1016,7 @@ class MailThread(models.AbstractModel): # disabled subscriptions during message_new/update to avoid having the system user running the # email gateway become a follower of all inbound messages - ModelCtx = Model.with_user(user_id).sudo() + ModelCtx = Model.with_user(related_user).sudo() if thread_id and hasattr(ModelCtx, 'message_update'): thread = ModelCtx.browse(thread_id) thread.message_update(message_dict) @@ -1048,6 +1049,9 @@ class MailThread(models.AbstractModel): if thread._name == 'mail.thread': # message with parent_id not linked to record new_msg = thread.message_notify(**post_params) else: + # parsing should find an author independently of user running mail gateway, and ensure it is not odoobot + partner_from_found = message_dict.get('author_id') and message_dict['author_id'] != self.env['ir.model.data'].xmlid_to_res_id('base.partner_root') + thread = thread.with_context(mail_create_nosubscribe=not partner_from_found) new_msg = thread.message_post(**post_params) if new_msg and original_partner_ids: diff --git a/addons/test_mail/tests/test_mail_gateway.py b/addons/test_mail/tests/test_mail_gateway.py index b615ceb5ff5..630ccf47ffa 100644 --- a/addons/test_mail/tests/test_mail_gateway.py +++ b/addons/test_mail/tests/test_mail_gateway.py @@ -223,19 +223,62 @@ class TestMailgateway(TestMailCommon): set(['rosaçée.gif', 'verte!µ.gif', 'orangée.gif'])) def test_message_process_followers(self): - pass - # TODO : the author of a message post should be added as follower - # currently it is not the case as otherwise Administrator would be follower of a lot of stuff - # this is a bug with mail_create_nosubscribe -> should be changed in master - # self.assertEqual(record.message_partner_ids, self.partner_1, - # 'message_process: recognized email -> added as follower') + """ Incoming email: recognized author not archived and not odoobot: added as follower """ + with self.mock_mail_gateway(): + record = self.format_and_process(MAIL_TEMPLATE, self.partner_1.email_formatted, 'groups@test.com') - # TODO : the author of a message post on mail.test should not be added as follower - # Test: author (and not recipient) added as follower - # self.assertEqual(self.test_public.message_partner_ids, self.partner_1 | self.partner_2, - # 'message_process: after reply, group should have 2 followers') - # self.assertEqual(self.test_public.message_channel_ids, self.env['mail.test.container'], - # 'message_process: after reply, group should have 2 followers (0 channels)') + self.assertEqual(record.message_ids[0].author_id, self.partner_1, + 'message_process: recognized email -> author_id') + self.assertEqual(record.message_ids[0].email_from, self.partner_1.email_formatted) + self.assertEqual(record.message_follower_ids.partner_id, self.partner_1, + 'message_process: recognized email -> added as follower') + self.assertEqual(record.message_partner_ids, self.partner_1, + 'message_process: recognized email -> added as follower') + + # just an email -> no follower + with self.mock_mail_gateway(): + record2 = self.format_and_process( + MAIL_TEMPLATE, self.email_from, 'groups@test.com', + subject='Another Email') + + self.assertEqual(record2.message_ids[0].author_id, self.env['res.partner']) + self.assertEqual(record2.message_ids[0].email_from, self.email_from) + self.assertEqual(record2.message_follower_ids.partner_id, self.env['res.partner'], + 'message_process: unrecognized email -> no follower') + self.assertEqual(record2.message_partner_ids, self.env['res.partner'], + 'message_process: unrecognized email -> no follower') + + # archived partner -> no follower + self.partner_1.active = False + self.partner_1.flush() + with self.mock_mail_gateway(): + record3 = self.format_and_process( + MAIL_TEMPLATE, self.partner_1.email_formatted, 'groups@test.com', + subject='Yet Another Email') + + self.assertEqual(record3.message_ids[0].author_id, self.env['res.partner']) + self.assertEqual(record3.message_ids[0].email_from, self.partner_1.email_formatted) + self.assertEqual(record3.message_follower_ids.partner_id, self.env['res.partner'], + 'message_process: unrecognized email -> no follower') + self.assertEqual(record3.message_partner_ids, self.env['res.partner'], + 'message_process: unrecognized email -> no follower') + + + # partner_root -> never again + odoobot = self.env.ref('base.partner_root') + odoobot.active = True + odoobot.email = 'odoobot@example.com' + with self.mock_mail_gateway(): + record4 = self.format_and_process( + MAIL_TEMPLATE, odoobot.email_formatted, 'groups@test.com', + subject='Odoobot Automatic Answer') + + self.assertEqual(record4.message_ids[0].author_id, odoobot) + self.assertEqual(record4.message_ids[0].email_from, odoobot.email_formatted) + self.assertEqual(record4.message_follower_ids.partner_id, self.env['res.partner'], + 'message_process: unrecognized email -> no follower') + self.assertEqual(record4.message_partner_ids, self.env['res.partner'], + 'message_process: unrecognized email -> no follower') # -------------------------------------------------- # Author recognition