This commit is contained in:
parent
ed9c289416
commit
996fe2eca6
|
@ -1,6 +1,5 @@
|
|||
from typing import List, Any
|
||||
|
||||
from sqlalchemy import and_
|
||||
from typing import List
|
||||
from sqlalchemy import and_, select
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.exc import SQLAlchemyError
|
||||
from orm.author import Author
|
||||
|
@ -35,27 +34,22 @@ class NotificationsResult:
|
|||
total: int
|
||||
|
||||
|
||||
@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()
|
||||
def get_notifications(author, session, limit, offset) -> List[Notification]:
|
||||
NotificationSeenAlias = aliased(NotificationSeen)
|
||||
|
||||
if author:
|
||||
nnn = session.query(
|
||||
query = select(
|
||||
NotificationMessage,
|
||||
NotificationSeenAlias.viewer.label("seen")
|
||||
).outerjoin(
|
||||
NotificationSeen,
|
||||
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 = []
|
||||
for n, seen in nnn:
|
||||
for n, seen in session.execute(query):
|
||||
ntf = Notification(
|
||||
id=n.id,
|
||||
payload=n.payload,
|
||||
|
@ -66,6 +60,21 @@ class Query:
|
|||
)
|
||||
if 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(
|
||||
notifications=notifications,
|
||||
unread=sum(1 for n in notifications if author.id in n.seen),
|
||||
|
@ -90,7 +99,8 @@ class Mutation:
|
|||
with local_session() as session:
|
||||
try:
|
||||
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.commit()
|
||||
except SQLAlchemyError as e:
|
||||
|
@ -109,7 +119,12 @@ class Mutation:
|
|||
try:
|
||||
author = session.query(Author).filter(Author.user == user_id).first()
|
||||
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:
|
||||
session.rollback()
|
||||
print(f"[mark_all_notifications_as_read] error: {str(e)}")
|
||||
|
|
Loading…
Reference in New Issue
Block a user