This commit is contained in:
parent
f940b7461e
commit
ed9c289416
|
@ -2,6 +2,7 @@ from typing import List, Any
|
||||||
|
|
||||||
from sqlalchemy import and_
|
from sqlalchemy import and_
|
||||||
from sqlalchemy.orm import aliased
|
from sqlalchemy.orm import aliased
|
||||||
|
from sqlalchemy.exc import SQLAlchemyError
|
||||||
from orm.author import Author
|
from orm.author import Author
|
||||||
from orm.notification import Notification as NotificationMessage, NotificationSeen
|
from orm.notification import Notification as NotificationMessage, NotificationSeen
|
||||||
from services.auth import login_required
|
from services.auth import login_required
|
||||||
|
@ -41,33 +42,38 @@ class Query:
|
||||||
async def load_notifications(self, info, limit: int = 50, offset: int = 0) -> NotificationsResult:
|
async def load_notifications(self, info, limit: int = 50, offset: int = 0) -> NotificationsResult:
|
||||||
user_id = info.context["user_id"]
|
user_id = info.context["user_id"]
|
||||||
with local_session() as session:
|
with local_session() as session:
|
||||||
author = session.query(Author).filter(Author.user == user_id).first()
|
try:
|
||||||
NotificationSeenAlias = aliased(NotificationSeen)
|
author = session.query(Author).filter(Author.user == user_id).first()
|
||||||
|
NotificationSeenAlias = aliased(NotificationSeen)
|
||||||
|
|
||||||
if author:
|
if author:
|
||||||
nnn = session.query(
|
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),
|
||||||
).limit(limit).offset(offset).all()
|
).group_by(NotificationSeen.notification).limit(limit).offset(offset).all()
|
||||||
notifications = []
|
notifications = []
|
||||||
for n in nnn:
|
for n, seen in nnn:
|
||||||
ntf = Notification()
|
ntf = Notification(
|
||||||
ntf.id = n.id
|
id=n.id,
|
||||||
ntf.payload = n.payload
|
payload=n.payload,
|
||||||
ntf.entity = n.entity
|
entity=n.entity,
|
||||||
ntf.action = n.action
|
action=n.action,
|
||||||
ntf.created_at = n.created_at
|
created_at=n.created_at,
|
||||||
ntf.seen = n.seen
|
seen=seen,
|
||||||
notifications.append(ntf)
|
)
|
||||||
nr = NotificationsResult(
|
if ntf:
|
||||||
notifications = notifications,
|
notifications.append(ntf)
|
||||||
unread = sum(1 for n in notifications if author.id in n.seen),
|
nr = NotificationsResult(
|
||||||
total = session.query(NotificationMessage).count()
|
notifications = notifications,
|
||||||
)
|
unread = sum(1 for n in notifications if author.id in n.seen),
|
||||||
return nr
|
total = session.query(NotificationMessage).count()
|
||||||
|
)
|
||||||
|
return nr
|
||||||
|
except Exception as ex:
|
||||||
|
print(f"[resolvers.schema] {ex}")
|
||||||
return NotificationsResult(
|
return NotificationsResult(
|
||||||
notifications=[],
|
notifications=[],
|
||||||
total = 0,
|
total = 0,
|
||||||
|
@ -87,10 +93,10 @@ class Mutation:
|
||||||
ns = NotificationSeen({"notification": notification_id, "viewer": author.id})
|
ns = NotificationSeen({"notification": notification_id, "viewer": author.id})
|
||||||
session.add(ns)
|
session.add(ns)
|
||||||
session.commit()
|
session.commit()
|
||||||
except Exception as e:
|
except SQLAlchemyError as e:
|
||||||
session.rollback()
|
session.rollback()
|
||||||
print(f"[mark_notification_as_read] error: {str(e)}")
|
print(f"[mark_notification_as_read] error: {str(e)}")
|
||||||
nsr = NotificationSeenResult(error = "cant mark as read")
|
nsr = NotificationSeenResult(error="cant mark as read")
|
||||||
return nsr
|
return nsr
|
||||||
return NotificationSeenResult()
|
return NotificationSeenResult()
|
||||||
|
|
||||||
|
@ -104,11 +110,10 @@ class Mutation:
|
||||||
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 = session.query(NotificationSeen).filter(NotificationSeen.viewer == author.id).all()
|
||||||
except Exception 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)}")
|
||||||
nsr = NotificationSeenResult()
|
nsr = NotificationSeenResult(error="cant mark as read")
|
||||||
nsr.error = "cant mark as read"
|
|
||||||
return nsr
|
return nsr
|
||||||
return NotificationSeenResult()
|
return NotificationSeenResult()
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user