[FIX] mail: avoid sending notif when not needed

When there is no channel members to be updated the seen status, we
should not send a notification to the channel members, otherwise it
will lead into creating a channel member with id undefined.

test case added to check that the notification is not sent when there
is no channel members to be updated.

closes odoo/odoo#156534

Signed-off-by: Sébastien Theys (seb) <seb@odoo.com>
This commit is contained in:
zel-odoo
2024-03-05 19:21:38 +00:00
parent b718dbd744
commit b1d403c4b4
2 changed files with 27 additions and 0 deletions
@@ -1045,6 +1045,8 @@ class Channel(models.Model):
])
])
member = self.env['discuss.channel.member'].search(channel_member_domain)
if not member:
return
member.write({
'fetched_message_id': max(member.fetched_message_id.id, last_message.id),
'seen_message_id': last_message.id,
@@ -199,6 +199,31 @@ class TestChannelInternals(MailCommon):
"Last message id should stay the same after mark channel as seen with an older message"
)
@users('employee')
def test_set_last_seen_message_should_send_notification_only_once(self):
chat = self.env['discuss.channel'].with_user(self.user_admin).channel_get((self.partner_employee | self.user_admin.partner_id).ids)
msg_1 = self._add_messages(chat, 'Body1', author=self.user_employee.partner_id)
self.env['bus.bus'].sudo().search([]).unlink()
with self.assertBus(
[(self.env.cr.dbname, "discuss.channel", chat.id)],
[{
"type": "discuss.channel.member/seen",
"payload": {
'channel_id': chat.id,
'id': chat.channel_member_ids.filtered(lambda m: m.partner_id == self.user_admin.partner_id).id,
'last_message_id': msg_1.id,
'partner_id': self.user_admin.partner_id.id,
},
}],
):
chat._channel_seen(msg_1.id)
# There should be no channel member to be set as seen in the second time
# So no notification should be sent
self.env['bus.bus'].sudo().search([]).unlink()
with self.assertBus([], []):
chat._channel_seen(msg_1.id)
def test_channel_message_post_should_not_allow_adding_wrong_parent(self):
channels = self.env['discuss.channel'].create([{'name': '1'}, {'name': '2'}])
message = self._add_messages(channels[0], 'Body1')