From 89eb06497771f0faff0fabc79e7046b3709af46d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Warnon?= Date: Fri, 18 Mar 2022 10:35:07 +0000 Subject: [PATCH] [IMP] mail: allow not posting join notifications when adding members Currently, when adding members to a mail.channel using the add_members method, we always post notification message on the channel mentioning that someone has joined the channel / was invited to join by another user. This commit simply adds a method parameter allowing to disable that behavior. This change is necessary for the upcoming chatbot, where we want to add members but handle the join notifications differently. Task-2030386 Part-of: odoo/odoo#84000 --- addons/mail/models/mail_channel.py | 37 ++++++++++++++++-------------- 1 file changed, 20 insertions(+), 17 deletions(-) diff --git a/addons/mail/models/mail_channel.py b/addons/mail/models/mail_channel.py index 574b9341bc1..d6494e88c73 100644 --- a/addons/mail/models/mail_channel.py +++ b/addons/mail/models/mail_channel.py @@ -326,7 +326,7 @@ class Channel(models.Model): }) return result - def add_members(self, partner_ids=None, guest_ids=None, invite_to_rtc_call=False): + def add_members(self, partner_ids=None, guest_ids=None, invite_to_rtc_call=False, post_joined_message=True): """ Adds the given partner_ids and guest_ids as member of self channels. """ self.check_access_rights('write') self.check_access_rule('write') @@ -365,27 +365,30 @@ class Channel(models.Model): members_data = [] guest_members_data = [] for channel_partner in new_members.filtered(lambda channel_partner: channel_partner.partner_id): - user = channel_partner.partner_id.user_ids[0] if channel_partner.partner_id.user_ids else self.env['res.users'] - # notify invited members through the bus - if user: - notifications.append((channel_partner.partner_id, 'mail.channel/joined', { - 'channel': channel_partner.channel_id.with_user(user).with_context(allowed_company_ids=user.company_ids.ids).sudo().channel_info()[0], - 'invited_by_user_id': self.env.user.id, - })) - # notify existing members with a new message in the channel - if channel_partner.partner_id == self.env.user.partner_id: - notification = _('
joined the channel
') - else: - notification = _( - '
invited %s to the channel
', - channel_partner.partner_id._get_html_link(), - ) - channel_partner.channel_id.message_post(body=notification, message_type="notification", subtype_xmlid="mail.mt_comment") members_data.append({ 'id': channel_partner.partner_id.id, 'im_status': channel_partner.partner_id.im_status, 'name': channel_partner.partner_id.name, }) + + # notify invited members through the bus + user = channel_partner.partner_id.user_ids[0] if channel_partner.partner_id.user_ids else self.env['res.users'] + if user: + notifications.append((channel_partner.partner_id, 'mail.channel/joined', { + 'channel': channel_partner.channel_id.with_user(user).with_context(allowed_company_ids=user.company_ids.ids).sudo().channel_info()[0], + 'invited_by_user_id': self.env.user.id, + })) + + if post_joined_message: + # notify existing members with a new message in the channel + if channel_partner.partner_id == self.env.user.partner_id: + notification = _('
joined the channel
') + else: + notification = _( + '
invited %s to the channel
', + channel_partner.partner_id._get_html_link(), + ) + channel_partner.channel_id.message_post(body=notification, message_type="notification", subtype_xmlid="mail.mt_comment") for channel_partner in new_members.filtered(lambda channel_partner: channel_partner.guest_id): channel_partner.channel_id.message_post(body=_('
joined the channel
'), message_type="notification", subtype_xmlid="mail.mt_comment") guest_members_data.append({