diff --git a/addons/im_livechat/models/im_livechat_channel.py b/addons/im_livechat/models/im_livechat_channel.py index 6a568bbb269..b599738915e 100644 --- a/addons/im_livechat/models/im_livechat_channel.py +++ b/addons/im_livechat/models/im_livechat_channel.py @@ -162,8 +162,11 @@ class ImLivechatChannel(models.Model): operator_partner_id = operator.partner_id.id # partner to add to the mail.channel channel_partner_to_add = [(4, operator_partner_id)] - if self.env.user and self.env.user.active: # valid session user (not public) - channel_partner_to_add.append((4, self.env.user.partner_id.id)) + visitor_user = False + if user_id: + visitor_user = self.env['res.users'].browse(user_id) + if visitor_user and visitor_user.active: # valid session user (not public) + channel_partner_to_add.append((4, visitor_user.partner_id.id)) # create the session, and add the link with the given channel mail_channel = self.env["mail.channel"].with_context(mail_create_nosubscribe=False).sudo().create({ 'channel_partner_ids': channel_partner_to_add, @@ -172,7 +175,7 @@ class ImLivechatChannel(models.Model): 'anonymous_name': False if user_id else anonymous_name, 'country_id': country_id, 'channel_type': 'livechat', - 'name': ', '.join([self.env['res.users'].browse(user_id).name if user_id else anonymous_name, operator.livechat_username if operator.livechat_username else operator.name]), + 'name': ', '.join([visitor_user.name if visitor_user else anonymous_name, operator.livechat_username if operator.livechat_username else operator.name]), 'public': 'private', 'email_send': False, }) diff --git a/addons/im_livechat/tests/test_get_mail_channel.py b/addons/im_livechat/tests/test_get_mail_channel.py index ac3d5807333..2c0106ea37b 100644 --- a/addons/im_livechat/tests/test_get_mail_channel.py +++ b/addons/im_livechat/tests/test_get_mail_channel.py @@ -24,6 +24,11 @@ class TestGetMailChannel(TransactionCase): 'login': 'georges' }]) + self.visitor_user = self.env['res.users'].create({ + 'name': 'Rajesh', + 'login': 'rajesh', + }) + self.livechat_channel = self.env['im_livechat.channel'].create({ 'name': 'The channel', 'user_ids': (6, 0, self.operators.ids) @@ -46,6 +51,9 @@ class TestGetMailChannel(TransactionCase): channel_operator_ids = [channel_operator[0] for channel_operator in channel_operators] self.assertTrue(all(partner_id in channel_operator_ids for partner_id in self.operators.mapped('partner_id').ids)) + visitor_user_channel = self.livechat_channel._get_mail_channel('Visitor', user_id=self.visitor_user.id) + self.assertEqual(visitor_user_channel['correspondent_name'], self.visitor_user.name, "Chat title should be correct") + def _get_mail_channels(self): mail_channels = []