From 51616198d914287700b40d7de9fc38a1b1866c44 Mon Sep 17 00:00:00 2001 From: trisdoan Date: Mon, 7 Oct 2024 15:19:30 +0700 Subject: [PATCH] [MIG] web_notify: Migration to 18.0 --- web_notify/README.rst | 6 ++ web_notify/__manifest__.py | 2 +- web_notify/models/res_users.py | 8 +- web_notify/readme/CREDITS.md | 1 + web_notify/static/description/index.html | 10 ++- .../src/js/services/notification.esm.js | 1 - .../js/services/notification_services.esm.js | 88 ++++++++----------- web_notify/tests/test_res_users.py | 10 +-- 8 files changed, 62 insertions(+), 64 deletions(-) create mode 100644 web_notify/readme/CREDITS.md diff --git a/web_notify/README.rst b/web_notify/README.rst index 08540719b507..4714f2af868d 100644 --- a/web_notify/README.rst +++ b/web_notify/README.rst @@ -154,6 +154,12 @@ Contributors - Nikul Chaudhary - Tris Doan +Other credits +------------- + +The migration of this module from 17.0 to 18.0 was financially supported +by Camptocamp. + Maintainers ----------- diff --git a/web_notify/__manifest__.py b/web_notify/__manifest__.py index 0275191f28d2..18e0bc8c72c6 100644 --- a/web_notify/__manifest__.py +++ b/web_notify/__manifest__.py @@ -6,7 +6,7 @@ "name": "Web Notify", "summary": """ Send notification messages to user""", - "version": "17.0.1.0.0", + "version": "18.0.1.0.0", "license": "AGPL-3", "author": "ACSONE SA/NV," "AdaptiveCity," "Odoo Community Association (OCA)", "development_status": "Production/Stable", diff --git a/web_notify/models/res_users.py b/web_notify/models/res_users.py index 11339fd6f611..e47a60e7d227 100644 --- a/web_notify/models/res_users.py +++ b/web_notify/models/res_users.py @@ -130,6 +130,8 @@ def _notify_channel( "action": action, "params": dict(params or []), } - - notifications = [[partner, "web.notify", [bus_message]] for partner in target] - self.env["bus.bus"]._sendmany(notifications) + for partner in target: + partner._bus_send( + "web_notify", + bus_message, + ) diff --git a/web_notify/readme/CREDITS.md b/web_notify/readme/CREDITS.md new file mode 100644 index 000000000000..83b3ec91f7d5 --- /dev/null +++ b/web_notify/readme/CREDITS.md @@ -0,0 +1 @@ +The migration of this module from 17.0 to 18.0 was financially supported by Camptocamp. diff --git a/web_notify/static/description/index.html b/web_notify/static/description/index.html index 978376d589da..c530b37ab365 100644 --- a/web_notify/static/description/index.html +++ b/web_notify/static/description/index.html @@ -390,7 +390,8 @@

