Merge branch 'main' of https://github.com/Discours/discours-backend into feature/refactoring-services

This commit is contained in:
Untone 2023-10-16 18:19:06 +03:00
commit 51ad266b62
4 changed files with 41 additions and 32 deletions

View File

@ -72,15 +72,13 @@ routes = [
] ]
app = Starlette( app = Starlette(
debug=True,
on_startup=[start_up], on_startup=[start_up],
on_shutdown=[shutdown], on_shutdown=[shutdown],
middleware=middleware, middleware=middleware,
routes=routes, routes=routes,
) )
app.mount("/", GraphQL( app.mount("/", GraphQL(
schema, schema
debug=True
)) ))
dev_app = Starlette( dev_app = Starlette(

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

@ -445,7 +445,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,28 @@ from orm.reaction import ReactionKind
from services.notifications.sse import connection_manager from services.notifications.sse import connection_manager
def update_prev_notification(notification, user): 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, reaction):
notification_data = json.loads(notification.data) notification_data = json.loads(notification.data)
notification_data["users"] = [ notification_data["users"] = [u for u in notification_data["users"] if u['id'] != user.id]
user for user in notification_data["users"] if user['id'] != user.id notification_data["users"].append(user_to_user_data(user))
] notification_data["reactionIds"].append(reaction.id)
notification_data["users"].append({
"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
@ -49,25 +61,23 @@ class NewReactionNotificator:
Notification.user == shout.createdBy, Notification.user == shout.createdBy,
Notification.type == NotificationType.NEW_REPLY, Notification.type == NotificationType.NEW_REPLY,
Notification.shout == shout.id, Notification.shout == shout.id,
Notification.reaction == parent_reaction.id Notification.reaction == parent_reaction.id,
Notification.seen == False
) )
).first() ).first()
if prev_new_reply_notification: if prev_new_reply_notification:
update_prev_notification(prev_new_reply_notification, user) update_prev_notification(prev_new_reply_notification, user, reaction)
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)],
}, "reactionIds": [reaction.id]
"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
@ -84,25 +94,23 @@ class NewReactionNotificator:
and_( and_(
Notification.user == shout.createdBy, Notification.user == shout.createdBy,
Notification.type == NotificationType.NEW_COMMENT, Notification.type == NotificationType.NEW_COMMENT,
Notification.shout == shout.id Notification.shout == shout.id,
Notification.seen == False
) )
).first() ).first()
if prev_new_comment_notification: if prev_new_comment_notification:
update_prev_notification(prev_new_comment_notification, user) update_prev_notification(prev_new_comment_notification, user, reaction)
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)],
}, "reactionIds": [reaction.id]
"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
}) })