[FIX] web_notify_channel_message

If other user than the admin is trying to send a message to a channel it will show an error message. Sending it as sudo solves the problem and makes sense that in this case you will always want to send the notification.
pull/3026/head
DavidJForgeFlow 2023-11-17 12:16:00 +01:00 committed by JasminSForgeFlow
parent fda9a706b7
commit 8f1fd86974
4 changed files with 54 additions and 4 deletions

View File

@ -1 +1,2 @@
from . import mail_channel from . import mail_channel
from . import res_users

View File

@ -1,5 +1,4 @@
# pylint: disable=missing-docstring # Copyright 2023 ForgeFlow
# Copyright 2016 ACSONE SA/NV
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, models from odoo import _, models
@ -15,7 +14,7 @@ class MailChannel(models.Model):
for user in users: for user in users:
if user in message.author_id.user_ids: if user in message.author_id.user_ids:
continue continue
user.notify_info( user.with_context(_notify_channel_message=True).notify_info(
message=_("You have a new message in channel %s") % self.name message=_("You have a new message in channel %s") % self.name
) )
return message return message

View File

@ -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,
)

View File

@ -10,6 +10,7 @@ class TestWebNotifyChannelMessage(common.TransactionCase):
def setUpClass(cls): def setUpClass(cls):
super(TestWebNotifyChannelMessage, cls).setUpClass() super(TestWebNotifyChannelMessage, cls).setUpClass()
cls.env.user = cls.env.ref("base.user_admin") 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 = api.Environment(cls.cr, cls.env.user.id, {})
cls.env.user.tz = False # Make sure there's no timezone in user cls.env.user.tz = False # Make sure there's no timezone in user
cls.user_internal = cls.env["res.users"].create( 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 = ( initial_message = (
self.env["mail.channel"] self.env["mail.channel"]
.search([("name", "=", "Test channel")], limit=1) .search([("name", "=", "Test channel")], limit=1)
@ -49,3 +50,23 @@ class TestWebNotifyChannelMessage(common.TransactionCase):
.message_ids[0] .message_ids[0]
) )
self.assertEqual(len(message), 1) 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)