This commit is contained in:
@@ -1,6 +1,3 @@
|
||||
import json
|
||||
import asyncio
|
||||
|
||||
from orm.notification import Notification
|
||||
from services.db import local_session
|
||||
from services.rediscache import redis
|
||||
|
@@ -35,15 +35,16 @@ class NotificationsResult:
|
||||
total: int
|
||||
|
||||
|
||||
def get_notifications(author, session, limit, offset) -> List[Notification]:
|
||||
def get_notifications(author_id, session, limit, offset) -> List[Notification]:
|
||||
NotificationSeenAlias = aliased(NotificationSeen)
|
||||
query = select(
|
||||
NotificationMessage,
|
||||
NotificationSeenAlias.viewer.label("seen")
|
||||
).outerjoin(
|
||||
NotificationSeen,
|
||||
and_(NotificationSeen.viewer == author.id, NotificationSeen.notification == NotificationMessage.id),
|
||||
).group_by(NotificationSeen.notification)
|
||||
query = (
|
||||
select(NotificationMessage, NotificationSeenAlias.viewer.label("seen"))
|
||||
.outerjoin(
|
||||
NotificationSeen,
|
||||
and_(NotificationSeen.viewer == author_id, NotificationSeen.notification == NotificationMessage.id),
|
||||
)
|
||||
.group_by(NotificationSeen.notification)
|
||||
)
|
||||
if limit:
|
||||
query = query.limit(limit)
|
||||
if offset:
|
||||
@@ -69,26 +70,21 @@ class Query:
|
||||
@strawberry.field
|
||||
@login_required
|
||||
async def load_notifications(self, info, limit: int = 50, offset: int = 0) -> NotificationsResult:
|
||||
user_id = info.context["user_id"]
|
||||
author_id = info.context.get("author_id")
|
||||
with local_session() as session:
|
||||
try:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
if author:
|
||||
notifications = get_notifications(author, session, limit, offset)
|
||||
if author_id:
|
||||
notifications = get_notifications(author_id, session, limit, offset)
|
||||
if notifications and len(notifications) > 0:
|
||||
nr = NotificationsResult(
|
||||
notifications=notifications,
|
||||
unread=sum(1 for n in notifications if author.id in n.seen),
|
||||
total=session.query(NotificationMessage).count()
|
||||
unread=sum(1 for n in notifications if author_id in n.seen),
|
||||
total=session.query(NotificationMessage).count(),
|
||||
)
|
||||
return nr
|
||||
except Exception as ex:
|
||||
print(f"[resolvers.schema] {ex}")
|
||||
return NotificationsResult(
|
||||
notifications=[],
|
||||
total=0,
|
||||
unread=0
|
||||
)
|
||||
return NotificationsResult(notifications=[], total=0, unread=0)
|
||||
|
||||
|
||||
@strawberry.type
|
||||
@@ -96,34 +92,31 @@ class Mutation:
|
||||
@strawberry.mutation
|
||||
@login_required
|
||||
async def mark_notification_as_read(self, info, notification_id: int) -> NotificationSeenResult:
|
||||
user_id = info.context["user_id"]
|
||||
with local_session() as session:
|
||||
try:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
if author:
|
||||
ns = NotificationSeen(notification=notification_id, viewer=author.id)
|
||||
author_id = info.context.get("author_id")
|
||||
if author_id:
|
||||
with local_session() as session:
|
||||
try:
|
||||
ns = NotificationSeen(notification=notification_id, viewer=author_id)
|
||||
session.add(ns)
|
||||
session.commit()
|
||||
except SQLAlchemyError as e:
|
||||
session.rollback()
|
||||
print(f"[mark_notification_as_read] error: {str(e)}")
|
||||
nsr = NotificationSeenResult(error="cant mark as read")
|
||||
return nsr
|
||||
except SQLAlchemyError as e:
|
||||
session.rollback()
|
||||
print(f"[mark_notification_as_read] error: {str(e)}")
|
||||
nsr = NotificationSeenResult(error="cant mark as read")
|
||||
return nsr
|
||||
return NotificationSeenResult()
|
||||
|
||||
@strawberry.mutation
|
||||
@login_required
|
||||
async def mark_all_notifications_as_read(self, info) -> NotificationSeenResult:
|
||||
user_id = info.context["user_id"]
|
||||
|
||||
with local_session() as session:
|
||||
author_id = info.context.get("author_id")
|
||||
if author_id:
|
||||
try:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
if author:
|
||||
nslist = get_notifications(author, session, None, None)
|
||||
with local_session() as session:
|
||||
nslist = get_notifications(author_id, session, None, None)
|
||||
for n in nslist:
|
||||
if author.id not in n.seen:
|
||||
ns = NotificationSeen(viewer=author.id, notification=n.id)
|
||||
if author_id not in n.seen:
|
||||
ns = NotificationSeen(viewer=author_id, notification=n.id)
|
||||
session.add(ns)
|
||||
session.commit()
|
||||
except SQLAlchemyError as e:
|
||||
|
Reference in New Issue
Block a user