upgrades
All checks were successful
deploy / deploy (push) Successful in 1m10s

This commit is contained in:
2023-12-18 01:20:13 +03:00
parent 693d8b6aee
commit 7fa6fcf2d7
7 changed files with 72 additions and 44 deletions

View File

@@ -1,15 +1,18 @@
import logging
from typing import List
from sqlalchemy import and_, select
from sqlalchemy.orm import aliased
from sqlalchemy.exc import SQLAlchemyError
from orm.notification import Notification as NotificationMessage, NotificationSeen
import strawberry
from sqlalchemy import and_, select
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import aliased
from strawberry.schema.config import StrawberryConfig
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
from orm.author import Author
from orm.notification import Notification as NotificationMessage
from orm.notification import NotificationSeen
from services.auth import LoginRequiredMiddleware
from services.db import local_session
import strawberry
from strawberry_sqlalchemy_mapper import StrawberrySQLAlchemyMapper
from strawberry.schema.config import StrawberryConfig
import logging
strawberry_sqlalchemy_mapper = StrawberrySQLAlchemyMapper()
@@ -39,7 +42,7 @@ class NotificationsResult:
total: int
def get_notifications(author_id: int, session, limit: int, offset: int) -> List[Notification]:
def get_notifications(author_id: int, session, after: int, limit: int = 9999, offset: int = 0) -> List[Notification]:
NotificationSeenAlias = aliased(NotificationSeen)
query = (
select(NotificationMessage, NotificationSeenAlias.viewer.label("seen"))
@@ -47,6 +50,7 @@ def get_notifications(author_id: int, session, limit: int, offset: int) -> List[
NotificationSeen,
and_(NotificationSeen.viewer == author_id, NotificationSeen.notification == NotificationMessage.id),
)
.filter(NotificationMessage.created_at > after)
.group_by(NotificationSeen.notification)
)
if limit:
@@ -77,7 +81,9 @@ class Query:
with local_session() as session:
try:
if author_id:
notifications = get_notifications(author_id, session, limit, offset)
author = session.query(Author).filter(Author.id == author_id).first()
after = author.last_seen
notifications = get_notifications(author_id, session, after, limit, offset)
if notifications and len(notifications) > 0:
nr = NotificationsResult(
notifications=notifications,
@@ -110,27 +116,27 @@ class Mutation:
f"[mark_notification_as_read] Ошибка при обновлении статуса прочтения уведомления: {str(e)}"
)
return NotificationSeenResult(error="cant mark as read")
return NotificationSeenResult()
return NotificationSeenResult(error=None)
@strawberry.mutation
async def mark_all_notifications_as_read(self, info) -> NotificationSeenResult:
author_id = info.context.get("author_id")
if author_id:
try:
with local_session() as session:
nslist = get_notifications(author_id, session, None, None)
with local_session() as session:
try:
nslist = get_notifications(author_id, session)
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()
logger.error(
f"[mark_all_notifications_as_read] Ошибка при обновлении статуса прочтения всех уведомлений: {str(e)}"
)
return NotificationSeenResult(error="cant mark as read")
return NotificationSeenResult()
except SQLAlchemyError as e:
session.rollback()
logger.error(
f"[mark_all_notifications_as_read] Ошибка обновления статуса прочтения всех уведомлений: {e}"
)
return NotificationSeenResult(error="cant mark as read")
return NotificationSeenResult(error=None)
schema = strawberry.Schema(