[REF] mail: remove code in model constructor

In preparation to improvements in models.
Some implementation techniques will want to `new Model()`
without any side-effects.

Part-of: odoo/odoo#133658
This commit is contained in:
Alexandre Kühn
2023-08-31 17:27:50 +00:00
parent a770d7aa33
commit 02ca218e3c
10 changed files with 58 additions and 100 deletions
@@ -17,7 +17,11 @@ export class ChatWindow extends Record {
static insert(data = {}) {
const chatWindow = this.records.find((c) => c.threadLocalId === data.thread?.localId);
if (!chatWindow) {
const chatWindow = new ChatWindow(this.store, data);
const chatWindow = new ChatWindow();
Object.assign(chatWindow, {
thread: data.thread,
_store: this.store,
});
assignDefined(chatWindow, data);
let index;
const visible = this.env.services["mail.chat_window"].visible;
@@ -56,19 +60,6 @@ export class ChatWindow extends Record {
folded = false;
hidden = false;
/**
* @param {import("@mail/core/common/store_service").Store} store
* @param {ChatWindowData} data
* @returns {ChatWindow}
*/
constructor(store, data) {
super();
Object.assign(this, {
thread: data.thread,
_store: store,
});
}
get thread() {
return this._store.Thread.records[this.threadLocalId];
}
@@ -18,7 +18,20 @@ export class Composer extends Record {
}
let composer = (thread ?? message)?.composer;
if (!composer) {
composer = new Composer(this.store, data);
composer = new Composer();
const { message, thread } = data;
if (thread) {
composer.thread = thread;
Object.assign(composer, { thread });
Object.assign(thread, { composer });
} else if (message) {
Object.assign(composer, { message });
Object.assign(message, { composer });
}
Object.assign(composer, {
textInputContent: "",
_store: this.store,
});
}
if ("textInputContent" in data) {
composer.textInputContent = data.textInputContent;
@@ -62,22 +75,6 @@ export class Composer extends Record {
/** @type {import("@mail/core/common/store_service").Store} */
_store;
isFocused = false;
constructor(store, data) {
super();
const { message, thread } = data;
if (thread) {
this.thread = thread;
thread.composer = this;
} else if (message) {
this.message = message;
message.composer = this;
}
Object.assign(this, {
textInputContent: "",
_store: store,
});
}
}
modelRegistry.add(Composer.name, Composer);
@@ -8,13 +8,14 @@ export class LinkPreview extends Record {
* @returns {LinkPreview}
*/
static insert(data) {
const linkPreview = data.message.linkPreviews.find(
(linkPreview) => linkPreview.id === data.id
);
let linkPreview = data.message.linkPreviews.find((lp) => lp.id === data.id);
if (linkPreview) {
return Object.assign(linkPreview, data);
}
return new LinkPreview(data);
linkPreview = new LinkPreview();
Object.assign(linkPreview, data);
this.store.Message.records[data.message.id]?.linkPreviews.push(linkPreview);
return linkPreview;
}
/** @type {number} */
@@ -36,15 +37,6 @@ export class LinkPreview extends Record {
/** @type {string} */
source_url;
/**
* @param {Object} data
* @returns {LinkPreview}
*/
constructor(data) {
super();
Object.assign(this, data);
}
get imageUrl() {
return this.og_image ? this.og_image : this.source_url;
}
@@ -1,6 +1,5 @@
/* @odoo-module */
import { LinkPreview } from "@mail/core/common/link_preview_model";
import { removeFromArrayWithPredicate } from "@mail/utils/common/arrays";
import { markup, reactive } from "@odoo/owl";
@@ -22,7 +22,12 @@ export class NotificationGroup extends Record {
);
});
if (!group) {
group = new NotificationGroup(this.store);
group = new NotificationGroup();
group._store = this.store;
this.store.NotificationGroup.records.push(group);
group.id = nextId++;
// return reactive
group = this.store.NotificationGroup.records.find((g) => g.eq(group));
}
this.env.services["mail.message"].updateNotificationGroup(group, data);
if (group.notifications.length === 0) {
@@ -46,15 +51,6 @@ export class NotificationGroup extends Record {
/** @type {import("@mail/core/common/store_service").Store} */
_store;
constructor(store) {
super();
this._store = store;
this._store.NotificationGroup.records.push(this);
this.id = nextId++;
// return reactive
return store.NotificationGroup.records.find((group) => group.eq(this));
}
get iconSrc() {
return "/mail/static/src/img/smiley/mailfailure.jpg";
}
@@ -14,7 +14,12 @@ export class Notification extends Record {
static insert(data) {
let notification = this.records[data.id];
if (!notification) {
this.records[data.id] = new Notification(this.store, data);
notification = new Notification();
this.records[data.id] = notification;
Object.assign(notification, {
id: data.id,
_store: this.store,
});
notification = this.records[data.id];
}
this.env.services["mail.message"].updateNotification(notification, data);
@@ -36,14 +41,6 @@ export class Notification extends Record {
/** @type {import("@mail/core/common/store_service").Store} */
_store;
constructor(store, data) {
super();
Object.assign(this, {
id: data.id,
_store: store,
});
}
get message() {
return this._store.Message.records[this.messageId];
}
@@ -6,11 +6,6 @@ export class ScrollPosition {
/** @type {number|undefined} */
left;
constructor(top, left) {
this.top = top;
this.left = left;
}
clear() {
this.top = this.left = undefined;
}
@@ -41,7 +41,16 @@ export class Thread extends Record {
this.env.services["mail.thread"].update(thread, data);
return thread;
}
const thread = new Thread(this.store, data);
let thread = new Thread();
Object.assign(thread, {
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;
@@ -57,7 +66,7 @@ export class Thread extends Record {
this.env.services["mail.thread"].update(thread, data);
this.store.Composer.insert({ thread });
// return reactive version.
return this.records[thread.localId];
return thread;
}
/** @type {number} */
@@ -173,18 +182,6 @@ export class Thread extends Record {
/** @type {Boolean} */
is_editable;
constructor(store, data) {
super();
Object.assign(this, {
id: data.id,
model: data.model,
type: data.type,
_store: store,
});
store.Thread.records[this.localId] = this;
return store.Thread.records[this.localId];
}
get accessRestrictedToGroupText() {
if (!this.authorizedGroupFullName) {
return false;
@@ -1,6 +1,5 @@
/* @odoo-module */
import { Composer } from "@mail/core/common/composer_model";
import { loadEmoji } from "@web/core/emoji_picker/emoji_picker";
import { DEFAULT_AVATAR } from "@mail/core/common/persona_service";
import {
@@ -44,7 +44,17 @@ export class Activity extends Record {
* @returns {Activity}
*/
static insert(data, { broadcast = true } = {}) {
const activity = this.records[data.id] ?? new Activity(this.store, data.id);
let activity = this.records[data.id];
if (!activity) {
activity = new Activity();
Object.assign(activity, {
id: data.id,
_store: this.store,
});
this.store.Activity.records[data.id] = activity;
// return reactive
activity = this.store.Activity.records[data.id];
}
if (data.request_partner_id) {
data.request_partner_id = data.request_partner_id[0];
}
@@ -114,21 +124,6 @@ export class Activity extends Record {
write_uid;
/** @type {import("@mail/core/common/store_service").Store} */
_store;
/**
* @param {import("@mail/core/common/store_service").Store} store
* @param {number} id
* @returns {Activity}
*/
constructor(store, id) {
super();
Object.assign(this, {
id,
_store: store,
});
store.Activity.records[id] = this;
return store.Activity.records[id];
}
}
modelRegistry.add(Activity.name, Activity);