Skip to content

Commit 469caae

Browse files
committed
[FIX] mail: message related to deleted record doesn't break discuss
Before this commit, when a message notification from inbox or history has its related record deleted, the user couldn't load inbox or history. Steps to reproduce: - Have mitchell admin receive notification in Odoo - Install `mail_group` - Wait a few seconds to receive following user_notification from mail group: ``` on My Company News Hello, You have messages to moderate, please go for the proceedings. ``` - Go to related record then delete it - Go back to inbox or history => One of them show `An error occurred while fetching messages.` This happens because the related record is not a `mail.thread`, but still creates some `mail.message` to send user notifications. `mail.thread` cascade delete the messages in message list, but threadless records do not. Because the related record is deleted, these messages are attempted to be displayed in mailbox, but due to related record having no `display_name` by non-existing, the fetch data of inbox/history crashes with: ``` MissingError: Record does not exist or has been deleted ``` This commit fixes the issue by doing its best to show message even when the related record has been deleted. opw-4546920 closes odoo#219791 X-original-commit: c922d6d Signed-off-by: Alexandre Kühn (aku) <aku@odoo.com>
1 parent 159f82a commit 469caae

3 files changed

Lines changed: 17 additions & 8 deletions

File tree

addons/mail/models/mail_message.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
# Part of Odoo. See LICENSE file for full copyright and licensing details.
22
from __future__ import annotations
33

4+
import contextlib
45
import logging
56
import re
67
import textwrap
78
from binascii import Error as binascii_error
89
from collections import defaultdict
910

1011
from odoo import _, api, fields, models, modules, tools
11-
from odoo.exceptions import AccessError
12+
from odoo.exceptions import AccessError, MissingError
1213
from odoo.osv import expression
1314
from odoo.tools import clean_context, format_list, groupby, SQL
1415
from odoo.tools.misc import OrderedSet
@@ -1033,13 +1034,17 @@ def _to_store(self, store: Store, fields, *, format_reply=True, msg_vals=False,
10331034
for message in self:
10341035
# model, res_id, record_name need to be kept for mobile app as iOS app cannot be updated
10351036
record = record_by_message.get(message)
1037+
record_name = False
1038+
default_subject = False
10361039
if record:
10371040
# sudo: if mentionned in a non accessible thread, user should be able to see the name
1038-
record_name = record.sudo().display_name
1039-
default_subject = record_name
1040-
if hasattr(record, "_message_compute_subject"):
1041-
# sudo: if mentionned in a non accessible thread, user should be able to see the subject
1042-
default_subject = record.sudo()._message_compute_subject()
1041+
with contextlib.suppress(MissingError):
1042+
record_name = record.sudo().display_name
1043+
if record_name:
1044+
default_subject = record_name
1045+
if hasattr(record, "_message_compute_subject"):
1046+
# sudo: if mentionned in a non accessible thread, user should be able to see the subject
1047+
default_subject = record.sudo()._message_compute_subject()
10431048
else:
10441049
record_name = False
10451050
default_subject = False

addons/mail/static/src/core/common/message.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
</small>
4444
<small t-if="isPersistentMessageFromAnotherThread" t-on-click.prevent="openRecord" class="ms-1 text-500">
4545
<t t-if="message.thread.model !== 'discuss.channel'">
46-
on <a t-att-href="message.resUrl"><t t-esc="message.thread.displayName"/></a>
46+
on <a t-if="message.thread.displayName" t-att-href="message.resUrl" t-esc="message.thread.displayName"/><em class="pe-1 text-decoration-line-through" t-else="">Deleted document</em>
4747
</t>
4848
<t t-else="">
4949
(from <a t-att-href="message.resUrl"><t t-esc="message.thread.prefix"/><t t-esc="message.thread.displayName or message.default_subject"/></a>)

addons/mail/tools/discuss.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
import odoo
88
from odoo import models
9+
from odoo.exceptions import MissingError
910
from odoo.tools import groupby
1011

1112

@@ -212,7 +213,10 @@ def _get_records_data(self, records, fields):
212213
if isinstance(field, dict):
213214
data.update(field)
214215
elif not field.predicate or field.predicate(record):
215-
data[field.rename or field.field_name] = field._get_value(record)
216+
try:
217+
data[field.rename or field.field_name] = field._get_value(record)
218+
except MissingError:
219+
break
216220
return records_data
217221

218222
def _get_record_index(self, model_name, values):

0 commit comments

Comments
 (0)