This commit is contained in:
parent
25b6a6f50a
commit
5c6a680832
|
@ -1,5 +1,6 @@
|
||||||
import logging
|
import logging
|
||||||
from typing import List
|
from typing import List
|
||||||
|
from sqlalchemy.schema import Column
|
||||||
|
|
||||||
import strawberry
|
import strawberry
|
||||||
from sqlalchemy import and_, select
|
from sqlalchemy import and_, select
|
||||||
|
@ -42,7 +43,7 @@ class NotificationsResult:
|
||||||
total: int
|
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)
|
NotificationSeenAlias = aliased(NotificationSeen)
|
||||||
query = (
|
query = (
|
||||||
select(NotificationMessage, NotificationSeenAlias.viewer.label("seen"))
|
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
|
@strawberry.type
|
||||||
class Query:
|
class Query:
|
||||||
@strawberry.field
|
@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")
|
author_id = info.context.get("author_id")
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
try:
|
try:
|
||||||
if author_id:
|
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)
|
notifications = get_notifications(author_id, session, after, limit, offset)
|
||||||
if notifications and len(notifications) > 0:
|
if notifications and len(notifications) > 0:
|
||||||
nr = NotificationsResult(
|
nr = NotificationsResult(
|
||||||
|
@ -124,12 +123,15 @@ class Mutation:
|
||||||
if author_id:
|
if author_id:
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
try:
|
try:
|
||||||
nslist = get_notifications(author_id, session)
|
author = session.query(Author).filter(Author.id == author_id).first()
|
||||||
for n in nslist:
|
if author:
|
||||||
if author_id not in n.seen:
|
after = author.last_seen
|
||||||
ns = NotificationSeen(viewer=author_id, notification=n.id)
|
nslist = get_notifications(author_id, session, after)
|
||||||
session.add(ns)
|
for n in nslist:
|
||||||
session.commit()
|
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()
|
||||||
logger.error(
|
logger.error(
|
||||||
|
|
Loading…
Reference in New Issue
Block a user