Spaces:
Running on T4
Running on T4
File size: 1,804 Bytes
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 | from datetime import datetime
from uuid import UUID
from sqlalchemy import func, select, tuple_, update
from app.common.base_repository import BaseRepository
from app.core.pagination import Cursor
from app.modules.notifications.models import Notification
class NotificationRepository(BaseRepository[Notification]):
model = Notification
async def page(self, recipient_id: UUID, cursor: Cursor | None, limit: int) -> list[Notification]:
query = select(Notification).where(Notification.recipient_id == recipient_id)
if cursor:
query = query.where(tuple_(Notification.created_at, Notification.id) < (cursor.created_at, cursor.id))
result = await self.session.scalars(
query.order_by(Notification.created_at.desc(), Notification.id.desc()).limit(limit))
return list(result.all())
async def unread_count(self, recipient_id: UUID) -> int:
return await self.session.scalar(select(func.count()).select_from(Notification).where(
Notification.recipient_id == recipient_id, Notification.read_at.is_(None))) or 0
async def mark_read(self, recipient_id: UUID, id_: UUID, now: datetime) -> bool:
"""False when the notification does not exist or belongs to someone else."""
row = await self.first_by(id=id_, recipient_id=recipient_id)
if row is None:
return False
if row.read_at is None:
await self.update(row, read_at=now)
return True
async def mark_read_through(self, recipient_id: UUID, through: datetime, now: datetime) -> None:
await self.session.execute(update(Notification).where(
Notification.recipient_id == recipient_id, Notification.read_at.is_(None),
Notification.created_at <= through).values(read_at=now))
|