diff --git a/addons/mail/static/src/core/common/attachment_model.js b/addons/mail/static/src/core/common/attachment_model.js index 25964f353f2..8590c7faed5 100644 --- a/addons/mail/static/src/core/common/attachment_model.js +++ b/addons/mail/static/src/core/common/attachment_model.js @@ -19,19 +19,12 @@ export class Attachment extends Record { if (!("id" in data)) { throw new Error("Cannot insert attachment: id is missing in data"); } - let attachment = this.get(data); - if (!attachment) { - attachment = this.new(data); - this.records[attachment.localId] = attachment; - attachment = this.records[attachment.localId]; - Object.assign(attachment, { _store: this.store, id: data.id }); - } + const attachment = this.get(data) ?? this.new(data); + Object.assign(attachment, { id: data.id }); this.env.services["mail.attachment"].update(attachment, data); return attachment; } - /** @type {import("@mail/core/common/store_service").Store} */ - _store; accessToken; checksum; extension; diff --git a/addons/mail/static/src/core/common/canned_response_model.js b/addons/mail/static/src/core/common/canned_response_model.js index 07cbc1e4456..0f48b3aea40 100644 --- a/addons/mail/static/src/core/common/canned_response_model.js +++ b/addons/mail/static/src/core/common/canned_response_model.js @@ -11,12 +11,7 @@ export class CannedResponse extends Record { * @returns {CannedResponse} */ static insert(data) { - let cannedResponse = this.get(data); - if (!cannedResponse) { - cannedResponse = this.new(data); - this.records[cannedResponse.localId] = cannedResponse; - cannedResponse = this.records[cannedResponse.localId]; - } + const cannedResponse = this.get(data) ?? this.new(data); Object.assign(cannedResponse, { id: data.id, name: data.source, 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 1e4532db078..290603bb9f1 100644 --- a/addons/mail/static/src/core/common/channel_member_model.js +++ b/addons/mail/static/src/core/common/channel_member_model.js @@ -19,13 +19,7 @@ export class ChannelMember extends Record { */ static insert(data) { const memberData = Array.isArray(data) ? data[1] : data; - let member = this.get(memberData); - if (!member) { - member = this.new(memberData); - this.records[member.localId] = member; - member = this.records[member.localId]; - member._store = this.store; - } + const member = this.get(memberData) ?? this.new(memberData); this.env.services["discuss.channel.member"].update(member, data); return member; } @@ -35,8 +29,6 @@ export class ChannelMember extends Record { personaLocalId; rtcSessionId; threadId; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; get persona() { return this._store.Persona.records[this.personaLocalId]; diff --git a/addons/mail/static/src/core/common/chat_window_model.js b/addons/mail/static/src/core/common/chat_window_model.js index 5b16ede3bac..249faa4fdd0 100644 --- a/addons/mail/static/src/core/common/chat_window_model.js +++ b/addons/mail/static/src/core/common/chat_window_model.js @@ -19,10 +19,7 @@ export class ChatWindow extends Record { const chatWindow = this.records.find((c) => c.threadLocalId === data.thread?.localId); if (!chatWindow) { const chatWindow = this.new(data); - Object.assign(chatWindow, { - thread: data.thread, - _store: this.store, - }); + Object.assign(chatWindow, { thread: data.thread }); assignDefined(chatWindow, data); let index; const visible = this.env.services["mail.chat_window"].visible; @@ -52,9 +49,6 @@ export class ChatWindow extends Record { return chatWindow; } - /** @type {import("@mail/core/common/store_service").Store} */ - _store; - /** @type {import("@mail/core/common/thread_model").Thread.localId} */ threadLocalId; autofocus = 0; diff --git a/addons/mail/static/src/core/common/composer_model.js b/addons/mail/static/src/core/common/composer_model.js index ab73449bab5..f94b9f83c47 100644 --- a/addons/mail/static/src/core/common/composer_model.js +++ b/addons/mail/static/src/core/common/composer_model.js @@ -29,10 +29,7 @@ export class Composer extends Record { Object.assign(composer, { message }); Object.assign(message, { composer }); } - Object.assign(composer, { - textInputContent: "", - _store: this.store, - }); + Object.assign(composer, { textInputContent: "" }); } if ("textInputContent" in data) { composer.textInputContent = data.textInputContent; @@ -73,8 +70,6 @@ export class Composer extends Record { }; /** @type {boolean} */ forceCursorMove; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; isFocused = false; /** @type {import("@mail/core/common/message_model").Message} */ diff --git a/addons/mail/static/src/core/common/follower_model.js b/addons/mail/static/src/core/common/follower_model.js index fa0c0243f29..08390912acc 100644 --- a/addons/mail/static/src/core/common/follower_model.js +++ b/addons/mail/static/src/core/common/follower_model.js @@ -19,18 +19,12 @@ export class Follower extends Record { * @returns {Follower} */ static insert(data) { - let follower = this.get(data); - if (!follower) { - follower = this.new(data); - this.records[follower.localId] = follower; - follower = this.records[follower.localId]; - } + const follower = this.get(data) ?? this.new(data); Object.assign(follower, { followedThread: data.followedThread, id: data.id, isActive: data.is_active, partner: this.store.Persona.insert({ ...data.partner, type: "partner" }), - _store: this.store, }); return follower; } @@ -43,8 +37,6 @@ export class Follower extends Record { isActive; /** @type {import("@mail/core/common/persona_model").Persona} */ partner; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; /** * @returns {boolean} diff --git a/addons/mail/static/src/core/common/message_model.js b/addons/mail/static/src/core/common/message_model.js index 62963c68320..fefe4cd2f91 100644 --- a/addons/mail/static/src/core/common/message_model.js +++ b/addons/mail/static/src/core/common/message_model.js @@ -21,22 +21,14 @@ export class Message extends Record { * @returns {Message} */ static insert(data) { - let message; if (data.res_id) { this.store.Thread.insert({ model: data.model, id: data.res_id, }); } - message = this.get(data); - if (!message) { - message = this.new(data); - message._store = this.store; - this.records[message.localId] = message; - message = this.records[message.localId]; - } + const message = this.get(data) ?? this.new(data); this.env.services["mail.message"].update(message, data); - // return reactive version return message; } @@ -102,8 +94,6 @@ export class Message extends Record { * have them. Message without date like transient message can be missordered */ now = DateTime.now().set({ milliseconds: 0 }); - /** @type {import("@mail/core/common/store_service").Store} */ - _store; /** * @returns {boolean} diff --git a/addons/mail/static/src/core/common/message_reactions_model.js b/addons/mail/static/src/core/common/message_reactions_model.js index 9944108b4bb..574c9ef5ec2 100644 --- a/addons/mail/static/src/core/common/message_reactions_model.js +++ b/addons/mail/static/src/core/common/message_reactions_model.js @@ -14,7 +14,6 @@ export class MessageReactions extends Record { ); if (!reaction) { reaction = this.new(data); - reaction._store = this.store; } const personasToUnlink = new Set(); const alreadyKnownPersonaIds = new Set(reaction.personaLocalIds); @@ -57,8 +56,6 @@ export class MessageReactions extends Record { personaLocalIds = []; /** @type {number} */ messageId; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; /** @type {import("@mail/core/common/persona_model").Persona[]} */ get personas() { diff --git a/addons/mail/static/src/core/common/notification_group_model.js b/addons/mail/static/src/core/common/notification_group_model.js index 716afd79a7c..79c8fc13358 100644 --- a/addons/mail/static/src/core/common/notification_group_model.js +++ b/addons/mail/static/src/core/common/notification_group_model.js @@ -25,7 +25,7 @@ export class NotificationGroup extends Record { if (!group) { const id = nextId++; group = this.new({ id }); - Object.assign(group, { id, _store: this.store }); + Object.assign(group, { id }); this.store.NotificationGroup.records.push(group); // return reactive group = this.store.NotificationGroup.records.find((g) => g.eq(group)); @@ -49,8 +49,6 @@ export class NotificationGroup extends Record { resIds = new Set(); /** @type {'sms' | 'email'} */ type; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; get iconSrc() { return "/mail/static/src/img/smiley/mailfailure.jpg"; diff --git a/addons/mail/static/src/core/common/notification_model.js b/addons/mail/static/src/core/common/notification_model.js index d2a92d7e744..a6295f9ae9d 100644 --- a/addons/mail/static/src/core/common/notification_model.js +++ b/addons/mail/static/src/core/common/notification_model.js @@ -13,16 +13,8 @@ export class Notification extends Record { * @returns {Notification} */ static insert(data) { - let notification = this.get(data); - if (!notification) { - notification = this.new(data); - this.records[notification.localId] = notification; - Object.assign(notification, { - id: data.id, - _store: this.store, - }); - notification = this.records[notification.localId]; - } + const notification = this.get(data) ?? this.new(data); + Object.assign(notification, { id: data.id }); this.env.services["mail.message"].updateNotification(notification, data); return notification; } @@ -39,8 +31,6 @@ export class Notification extends Record { failure_type; /** @type {import("@mail/core/common/persona_model").Persona} */ persona; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; get message() { return this._store.Message.get(this.messageId); diff --git a/addons/mail/static/src/core/common/persona_model.js b/addons/mail/static/src/core/common/persona_model.js index ac791c11685..1a79d1f15c1 100644 --- a/addons/mail/static/src/core/common/persona_model.js +++ b/addons/mail/static/src/core/common/persona_model.js @@ -21,13 +21,7 @@ export class Persona extends Record { * @returns {Persona} */ static insert(data) { - let persona = this.get(data); - if (!persona) { - persona = this.new(data); - persona._store = this.store; - this.records[persona.localId] = persona; - persona = this.records[persona.localId]; - } + const persona = this.get(data) ?? this.new(data); this.env.services["mail.persona"].update(persona, data); // return reactive version return persona; @@ -54,8 +48,6 @@ export class Persona extends Record { /** @type {ImStatus} */ im_status; isAdmin = false; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; get nameOrDisplayName() { return this.name || this.displayName; diff --git a/addons/mail/static/src/core/common/record.js b/addons/mail/static/src/core/common/record.js index 01bc2a8b665..1244c58fafc 100644 --- a/addons/mail/static/src/core/common/record.js +++ b/addons/mail/static/src/core/common/record.js @@ -18,6 +18,8 @@ export function OR(...args) { export class Record { static id; static records = {}; + /** @type {import("@mail/core/common/store_service").Store} */ + static store; static get(data) { return this.records[this.localId(data)]; } @@ -68,7 +70,14 @@ export class Record { */ static new(data) { const obj = new this.Class(); - return Object.assign(obj, { localId: this.localId(data) }); + let record = Object.assign(obj, { localId: this.localId(data) }); + Object.assign(record, { _store: this.store }); + if (!Array.isArray(this.records)) { + this.records[record.localId] = record; + // return reactive version + record = this.records[record.localId]; + } + return record; } /** @@ -77,6 +86,9 @@ export class Record { */ static insert(data) {} + /** @type {import("@mail/core/common/store_service").Store} */ + _store; + /** @param {Record} record */ eq(record) { return toRaw(this) === toRaw(record); diff --git a/addons/mail/static/src/core/common/thread_model.js b/addons/mail/static/src/core/common/thread_model.js index df7945d45e8..93c3e4cf17c 100644 --- a/addons/mail/static/src/core/common/thread_model.js +++ b/addons/mail/static/src/core/common/thread_model.js @@ -57,11 +57,7 @@ export class Thread extends Record { id: data.id, model: data.model, type: data.type, - _store: this.store, }); - this.records[thread.localId] = thread; - // return reactive version. - thread = this.records[thread.localId]; onChange(thread, "message_unread_counter", () => { if (thread.channel) { thread.channel.message_unread_counter = thread.message_unread_counter; @@ -176,8 +172,6 @@ export class Thread extends Record { transientMessages = []; /** @type {'channel'|'chat'|'chatter'|'livechat'|'group'|'mailbox'} */ type; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; /** @type {string} */ defaultDisplayMode; /** @type {SeenInfo[]} */ diff --git a/addons/mail/static/src/core/web/activity_model.js b/addons/mail/static/src/core/web/activity_model.js index ec2f9d0e3b7..c597209991d 100644 --- a/addons/mail/static/src/core/web/activity_model.js +++ b/addons/mail/static/src/core/web/activity_model.js @@ -45,17 +45,8 @@ export class Activity extends Record { * @returns {Activity} */ static insert(data, { broadcast = true } = {}) { - let activity = this.get(data); - if (!activity) { - activity = this.new(data); - Object.assign(activity, { - id: data.id, - _store: this.store, - }); - this.records[activity.localId] = activity; - // return reactive - activity = this.records[activity.localId]; - } + const activity = this.get(data) ?? this.new(data); + Object.assign(activity, { id: data.id }); if (data.request_partner_id) { data.request_partner_id = data.request_partner_id[0]; } @@ -123,8 +114,6 @@ export class Activity extends Record { write_date; /** @type {[number, string]} */ write_uid; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; } Activity.register(); 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 d8754923653..40dc83849bd 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 @@ -11,11 +11,7 @@ export class RtcSession extends Record { * @returns {number, RtcSession} */ static insert(data) { - let session = this.get(data); - if (!session) { - session = this.new(data); - session._store = this.store; - } + const session = this.get(data) ?? this.new(data); const { channelMember, ...remainingData } = data; for (const key in remainingData) { session[key] = remainingData[key]; @@ -31,9 +27,7 @@ export class RtcSession extends Record { channelMemberRecord.thread.rtcSessions[session.id] = session; } } - this.records[session.localId] = session; - // return reactive version - return this.records[session.localId]; + return session; } // Server data @@ -62,8 +56,6 @@ export class RtcSession extends Record { videoComponentCount = 0; /** @type {MediaStream} */ videoStream; - /** @type {import("@mail/core/common/store_service").Store} */ - _store; // RTC stats connectionState; localCandidateType;