Skip to content

Commit 2088883

Browse files
committed
[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#71427 Signed-off-by: Thibault Delavallee (tde) <[email protected]>
1 parent da675ae commit 2088883

File tree

3 files changed

+16
-5
lines changed

3 files changed

+16
-5
lines changed

addons/im_livechat/models/im_livechat_channel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import random
55
import re
66

7-
from odoo import api, fields, models, modules, _
7+
from odoo import api, Command, fields, models, modules, _
88

99

1010
class ImLivechatChannel(models.Model):
@@ -115,14 +115,14 @@ def _get_available_users(self):
115115
def _get_livechat_mail_channel_vals(self, anonymous_name, operator, user_id=None, country_id=None):
116116
# partner to add to the mail.channel
117117
operator_partner_id = operator.partner_id.id
118-
channel_partner_to_add = [(4, operator_partner_id)]
118+
channel_partner_to_add = [Command.create({'partner_id': operator_partner_id, 'is_pinned': False})]
119119
visitor_user = False
120120
if user_id:
121121
visitor_user = self.env['res.users'].browse(user_id)
122122
if visitor_user and visitor_user.active: # valid session user (not public)
123-
channel_partner_to_add.append((4, visitor_user.partner_id.id))
123+
channel_partner_to_add.append(Command.create({'partner_id': visitor_user.partner_id.id}))
124124
return {
125-
'channel_partner_ids': channel_partner_to_add,
125+
'channel_last_seen_partner_ids': channel_partner_to_add,
126126
'livechat_active': True,
127127
'livechat_operator_id': operator_partner_id,
128128
'livechat_channel_id': self.id,

addons/im_livechat/models/mail_channel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def channel_fetch_slot(self):
7676
values = super(MailChannel, self).channel_fetch_slot()
7777
livechat_channels = self.env['mail.channel'].search([
7878
('channel_type', '=', 'livechat'),
79-
('channel_partner_ids', 'in', self.env['mail.channel.partner'].sudo()._search([
79+
('channel_last_seen_partner_ids', 'in', self.env['mail.channel.partner'].sudo()._search([
8080
('partner_id', '=', self.env.user.partner_id.id),
8181
('is_pinned', '=', True)])
8282
),

addons/im_livechat/tests/test_get_mail_channel.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,14 @@ def _open_livechat_mail_channel(self):
9090
self.env['mail.channel'].browse(mail_channel['id']).message_post(body='cc')
9191

9292
return mail_channels
93+
94+
def test_channel_not_pinned_for_operator_before_first_message(self):
95+
public_user = self.env.ref('base.public_user')
96+
channel_info = self.livechat_channel.with_user(public_user)._open_livechat_mail_channel(anonymous_name='whatever')
97+
operator_channel_partner = self.env['mail.channel.partner'].search([('channel_id', '=', channel_info['id']), ('partner_id', 'in', self.operators.partner_id.ids)])
98+
self.assertEqual(len(operator_channel_partner), 1, "operator should be member of channel")
99+
self.assertFalse(operator_channel_partner.is_pinned, "channel should not be pinned for operator initially")
100+
self.env['mail.channel'].browse(channel_info['id']).message_post(body='cc')
101+
self.assertTrue(operator_channel_partner.is_pinned, "channel should be pinned for operator after visitor sent a message")
102+
init_channels = self.env['mail.channel'].with_user(operator_channel_partner.partner_id.user_ids[0]).channel_fetch_slot()
103+
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")

0 commit comments

Comments
 (0)