Spaces:
Running on T4
Running on T4
File size: 1,829 Bytes
fe40b87 ae2c68f fe40b87 ae2c68f fe40b87 ae2c68f fe40b87 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | """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)
|