schema-fix
All checks were successful
deploy / deploy (push) Successful in 1m18s

This commit is contained in:
2023-11-26 21:21:14 +03:00
parent 391958e60c
commit 2eaefaa62f
4 changed files with 79 additions and 69 deletions

View File

@@ -1,27 +1,25 @@
from typing import List
from typing import List, Any
import strawberry
from sqlalchemy import and_
from sqlalchemy.orm import aliased
from orm.author import Author
from orm.notification import Notification as NotificationMessage, NotificationSeen
from services.auth import login_required
from services.db import local_session
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
import strawberry
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
@strawberry.type
class NotificationSeen:
notification: int # notification id
viewer: int # author id
@strawberry.type
@strawberry_sqlalchemy_mapper.type(NotificationMessage)
class Notification:
id: int
action: str # create update delete join follow etc.
entity: str # REACTION SHOUT
created_at: int
seen: list[NotificationSeen]
data: str # JSON data
occurrences: int
payload: str # JSON data
seen: List[int]
@strawberry.type
@@ -35,14 +33,6 @@ class NotificationsResult:
unread: int
total: int
def notification_seen_by_viewer(viewer_id, notification_id, session):
seen = (
session.query(NotificationSeen)
.filter(NotificationSeen.viewer == viewer_id, NotificationSeen.notification == notification_id)
.first()
)
return seen is not None
@strawberry.type
class Query:
@@ -50,25 +40,32 @@ class Query:
@login_required
async def load_notifications(self, info, limit: int = 50, offset: int = 0) -> NotificationsResult:
user_id = info.context["user_id"]
nr = NotificationsResult()
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
NotificationSeenAlias = aliased(NotificationSeen)
query = (
session.query(Notification)
.outerjoin(
NotificationSeen,
and_(NotificationSeen.viewer == author.id, NotificationSeen.notification == Notification.id),
)
.limit(limit)
.offset(offset)
)
nslist = query.all()
total = query.group_by(Notification.id).count()
unread = sum(1 for n in nslist if not n.seen_by_viewer)
return NotificationsResult(notifications=nslist, unread=unread, total=total)
if author:
nnn = session.query(
NotificationMessage,
NotificationSeenAlias.viewer.label("seen")
).outerjoin(
NotificationSeen,
and_(NotificationSeen.viewer == author.id, NotificationSeen.notification == NotificationMessage.id),
).limit(limit).offset(offset).all()
nr.notifications = []
for n in nnn:
ntf = Notification()
ntf.id = n.id
ntf.payload = n.payload
ntf.entity = n.entity
ntf.action = n.action
ntf.created_at = n.created_at
ntf.seen = n.seen
nr.notifications.append(ntf)
nr.unread = sum(1 for n in nr.notifications if author.id in n.seen)
nr.total = session.query(NotificationMessage).count()
return nr
@strawberry.type
@@ -77,16 +74,18 @@ class Mutation:
@login_required
async def mark_notification_as_read(self, info, notification_id: int) -> NotificationSeenResult:
user_id = info.context["user_id"]
try:
with local_session() as session:
with local_session() as session:
try:
author = session.query(Author).filter(Author.user == user_id).first()
ns = NotificationSeen({"notification": notification_id, "viewer": author.id})
session.add(ns)
session.commit()
except Exception as e:
session.rollback()
print(f"[mark_notification_as_read] error: {str(e)}")
return NotificationSeenResult(error="cant mark as read")
except Exception as e:
session.rollback()
print(f"[mark_notification_as_read] error: {str(e)}")
nsr = NotificationSeenResult()
nsr.error = "cant mark as read"
return nsr
return NotificationSeenResult()
@strawberry.mutation
@@ -97,11 +96,14 @@ class Mutation:
with local_session() as session:
try:
author = session.query(Author).filter(Author.user == user_id).first()
_nslist = session.quuery(NotificationSeen).filter(NotificationSeen.viewer == author.id).all()
if author:
_nslist = session.query(NotificationSeen).filter(NotificationSeen.viewer == author.id).all()
except Exception as e:
session.rollback()
print(f"[mark_all_notifications_as_read] error: {str(e)}")
return NotificationSeenResult(error="cant mark as read")
nsr = NotificationSeenResult()
nsr.error = "cant mark as read"
return nsr
return NotificationSeenResult()