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(
debug=True,
on_startup=[start_up],
on_shutdown=[shutdown],
middleware=middleware,
routes=routes,
)
app.mount("/", GraphQL(
schema,
debug=True
schema
))
dev_app = Starlette(

View File

@ -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,

View File

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

View File

@ -11,16 +11,28 @@ from orm.reaction import ReactionKind
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["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"] = [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
@ -49,25 +61,23 @@ 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": {
"title": shout.title
},
"users": [
{"id": user.id, "name": user.name}
]
"shout": shout_to_shout_data(shout),
"users": [user_to_user_data(user)],
"reactionIds": [reaction.id]
}, 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
@ -84,25 +94,23 @@ 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": {
"title": shout.title
},
"users": [
{"id": user.id, "name": user.name}
]
"shout": shout_to_shout_data(shout),
"users": [user_to_user_data(user)],
"reactionIds": [reaction.id]
}, 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
})