diff --git a/web_notify_channel_message/models/__init__.py b/web_notify_channel_message/models/__init__.py index 3ce7ca7fe..0783cf9ec 100644 --- a/web_notify_channel_message/models/__init__.py +++ b/web_notify_channel_message/models/__init__.py @@ -1 +1,2 @@ from . import mail_channel +from . import res_users diff --git a/web_notify_channel_message/models/mail_channel.py b/web_notify_channel_message/models/mail_channel.py index ce1c55772..c286501b2 100644 --- a/web_notify_channel_message/models/mail_channel.py +++ b/web_notify_channel_message/models/mail_channel.py @@ -1,5 +1,4 @@ -# pylint: disable=missing-docstring -# Copyright 2016 ACSONE SA/NV +# Copyright 2023 ForgeFlow # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). from odoo import _, models @@ -15,7 +14,7 @@ class MailChannel(models.Model): for user in users: if user in message.author_id.user_ids: continue - user.notify_info( + user.with_context(_notify_channel_message=True).notify_info( message=_("You have a new message in channel %s") % self.name ) return message diff --git a/web_notify_channel_message/models/res_users.py b/web_notify_channel_message/models/res_users.py new file mode 100644 index 000000000..f480d8ced --- /dev/null +++ b/web_notify_channel_message/models/res_users.py @@ -0,0 +1,29 @@ +from odoo import models + + +class ResUsers(models.Model): + _inherit = "res.users" + + def _notify_channel( + self, + type_message="default", + message="Default message", + title=None, + sticky=False, + target=None, + ): + if self.env.context.get("_notify_channel_message", False): + return super(ResUsers, self.sudo())._notify_channel( + type_message=type_message, + message=message, + title=title, + sticky=sticky, + target=target, + ) + return super(ResUsers, self)._notify_channel( + type_message=type_message, + message=message, + title=title, + sticky=sticky, + target=target, + ) diff --git a/web_notify_channel_message/tests/test_notify_channel_message.py b/web_notify_channel_message/tests/test_notify_channel_message.py index edaec369c..0ca995290 100644 --- a/web_notify_channel_message/tests/test_notify_channel_message.py +++ b/web_notify_channel_message/tests/test_notify_channel_message.py @@ -10,6 +10,7 @@ class TestWebNotifyChannelMessage(common.TransactionCase): def setUpClass(cls): super(TestWebNotifyChannelMessage, cls).setUpClass() cls.env.user = cls.env.ref("base.user_admin") + cls.other_user = cls.env.ref("base.user_demo") cls.env = api.Environment(cls.cr, cls.env.user.id, {}) cls.env.user.tz = False # Make sure there's no timezone in user cls.user_internal = cls.env["res.users"].create( @@ -30,7 +31,7 @@ class TestWebNotifyChannelMessage(common.TransactionCase): } ) - def test_01_post_message(self): + def test_01_post_message_admin(self): initial_message = ( self.env["mail.channel"] .search([("name", "=", "Test channel")], limit=1) @@ -49,3 +50,23 @@ class TestWebNotifyChannelMessage(common.TransactionCase): .message_ids[0] ) self.assertEqual(len(message), 1) + + def test_02_post_message_non_admin(self): + initial_message = ( + self.env["mail.channel"] + .search([("name", "=", "Test channel")], limit=1) + .message_ids + ) + self.assertEqual(len(initial_message), 0) + self.channel.with_user(self.other_user).message_post( + author_id=self.other_user.partner_id.id, + body="Hello", + message_type="notification", + subtype_xmlid="mail.mt_comment", + ) + message = ( + self.env["mail.channel"] + .search([("name", "=", "Test channel")], limit=1) + .message_ids[0] + ) + self.assertEqual(len(message), 1)