From 6abb65e98c9562b224bb244e707ade4a48211f47 Mon Sep 17 00:00:00 2001 From: BT-cjimeno Date: Wed, 23 Oct 2024 12:03:48 +0200 Subject: [PATCH] [IMP] web_notify: when closing a notification it gets closed everywhere --- web_notify/README.rst | 1 + web_notify/models/res_users.py | 21 +++++++++++++++++++ web_notify/readme/CONTRIBUTORS.rst | 1 + web_notify/static/description/index.html | 1 + .../js/services/notification_services.esm.js | 19 +++++++++++++++-- web_notify/tests/test_res_users.py | 21 +++++++++++++++++++ 6 files changed, 62 insertions(+), 2 deletions(-) diff --git a/web_notify/README.rst b/web_notify/README.rst index ddea206f0..8701627d2 100644 --- a/web_notify/README.rst +++ b/web_notify/README.rst @@ -143,6 +143,7 @@ Contributors * Aitor Bouzas * Shepilov Vladislav * Kevin Khao +* Carlos Jimeno * `Tecnativa `_: * David Vidal diff --git a/web_notify/models/res_users.py b/web_notify/models/res_users.py index 11339fd6f..b5ab68d6b 100644 --- a/web_notify/models/res_users.py +++ b/web_notify/models/res_users.py @@ -1,5 +1,7 @@ # Copyright 2016 ACSONE SA/NV # License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl). +import uuid + from odoo import _, api, exceptions, fields, models from odoo.addons.bus.models.bus import channel_with_db, json_dump @@ -122,7 +124,11 @@ class ResUsers(models.Model): target = self.partner_id if action: action = clean_action(action, self.env) + + unique_id = str(uuid.uuid4()) + bus_message = { + "id": unique_id, "type": type_message, "message": message, "title": title, @@ -133,3 +139,18 @@ class ResUsers(models.Model): notifications = [[partner, "web.notify", [bus_message]] for partner in target] self.env["bus.bus"]._sendmany(notifications) + + @api.model + def notify_dismiss(self, notif_id): + partner_id = self.env.user.partner_id.id + bus_message = { + "id": notif_id, + } + notifications = [ + [ + (self.env.cr.dbname, "res.partner", partner_id), + "web.notify.dismiss", + [bus_message], + ] + ] + self.env["bus.bus"]._sendmany(notifications) diff --git a/web_notify/readme/CONTRIBUTORS.rst b/web_notify/readme/CONTRIBUTORS.rst index 77bb03c5e..4d2672e89 100644 --- a/web_notify/readme/CONTRIBUTORS.rst +++ b/web_notify/readme/CONTRIBUTORS.rst @@ -3,6 +3,7 @@ * Aitor Bouzas * Shepilov Vladislav * Kevin Khao +* Carlos Jimeno * `Tecnativa `_: * David Vidal diff --git a/web_notify/static/description/index.html b/web_notify/static/description/index.html index 201ace0e5..27417615f 100644 --- a/web_notify/static/description/index.html +++ b/web_notify/static/description/index.html @@ -473,6 +473,7 @@ If you spotted it first, help us to smash it by providing a detailed and welcome
  • Aitor Bouzas <aitor.bouzas@adaptivecity.com>
  • Shepilov Vladislav <shepilov.v@protonmail.com>
  • Kevin Khao <kevin.khao@akretion.com>
  • +
  • Carlos Jimeno <carlos.jimeno@braintec.com>
  • Tecnativa:
    • David Vidal
    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 491a72f65..c682603a9 100644 --- a/web_notify/static/src/js/services/notification_services.esm.js +++ b/web_notify/static/src/js/services/notification_services.esm.js @@ -4,10 +4,11 @@ import {browser} from "@web/core/browser/browser"; import {registry} from "@web/core/registry"; export const webNotificationService = { - dependencies: ["bus_service", "notification", "action"], + dependencies: ["bus_service", "notification", "action", "orm"], - start(env, {bus_service, notification, action}) { + start(env, {bus_service, notification, action, orm}) { let webNotifTimeouts = {}; + const displayedNotifications = {}; /** * Displays the web notification on user's screen * @param {*} notifications @@ -44,10 +45,17 @@ export const webNotificationService = { button.onClick = async () => { await onClick(); notificationRemove(); + await orm.call("res.users", "notify_dismiss", [ + notif.id, + ]); }; return button; }), + onClose: async () => { + await orm.call("res.users", "notify_dismiss", [notif.id]); + }, }); + displayedNotifications[notif.id] = notificationRemove; }); }); } @@ -56,9 +64,16 @@ export const webNotificationService = { for (const {payload, type} of notifications) { if (type === "web.notify") { displaywebNotification(payload); + } else if (type === "web.notify.dismiss") { + const notifId = payload[0].id; + if (displayedNotifications[notifId]) { + displayedNotifications[notifId](); + delete displayedNotifications[notifId]; + } } } }); + bus_service.start(); }, }; diff --git a/web_notify/tests/test_res_users.py b/web_notify/tests/test_res_users.py index f05e6709a..6334e61f5 100644 --- a/web_notify/tests/test_res_users.py +++ b/web_notify/tests/test_res_users.py @@ -26,6 +26,8 @@ class TestResUsers(common.TransactionCase): self.assertEqual(1, len(news)) test_msg.update({"type": SUCCESS}) payload = json.loads(news.message)["payload"][0] + self.assertIn("id", payload) + payload.pop("id", None) self.assertDictEqual(test_msg, payload) def test_notify_danger(self): @@ -44,6 +46,8 @@ class TestResUsers(common.TransactionCase): self.assertEqual(1, len(news)) test_msg.update({"type": DANGER}) payload = json.loads(news.message)["payload"][0] + self.assertIn("id", payload) + payload.pop("id", None) self.assertDictEqual(test_msg, payload) def test_notify_warning(self): @@ -62,6 +66,8 @@ class TestResUsers(common.TransactionCase): self.assertEqual(1, len(news)) test_msg.update({"type": WARNING}) payload = json.loads(news.message)["payload"][0] + self.assertIn("id", payload) + payload.pop("id", None) self.assertDictEqual(test_msg, payload) def test_notify_info(self): @@ -80,6 +86,8 @@ class TestResUsers(common.TransactionCase): self.assertEqual(1, len(news)) test_msg.update({"type": INFO}) payload = json.loads(news.message)["payload"][0] + self.assertIn("id", payload) + payload.pop("id", None) self.assertDictEqual(test_msg, payload) def test_notify_default(self): @@ -98,6 +106,8 @@ class TestResUsers(common.TransactionCase): self.assertEqual(1, len(news)) test_msg.update({"type": DEFAULT}) payload = json.loads(news.message)["payload"][0] + self.assertIn("id", payload) + payload.pop("id", None) self.assertDictEqual(test_msg, payload) def test_notify_many(self): @@ -117,3 +127,14 @@ class TestResUsers(common.TransactionCase): def test_notify_admin_allowed_other_user(self): other_user = self.env.ref("base.user_demo") other_user.notify_info(message="hello") + + def test_notify_dismiss(self): + bus_bus = self.env["bus.bus"] + domain = [("channel", "=", self.env.user.notify_default_channel_name)] + existing = bus_bus.search(domain) + notif_id = "test-notif-id" + self.env.user.notify_dismiss(notif_id) + news = bus_bus.search(domain) - existing + self.assertEqual(1, len(news)) + payload = json.loads(news.message)["payload"][0] + self.assertEqual(payload["id"], notif_id)