Merge pull request #91 from Discours/feature/notifications2

notifications fixes
This commit is contained in:
Kosta 2023-10-13 18:27:10 +03:00 committed by GitHub
commit f44e4d1146
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 29 additions and 21 deletions

View File

@ -23,6 +23,7 @@ async def load_notifications(_, info, params=None):
Notification.user == user_id Notification.user == user_id
).order_by(desc(Notification.createdAt)).limit(limit).offset(offset) ).order_by(desc(Notification.createdAt)).limit(limit).offset(offset)
notifications = []
with local_session() as session: with local_session() as session:
total_count = session.query(Notification).where( total_count = session.query(Notification).where(
Notification.user == user_id Notification.user == user_id
@ -31,11 +32,13 @@ async def load_notifications(_, info, params=None):
total_unread_count = session.query(Notification).where( total_unread_count = session.query(Notification).where(
and_( and_(
Notification.user == user_id, Notification.user == user_id,
Notification.seen is False Notification.seen == False
) )
).count() ).count()
notifications = session.execute(q).fetchall() for [notification] in session.execute(q):
notification.type = notification.type.name
notifications.append(notification)
return { return {
"notifications": notifications, "notifications": notifications,

View File

@ -516,7 +516,7 @@ type Notification {
id: Int! id: Int!
shout: Int shout: Int
reaction: Int reaction: Int
type: NotificationType type: NotificationType!
createdAt: DateTime! createdAt: DateTime!
seen: Boolean! seen: Boolean!
data: String # JSON data: String # JSON

View File

@ -11,16 +11,29 @@ from orm.reaction import ReactionKind
from services.notifications.sse import connection_manager 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): def update_prev_notification(notification, user):
notification_data = json.loads(notification.data) notification_data = json.loads(notification.data)
notification_data["users"] = [ notification_data["users"] = [
user for user in notification_data["users"] if user['id'] != user.id user for user in notification_data["users"] if user['id'] != user.id
] ]
notification_data["users"].append({ notification_data["users"].append(user_to_user_data(user))
"id": user.id,
"name": user.name
})
notification.data = json.dumps(notification_data, ensure_ascii=False) notification.data = json.dumps(notification_data, ensure_ascii=False)
notification.seen = False notification.seen = False
@ -57,17 +70,13 @@ class NewReactionNotificator:
update_prev_notification(prev_new_reply_notification, user) update_prev_notification(prev_new_reply_notification, user)
else: else:
reply_notification_data = json.dumps({ reply_notification_data = json.dumps({
"shout": { "shout": shout_to_shout_data(shout),
"title": shout.title "users": [user_to_user_data(user)]
},
"users": [
{"id": user.id, "name": user.name}
]
}, ensure_ascii=False) }, ensure_ascii=False)
reply_notification = Notification.create(**{ reply_notification = Notification.create(**{
"user": parent_reaction.createdBy, "user": parent_reaction.createdBy,
"type": NotificationType.NEW_REPLY.name, "type": NotificationType.NEW_REPLY,
"shout": shout.id, "shout": shout.id,
"reaction": parent_reaction.id, "reaction": parent_reaction.id,
"data": reply_notification_data "data": reply_notification_data
@ -92,17 +101,13 @@ class NewReactionNotificator:
update_prev_notification(prev_new_comment_notification, user) update_prev_notification(prev_new_comment_notification, user)
else: else:
notification_data_string = json.dumps({ notification_data_string = json.dumps({
"shout": { "shout": shout_to_shout_data(shout),
"title": shout.title "users": [user_to_user_data(user)]
},
"users": [
{"id": user.id, "name": user.name}
]
}, ensure_ascii=False) }, ensure_ascii=False)
author_notification = Notification.create(**{ author_notification = Notification.create(**{
"user": shout.createdBy, "user": shout.createdBy,
"type": NotificationType.NEW_COMMENT.name, "type": NotificationType.NEW_COMMENT,
"shout": shout.id, "shout": shout.id,
"data": notification_data_string "data": notification_data_string
}) })