]> git.ipfire.org Git - pbs.git/commitdiff
users: Send an extended welcome message
authorMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 May 2023 17:09:45 +0000 (17:09 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Fri, 19 May 2023 17:09:45 +0000 (17:09 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/buildservice/users.py
src/static/js/notification-worker.js

index 1976a3851f0267d5d20d01c54682bae2d4fed69a..e0abac5d6038ef2fdf8e5236afd768a93748662b 100644 (file)
@@ -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"
index b79b222fdd924739d89fa641b8ed8b746224c712..1610b478eed4642e80a0fd4a36a69b8e33481dfc 100644 (file)
@@ -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);
 });