From b1d403c4b4bc0a070a03fdd13fa66593f31af201 Mon Sep 17 00:00:00 2001 From: zel-odoo Date: Tue, 5 Mar 2024 15:52:44 +0100 Subject: [PATCH] [FIX] mail: avoid sending notif when not needed MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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) --- addons/mail/models/discuss/discuss_channel.py | 2 ++ .../tests/discuss/test_discuss_channel.py | 25 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/addons/mail/models/discuss/discuss_channel.py b/addons/mail/models/discuss/discuss_channel.py index 9f0ebaa956d..63c148b5fe1 100644 --- a/addons/mail/models/discuss/discuss_channel.py +++ b/addons/mail/models/discuss/discuss_channel.py @@ -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, diff --git a/addons/mail/tests/discuss/test_discuss_channel.py b/addons/mail/tests/discuss/test_discuss_channel.py index dbcc77a4c3c..df10bc156f7 100644 --- a/addons/mail/tests/discuss/test_discuss_channel.py +++ b/addons/mail/tests/discuss/test_discuss_channel.py @@ -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')