[REF] mail: simplify code of Record.insert() when new record
- automatically add `_store` on the record - automatically register in `records` and return reactive when `records` is a obj (array => should keep doing it manually) Part-of: odoo/odoo#134008
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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];
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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} */
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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}
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -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";
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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[]} */
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user