diff --git a/addons/hr/static/src/models/employee.js b/addons/hr/static/src/models/employee.js index 8423338d69f..ed0a8c402bd 100644 --- a/addons/hr/static/src/models/employee.js +++ b/addons/hr/static/src/models/employee.js @@ -99,7 +99,7 @@ registerModel({ * * If a chat is not appropriate, a notification is displayed instead. * - * @returns {Thread|undefined} + * @returns {Channel|undefined} */ async getChat() { if (!this.user && !this.hasCheckedUser) { @@ -125,7 +125,6 @@ registerModel({ * If a chat is not appropriate, a notification is displayed instead. * * @param {Object} [options] forwarded to @see `Thread:open()` - * @returns {Thread|undefined} */ async openChat(options) { const chat = await this.getChat(); @@ -135,11 +134,10 @@ registerModel({ if (!chat) { return; } - await chat.open(options); + await chat.thread.open(options); if (!this.exists()) { return; } - return chat; }, /** * Opens the most appropriate view that is a profile for this employee. diff --git a/addons/mail/static/src/js/m2x_avatar_user.js b/addons/mail/static/src/js/m2x_avatar_user.js index 7eecc1045a4..500482240b0 100644 --- a/addons/mail/static/src/js/m2x_avatar_user.js +++ b/addons/mail/static/src/js/m2x_avatar_user.js @@ -42,11 +42,10 @@ const M2XAvatarMixin = { * * @private * @param {Object} params - * @returns {Promise} */ async _openChat(params) { const messaging = await Component.env.services.messaging.get(); - return messaging.openChat(params); + messaging.openChat(params); }, }; diff --git a/addons/mail/static/src/models/channel.js b/addons/mail/static/src/models/channel.js index 3c407c7a0c8..0e4096542de 100644 --- a/addons/mail/static/src/models/channel.js +++ b/addons/mail/static/src/models/channel.js @@ -6,6 +6,37 @@ import { clear } from '@mail/model/model_field_command'; registerModel({ name: 'Channel', + modelMethods: { + /** + * Performs the `channel_get` RPC on `mail.channel`. + * + * `openChat` is preferable in business code because it will avoid the + * RPC if the chat already exists. + * + * @param {Object} param0 + * @param {integer[]} param0.partnerIds + * @param {boolean} [param0.pinForCurrentPartner] + * @returns {Channel|undefined} the created or existing chat + */ + async performRpcCreateChat({ partnerIds, pinForCurrentPartner }) { + // TODO FIX: potential duplicate chat task-2276490 + const data = await this.messaging.rpc({ + model: 'mail.channel', + method: 'channel_get', + kwargs: { + partners_to: partnerIds, + pin: pinForCurrentPartner, + }, + }); + if (!data) { + return; + } + const { channel } = this.messaging.models['Thread'].insert( + this.messaging.models['Thread'].convertData(data) + ); + return channel; + }, + }, recordMethods: { async fetchChannelMembers() { const channelData = await this.messaging.rpc({ diff --git a/addons/mail/static/src/models/chat_window.js b/addons/mail/static/src/models/chat_window.js index 4f31d8ae7f8..db3e45acc3f 100644 --- a/addons/mail/static/src/models/chat_window.js +++ b/addons/mail/static/src/models/chat_window.js @@ -118,7 +118,7 @@ registerModel({ if (!chat) { return; } - this.messaging.chatWindowManager.openThread(chat, { + this.messaging.chatWindowManager.openThread(chat.thread, { makeActive: true, replaceNewMessage: true, }); diff --git a/addons/mail/static/src/models/messaging.js b/addons/mail/static/src/models/messaging.js index 8047354f869..2122ac7a157 100644 --- a/addons/mail/static/src/models/messaging.js +++ b/addons/mail/static/src/models/messaging.js @@ -47,7 +47,7 @@ registerModel({ * @param {integer} [param0.partnerId] * @param {integer} [param0.userId] * @param {Object} [options] - * @returns {Thread|undefined} + * @returns {Channel|undefined} */ async getChat({ partnerId, userId }) { if (userId) { @@ -86,18 +86,16 @@ registerModel({ * * @param {Object} person forwarded to @see `getChat()` * @param {Object} [options] forwarded to @see `Thread:open()` - * @returns {Thread|undefined} */ async openChat(person, options) { const chat = await this.getChat(person); if (!this.exists() || !chat) { return; } - await chat.open(options); + await chat.thread.open(options); if (!this.exists()) { return; } - return chat; }, /** * Opens the form view of the record with provided id and model. diff --git a/addons/mail/static/src/models/messaging_notification_handler.js b/addons/mail/static/src/models/messaging_notification_handler.js index ede8847a0b7..6222e9434b9 100644 --- a/addons/mail/static/src/models/messaging_notification_handler.js +++ b/addons/mail/static/src/models/messaging_notification_handler.js @@ -617,7 +617,7 @@ registerModel({ if (!this.exists() || !chat || this.messaging.device.isSmall) { return; } - this.messaging.chatWindowManager.openThread(chat); + this.messaging.chatWindowManager.openThread(chat.thread); }, /** * @private diff --git a/addons/mail/static/src/models/partner.js b/addons/mail/static/src/models/partner.js index 5a05b9aa874..f44637db065 100644 --- a/addons/mail/static/src/models/partner.js +++ b/addons/mail/static/src/models/partner.js @@ -283,7 +283,7 @@ registerModel({ * * If a chat is not appropriate, a notification is displayed instead. * - * @returns {Thread|undefined} + * @returns {Channel|undefined} */ async getChat() { if (!this.user && !this.hasCheckedUser) { @@ -309,18 +309,16 @@ registerModel({ * If a chat is not appropriate, a notification is displayed instead. * * @param {Object} [options] forwarded to @see `Thread:open()` - * @returns {Thread|undefined} */ async openChat(options) { const chat = await this.getChat(); if (!this.exists() || !chat) { return; } - await chat.open(options); + await chat.thread.open(options); if (!this.exists()) { return; } - return chat; }, /** * Opens the most appropriate view that is a profile for this partner. diff --git a/addons/mail/static/src/models/thread.js b/addons/mail/static/src/models/thread.js index 125b6d7b58e..45b4d2b7505 100644 --- a/addons/mail/static/src/models/thread.js +++ b/addons/mail/static/src/models/thread.js @@ -361,34 +361,6 @@ registerModel({ this.messaging.models['Thread'].convertData(data) ); }, - /** - * Performs the `channel_get` RPC on `mail.channel`. - * - * `openChat` is preferable in business code because it will avoid the - * RPC if the chat already exists. - * - * @param {Object} param0 - * @param {integer[]} param0.partnerIds - * @param {boolean} [param0.pinForCurrentPartner] - * @returns {Thread|undefined} the created or existing chat - */ - async performRpcCreateChat({ partnerIds, pinForCurrentPartner }) { - // TODO FIX: potential duplicate chat task-2276490 - const data = await this.messaging.rpc({ - model: 'mail.channel', - method: 'channel_get', - kwargs: { - partners_to: partnerIds, - pin: pinForCurrentPartner, - }, - }); - if (!data) { - return; - } - return this.messaging.models['Thread'].insert( - this.messaging.models['Thread'].convertData(data) - ); - }, /** * Search for thread matching `searchTerm`. * diff --git a/addons/mail/static/src/models/user.js b/addons/mail/static/src/models/user.js index d690e41023d..864ccf93b71 100644 --- a/addons/mail/static/src/models/user.js +++ b/addons/mail/static/src/models/user.js @@ -69,7 +69,7 @@ registerModel({ * * If a chat is not appropriate, a notification is displayed instead. * - * @returns {Thread|undefined} + * @returns {Channel|undefined} */ async getChat() { if (!this.partner) { @@ -90,11 +90,11 @@ registerModel({ return; } // in other cases a chat would be valid, find it or try to create it - let chat = this.partner.dmChatWithCurrentPartner && this.partner.dmChatWithCurrentPartner.thread; - if (!chat || !chat.isPinned) { + let chat = this.partner.dmChatWithCurrentPartner; + if (!chat || !chat.thread.isPinned) { // if chat is not pinned then it has to be pinned client-side // and server-side, which is a side effect of following rpc - chat = await this.messaging.models['Thread'].performRpcCreateChat({ + chat = await this.messaging.models['Channel'].performRpcCreateChat({ partnerIds: [this.partner.id], }); if (!this.exists()) { @@ -116,18 +116,16 @@ registerModel({ * If a chat is not appropriate, a notification is displayed instead. * * @param {Object} [options] forwarded to @see `Thread:open()` - * @returns {Thread|undefined} */ async openChat(options) { const chat = await this.getChat(); if (!this.exists() || !chat) { return; } - await chat.open(options); + await chat.thread.open(options); if (!this.exists() || !chat.exists()) { return; } - return chat; }, /** * Opens the most appropriate view that is a profile for this user.