[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) <tde@openerp.com>
This commit is contained in:
Sébastien Theys
2021-06-03 07:45:30 +00:00
parent 8d385119ed
commit 93c4b03737
3 changed files with 16 additions and 5 deletions
@@ -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,
+1 -1
View File
@@ -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)])
),
@@ -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")