From 87f605aa902ddece916abf341b4520f257530a1c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexandre=20K=C3=BChn?= Date: Fri, 22 Sep 2023 19:18:40 +0200 Subject: [PATCH] [REF] mail: turn 'store.ringingThreads' into relational field Part-of: odoo/odoo#136308 --- .../static/src/core/common/store_service.js | 1 - .../discuss/call/common/@types/models.d.ts | 3 ++ .../discuss/call/common/call_invitations.xml | 7 ++--- .../call/common/discuss_app_model_patch.js | 28 +++++++++++++++++++ .../src/discuss/call/common/rtc_service.js | 11 -------- .../discuss/call/common/thread_model_patch.js | 18 ++++++------ addons/mail/static/src/utils/common/misc.js | 23 +++++++++------ 7 files changed, 56 insertions(+), 35 deletions(-) create mode 100644 addons/mail/static/src/discuss/call/common/discuss_app_model_patch.js diff --git a/addons/mail/static/src/core/common/store_service.js b/addons/mail/static/src/core/common/store_service.js index 1f544c4701a..2e4f10be614 100644 --- a/addons/mail/static/src/core/common/store_service.js +++ b/addons/mail/static/src/core/common/store_service.js @@ -134,7 +134,6 @@ export class Store { users = {}; internalUserGroupId = null; registeredImStatusPartners = null; - ringingThreads = null; hasLinkPreviewFeature = true; diff --git a/addons/mail/static/src/discuss/call/common/@types/models.d.ts b/addons/mail/static/src/discuss/call/common/@types/models.d.ts index 7a56ab50f0c..2274f118837 100644 --- a/addons/mail/static/src/discuss/call/common/@types/models.d.ts +++ b/addons/mail/static/src/discuss/call/common/@types/models.d.ts @@ -1,6 +1,9 @@ declare module "models" { import { RtcSession as RtcSessionClass } from "@mail/discuss/call/common/rtc_session_model"; + export interface DiscussApp { + ringingThreads: Thread[], + } export interface RtcSession extends RtcSessionClass {} export interface Models { diff --git a/addons/mail/static/src/discuss/call/common/call_invitations.xml b/addons/mail/static/src/discuss/call/common/call_invitations.xml index 6eac7cf3c3f..7f81eaa7d6c 100644 --- a/addons/mail/static/src/discuss/call/common/call_invitations.xml +++ b/addons/mail/static/src/discuss/call/common/call_invitations.xml @@ -3,11 +3,8 @@
- - - - - + +
diff --git a/addons/mail/static/src/discuss/call/common/discuss_app_model_patch.js b/addons/mail/static/src/discuss/call/common/discuss_app_model_patch.js new file mode 100644 index 00000000000..03cd6b5ecd6 --- /dev/null +++ b/addons/mail/static/src/discuss/call/common/discuss_app_model_patch.js @@ -0,0 +1,28 @@ +/* @odoo-module */ + +import { DiscussApp } from "@mail/core/common/discuss_app_model"; +import { Record } from "@mail/core/common/record"; +import { onChange } from "@mail/utils/common/misc"; + +import { patch } from "@web/core/utils/patch"; + +patch(DiscussApp, { + new(data) { + const app = super.new(data); + onChange(app, "ringingThreads", () => { + if (app.ringingThreads.length > 0) { + this.env.services["mail.sound_effects"].play("incoming-call", { loop: true }); + } else { + this.env.services["mail.sound_effects"].stop("incoming-call"); + } + }); + return app; + }, +}); + +patch(DiscussApp.prototype, { + setup() { + super.setup(); + this.ringingThreads = Record.many("Thread"); + }, +}); 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 94a69ac1a02..479190b1c75 100644 --- a/addons/mail/static/src/discuss/call/common/rtc_service.js +++ b/addons/mail/static/src/discuss/call/common/rtc_service.js @@ -135,9 +135,6 @@ export class Rtc { sourceScreenStream: null, }); this.blurManager = undefined; - this.ringingThreads = reactive([], () => this.onRingingThreadsChange()); - void this.ringingThreads.length; - this.store.ringingThreads = this.ringingThreads; onChange(this.userSettingsService, "useBlur", () => { if (this.state.sendCamera) { this.toggleVideo("camera", true); @@ -283,14 +280,6 @@ export class Rtc { } } - onRingingThreadsChange() { - if (this.ringingThreads.length > 0) { - this.soundEffectsService.play("incoming-call", { loop: true }); - } else { - this.soundEffectsService.stop("incoming-call"); - } - } - async deafen() { await this.setDeaf(true); this.soundEffectsService.play("deafen"); 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 511808123c1..3a5ec6a4db0 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 @@ -1,7 +1,6 @@ /* @odoo-module */ import { Thread } from "@mail/core/common/thread_model"; -import { removeFromArray } from "@mail/utils/common/arrays"; import { patch } from "@web/core/utils/patch"; @@ -13,8 +12,8 @@ patch(Thread.prototype, { if (session.notIn(this.rtcSessions)) { this.rtcSessions.push(session); } - if (!this._store.ringingThreads.includes(this.localId)) { - this._store.ringingThreads.push(this.localId); + if (this.notIn(this._store.discuss.ringingThreads)) { + this._store.discuss.ringingThreads.push(this); } } let rtcContinue = true; @@ -22,15 +21,16 @@ patch(Thread.prototype, { if (Array.isArray(data.rtcInvitingSession)) { if (data.rtcInvitingSession[0][0] === "unlink") { this.rtcInvitingSession = undefined; - removeFromArray(this._store.ringingThreads, this.localId); + this._store.discuss.ringingThreads.delete(this); } 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); } - const session = this._store.RtcSession.insert(data.rtcInvitingSession); - if (session.notIn(this.rtcSessions)) { - this.rtcSessions.push(session); - } - this._store.ringingThreads.push(this.localId); } if (rtcContinue && "rtcSessions" in data) { for (const command of data.rtcSessions) { diff --git a/addons/mail/static/src/utils/common/misc.js b/addons/mail/static/src/utils/common/misc.js index f536bc0b39a..c1a5247f625 100644 --- a/addons/mail/static/src/utils/common/misc.js +++ b/addons/mail/static/src/utils/common/misc.js @@ -41,23 +41,28 @@ export function isDragSourceExternalFile(dataTransfer) { * @param {Function} callback */ export function onChange(target, key, callback) { + let proxy; + function _observe() { + void proxy[key]; + if (proxy[key] instanceof Object) { + void Object.keys(proxy[key]); + } + if (proxy[key] instanceof Array) { + void proxy[key].length; + void proxy[key].forEach((i) => i); + } + } if (Array.isArray(key)) { for (const k of key) { onChange(target, k, callback); } return; } - const proxy = reactive(target, () => { - void proxy[key]; - if (proxy[key] instanceof Object) { - void Object.keys(proxy[key]); - } + proxy = reactive(target, () => { + _observe(); callback(); }); - void proxy[key]; - if (proxy[key] instanceof Object) { - void Object.keys(proxy[key]); - } + _observe(); return proxy; }