diff --git a/addons/mail/controllers/thread.py b/addons/mail/controllers/thread.py index 050fcb16bad..d63ea6751a9 100644 --- a/addons/mail/controllers/thread.py +++ b/addons/mail/controllers/thread.py @@ -62,13 +62,7 @@ class ThreadController(http.Controller): @http.route("/mail/message/post", methods=["POST"], type="json", auth="public") def mail_message_post(self, thread_model, thread_id, post_data): - if thread_model == "discuss.channel": - channel_member_sudo = request.env["discuss.channel.member"]._get_as_sudo_from_request_or_raise( - request=request, channel_id=int(thread_id) - ) - thread = channel_member_sudo.channel_id - else: - thread = request.env[thread_model].browse(int(thread_id)).exists() + thread = request.env[thread_model]._get_from_request_or_raise(request, int(thread_id)) if "body" in post_data: post_data["body"] = Markup(post_data["body"]) # contains HTML such as @mentions message_data = thread.message_post( diff --git a/addons/mail/models/discuss/discuss_channel.py b/addons/mail/models/discuss/discuss_channel.py index 299a5e551a8..ccc62ba00f0 100644 --- a/addons/mail/models/discuss/discuss_channel.py +++ b/addons/mail/models/discuss/discuss_channel.py @@ -279,6 +279,13 @@ class Channel(models.Model): if not self._cr.fetchone(): self._cr.execute('CREATE INDEX discuss_channel_member_seen_message_id_idx ON discuss_channel_member (channel_id,partner_id,seen_message_id)') + @api.model + def _get_from_request_or_raise(self, request, thread_id): + """Overridden because guests and (portal) users need sudo to post on channels. This ensures they are actually + members before granting them access, as well as properly setting up the guest context on the resulting thread + if applicable.""" + return self.env["discuss.channel.member"]._get_as_sudo_from_request_or_raise(request, thread_id).channel_id + # ------------------------------------------------------------ # MEMBERS MANAGEMENT # ------------------------------------------------------------ diff --git a/addons/mail/models/mail_thread.py b/addons/mail/models/mail_thread.py index aaf4dc5e57d..730d7db9ecc 100644 --- a/addons/mail/models/mail_thread.py +++ b/addons/mail/models/mail_thread.py @@ -454,6 +454,10 @@ class MailThread(models.AbstractModel): if any(message.message_type != 'comment' for message in messages): raise exceptions.UserError(_("Only messages type comment can have their content updated")) + @api.model + def _get_from_request_or_raise(self, request, thread_id): + return self.search([("id", "=", thread_id)]) + # ------------------------------------------------------------ # FIELDS HELPERS # ------------------------------------------------------------