[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
This commit is contained in:
Alexandre Kühn
2023-09-27 13:11:58 +00:00
parent 22ed4ec268
commit bef4069195
14 changed files with 40 additions and 77 deletions
@@ -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 (
@@ -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));
}
}
@@ -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();
@@ -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));
@@ -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) {
@@ -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";
@@ -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 });
}
@@ -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) {
@@ -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) {
@@ -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;
}
@@ -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;
}
@@ -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;
}
@@ -34,7 +34,7 @@
<t t-set="thread" t-value="thread"/>
</t>
</t>
<t t-elif="category.threads.includes(store.discuss.thread)" t-call="mail.DiscussSidebarChannel">
<t t-elif="store.discuss.thread.in(category.threads)" t-call="mail.DiscussSidebarChannel">
<t t-set="thread" t-value="store.discuss.thread"/>
</t>
</t>
@@ -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;