From db5150913b9a1a64c698d1270f6d6514c55465e4 Mon Sep 17 00:00:00 2001 From: Nox Date: Thu, 7 Dec 2023 19:29:53 -0300 Subject: [PATCH] Add new_reply_notification endpoint --- .../services/push_notifications.py | 31 +++++++++++++++++++ .../web/api/notification/views.py | 17 ++++++++++ 2 files changed, 48 insertions(+) diff --git a/identity_socializer/services/push_notifications.py b/identity_socializer/services/push_notifications.py index 3921dae..6bb48e4 100644 --- a/identity_socializer/services/push_notifications.py +++ b/identity_socializer/services/push_notifications.py @@ -121,6 +121,37 @@ async def new_like( self.send(notification) + async def new_reply( + self, + from_id: str, + to_id: str, + snap: Any, + user_dao: UserDAO, + push_token_dao: PushTokenDAO, + ) -> None: + """Send push notification for new reply.""" + username = await user_dao.get_username_by_id(from_id) or "unknown" + + # Create and save notification to database + title = "Your snap have a new reply!" + body = f"@{username} replied your snap!" + notif_type = "NewCommentNotification" + + self.save_notification(to_id, title, body, notif_type, snap["id"]) + + # Send push notification to user + push_tokens = await push_token_dao.get_push_tokens_by_user(to_id) + + for push_token in push_tokens: + data = { + "screen": notif_type, + "params": {"snap": snap}, + } + + notification = _create_push_notification(push_token, title, body, data) + + self.send(notification) + async def new_follower( self, from_id: str, diff --git a/identity_socializer/web/api/notification/views.py b/identity_socializer/web/api/notification/views.py index 0bf6e2e..4dafd9c 100644 --- a/identity_socializer/web/api/notification/views.py +++ b/identity_socializer/web/api/notification/views.py @@ -74,6 +74,23 @@ async def new_like_notification( ) +@router.post("/new_reply", response_model=None) +async def new_reply_notification( + body: NotificationDTO, + user_dao: UserDAO = Depends(), + push_token_dao: PushTokenDAO = Depends(), + push_notifications: PushNotifications = Depends(), +) -> None: + """Creates a notification for new reply event.""" + await push_notifications.new_reply( + body.from_id, + body.to_id, + body.snap, + user_dao, + push_token_dao, + ) + + @router.post("/new_mention", response_model=None) async def new_mention_notification( body: NotificationDTO,