Web Notify

  • Credits
  • @@ -483,8 +484,13 @@

    Contributors

  • Tris Doan <tridm@trobz.com>
  • +
    +

    Other credits

    +

    The migration of this module from 17.0 to 18.0 was financially supported +by Camptocamp.

    +
    -

    Maintainers

    +

    Maintainers

    This module is maintained by the OCA.

    Odoo Community Association diff --git a/web_notify/static/src/js/services/notification.esm.js b/web_notify/static/src/js/services/notification.esm.js index 7f8c281367d2..eebc3c1260de 100644 --- a/web_notify/static/src/js/services/notification.esm.js +++ b/web_notify/static/src/js/services/notification.esm.js @@ -1,4 +1,3 @@ -/** @odoo-module */ import {Notification} from "@web/core/notifications/notification"; import {patch} from "@web/core/utils/patch"; diff --git a/web_notify/static/src/js/services/notification_services.esm.js b/web_notify/static/src/js/services/notification_services.esm.js index c37cc174f450..1ac2f71d69c1 100644 --- a/web_notify/static/src/js/services/notification_services.esm.js +++ b/web_notify/static/src/js/services/notification_services.esm.js @@ -1,64 +1,48 @@ -/** @odoo-module **/ - import {markup} from "@odoo/owl"; -import {browser} from "@web/core/browser/browser"; import {registry} from "@web/core/registry"; export const webNotificationService = { dependencies: ["bus_service", "notification", "action"], - start(env, {bus_service, notification, action}) { - let webNotifTimeouts = {}; - /** - * Displays the web notification on user's screen - * @param {*} notifications - */ - function displaywebNotification(notifications) { - Object.values(webNotifTimeouts).forEach((notif) => - browser.clearTimeout(notif) + start(env, {bus_service, notification: notificationService, action}) { + function displayWebNotification(notification) { + let buttons = []; + if (notification.action) { + const params = notification.action.context?.params || {}; + + buttons = [ + { + name: params.button_name || env._t("Open"), + primary: true, + onClick: async () => { + await action.doAction(notification.action); + }, + ...(params.button_icon && {icon: params.button_icon}), + }, + ]; + } + + const notificationRemove = notificationService.add( + markup(notification.message), + { + title: notification.title, + type: notification.type, + sticky: notification.sticky, + className: notification.className, + buttons: buttons.map((button) => { + const onClick = button.onClick; + button.onClick = async () => { + await onClick(); + notificationRemove(); + }; + return button; + }), + } ); - webNotifTimeouts = {}; - notifications.forEach((notif) => { - browser.setTimeout(() => { - var buttons = []; - if (notif.action) { - const params = - (notif.action.context && notif.action.context.params) || {}; - buttons = [ - { - name: params.button_name || env._t("Open"), - primary: true, - onClick: async () => { - await action.doAction(notif.action); - }, - ...(params.button_icon && {icon: params.button_icon}), - }, - ]; - } - const notificationRemove = notification.add(markup(notif.message), { - title: notif.title, - type: notif.type, - sticky: notif.sticky, - className: notif.className, - buttons: buttons.map((button) => { - const onClick = button.onClick; - button.onClick = async () => { - await onClick(); - notificationRemove(); - }; - return button; - }), - }); - }); - }); } - bus_service.addEventListener("notification", ({detail: notifications}) => { - for (const {payload, type} of notifications) { - if (type === "web.notify") { - displaywebNotification(payload); - } - } + bus_service.subscribe("web_notify", (payload) => { + displayWebNotification(payload); }); bus_service.start(); }, diff --git a/web_notify/tests/test_res_users.py b/web_notify/tests/test_res_users.py index 6cae1b256791..16f0659661e5 100644 --- a/web_notify/tests/test_res_users.py +++ b/web_notify/tests/test_res_users.py @@ -26,7 +26,7 @@ def test_notify_success(self): news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) test_msg.update({"type": SUCCESS}) - payload = json.loads(news.message)["payload"][0] + payload = json.loads(news.message)["payload"] self.assertDictEqual(test_msg, payload) def test_notify_danger(self): @@ -45,7 +45,7 @@ def test_notify_danger(self): news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) test_msg.update({"type": DANGER}) - payload = json.loads(news.message)["payload"][0] + payload = json.loads(news.message)["payload"] self.assertDictEqual(test_msg, payload) def test_notify_warning(self): @@ -64,7 +64,7 @@ def test_notify_warning(self): news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) test_msg.update({"type": WARNING}) - payload = json.loads(news.message)["payload"][0] + payload = json.loads(news.message)["payload"] self.assertDictEqual(test_msg, payload) def test_notify_info(self): @@ -83,7 +83,7 @@ def test_notify_info(self): news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) test_msg.update({"type": INFO}) - payload = json.loads(news.message)["payload"][0] + payload = json.loads(news.message)["payload"] self.assertDictEqual(test_msg, payload) def test_notify_default(self): @@ -102,7 +102,7 @@ def test_notify_default(self): news = bus_bus.search(domain) - existing self.assertEqual(1, len(news)) test_msg.update({"type": DEFAULT}) - payload = json.loads(news.message)["payload"][0] + payload = json.loads(news.message)["payload"] self.assertDictEqual(test_msg, payload) def test_notify_many(self):