From 5c6a680832b091db1b241afb355457028c0b7759 Mon Sep 17 00:00:00 2001 From: Untone Date: Mon, 18 Dec 2023 18:41:46 +0300 Subject: [PATCH] after-fix --- resolvers/schema.py | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/resolvers/schema.py b/resolvers/schema.py index 8e79bf5..f7155b8 100644 --- a/resolvers/schema.py +++ b/resolvers/schema.py @@ -1,5 +1,6 @@ import logging from typing import List +from sqlalchemy.schema import Column import strawberry from sqlalchemy import and_, select @@ -42,7 +43,7 @@ class NotificationsResult: total: int -def get_notifications(author_id: int, session, after: int, limit: int = 9999, offset: int = 0) -> List[Notification]: +def get_notifications(author_id: int, session, after: int | Column[int], limit: int = 9999, offset: int = 0) -> List[Notification]: NotificationSeenAlias = aliased(NotificationSeen) query = ( select(NotificationMessage, NotificationSeenAlias.viewer.label("seen")) @@ -76,13 +77,11 @@ def get_notifications(author_id: int, session, after: int, limit: int = 9999, of @strawberry.type class Query: @strawberry.field - async def load_notifications(self, info, limit: int = 50, offset: int = 0) -> NotificationsResult: + async def load_notifications(self, info, after: int, limit: int = 50, offset: int = 0) -> NotificationsResult: author_id = info.context.get("author_id") with local_session() as session: try: if author_id: - author = session.query(Author).filter(Author.id == author_id).first() - after = author.last_seen notifications = get_notifications(author_id, session, after, limit, offset) if notifications and len(notifications) > 0: nr = NotificationsResult( @@ -124,12 +123,15 @@ class Mutation: if author_id: with local_session() as session: try: - nslist = get_notifications(author_id, session) - for n in nslist: - if author_id not in n.seen: - ns = NotificationSeen(viewer=author_id, notification=n.id) - session.add(ns) - session.commit() + author = session.query(Author).filter(Author.id == author_id).first() + if author: + after = author.last_seen + nslist = get_notifications(author_id, session, after) + 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: session.rollback() logger.error(