From 41445f2ae61e308a587e0d0b481492568791cf8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Theys?= Date: Wed, 18 Aug 2021 20:34:07 +0000 Subject: [PATCH] [IMP] mail, hr: access messaging without env in remaining places Part of task-2582313 --- addons/hr/static/src/js/chat_mixin.js | 6 ++--- .../chatter_container/chatter_container.js | 2 +- addons/mail/static/src/js/core/translation.js | 23 +++++++++++-------- addons/mail/static/src/js/m2x_avatar_user.js | 6 ++--- addons/mail/static/src/main.js | 5 ---- .../src/services/messaging/messaging.js | 10 ++++++++ .../new_message_service.js | 2 +- addons/mail/static/src/utils/test_utils.js | 15 ------------ .../src/webclient/commands/mail_providers.js | 4 ++-- .../static/src/widgets/discuss/discuss.js | 3 ++- addons/mail/static/tests/chatter_tests.js | 4 ++-- 11 files changed, 37 insertions(+), 43 deletions(-) diff --git a/addons/hr/static/src/js/chat_mixin.js b/addons/hr/static/src/js/chat_mixin.js index 040093b00a6..58523e85fc3 100644 --- a/addons/hr/static/src/js/chat_mixin.js +++ b/addons/hr/static/src/js/chat_mixin.js @@ -23,11 +23,11 @@ const ChatMixin = { return this._super(); }, - _onOpenChat: function (ev) { + async _onOpenChat(ev) { ev.preventDefault(); ev.stopImmediatePropagation(); - const env = Component.env; - env.messaging.openChat({ employeeId: this.state.data.id }); + const messaging = await Component.env.services.messaging.get(); + messaging.openChat({ employeeId: this.state.data.id }); return true; }, }; diff --git a/addons/mail/static/src/components/chatter_container/chatter_container.js b/addons/mail/static/src/components/chatter_container/chatter_container.js index baa2107a243..65fbd804107 100644 --- a/addons/mail/static/src/components/chatter_container/chatter_container.js +++ b/addons/mail/static/src/components/chatter_container/chatter_container.js @@ -50,7 +50,7 @@ export class ChatterContainer extends Component { * @private */ async _insertFromProps(props) { - const messaging = await this.env.services.messaging.modelManager.getMessaging(); + const messaging = await this.env.services.messaging.get(); if (this.__owl__.status === 5 /* destroyed */) { return; } diff --git a/addons/mail/static/src/js/core/translation.js b/addons/mail/static/src/js/core/translation.js index daff5e88329..b968c158a73 100644 --- a/addons/mail/static/src/js/core/translation.js +++ b/addons/mail/static/src/js/core/translation.js @@ -2,22 +2,25 @@ import { TranslationDataBase } from 'web.translation'; -const { Component } = owl; - TranslationDataBase.include({ /** * @override */ set_bundle() { const res = this._super(...arguments); - if (Component.env.messaging) { - // Update messaging locale whenever the translation bundle changes. - // In particular if messaging is created before the end of the - // `load_translations` RPC, the default values have to be - // updated by the received ones. - Component.env.messaging.locale.update({ - language: this.parameters.code, - textDirection: this.parameters.direction, + if (owl.Component.env.services && owl.Component.env.services.messaging) { + // During boot `env.services` might not even be set yet, in this + // case this can safely be ignored as messaging will then load + // locale information during its initialization. + owl.Component.env.services.messaging.get().then(messaging => { + // Update messaging locale whenever the translation bundle changes. + // In particular if messaging is created before the end of the + // `load_translations` RPC, the default values have to be + // updated by the received ones. + messaging.locale.update({ + language: this.parameters.code, + textDirection: this.parameters.direction, + }); }); } return res; diff --git a/addons/mail/static/src/js/m2x_avatar_user.js b/addons/mail/static/src/js/m2x_avatar_user.js index 419a7b650f8..f0a6f12731e 100644 --- a/addons/mail/static/src/js/m2x_avatar_user.js +++ b/addons/mail/static/src/js/m2x_avatar_user.js @@ -43,9 +43,9 @@ const M2XAvatarMixin = { * @param {Object} params * @returns {Promise} */ - _openChat(params) { - const env = Component.env; - return env.messaging.openChat(params); + async _openChat(params) { + const messaging = await Component.env.services.messaging.get(); + return messaging.openChat(params); }, }; diff --git a/addons/mail/static/src/main.js b/addons/mail/static/src/main.js index b31f06ebc31..74be18b1904 100644 --- a/addons/mail/static/src/main.js +++ b/addons/mail/static/src/main.js @@ -16,10 +16,5 @@ Object.assign(env, { ); }, }); -Object.defineProperty(env, 'messaging', { - get() { - return this.services.messaging.modelManager.messaging; - }, -}); serviceRegistry.add('messaging', MessagingService); diff --git a/addons/mail/static/src/services/messaging/messaging.js b/addons/mail/static/src/services/messaging/messaging.js index 4d916ff9503..bd42e88f645 100644 --- a/addons/mail/static/src/services/messaging/messaging.js +++ b/addons/mail/static/src/services/messaging/messaging.js @@ -21,4 +21,14 @@ export const MessagingService = AbstractService.extend({ this._super(); this.modelManager.start(this.messagingValues); }, + /** + * Returns the messaging record once it is initialized. This method should + * be considered the main entry point to the messaging system for outside + * code. + * + * @returns {mail.messaging} + **/ + async get() { + return this.modelManager.getMessaging(); + } }); diff --git a/addons/mail/static/src/services/new_message_service/new_message_service.js b/addons/mail/static/src/services/new_message_service/new_message_service.js index 0352f44aa16..88c548f702f 100644 --- a/addons/mail/static/src/services/new_message_service/new_message_service.js +++ b/addons/mail/static/src/services/new_message_service/new_message_service.js @@ -6,7 +6,7 @@ export const newMessageService = { dependencies: ["command"], start(newEnv, { command }) { command.add("Message a user", async () => { - const messaging = await owl.Component.env.services.messaging.modelManager.getMessaging(); + const messaging = await owl.Component.env.services.messaging.get(); if (messaging.discuss.isOpen) { messaging.discuss.update({ isAddingChannel: false, diff --git a/addons/mail/static/src/utils/test_utils.js b/addons/mail/static/src/utils/test_utils.js index 19f4ec2b531..54977ac8e35 100644 --- a/addons/mail/static/src/utils/test_utils.js +++ b/addons/mail/static/src/utils/test_utils.js @@ -542,21 +542,6 @@ async function start(param0 = {}) { loadingBaseDelayDuration, messagingBus, }, - /** - * Override to bind the getters on the actual env that is given, - * because the env in the scope of this test might only serve as - * template and not be the one actually used. - * - * @override - */ - init(env) { - Object.defineProperty(env, 'messaging', { - get() { - return this.services.messaging.modelManager.messaging; - }, - }); - this._super(env); - }, /** * Override: * - to ensure the test setup is complete before starting otherwise diff --git a/addons/mail/static/src/webclient/commands/mail_providers.js b/addons/mail/static/src/webclient/commands/mail_providers.js index bde9023d78c..c81aa8b9fd7 100644 --- a/addons/mail/static/src/webclient/commands/mail_providers.js +++ b/addons/mail/static/src/webclient/commands/mail_providers.js @@ -18,7 +18,7 @@ const commandProviderRegistry = registry.category("command_provider"); commandProviderRegistry.add("partner", { nameSpace: "@", async provide(newEnv, options) { - const messaging = await Component.env.services.messaging.modelManager.getMessaging(); + const messaging = await Component.env.services.messaging.get(); const suggestions = []; await messaging.models['mail.partner'].imSearch({ callback(partners) { @@ -45,7 +45,7 @@ commandProviderRegistry.add("partner", { commandProviderRegistry.add("channel", { nameSpace: "#", async provide(newEnv, options) { - const messaging = await Component.env.services.messaging.modelManager.getMessaging(); + const messaging = await Component.env.services.messaging.get(); const channels = await messaging.models['mail.thread'].searchChannelsToOpen({ limit: 10, searchTerm: options.searchValue, diff --git a/addons/mail/static/src/widgets/discuss/discuss.js b/addons/mail/static/src/widgets/discuss/discuss.js index c78b6960447..246929dcf94 100644 --- a/addons/mail/static/src/widgets/discuss/discuss.js +++ b/addons/mail/static/src/widgets/discuss/discuss.js @@ -33,11 +33,12 @@ const DiscussWidget = AbstractAction.extend({ this._lastPushStateActiveThread = null; this.env = Component.env; Component.env.services.messaging.modelManager.messagingCreatedPromise.then(() => { + const messaging = Component.env.services.messaging.modelManager.messaging; const initActiveId = this.options.active_id || (this.action.context && this.action.context.active_id) || (this.action.params && this.action.params.default_active_id) || 'mail.box_inbox'; - this.discuss = this.env.messaging.discuss; + this.discuss = messaging.discuss; this.discuss.update({ initActiveId }); }); }, diff --git a/addons/mail/static/tests/chatter_tests.js b/addons/mail/static/tests/chatter_tests.js index 67bd62cb0ea..cd5e117b0e7 100644 --- a/addons/mail/static/tests/chatter_tests.js +++ b/addons/mail/static/tests/chatter_tests.js @@ -252,7 +252,7 @@ QUnit.test('list activity widget: open dropdown', async function (assert) { } ); - const { env, widget: list } = await start({ + const { widget: list } = await start({ hasView: true, View: ListView, model: 'res.users', @@ -266,7 +266,7 @@ QUnit.test('list activity widget: open dropdown', async function (assert) { assert.step(args.method || route); if (args.method === 'action_feedback') { const currentUser = this.data['res.users'].records.find(user => - user.id === env.messaging.currentUser.id + user.id === this.currentUserId ); Object.assign(currentUser, { activity_ids: [4],