notifier/resolvers/schema.py

111 lines
3.9 KiB
Python
Raw Normal View History

2023-11-26 18:21:14 +00:00
from typing import List, Any
2023-11-26 10:51:06 +00:00
2023-11-26 10:18:57 +00:00
from sqlalchemy import and_
2023-11-26 18:21:14 +00:00
from sqlalchemy.orm import aliased
2023-11-26 10:18:57 +00:00
from orm.author import Author
2023-11-26 18:21:14 +00:00
from orm.notification import Notification as NotificationMessage, NotificationSeen
2023-11-26 10:18:57 +00:00
from services.auth import login_required
from services.db import local_session
2023-11-26 18:21:14 +00:00
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
import strawberry
2023-11-26 10:18:57 +00:00
2023-11-26 18:21:14 +00:00
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
2023-11-26 10:18:57 +00:00
2023-11-26 18:21:14 +00:00
@strawberry_sqlalchemy_mapper.type(NotificationMessage)
2023-11-26 10:18:57 +00:00
class Notification:
id: int
action: str # create update delete join follow etc.
entity: str # REACTION SHOUT
created_at: int
2023-11-26 18:21:14 +00:00
payload: str # JSON data
seen: List[int]
2023-11-26 10:18:57 +00:00
@strawberry.type
class NotificationSeenResult:
error: str
2023-11-26 11:21:32 +00:00
@strawberry.type
class NotificationsResult:
notifications: List[Notification]
unread: int
2023-11-26 12:37:22 +00:00
total: int
2023-11-26 11:21:32 +00:00
2023-11-26 10:18:57 +00:00
@strawberry.type
class Query:
@strawberry.field
2023-11-26 10:51:06 +00:00
@login_required
2023-11-26 11:21:32 +00:00
async def load_notifications(self, info, limit: int = 50, offset: int = 0) -> NotificationsResult:
2023-11-26 10:18:57 +00:00
user_id = info.context["user_id"]
2023-11-26 18:21:14 +00:00
nr = NotificationsResult()
2023-11-26 10:18:57 +00:00
with local_session() as session:
author = session.query(Author).filter(Author.user == user_id).first()
2023-11-26 18:21:14 +00:00
NotificationSeenAlias = aliased(NotificationSeen)
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
2023-11-26 10:18:57 +00:00
@strawberry.type
class Mutation:
@strawberry.mutation
@login_required
async def mark_notification_as_read(self, info, notification_id: int) -> NotificationSeenResult:
user_id = info.context["user_id"]
2023-11-26 18:21:14 +00:00
with local_session() as session:
try:
2023-11-26 10:18:57 +00:00
author = session.query(Author).filter(Author.user == user_id).first()
ns = NotificationSeen({"notification": notification_id, "viewer": author.id})
session.add(ns)
session.commit()
2023-11-26 18:21:14 +00:00
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
2023-11-26 11:21:32 +00:00
return NotificationSeenResult()
2023-11-26 10:18:57 +00:00
@strawberry.mutation
@login_required
async def mark_all_notifications_as_read(self, info) -> NotificationSeenResult:
user_id = info.context["user_id"]
with local_session() as session:
try:
author = session.query(Author).filter(Author.user == user_id).first()
2023-11-26 18:21:14 +00:00
if author:
_nslist = session.query(NotificationSeen).filter(NotificationSeen.viewer == author.id).all()
2023-11-26 10:18:57 +00:00
except Exception as e:
session.rollback()
print(f"[mark_all_notifications_as_read] error: {str(e)}")
2023-11-26 18:21:14 +00:00
nsr = NotificationSeenResult()
nsr.error = "cant mark as read"
return nsr
2023-11-26 11:21:32 +00:00
return NotificationSeenResult()
2023-11-26 10:18:57 +00:00
schema = strawberry.Schema(query=Query, mutation=Mutation)