feat(notifications): per-chat mute settings
Some checks failed
CI / test (push) Failing after 18s

- add chat_notification_settings table and migration
- add chat notifications API (get/update muted)
- skip message notifications for muted recipients
- add mute/unmute control in chat info panel
This commit is contained in:
2026-03-08 02:17:09 +03:00
parent eddd4bda0b
commit ea8a50ee05
9 changed files with 238 additions and 3 deletions

View File

@@ -57,3 +57,19 @@ class ChatMember(Base):
chat: Mapped["Chat"] = relationship(back_populates="members")
user: Mapped["User"] = relationship(back_populates="memberships")
class ChatNotificationSetting(Base):
__tablename__ = "chat_notification_settings"
__table_args__ = (UniqueConstraint("chat_id", "user_id", name="uq_chat_notification_settings_chat_user"),)
id: Mapped[int] = mapped_column(primary_key=True, index=True)
chat_id: Mapped[int] = mapped_column(ForeignKey("chats.id", ondelete="CASCADE"), nullable=False, index=True)
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), nullable=False, index=True)
muted: Mapped[bool] = mapped_column(Boolean, nullable=False, default=False, server_default="false")
updated_at: Mapped[datetime] = mapped_column(
DateTime(timezone=True),
server_default=func.now(),
onupdate=func.now(),
nullable=False,
)

View File

@@ -2,7 +2,7 @@ from sqlalchemy import Select, String, func, or_, select
from sqlalchemy.orm import aliased
from sqlalchemy.ext.asyncio import AsyncSession
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatType
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatNotificationSetting, ChatType
from app.messages.models import Message, MessageHidden, MessageReceipt
@@ -195,3 +195,38 @@ async def get_unread_count_for_chat(db: AsyncSession, *, chat_id: int, user_id:
)
result = await db.execute(stmt)
return int(result.scalar_one() or 0)
async def get_chat_notification_setting(
db: AsyncSession, *, chat_id: int, user_id: int
) -> ChatNotificationSetting | None:
result = await db.execute(
select(ChatNotificationSetting).where(
ChatNotificationSetting.chat_id == chat_id,
ChatNotificationSetting.user_id == user_id,
)
)
return result.scalar_one_or_none()
async def upsert_chat_notification_setting(
db: AsyncSession,
*,
chat_id: int,
user_id: int,
muted: bool,
) -> ChatNotificationSetting:
setting = await get_chat_notification_setting(db, chat_id=chat_id, user_id=user_id)
if setting:
setting.muted = muted
await db.flush()
return setting
setting = ChatNotificationSetting(chat_id=chat_id, user_id=user_id, muted=muted)
db.add(setting)
await db.flush()
return setting
async def is_chat_muted_for_user(db: AsyncSession, *, chat_id: int, user_id: int) -> bool:
setting = await get_chat_notification_setting(db, chat_id=chat_id, user_id=user_id)
return bool(setting and setting.muted)

View File

@@ -10,6 +10,8 @@ from app.chats.schemas import (
ChatMemberAddRequest,
ChatMemberRead,
ChatMemberRoleUpdateRequest,
ChatNotificationSettingsRead,
ChatNotificationSettingsUpdate,
ChatPinMessageRequest,
ChatRead,
ChatTitleUpdateRequest,
@@ -22,6 +24,7 @@ from app.chats.service import (
discover_public_chats_for_user,
ensure_saved_messages_chat,
get_chat_for_user,
get_chat_notification_settings_for_user,
get_chats_for_user,
join_public_chat_for_user,
leave_chat_for_user,
@@ -30,6 +33,7 @@ from app.chats.service import (
serialize_chat_for_user,
serialize_chats_for_user,
update_chat_member_role_for_user,
update_chat_notification_settings_for_user,
update_chat_title_for_user,
)
from app.database.session import get_db
@@ -200,3 +204,27 @@ async def pin_chat_message(
) -> ChatRead:
chat = await pin_chat_message_for_user(db, chat_id=chat_id, user_id=current_user.id, payload=payload)
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
@router.get("/{chat_id}/notifications", response_model=ChatNotificationSettingsRead)
async def get_chat_notifications(
chat_id: int,
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
) -> ChatNotificationSettingsRead:
return await get_chat_notification_settings_for_user(db, chat_id=chat_id, user_id=current_user.id)
@router.put("/{chat_id}/notifications", response_model=ChatNotificationSettingsRead)
async def update_chat_notifications(
chat_id: int,
payload: ChatNotificationSettingsUpdate,
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
) -> ChatNotificationSettingsRead:
return await update_chat_notification_settings_for_user(
db,
chat_id=chat_id,
user_id=current_user.id,
payload=payload,
)

View File

@@ -74,3 +74,13 @@ class ChatDeleteRequest(BaseModel):
class ChatDiscoverRead(ChatRead):
is_member: bool
class ChatNotificationSettingsRead(BaseModel):
chat_id: int
user_id: int
muted: bool
class ChatNotificationSettingsUpdate(BaseModel):
muted: bool

View File

@@ -4,7 +4,16 @@ from sqlalchemy.ext.asyncio import AsyncSession
from app.chats import repository
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatType
from app.chats.schemas import ChatCreateRequest, ChatDeleteRequest, ChatDiscoverRead, ChatPinMessageRequest, ChatRead, ChatTitleUpdateRequest
from app.chats.schemas import (
ChatCreateRequest,
ChatDeleteRequest,
ChatDiscoverRead,
ChatNotificationSettingsRead,
ChatNotificationSettingsUpdate,
ChatPinMessageRequest,
ChatRead,
ChatTitleUpdateRequest,
)
from app.messages.repository import (
delete_messages_in_chat,
get_hidden_message,
@@ -420,3 +429,41 @@ async def clear_chat_for_user(db: AsyncSession, *, chat_id: int, user_id: int) -
continue
await hide_message_for_user(db, message_id=message_id, user_id=user_id)
await db.commit()
async def get_chat_notification_settings_for_user(
db: AsyncSession,
*,
chat_id: int,
user_id: int,
) -> ChatNotificationSettingsRead:
await ensure_chat_membership(db, chat_id=chat_id, user_id=user_id)
setting = await repository.get_chat_notification_setting(db, chat_id=chat_id, user_id=user_id)
return ChatNotificationSettingsRead(
chat_id=chat_id,
user_id=user_id,
muted=bool(setting and setting.muted),
)
async def update_chat_notification_settings_for_user(
db: AsyncSession,
*,
chat_id: int,
user_id: int,
payload: ChatNotificationSettingsUpdate,
) -> ChatNotificationSettingsRead:
await ensure_chat_membership(db, chat_id=chat_id, user_id=user_id)
setting = await repository.upsert_chat_notification_setting(
db,
chat_id=chat_id,
user_id=user_id,
muted=payload.muted,
)
await db.commit()
await db.refresh(setting)
return ChatNotificationSettingsRead(
chat_id=chat_id,
user_id=user_id,
muted=setting.muted,
)