Уведомления: persistent + live (push-to-invalidate → pull)
Новая система уведомлений поверх готовой SSE-шины: события пишутся в БД,
живут 72 часа и чистятся (при чтении списка + фоновой задачей), всплывают
сверху экрана в момент прихода, доступны через колокольчик в правом верхнем
углу (бейдж непрочитанных + панель).
Типы: приглашение в группу (→ /group), старт/финиш партии участникам кроме
инициатора (→ /match/{id}). Титулы — готовый хелпер-задел (не подключён, т.к.
выдача титулов игрокам ещё не реализована).
Бэкенд: модель Notification + миграция 0008 (идемпотентная), notification_service,
notify.notifications_changed, роутер /api/notifications (GET + /read), триггеры
в groups/matches, фоновая чистка в lifespan, защита hub.publish от закрытого loop.
Фронт: useNotifications/useMarkNotificationsRead, NotificationBell/Panel/Toaster,
перекомпоновка top-bar, стили; useServerEvents знает тип notifications.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
@@ -14,6 +14,7 @@ from app.services import (
|
||||
group_service,
|
||||
invitation_service,
|
||||
membership_service,
|
||||
notification_service,
|
||||
notify,
|
||||
stats_service,
|
||||
)
|
||||
@@ -155,6 +156,9 @@ def invite_member(
|
||||
)
|
||||
session.commit()
|
||||
notify.invitations_changed(invited.id) # type: ignore[arg-type] # живое появление у получателя
|
||||
notification_service.invited_to_group(
|
||||
session, invited.id, group.name, user.nickname # type: ignore[arg-type]
|
||||
)
|
||||
return s.InvitationRead(
|
||||
id=inv.id, # type: ignore[arg-type]
|
||||
group_id=group_id,
|
||||
|
||||
@@ -16,6 +16,7 @@ from app.services import (
|
||||
audit_service,
|
||||
group_service,
|
||||
match_service,
|
||||
notification_service,
|
||||
notify,
|
||||
user_service,
|
||||
)
|
||||
@@ -121,6 +122,7 @@ def start_match(
|
||||
)
|
||||
session.commit()
|
||||
notify.match_changed(session, match)
|
||||
notification_service.match_started(session, match, user.id) # type: ignore[arg-type]
|
||||
return build_match_read(session, match, can_modify=match_service.can_modify(session, match, user))
|
||||
|
||||
|
||||
@@ -160,6 +162,7 @@ def finish_match(
|
||||
)
|
||||
session.commit()
|
||||
notify.match_changed(session, match)
|
||||
notification_service.match_finished(session, match, user.id) # type: ignore[arg-type]
|
||||
return build_match_read(session, match, can_modify=match_service.can_modify(session, match, user))
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,34 @@
|
||||
"""Уведомления игрока: список (pull) и отметка прочитанными.
|
||||
|
||||
Появление в реальном времени обеспечивает SSE-сигнал `{type:"notifications"}` — по нему клиент
|
||||
перезапрашивает этот список. Хранение — 72 часа; протухшие чистятся при чтении (и фоном)."""
|
||||
from __future__ import annotations
|
||||
|
||||
from fastapi import APIRouter, Depends
|
||||
from sqlmodel import Session
|
||||
|
||||
from app.auth.deps import get_current_user
|
||||
from app.db.session import get_session
|
||||
from app.models import User
|
||||
from app.schemas import api as s
|
||||
from app.services import notification_service
|
||||
|
||||
router = APIRouter(prefix="/notifications", tags=["notifications"])
|
||||
|
||||
|
||||
@router.get("", response_model=s.NotificationList)
|
||||
def my_notifications(
|
||||
session: Session = Depends(get_session),
|
||||
user: User = Depends(get_current_user),
|
||||
) -> dict:
|
||||
return notification_service.list_for_user(session, user.id) # type: ignore[arg-type]
|
||||
|
||||
|
||||
@router.post("/read", response_model=s.OkResponse)
|
||||
def mark_read(
|
||||
body: s.NotificationMarkRead,
|
||||
session: Session = Depends(get_session),
|
||||
user: User = Depends(get_current_user),
|
||||
) -> s.OkResponse:
|
||||
notification_service.mark_read(session, user.id, body.ids) # type: ignore[arg-type]
|
||||
return s.OkResponse()
|
||||
Reference in New Issue
Block a user