Add notification settings
This commit is contained in:
@@ -73,6 +73,21 @@ class TelegramStatsResponse(BaseModel):
|
||||
best_streak: int
|
||||
|
||||
|
||||
class TelegramNotificationSettings(BaseModel):
|
||||
notify_events: bool = True
|
||||
notify_disputes: bool = True
|
||||
notify_moderation: bool = True
|
||||
|
||||
class Config:
|
||||
from_attributes = True
|
||||
|
||||
|
||||
class TelegramNotificationSettingsUpdate(BaseModel):
|
||||
notify_events: bool | None = None
|
||||
notify_disputes: bool | None = None
|
||||
notify_moderation: bool | None = None
|
||||
|
||||
|
||||
# Endpoints
|
||||
@router.post("/generate-link-token", response_model=TelegramLinkToken)
|
||||
async def generate_link_token(current_user: CurrentUser):
|
||||
@@ -391,3 +406,46 @@ async def get_user_stats(telegram_id: int, db: DbSession, _: BotSecretDep):
|
||||
total_points=total_points,
|
||||
best_streak=best_streak
|
||||
)
|
||||
|
||||
|
||||
@router.get("/notifications/{telegram_id}", response_model=TelegramNotificationSettings | None)
|
||||
async def get_notification_settings(telegram_id: int, db: DbSession, _: BotSecretDep):
|
||||
"""Get user's notification settings by Telegram ID."""
|
||||
result = await db.execute(
|
||||
select(User).where(User.telegram_id == telegram_id)
|
||||
)
|
||||
user = result.scalar_one_or_none()
|
||||
|
||||
if not user:
|
||||
return None
|
||||
|
||||
return TelegramNotificationSettings.model_validate(user)
|
||||
|
||||
|
||||
@router.patch("/notifications/{telegram_id}", response_model=TelegramNotificationSettings | None)
|
||||
async def update_notification_settings(
|
||||
telegram_id: int,
|
||||
data: TelegramNotificationSettingsUpdate,
|
||||
db: DbSession,
|
||||
_: BotSecretDep
|
||||
):
|
||||
"""Update user's notification settings by Telegram ID."""
|
||||
result = await db.execute(
|
||||
select(User).where(User.telegram_id == telegram_id)
|
||||
)
|
||||
user = result.scalar_one_or_none()
|
||||
|
||||
if not user:
|
||||
return None
|
||||
|
||||
if data.notify_events is not None:
|
||||
user.notify_events = data.notify_events
|
||||
if data.notify_disputes is not None:
|
||||
user.notify_disputes = data.notify_disputes
|
||||
if data.notify_moderation is not None:
|
||||
user.notify_moderation = data.notify_moderation
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(user)
|
||||
|
||||
return TelegramNotificationSettings.model_validate(user)
|
||||
|
||||
@@ -9,7 +9,8 @@ from app.models.assignment import AssignmentStatus
|
||||
from app.models.marathon import MarathonStatus
|
||||
from app.schemas import (
|
||||
UserPublic, UserPrivate, UserUpdate, TelegramLink, MessageResponse,
|
||||
PasswordChange, UserStats, UserProfilePublic,
|
||||
PasswordChange, UserStats, UserProfilePublic, NotificationSettings,
|
||||
NotificationSettingsUpdate,
|
||||
)
|
||||
from app.services.storage import storage_service
|
||||
|
||||
@@ -189,6 +190,32 @@ async def change_password(
|
||||
return MessageResponse(message="Пароль успешно изменен")
|
||||
|
||||
|
||||
@router.get("/me/notifications", response_model=NotificationSettings)
|
||||
async def get_notification_settings(current_user: CurrentUser):
|
||||
"""Get current user's notification settings"""
|
||||
return NotificationSettings.model_validate(current_user)
|
||||
|
||||
|
||||
@router.patch("/me/notifications", response_model=NotificationSettings)
|
||||
async def update_notification_settings(
|
||||
data: NotificationSettingsUpdate,
|
||||
current_user: CurrentUser,
|
||||
db: DbSession,
|
||||
):
|
||||
"""Update current user's notification settings"""
|
||||
if data.notify_events is not None:
|
||||
current_user.notify_events = data.notify_events
|
||||
if data.notify_disputes is not None:
|
||||
current_user.notify_disputes = data.notify_disputes
|
||||
if data.notify_moderation is not None:
|
||||
current_user.notify_moderation = data.notify_moderation
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(current_user)
|
||||
|
||||
return NotificationSettings.model_validate(current_user)
|
||||
|
||||
|
||||
@router.get("/me/stats", response_model=UserStats)
|
||||
async def get_my_stats(current_user: CurrentUser, db: DbSession):
|
||||
"""Получить свою статистику"""
|
||||
|
||||
Reference in New Issue
Block a user