From 93c4b0373721f08f1479856eeaaa7b39aef54987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Mon, 31 May 2021 10:28:28 +0000 Subject: [PATCH] [FIX] im_livechat: show chat to operator only after first message Not sure exactly because of which change, but channel partner is now created with its default is_pinned value (True) taken into account, which was not the case before. This is a good change in general, but the livechat code relied on the old behavior. This commit makes it explicit that in this case the value should be False, and a test is added to guarantee it once and for all. This commit also fixes an issue during init messaging, where ids of res.partner were searched instead of ids of channel.partner. task-2547806 closes odoo/odoo#71647 X-original-commit: 2088883c4f6dda9dd3ccddc14a7b9da68ee873cd Signed-off-by: Thibault Delavallee (tde) --- addons/im_livechat/models/im_livechat_channel.py | 8 ++++---- addons/im_livechat/models/mail_channel.py | 2 +- addons/im_livechat/tests/test_get_mail_channel.py | 11 +++++++++++ 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/addons/im_livechat/models/im_livechat_channel.py b/addons/im_livechat/models/im_livechat_channel.py index b898352aa41..7b6e334a352 100644 --- a/addons/im_livechat/models/im_livechat_channel.py +++ b/addons/im_livechat/models/im_livechat_channel.py @@ -4,7 +4,7 @@ import base64 import random import re -from odoo import api, fields, models, modules, _ +from odoo import api, Command, fields, models, modules, _ class ImLivechatChannel(models.Model): @@ -114,14 +114,14 @@ class ImLivechatChannel(models.Model): def _get_livechat_mail_channel_vals(self, anonymous_name, operator, user_id=None, country_id=None): # partner to add to the mail.channel operator_partner_id = operator.partner_id.id - channel_partner_to_add = [(4, operator_partner_id)] + channel_partner_to_add = [Command.create({'partner_id': operator_partner_id, 'is_pinned': False})] 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)) + channel_partner_to_add.append(Command.create({'partner_id': visitor_user.partner_id.id})) return { - 'channel_partner_ids': channel_partner_to_add, + 'channel_last_seen_partner_ids': channel_partner_to_add, 'livechat_active': True, 'livechat_operator_id': operator_partner_id, 'livechat_channel_id': self.id, diff --git a/addons/im_livechat/models/mail_channel.py b/addons/im_livechat/models/mail_channel.py index c77e0082702..083f92aefe3 100644 --- a/addons/im_livechat/models/mail_channel.py +++ b/addons/im_livechat/models/mail_channel.py @@ -76,7 +76,7 @@ class MailChannel(models.Model): values = super(MailChannel, self).channel_fetch_slot() livechat_channels = self.env['mail.channel'].search([ ('channel_type', '=', 'livechat'), - ('channel_partner_ids', 'in', self.env['mail.channel.partner'].sudo()._search([ + ('channel_last_seen_partner_ids', 'in', self.env['mail.channel.partner'].sudo()._search([ ('partner_id', '=', self.env.user.partner_id.id), ('is_pinned', '=', True)]) ), diff --git a/addons/im_livechat/tests/test_get_mail_channel.py b/addons/im_livechat/tests/test_get_mail_channel.py index fb31ab31121..969d10bd528 100644 --- a/addons/im_livechat/tests/test_get_mail_channel.py +++ b/addons/im_livechat/tests/test_get_mail_channel.py @@ -90,3 +90,14 @@ class TestGetMailChannel(TransactionCase): self.env['mail.channel'].browse(mail_channel['id']).message_post(body='cc') return mail_channels + + def test_channel_not_pinned_for_operator_before_first_message(self): + public_user = self.env.ref('base.public_user') + channel_info = self.livechat_channel.with_user(public_user)._open_livechat_mail_channel(anonymous_name='whatever') + operator_channel_partner = self.env['mail.channel.partner'].search([('channel_id', '=', channel_info['id']), ('partner_id', 'in', self.operators.partner_id.ids)]) + self.assertEqual(len(operator_channel_partner), 1, "operator should be member of channel") + self.assertFalse(operator_channel_partner.is_pinned, "channel should not be pinned for operator initially") + self.env['mail.channel'].browse(channel_info['id']).message_post(body='cc') + self.assertTrue(operator_channel_partner.is_pinned, "channel should be pinned for operator after visitor sent a message") + init_channels = self.env['mail.channel'].with_user(operator_channel_partner.partner_id.user_ids[0]).channel_fetch_slot() + self.assertIn(channel_info['id'], [init_channel_info['id'] for init_channel_info in init_channels['channel_livechat']], "channel should be fetched by operator on new page")