Skip to content

Commit

Permalink
[IMP] web_notify: when closing a notification it gets closed everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
BT-cjimeno committed Oct 23, 2024
1 parent 7c2a4ea commit 70779bd
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
16 changes: 16 additions & 0 deletions web_notify/models/res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from odoo.addons.bus.models.bus import channel_with_db, json_dump
from odoo.addons.web.controllers.utils import clean_action

import uuid

DEFAULT_MESSAGE = "Default message"

SUCCESS = "success"
Expand Down Expand Up @@ -122,7 +124,11 @@ def _notify_channel(
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,
Expand All @@ -133,3 +139,13 @@ def _notify_channel(

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)

1 change: 1 addition & 0 deletions web_notify/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* Aitor Bouzas <[email protected]>
* Shepilov Vladislav <[email protected]>
* Kevin Khao <[email protected]>
* Carlos Jimeno <[email protected]>
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
18 changes: 16 additions & 2 deletions web_notify/static/src/js/services/notification_services.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {};
let displayedNotifications = {};
/**
* Displays the web notification on user's screen
* @param {*} notifications
Expand Down Expand Up @@ -44,10 +45,15 @@ 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;
});
});
}
Expand All @@ -56,11 +62,19 @@ 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();
},
};

registry.category("services").add("webNotification", webNotificationService);

22 changes: 22 additions & 0 deletions web_notify/tests/test_res_users.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ def test_notify_success(self):
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):
Expand All @@ -44,6 +46,8 @@ def test_notify_danger(self):
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):
Expand All @@ -62,6 +66,8 @@ def test_notify_warning(self):
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):
Expand All @@ -80,6 +86,8 @@ def test_notify_info(self):
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):
Expand All @@ -98,6 +106,8 @@ def test_notify_default(self):
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):
Expand All @@ -117,3 +127,15 @@ def test_notify_other_user(self):
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)

0 comments on commit 70779bd

Please sign in to comment.