From: Michael Tremer Date: Fri, 19 May 2023 17:09:45 +0000 (+0000) Subject: users: Send an extended welcome message X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=88dac623ab6f72560d6d04f098676924c3d00df5;p=pbs.git users: Send an extended welcome message Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/users.py b/src/buildservice/users.py index 1976a385..e0abac5d 100644 --- a/src/buildservice/users.py +++ b/src/buildservice/users.py @@ -791,6 +791,8 @@ class User(base.DataObject): """ Creates a new subscription for this user """ + _ = self.locale.translate + # Decode p256dh if not isinstance(p256dh, bytes): p256dh = base64.urlsafe_b64decode(p256dh + "==") @@ -818,18 +820,45 @@ class User(base.DataObject): ) # Send a message - await subscription.send("Hello World!") + await subscription.send( + self._make_message( + _("Hello, %s!") % self, + _("You have successfully subscribed to push notifications."), + ), + ) return subscription - async def send_push_message(self, message): + async def send_push_message(self, title, body, **kwargs): """ Sends a message to all active subscriptions """ + # Return False if the user has no subscriptions + if not self.subscriptions: + return False + + # Format the message + message = self._make_message(title, body, **kwargs) + + # Send the message to all subscriptions async with asyncio.TaskGroup() as tg: for subscription in self.subscriptions: tg.create_task(subscription.send(message)) + return True + + def _make_message(self, title, body, **kwargs): + """ + Formats a push message with the given attributes and some useful defaults + """ + # Format the message as a dictionary + message = { + "title" : title, + "body" : body, + } | kwargs + + return message + class UserPushSubscription(base.DataObject): table = "user_push_subscriptions" diff --git a/src/static/js/notification-worker.js b/src/static/js/notification-worker.js index b79b222f..1610b478 100644 --- a/src/static/js/notification-worker.js +++ b/src/static/js/notification-worker.js @@ -13,14 +13,11 @@ self.addEventListener("push", function(event) { // Log what we have received console.debug("Push notification has been received: " + data); + // Extract the title const title = data.title || event.data.text(); - const options = { - "body" : data.message, - }; - // Show the notification - const notification = self.registration.showNotification(title, options); + const notification = self.registration.showNotification(title, data); event.waitUntil(notification); });