From bef40691958b8c85e08e53d93efc32749ffefed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Mon, 25 Sep 2023 12:51:26 +0200 Subject: [PATCH] [IMP] mail: introduce RecordList.add() in discuss model relations This method pushes the provided record if it's not in the record list, otherwise it does nothing. This works a bit like `add()` on Sets. Part-of: odoo/odoo#136308 --- .../src/embed/chatbot/chatbot_service.js | 8 ++------ .../static/src/core/common/attachment_model.js | 6 ++---- .../src/core/common/channel_member_model.js | 6 +----- .../static/src/core/common/message_service.js | 4 +--- addons/mail/static/src/core/common/record.js | 7 +++++++ .../static/src/core/common/thread_model.js | 15 +++++---------- .../static/src/core/common/thread_service.js | 8 +++----- .../src/core/web/mail_core_web_service.js | 8 +++----- .../src/core/web/thread_service_patch.js | 16 ++++++---------- .../src/discuss/call/common/rtc_service.js | 8 ++------ .../discuss/call/common/rtc_session_model.js | 7 +------ .../discuss/call/common/thread_model_patch.js | 18 +++++------------- .../core/web/discuss_sidebar_categories.xml | 2 +- .../message_pin/common/message_pin_service.js | 4 +--- 14 files changed, 40 insertions(+), 77 deletions(-) diff --git a/addons/im_livechat/static/src/embed/chatbot/chatbot_service.js b/addons/im_livechat/static/src/embed/chatbot/chatbot_service.js index 881b1aeff0f..58dde3c44ee 100644 --- a/addons/im_livechat/static/src/embed/chatbot/chatbot_service.js +++ b/addons/im_livechat/static/src/embed/chatbot/chatbot_service.js @@ -151,9 +151,7 @@ export class ChatBotService { ...rawMessage, body: markup(rawMessage.body), }); - if (message.notIn(!this.livechatService.thread?.messages)) { - this.livechatService.thread?.messages.push(message); - } + this.livechatService.thread?.messages.add(message); } this.hasPostedWelcomeSteps = true; } @@ -186,9 +184,7 @@ export class ChatBotService { ...stepMessage, body: markup(stepMessage.body), }); - if (message.notIn(this.livechatService.thread?.messages)) { - this.livechatService.thread?.messages.push(message); - } + this.livechatService.thread?.messages.add(message); } this.currentStep = step; if ( diff --git a/addons/mail/static/src/core/common/attachment_model.js b/addons/mail/static/src/core/common/attachment_model.js index aae6784b6be..6d46c8b3de5 100644 --- a/addons/mail/static/src/core/common/attachment_model.js +++ b/addons/mail/static/src/core/common/attachment_model.js @@ -60,10 +60,8 @@ export class Attachment extends Record { id: threadData.id, }); const thread = this.originThread; - if (this.notIn(thread.attachments)) { - thread.attachments.push(this); - thread.attachments.sort((a1, a2) => (a1.id < a2.id ? 1 : -1)); - } + thread.attachments.add(this); + thread.attachments.sort((a1, a2) => (a1.id < a2.id ? 1 : -1)); } } diff --git a/addons/mail/static/src/core/common/channel_member_model.js b/addons/mail/static/src/core/common/channel_member_model.js index d3cfbd5bd0c..7f1fe2057eb 100644 --- a/addons/mail/static/src/core/common/channel_member_model.js +++ b/addons/mail/static/src/core/common/channel_member_model.js @@ -55,11 +55,7 @@ export class ChannelMember extends Record { } switch (command) { case "ADD": - { - if (this.thread && this.notIn(this.thread.channelMembers)) { - this.thread.channelMembers.push(this); - } - } + this.thread?.channelMembers.add(this); break; case "DELETE": this.delete(); diff --git a/addons/mail/static/src/core/common/message_service.js b/addons/mail/static/src/core/common/message_service.js index c081790ca54..c2d882f1c3e 100644 --- a/addons/mail/static/src/core/common/message_service.js +++ b/addons/mail/static/src/core/common/message_service.js @@ -192,9 +192,7 @@ export class MessageService { const starred = this.store.discuss.starred; if (isStarred) { starred.counter++; - if (message.notIn(starred.messages)) { - starred.messages.push(message); - } + starred.messages.add(message); } else { starred.counter--; removeFromArrayWithPredicate(starred.messages, (msg) => msg.eq(message)); diff --git a/addons/mail/static/src/core/common/record.js b/addons/mail/static/src/core/common/record.js index 09ff16fffe6..b466c251b06 100644 --- a/addons/mail/static/src/core/common/record.js +++ b/addons/mail/static/src/core/common/record.js @@ -252,6 +252,13 @@ export class RecordList extends Array { .concat(...collections.map((c) => [...c])); } /** @param {R} */ + add(r) { + if (this.indexOf(r) !== -1) { + return; + } + this.push(r); + } + /** @param {R} */ delete(r) { const index = this.indexOf(r); if (index === -1) { diff --git a/addons/mail/static/src/core/common/thread_model.js b/addons/mail/static/src/core/common/thread_model.js index e3271983447..32edb4db565 100644 --- a/addons/mail/static/src/core/common/thread_model.js +++ b/addons/mail/static/src/core/common/thread_model.js @@ -166,9 +166,7 @@ export class Thread extends Record { if (members) { for (const member of members) { const record = this._store.ChannelMember.insert(member); - if (record.notIn(this.invitedMembers)) { - this.invitedMembers.push(record); - } + this.invitedMembers.add(record); } } break; @@ -199,13 +197,10 @@ export class Thread extends Record { ); } } - if (this.type === "channel" && !this._store.discuss.channels.threads.includes(this)) { - this._store.discuss.channels.threads.push(this); - } else if ( - (this.type === "chat" || this.type === "group") && - !this._store.discuss.chats.threads.includes(this) - ) { - this._store.discuss.chats.threads.push(this); + if (this.type === "channel") { + this._store.discuss.channels.threads.add(this); + } else if (this.type === "chat" || this.type === "group") { + this._store.discuss.chats.threads.add(this); } if (!this.type && !["mail.box", "discuss.channel"].includes(this.model)) { this.type = "chatter"; diff --git a/addons/mail/static/src/core/common/thread_service.js b/addons/mail/static/src/core/common/thread_service.js index 8b24013ef4f..6d5a9b1116b 100644 --- a/addons/mail/static/src/core/common/thread_service.js +++ b/addons/mail/static/src/core/common/thread_service.js @@ -346,8 +346,8 @@ export class ThreadService { }); if (!thread.isLoaded) { thread.messages.push(message); - if (message.isNeedaction && message.notIn(thread.needactionMessages)) { - thread.needactionMessages.push(message); + if (message.isNeedaction) { + thread.needactionMessages.add(message); } } thread.isLoaded = true; @@ -731,9 +731,7 @@ export class ThreadService { data.temporary_id = null; } const message = this.store.Message.insert(Object.assign(data, { body: markup(data.body) })); - if (message.notIn(thread.messages)) { - thread.messages.push(message); - } + thread.messages.add(message); if (!message.isEmpty && this.store.hasLinkPreviewFeature) { this.rpc("/mail/link_preview", { message_id: data.id }, { silent: true }); } diff --git a/addons/mail/static/src/core/web/mail_core_web_service.js b/addons/mail/static/src/core/web/mail_core_web_service.js index 5c3ffd11345..1bbf7d2d58c 100644 --- a/addons/mail/static/src/core/web/mail_core_web_service.js +++ b/addons/mail/static/src/core/web/mail_core_web_service.js @@ -58,14 +58,14 @@ export class MailCoreWeb { const message = this.store.Message.insert(data); const inbox = this.store.discuss.inbox; if (message.notIn(inbox.messages)) { - inbox.messages.push(message); inbox.counter++; } + inbox.messages.add(message); const thread = message.originThread; if (message.notIn(thread.needactionMessages)) { - thread.needactionMessages.push(message); thread.message_needaction_counter++; } + thread.needactionMessages.add(message); }); this.busService.subscribe("mail.message/mark_as_read", (payload) => { const { message_ids: messageIds, needaction_inbox_counter } = payload; @@ -96,9 +96,7 @@ export class MailCoreWeb { removeFromArray(message.needaction_partner_ids, partnerIndex); removeFromArrayWithPredicate(inbox.messages, ({ id }) => id === messageId); const history = this.store.discuss.history; - if (message.notIn(history.messages)) { - history.messages.push(message); - } + history.messages.add(message); } inbox.counter = needaction_inbox_counter; if (inbox.counter > inbox.messages.length) { diff --git a/addons/mail/static/src/core/web/thread_service_patch.js b/addons/mail/static/src/core/web/thread_service_patch.js index 367883deeae..920286cf153 100644 --- a/addons/mail/static/src/core/web/thread_service_patch.js +++ b/addons/mail/static/src/core/web/thread_service_patch.js @@ -90,8 +90,8 @@ patch(ThreadService.prototype, { followedThread: thread, ...followerData, }); - if (follower.notEq(thread.selfFollower) && follower.notIn(thread.followers)) { - thread.followers.push(follower); + if (follower.notEq(thread.selfFollower)) { + thread.followers.add(follower); } } thread.recipientsCount = result.recipientsCount; @@ -100,9 +100,7 @@ patch(ThreadService.prototype, { followedThread: thread, ...recipientData, }); - if (recipient.notIn(thread.recipients)) { - thread.recipients.push(recipient); - } + thread.recipients.add(recipient); } } if ("suggestedRecipients" in result) { @@ -187,8 +185,8 @@ patch(ThreadService.prototype, { followedThread: thread, ...data, }); - if (follower.notEq(thread.selfFollower) && follower.notIn(thread.followers)) { - thread.followers.push(follower); + if (follower.notEq(thread.selfFollower)) { + thread.followers.add(follower); } } }, @@ -204,9 +202,7 @@ patch(ThreadService.prototype, { followedThread: thread, ...data, }); - if (recipient.notIn(thread.recipients)) { - thread.recipients.push(recipient); - } + thread.recipients.add(recipient); } }, open(thread, replaceNewMessageChatWindow) { diff --git a/addons/mail/static/src/discuss/call/common/rtc_service.js b/addons/mail/static/src/discuss/call/common/rtc_service.js index 90ca5fe706c..4d96a3683db 100644 --- a/addons/mail/static/src/discuss/call/common/rtc_service.js +++ b/addons/mail/static/src/discuss/call/common/rtc_service.js @@ -831,9 +831,7 @@ export class Rtc { const activeSessionsData = rtcSessions[0][1]; for (const sessionData of activeSessionsData) { const session = this.store.RtcSession.insert(sessionData); - if (session.notIn(this.state.channel.rtcSessions)) { - this.state.channel.rtcSessions.push(session); - } + this.state.channel.rtcSessions.add(session); } const outdatedSessionsData = rtcSessions[1][1]; for (const sessionData of outdatedSessionsData) { @@ -1540,9 +1538,7 @@ export class Rtc { case "ADD": for (const sessionData of sessionsData) { const session = this.store.RtcSession.insert(sessionData); - if (session.notIn(channel.rtcSessions)) { - channel.rtcSessions.push(session); - } + channel.rtcSessions.add(session); } break; } diff --git a/addons/mail/static/src/discuss/call/common/rtc_session_model.js b/addons/mail/static/src/discuss/call/common/rtc_session_model.js index 95acf29824b..83cc415722e 100644 --- a/addons/mail/static/src/discuss/call/common/rtc_session_model.js +++ b/addons/mail/static/src/discuss/call/common/rtc_session_model.js @@ -31,12 +31,7 @@ export class RtcSession extends Record { const channelMemberRecord = this.store.ChannelMember.insert(channelMember); channelMemberRecord.rtcSession = session; session.channelMemberId = channelMemberRecord.id; - if ( - channelMemberRecord.thread && - session.notIn(channelMemberRecord.thread.rtcSessions) - ) { - channelMemberRecord.thread.rtcSessions.push(session); - } + channelMemberRecord.thread?.rtcSessions.add(session); } return session; } diff --git a/addons/mail/static/src/discuss/call/common/thread_model_patch.js b/addons/mail/static/src/discuss/call/common/thread_model_patch.js index dac4306a892..bc7058a0b9c 100644 --- a/addons/mail/static/src/discuss/call/common/thread_model_patch.js +++ b/addons/mail/static/src/discuss/call/common/thread_model_patch.js @@ -9,12 +9,8 @@ patch(Thread.prototype, { super.update(data); if ("rtc_inviting_session" in data) { const session = this._store.RtcSession.insert(data.rtc_inviting_session); - if (session.notIn(this.rtcSessions)) { - this.rtcSessions.push(session); - } - if (this.notIn(this._store.discuss.ringingThreads)) { - this._store.discuss.ringingThreads.push(this); - } + this.rtcSessions.add(session); + this._store.discuss.ringingThreads.add(this); } let rtcContinue = true; if ("rtcInvitingSession" in data) { @@ -26,10 +22,8 @@ patch(Thread.prototype, { rtcContinue = false; } else { const session = this._store.RtcSession.insert(data.rtcInvitingSession); - if (session.notIn(this.rtcSessions)) { - this.rtcSessions.push(session); - } - this._store.discuss.ringingThreads.push(this); + this.rtcSessions.add(session); + this._store.discuss.ringingThreads.add(this); } } if (rtcContinue && "rtcSessions" in data) { @@ -44,9 +38,7 @@ patch(Thread.prototype, { case "ADD": for (const rtcSessionData of sessionsData) { const session = this._store.RtcSession.insert(rtcSessionData); - if (session.notIn(this.rtcSessions)) { - this.rtcSessions.push(session); - } + this.rtcSessions.add(session); } break; } diff --git a/addons/mail/static/src/discuss/core/web/discuss_sidebar_categories.xml b/addons/mail/static/src/discuss/core/web/discuss_sidebar_categories.xml index 1c9871d092e..b1f7d4521dc 100644 --- a/addons/mail/static/src/discuss/core/web/discuss_sidebar_categories.xml +++ b/addons/mail/static/src/discuss/core/web/discuss_sidebar_categories.xml @@ -34,7 +34,7 @@ - + diff --git a/addons/mail/static/src/discuss/message_pin/common/message_pin_service.js b/addons/mail/static/src/discuss/message_pin/common/message_pin_service.js index 0157c9caea8..26701c5a00e 100644 --- a/addons/mail/static/src/discuss/message_pin/common/message_pin_service.js +++ b/addons/mail/static/src/discuss/message_pin/common/message_pin_service.js @@ -37,9 +37,7 @@ patch(MessageModel.prototype, { (pinnedAt !== undefined || this.isEmpty) ) { if (pinnedAt && !this.isEmpty) { - if (this.notIn(this.originThread.pinnedMessages)) { - this.originThread.pinnedMessages.push(this); - } + this.originThread.pinnedMessages.add(this); this.pinnedAt = pinnedAt; } else { delete this.pinnedAt;