From e3dbb1b06404e4470fdd93323c6f8ebb8e5b84a9 Mon Sep 17 00:00:00 2001 From: "Anh Thao Pham (pta)" Date: Mon, 31 Jan 2022 16:17:40 +0000 Subject: [PATCH] [FIX] mail: restore mention links after note edition MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Open any record with a chatter (i.e. a SO) - Log a note with a mention to any res.partner - Edit note and save The mention is not a link anymore, but just plain text. During edition the list of mentioned partners is not retrieved correctly, preventing function that generates links from mentions to work properly. On client-side, a variable named MentionedPartners is used to generate mention links when posting a message. It should contain the id and name (to match the mention text) of each mentioned partners. During edition, MentionedPartners is empty, leading to this issue. As the list of mentioned partners is stored in message model in partner_ids field, "id" and "name" of partner_ids will be retrieved and added to messages data when requested by client side. This will allowed to populate MentionedPartners with all required data. Note that retrieving "name" from partner_ids will add an extra query, as seen in the modified test. opw-2739665 closes odoo/odoo#84152 X-original-commit: 510764730057b20a1706ae20d50549e280f7f9a7 Signed-off-by: Sébastien Theys (seb) --- addons/mail/models/mail_message.py | 2 +- addons/mail/static/src/models/message/message.js | 13 +++++++++++-- .../static/src/models/message_view/message_view.js | 1 + addons/mail/static/tests/helpers/mock_server.js | 5 +++++ addons/test_mail/tests/test_performance.py | 2 +- 5 files changed, 19 insertions(+), 4 deletions(-) diff --git a/addons/mail/models/mail_message.py b/addons/mail/models/mail_message.py index 4a96c7e4991..daca9e6e019 100644 --- a/addons/mail/models/mail_message.py +++ b/addons/mail/models/mail_message.py @@ -974,6 +974,7 @@ class Message(models.Model): 'is_discussion': message_sudo.subtype_id.id == com_id, 'subtype_description': message_sudo.subtype_id.description, 'is_notification': vals['message_type'] == 'user_notification', + 'recipients': [{'id': p.id, 'name': p.name} for p in message_sudo.partner_ids], }) if vals['model'] and self.env[vals['model']]._original_module: vals['module_icon'] = modules.module.get_module_icon(self.env[vals['model']]._original_module) @@ -984,7 +985,6 @@ class Message(models.Model): 'id', 'body', 'date', 'author_id', 'email_from', # base message fields 'message_type', 'subtype_id', 'subject', # message specific 'model', 'res_id', 'record_name', # document related - 'partner_ids', # recipients 'starred_partner_ids', # list of partner ids for whom the message is starred ] diff --git a/addons/mail/static/src/models/message/message.js b/addons/mail/static/src/models/message/message.js index 2769edeebd6..be9d010037a 100644 --- a/addons/mail/static/src/models/message/message.js +++ b/addons/mail/static/src/models/message/message.js @@ -106,8 +106,8 @@ registerModel({ data2.parentMessage = insertAndReplace(this.convertData(data.parentMessage)); } } - if ('partner_ids' in data && this.messaging.currentPartner) { - data2.isCurrentPartnerMentioned = data.partner_ids.includes(this.messaging.currentPartner.id); + if ('recipients' in data) { + data2.recipients = insertAndReplace(data.recipients); } if ('starred_partner_ids' in data && this.messaging.currentPartner) { data2.isStarred = data.starred_partner_ids.includes(this.messaging.currentPartner.id); @@ -421,6 +421,13 @@ registerModel({ const inlineBody = htmlToTextContentInline(this.body); return inlineBody.toLowerCase() === this.subtype_description.toLowerCase(); }, + /** + * @private + * @returns {boolean} + */ + _computeIsCurrentPartnerMentioned() { + return this.recipients.includes(this.messaging.currentPartner); + }, /** * The method does not attempt to cover all possible cases of empty * messages, but mostly those that happen with a standard flow. Indeed @@ -704,6 +711,7 @@ registerModel({ * Determine whether the current partner is mentioned. */ isCurrentPartnerMentioned: attr({ + compute: '_computeIsCurrentPartnerMentioned', default: false, }), /** @@ -762,6 +770,7 @@ registerModel({ compute: '_computePrettyBody', default: "", }), + recipients: many('Partner'), subject: attr(), subtype_description: attr(), subtype_id: attr(), diff --git a/addons/mail/static/src/models/message_view/message_view.js b/addons/mail/static/src/models/message_view/message_view.js index e0017954358..cc56bfd5c75 100644 --- a/addons/mail/static/src/models/message_view/message_view.js +++ b/addons/mail/static/src/models/message_view/message_view.js @@ -75,6 +75,7 @@ registerModel({ this.update({ composerForEditing: insertAndReplace({ isLastStateChangeProgrammatic: true, + mentionedPartners: replace(this.message.recipients), textInputContent, textInputCursorEnd: textInputContent.length, textInputCursorStart: textInputContent.length, diff --git a/addons/mail/static/tests/helpers/mock_server.js b/addons/mail/static/tests/helpers/mock_server.js index 32cd25dcbc0..a09c02c17e1 100644 --- a/addons/mail/static/tests/helpers/mock_server.js +++ b/addons/mail/static/tests/helpers/mock_server.js @@ -1431,12 +1431,17 @@ MockServer.include({ const trackingValueIds = this._getRecords('mail.tracking.value', [ ['id', 'in', message.tracking_value_ids], ]); + const partners = this._getRecords( + 'res.partner', + [['id', 'in', message.partner_ids]], + ); const response = Object.assign({}, message, { attachment_ids: formattedAttachments, author_id: formattedAuthor, history_partner_ids: historyPartnerIds, needaction_partner_ids: needactionPartnerIds, notifications, + recipients: partners.map(p => ({ id: p.id, name: p.name })), record_name: thread && (thread.name !== undefined ? thread.name : thread.display_name), tracking_value_ids: trackingValueIds, }); diff --git a/addons/test_mail/tests/test_performance.py b/addons/test_mail/tests/test_performance.py index b9ec8595e9f..29a014a18a2 100644 --- a/addons/test_mail/tests/test_performance.py +++ b/addons/test_mail/tests/test_performance.py @@ -428,7 +428,7 @@ class TestMailAPIPerformance(BaseMailPerformance): def test_message_post_one_inbox_notification(self): record = self.env['mail.test.simple'].create({'name': 'Test'}) - with self.assertQueryCount(__system__=12, employee=16): + with self.assertQueryCount(__system__=13, employee=17): record.message_post( body='

Test Post Performances with an inbox ping

', partner_ids=self.user_test.partner_id.ids,