From 6dbd5918c4b406f5b44bbb11ad81c58bebee2015 Mon Sep 17 00:00:00 2001 From: Igor Lobanov Date: Fri, 13 Oct 2023 17:15:14 +0200 Subject: [PATCH 1/3] notifications fixes --- resolvers/notifications.py | 7 +++- schema.graphql | 2 +- .../notifications/notification_service.py | 41 +++++++++++-------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/resolvers/notifications.py b/resolvers/notifications.py index e11277c6..0cfc2244 100644 --- a/resolvers/notifications.py +++ b/resolvers/notifications.py @@ -23,6 +23,7 @@ async def load_notifications(_, info, params=None): Notification.user == user_id ).order_by(desc(Notification.createdAt)).limit(limit).offset(offset) + notifications = [] with local_session() as session: total_count = session.query(Notification).where( Notification.user == user_id @@ -31,11 +32,13 @@ async def load_notifications(_, info, params=None): total_unread_count = session.query(Notification).where( and_( Notification.user == user_id, - Notification.seen is False + Notification.seen == False ) ).count() - notifications = session.execute(q).fetchall() + for [notification] in session.execute(q): + notification.type = notification.type.name + notifications.append(notification) return { "notifications": notifications, diff --git a/schema.graphql b/schema.graphql index 57147169..2a3af2d7 100644 --- a/schema.graphql +++ b/schema.graphql @@ -516,7 +516,7 @@ type Notification { id: Int! shout: Int reaction: Int - type: NotificationType + type: NotificationType! createdAt: DateTime! seen: Boolean! data: String # JSON diff --git a/services/notifications/notification_service.py b/services/notifications/notification_service.py index bfaf8e79..ad6c87e0 100644 --- a/services/notifications/notification_service.py +++ b/services/notifications/notification_service.py @@ -11,16 +11,29 @@ from orm.reaction import ReactionKind from services.notifications.sse import connection_manager +def shout_to_shout_data(shout): + return { + "title": shout.title, + "slug": shout.slug + } + + +def user_to_user_data(user): + return { + "id": user.id, + "name": user.name, + "slug": user.slug, + "userpic": user.userpic + } + + def update_prev_notification(notification, user): notification_data = json.loads(notification.data) notification_data["users"] = [ user for user in notification_data["users"] if user['id'] != user.id ] - notification_data["users"].append({ - "id": user.id, - "name": user.name - }) + notification_data["users"].append(user_to_user_data(user)) notification.data = json.dumps(notification_data, ensure_ascii=False) notification.seen = False @@ -57,17 +70,13 @@ class NewReactionNotificator: update_prev_notification(prev_new_reply_notification, user) else: reply_notification_data = json.dumps({ - "shout": { - "title": shout.title - }, - "users": [ - {"id": user.id, "name": user.name} - ] + "shout": shout_to_shout_data(shout), + "users": [user_to_user_data(user)] }, ensure_ascii=False) reply_notification = Notification.create(**{ "user": parent_reaction.createdBy, - "type": NotificationType.NEW_REPLY.name, + "type": NotificationType.NEW_REPLY, "shout": shout.id, "reaction": parent_reaction.id, "data": reply_notification_data @@ -92,17 +101,13 @@ class NewReactionNotificator: update_prev_notification(prev_new_comment_notification, user) else: notification_data_string = json.dumps({ - "shout": { - "title": shout.title - }, - "users": [ - {"id": user.id, "name": user.name} - ] + "shout": shout_to_shout_data(shout), + "users": [user_to_user_data(user)] }, ensure_ascii=False) author_notification = Notification.create(**{ "user": shout.createdBy, - "type": NotificationType.NEW_COMMENT.name, + "type": NotificationType.NEW_COMMENT, "shout": shout.id, "data": notification_data_string }) From 7a3da99e65d46c976d1194f3e2ac42586544781d Mon Sep 17 00:00:00 2001 From: Igor Lobanov Date: Sat, 14 Oct 2023 09:11:47 +0200 Subject: [PATCH 2/3] notifications system fix --- services/notifications/notification_service.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/services/notifications/notification_service.py b/services/notifications/notification_service.py index ad6c87e0..fcc77176 100644 --- a/services/notifications/notification_service.py +++ b/services/notifications/notification_service.py @@ -30,9 +30,7 @@ def user_to_user_data(user): def update_prev_notification(notification, user): notification_data = json.loads(notification.data) - notification_data["users"] = [ - user for user in notification_data["users"] if user['id'] != user.id - ] + notification_data["users"] = [u for u in notification_data["users"] if u['id'] != user.id] notification_data["users"].append(user_to_user_data(user)) notification.data = json.dumps(notification_data, ensure_ascii=False) From aa7ce6bc6f6a3b108093f98e962fe53784c42fe4 Mon Sep 17 00:00:00 2001 From: Ilya Y <75578537+ilya-bkv@users.noreply.github.com> Date: Mon, 16 Oct 2023 11:16:01 +0300 Subject: [PATCH 3/3] notifications logic update (#93) Co-authored-by: Igor Lobanov --- main.py | 4 +--- .../notifications/notification_service.py | 19 ++++++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/main.py b/main.py index fcd159f8..6bb17a86 100644 --- a/main.py +++ b/main.py @@ -77,15 +77,13 @@ routes = [ ] app = Starlette( - debug=True, on_startup=[start_up], on_shutdown=[shutdown], middleware=middleware, routes=routes, ) app.mount("/", GraphQL( - schema, - debug=True + schema )) dev_app = Starlette( diff --git a/services/notifications/notification_service.py b/services/notifications/notification_service.py index fcc77176..f7078b95 100644 --- a/services/notifications/notification_service.py +++ b/services/notifications/notification_service.py @@ -27,11 +27,12 @@ def user_to_user_data(user): } -def update_prev_notification(notification, user): +def update_prev_notification(notification, user, reaction): notification_data = json.loads(notification.data) notification_data["users"] = [u for u in notification_data["users"] if u['id'] != user.id] notification_data["users"].append(user_to_user_data(user)) + notification_data["reactionIds"].append(reaction.id) notification.data = json.dumps(notification_data, ensure_ascii=False) notification.seen = False @@ -60,16 +61,18 @@ class NewReactionNotificator: Notification.user == shout.createdBy, Notification.type == NotificationType.NEW_REPLY, Notification.shout == shout.id, - Notification.reaction == parent_reaction.id + Notification.reaction == parent_reaction.id, + Notification.seen == False ) ).first() if prev_new_reply_notification: - update_prev_notification(prev_new_reply_notification, user) + update_prev_notification(prev_new_reply_notification, user, reaction) else: reply_notification_data = json.dumps({ "shout": shout_to_shout_data(shout), - "users": [user_to_user_data(user)] + "users": [user_to_user_data(user)], + "reactionIds": [reaction.id] }, ensure_ascii=False) reply_notification = Notification.create(**{ @@ -91,16 +94,18 @@ class NewReactionNotificator: and_( Notification.user == shout.createdBy, Notification.type == NotificationType.NEW_COMMENT, - Notification.shout == shout.id + Notification.shout == shout.id, + Notification.seen == False ) ).first() if prev_new_comment_notification: - update_prev_notification(prev_new_comment_notification, user) + update_prev_notification(prev_new_comment_notification, user, reaction) else: notification_data_string = json.dumps({ "shout": shout_to_shout_data(shout), - "users": [user_to_user_data(user)] + "users": [user_to_user_data(user)], + "reactionIds": [reaction.id] }, ensure_ascii=False) author_notification = Notification.create(**{