markall-fix
All checks were successful
deploy / deploy (push) Successful in 1m17s

This commit is contained in:
Untone 2023-11-26 22:23:43 +03:00
parent ed9c289416
commit 996fe2eca6

View File

@ -1,6 +1,5 @@
from typing import List, Any from typing import List
from sqlalchemy import and_, select
from sqlalchemy import and_
from sqlalchemy.orm import aliased from sqlalchemy.orm import aliased
from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.exc import SQLAlchemyError
from orm.author import Author from orm.author import Author
@ -35,27 +34,22 @@ class NotificationsResult:
total: int total: int
@strawberry.type def get_notifications(author, session, limit, offset) -> List[Notification]:
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"]
with local_session() as session:
try:
author = session.query(Author).filter(Author.user == user_id).first()
NotificationSeenAlias = aliased(NotificationSeen) NotificationSeenAlias = aliased(NotificationSeen)
query = select(
if author:
nnn = session.query(
NotificationMessage, NotificationMessage,
NotificationSeenAlias.viewer.label("seen") NotificationSeenAlias.viewer.label("seen")
).outerjoin( ).outerjoin(
NotificationSeen, NotificationSeen,
and_(NotificationSeen.viewer == author.id, NotificationSeen.notification == NotificationMessage.id), and_(NotificationSeen.viewer == author.id, NotificationSeen.notification == NotificationMessage.id),
).group_by(NotificationSeen.notification).limit(limit).offset(offset).all() ).group_by(NotificationSeen.notification)
if limit:
query = query.limit(limit)
if offset:
query = query.offset(offset)
notifications = [] notifications = []
for n, seen in nnn: for n, seen in session.execute(query):
ntf = Notification( ntf = Notification(
id=n.id, id=n.id,
payload=n.payload, payload=n.payload,
@ -66,18 +60,33 @@ class Query:
) )
if ntf: if ntf:
notifications.append(ntf) notifications.append(ntf)
return notifications
@strawberry.type
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"]
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 notifications and len(notifications) > 0:
nr = NotificationsResult( nr = NotificationsResult(
notifications = notifications, notifications=notifications,
unread = sum(1 for n in notifications if author.id in n.seen), unread=sum(1 for n in notifications if author.id in n.seen),
total = session.query(NotificationMessage).count() total=session.query(NotificationMessage).count()
) )
return nr return nr
except Exception as ex: except Exception as ex:
print(f"[resolvers.schema] {ex}") print(f"[resolvers.schema] {ex}")
return NotificationsResult( return NotificationsResult(
notifications=[], notifications=[],
total = 0, total=0,
unread = 0 unread=0
) )
@ -90,7 +99,8 @@ class Mutation:
with local_session() as session: with local_session() as session:
try: try:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
ns = NotificationSeen({"notification": notification_id, "viewer": author.id}) if author:
ns = NotificationSeen(notification=notification_id, viewer=author.id)
session.add(ns) session.add(ns)
session.commit() session.commit()
except SQLAlchemyError as e: except SQLAlchemyError as e:
@ -109,7 +119,12 @@ class Mutation:
try: try:
author = session.query(Author).filter(Author.user == user_id).first() author = session.query(Author).filter(Author.user == user_id).first()
if author: if author:
_nslist = session.query(NotificationSeen).filter(NotificationSeen.viewer == author.id).all() nslist = get_notifications(author, session, None, None)
for n in nslist:
if author.id not in n.seen:
ns = NotificationSeen(viewer=author.id, notification=n.id)
session.add(ns)
session.commit()
except SQLAlchemyError as e: except SQLAlchemyError as e:
session.rollback() session.rollback()
print(f"[mark_all_notifications_as_read] error: {str(e)}") print(f"[mark_all_notifications_as_read] error: {str(e)}")