[IMP] mail: subscribe creator of document through mail gateway if not superuser
Document creation or update is still done without auto subscribe. Indeed user running mailgateway or owning alias is not necessarily linked to the email author. That way we avoid auto subscription of irrelevant people. Posting message based on incoming email is now allowing auto subscription if there is an author found during email parsing. We also ensure this author is not root, to be sure he is not added in followers of documents. LINKS Task ID-2326281 PR odoo/odoo#56560 Closes odoo/odoo#38383 X-original-commit: fe27f9fe0cf9581c15a2a9e45125abe82dd303ad
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user