configured isort, black, flake8
This commit is contained in:
@@ -5,32 +5,24 @@ from datetime import datetime, timezone
|
||||
from sqlalchemy import and_
|
||||
|
||||
from base.orm import local_session
|
||||
from orm import Reaction, Shout, Notification, User
|
||||
from orm import Notification, Reaction, Shout, User
|
||||
from orm.notification import NotificationType
|
||||
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
|
||||
}
|
||||
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
|
||||
}
|
||||
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"] = [u for u in notification_data["users"] if u['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))
|
||||
|
||||
if notification_data["reactionIds"] is None:
|
||||
@@ -57,34 +49,45 @@ class NewReactionNotificator:
|
||||
if reaction.kind == ReactionKind.COMMENT:
|
||||
parent_reaction = None
|
||||
if reaction.replyTo:
|
||||
parent_reaction = session.query(Reaction).where(Reaction.id == reaction.replyTo).one()
|
||||
parent_reaction = (
|
||||
session.query(Reaction).where(Reaction.id == reaction.replyTo).one()
|
||||
)
|
||||
if parent_reaction.createdBy != reaction.createdBy:
|
||||
prev_new_reply_notification = session.query(Notification).where(
|
||||
and_(
|
||||
Notification.user == shout.createdBy,
|
||||
Notification.type == NotificationType.NEW_REPLY,
|
||||
Notification.shout == shout.id,
|
||||
Notification.reaction == parent_reaction.id,
|
||||
Notification.seen == False
|
||||
prev_new_reply_notification = (
|
||||
session.query(Notification)
|
||||
.where(
|
||||
and_(
|
||||
Notification.user == shout.createdBy,
|
||||
Notification.type == NotificationType.NEW_REPLY,
|
||||
Notification.shout == shout.id,
|
||||
Notification.reaction == parent_reaction.id,
|
||||
Notification.seen == False, # noqa: E712
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if prev_new_reply_notification:
|
||||
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)],
|
||||
"reactionIds": [reaction.id]
|
||||
}, ensure_ascii=False)
|
||||
reply_notification_data = json.dumps(
|
||||
{
|
||||
"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,
|
||||
"shout": shout.id,
|
||||
"reaction": parent_reaction.id,
|
||||
"data": reply_notification_data
|
||||
})
|
||||
reply_notification = Notification.create(
|
||||
**{
|
||||
"user": parent_reaction.createdBy,
|
||||
"type": NotificationType.NEW_REPLY,
|
||||
"shout": shout.id,
|
||||
"reaction": parent_reaction.id,
|
||||
"data": reply_notification_data,
|
||||
}
|
||||
)
|
||||
|
||||
session.add(reply_notification)
|
||||
|
||||
@@ -93,30 +96,39 @@ class NewReactionNotificator:
|
||||
if reaction.createdBy != shout.createdBy and (
|
||||
parent_reaction is None or parent_reaction.createdBy != shout.createdBy
|
||||
):
|
||||
prev_new_comment_notification = session.query(Notification).where(
|
||||
and_(
|
||||
Notification.user == shout.createdBy,
|
||||
Notification.type == NotificationType.NEW_COMMENT,
|
||||
Notification.shout == shout.id,
|
||||
Notification.seen == False
|
||||
prev_new_comment_notification = (
|
||||
session.query(Notification)
|
||||
.where(
|
||||
and_(
|
||||
Notification.user == shout.createdBy,
|
||||
Notification.type == NotificationType.NEW_COMMENT,
|
||||
Notification.shout == shout.id,
|
||||
Notification.seen == False, # noqa: E712
|
||||
)
|
||||
)
|
||||
).first()
|
||||
.first()
|
||||
)
|
||||
|
||||
if prev_new_comment_notification:
|
||||
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)],
|
||||
"reactionIds": [reaction.id]
|
||||
}, ensure_ascii=False)
|
||||
notification_data_string = json.dumps(
|
||||
{
|
||||
"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,
|
||||
"shout": shout.id,
|
||||
"data": notification_data_string
|
||||
})
|
||||
author_notification = Notification.create(
|
||||
**{
|
||||
"user": shout.createdBy,
|
||||
"type": NotificationType.NEW_COMMENT,
|
||||
"shout": shout.id,
|
||||
"data": notification_data_string,
|
||||
}
|
||||
)
|
||||
|
||||
session.add(author_notification)
|
||||
|
||||
@@ -142,7 +154,7 @@ class NotificationService:
|
||||
try:
|
||||
await notificator.run()
|
||||
except Exception as e:
|
||||
print(f'[NotificationService.worker] error: {str(e)}')
|
||||
print(f"[NotificationService.worker] error: {str(e)}")
|
||||
|
||||
|
||||
notification_service = NotificationService()
|
||||
|
@@ -1,8 +1,8 @@
|
||||
import asyncio
|
||||
import json
|
||||
|
||||
from sse_starlette.sse import EventSourceResponse
|
||||
from starlette.requests import Request
|
||||
import asyncio
|
||||
|
||||
|
||||
class ConnectionManager:
|
||||
@@ -28,9 +28,7 @@ class ConnectionManager:
|
||||
return
|
||||
|
||||
for connection in self.connections_by_user_id[user_id]:
|
||||
data = {
|
||||
"type": "newNotifications"
|
||||
}
|
||||
data = {"type": "newNotifications"}
|
||||
data_string = json.dumps(data, ensure_ascii=False)
|
||||
await connection.put(data_string)
|
||||
|
||||
|
Reference in New Issue
Block a user