From 4ab5c2a48b9ed5207d2ea1c6f6a8d6421c611cf1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Mon, 24 Aug 2020 10:37:36 +0000 Subject: [PATCH] [FIX] mail, im_livechat: ignore notifications for unknown channels Inserting channels and handling notifications for channels that have not been properly initialized through the `channel_info` leads to issues due to incomplete data that is not properly handled, such as bad correspondent, missing uuid, inconsistent name, etc. Moreover it doesn't make sense functionally to handle notifications such as typing status if the channel isn't going to be displayed. PR: #56243 Part of task-2323670 X-original-commit: 75070665060d64fe7b532aac4fc8e8410381b083 --- .../messaging_notification_handler.js | 5 ++- .../mail/static/src/models/message/message.js | 18 +++++------ .../messaging_notification_handler.js | 32 ++++++++++--------- 3 files changed, 29 insertions(+), 26 deletions(-) diff --git a/addons/im_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js b/addons/im_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js index a418b4c331d..dd9d08b606c 100644 --- a/addons/im_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js +++ b/addons/im_livechat/static/src/models/messaging_notification_handler/messaging_notification_handler.js @@ -17,10 +17,13 @@ registerInstancePatchModel('mail.messaging_notification_handler', 'im_livechat/s */ _handleNotificationChannelTypingStatus(data) { const { channelId, partner_id } = data; - const channel = this.env.models['mail.thread'].insert({ + const channel = this.env.models['mail.thread'].findFromIdentifyingData({ id: channelId, model: 'mail.channel', }); + if (!channel) { + return; + } let partnerId; if (partner_id === this.env.messaging.publicPartner.id) { // Some shenanigans that this is a typing notification diff --git a/addons/mail/static/src/models/message/message.js b/addons/mail/static/src/models/message/message.js index d38b50a3083..582745e28f2 100644 --- a/addons/mail/static/src/models/message/message.js +++ b/addons/mail/static/src/models/message/message.js @@ -63,16 +63,14 @@ function factory(dependencies) { data2.body = data.body; } if ('channel_ids' in data && data.channel_ids) { - // AKU FIXME: side-effect of calling convert... - const channelList = []; - for (const channelId of data.channel_ids) { - const channel = this.env.models['mail.thread'].insert({ - id: channelId, - model: 'mail.channel', - }); - channelList.push(channel); - } - data2.serverChannels = [['replace', channelList]]; + const channels = data.channel_ids + .map(channelId => + this.env.models['mail.thread'].findFromIdentifyingData({ + id: channelId, + model: 'mail.channel', + }) + ).filter(channel => !!channel); + data2.serverChannels = [['replace', channels]]; } if ('date' in data && data.date) { data2.date = moment(str_to_datetime(data.date)); diff --git a/addons/mail/static/src/models/messaging_notification_handler/messaging_notification_handler.js b/addons/mail/static/src/models/messaging_notification_handler/messaging_notification_handler.js index 7bd0e16d6f2..6764f28ceb4 100644 --- a/addons/mail/static/src/models/messaging_notification_handler/messaging_notification_handler.js +++ b/addons/mail/static/src/models/messaging_notification_handler/messaging_notification_handler.js @@ -144,9 +144,10 @@ function factory(dependencies) { last_message_id, partner_id, }) { - const channel = this.env.models['mail.thread'].find(thread => - thread.id === channelId && thread.model === 'mail.channel' - ); + const channel = this.env.models['mail.thread'].findFromIdentifyingData({ + id: channelId, + model: 'mail.channel', + }); if (!channel) { // for example seen from another browser, the current one has no // knowledge of the channel @@ -198,10 +199,10 @@ function factory(dependencies) { const message = this.env.models['mail.message'].insert(convertedData); // join the corresponding channel if necessary - const channel = this.env.models['mail.thread'].find(thread => - thread.id === channelId && - thread.model === 'mail.channel' - ); + const channel = this.env.models['mail.thread'].findFromIdentifyingData({ + id: channelId, + model: 'mail.channel', + }); if (!channel || !channel.isPinned) { this.env.models['mail.thread'].joinChannel(channelId); } @@ -275,9 +276,10 @@ function factory(dependencies) { last_message_id, partner_id, }) { - const channel = this.env.models['mail.thread'].find(thread => - thread.id === channelId && thread.model === 'mail.channel' - ); + const channel = this.env.models['mail.thread'].findFromIdentifyingData({ + id: channelId, + model: 'mail.channel', + }); if (!channel) { // for example seen from another browser, the current one has no // knowledge of the channel @@ -327,10 +329,13 @@ function factory(dependencies) { is_typing, partner_id, }) { - const channel = this.env.models['mail.thread'].insert({ + const channel = this.env.models['mail.thread'].findFromIdentifyingData({ id: channelId, model: 'mail.channel', }); + if (!channel) { + return; + } const partner = this.env.models['mail.partner'].insert({ id: partner_id }); if (partner === this.env.messaging.currentPartner) { // Ignore management of current partner is typing notification. @@ -444,10 +449,7 @@ function factory(dependencies) { _handleNotificationPartnerChannel(data) { const convertedData = this.env.models['mail.thread'].convertData(data); - let channel = this.env.models['mail.thread'].find(thread => - thread.id === convertedData.id && - thread.model === 'mail.channel' - ); + let channel = this.env.models['mail.thread'].findFromIdentifyingData(convertedData); const wasCurrentPartnerMember = ( channel && channel.members.includes(this.env.messaging.currentPartner)