Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[16.0][IMP] web_notify: close everywhere #2971

Open
wants to merge 1 commit into
base: 16.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions web_notify/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ Contributors
* Aitor Bouzas <[email protected]>
* Shepilov Vladislav <[email protected]>
* Kevin Khao <[email protected]>
* Carlos Jimeno <[email protected]>
* `Tecnativa <https://www.tecnativa.com>`_:

* David Vidal
Expand Down
21 changes: 21 additions & 0 deletions web_notify/models/res_users.py
Original file line number Diff line number Diff line change
@@ -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
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,18 @@ 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
1 change: 1 addition & 0 deletions web_notify/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Contributors</a></h2>
<li>Aitor Bouzas &lt;<a class="reference external" href="mailto:aitor.bouzas&#64;adaptivecity.com">aitor.bouzas&#64;adaptivecity.com</a>&gt;</li>
<li>Shepilov Vladislav &lt;<a class="reference external" href="mailto:shepilov.v&#64;protonmail.com">shepilov.v&#64;protonmail.com</a>&gt;</li>
<li>Kevin Khao &lt;<a class="reference external" href="mailto:kevin.khao&#64;akretion.com">kevin.khao&#64;akretion.com</a>&gt;</li>
<li>Carlos Jimeno &lt;<a class="reference external" href="mailto:carlos.jimeno&#64;braintec.com">carlos.jimeno&#64;braintec.com</a>&gt;</li>
<li><a class="reference external" href="https://www.tecnativa.com">Tecnativa</a>:<ul>
<li>David Vidal</li>
</ul>
Expand Down
19 changes: 17 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 = {};
const displayedNotifications = {};
/**
* Displays the web notification on user's screen
* @param {*} notifications
Expand Down Expand Up @@ -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;
});
});
}
Expand All @@ -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();
},
};
Expand Down
21 changes: 21 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,14 @@ 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)
Loading