github-actions[bot]
Automated Deploy: 83f1f6fa59ef9d22788ee608e3ba12967f91dd5c
ae2c68f
Raw History Blame Contribute Delete
1.83 kB
"""N06 to N09: the in-app inbox. Listing never marks anything read."""
from datetime import UTC, datetime
from uuid import UUID
from sqlalchemy.ext.asyncio import AsyncSession
from app.common.schemas import Page
from app.core.errors import NotFound
from app.core.pagination import PageParams, paginate
from app.modules.accounts.models import Account
from app.modules.notifications.repository import NotificationRepository
from app.modules.notifications.schemas import NotificationOut, UnreadCount
class NotificationService:
def __init__(self, db: AsyncSession):
self.notifications = NotificationRepository(db)
async def list(self, account: Account, paging: PageParams) -> Page[NotificationOut]:
rows = await self.notifications.page(account.id, paging.cursor, paging.limit + 1)
page, next_cursor = paginate(rows, paging.limit)
items = [NotificationOut(id=r.id, kind=r.kind, title=r.title, body=r.body, at=r.created_at,
read=r.read_at is not None, target=r.target) for r in page]
return Page[NotificationOut](items=items, next_cursor=next_cursor)
async def unread_count(self, account: Account) -> UnreadCount:
return UnreadCount(count=await self.notifications.unread_count(account.id))
async def mark_read(self, account: Account, notification_id: UUID) -> None:
if not await self.notifications.mark_read(account.id, notification_id, datetime.now(UTC)):
raise NotFound()
async def mark_all_read(self, account: Account, through: datetime | None) -> None:
now = datetime.now(UTC)
if through is not None and through.tzinfo is None:
through = through.replace(tzinfo=UTC)
await self.notifications.mark_read_through(account.id, min(through, now) if through else now, now)