feat: realtime sync, settings UX and chat list improvements
Some checks failed
CI / test (push) Failing after 21s

- add chat_updated realtime event and dynamic chat subscriptions

- auto-join invite links in web app

- implement Telegram-like settings panel (general/notifications/privacy)

- add browser notification preferences and keyboard send mode

- improve chat list with last message preview/time and online badge

- rework chat members UI with context actions and role crowns
This commit is contained in:
2026-03-08 10:59:44 +03:00
parent a4fa72df30
commit 99e7c70901
18 changed files with 1007 additions and 78 deletions

View File

@@ -237,6 +237,29 @@ async def get_unread_count_for_chat(db: AsyncSession, *, chat_id: int, user_id:
return int(result.scalar_one() or 0)
async def get_last_visible_message_for_user(
db: AsyncSession,
*,
chat_id: int,
user_id: int,
) -> Message | None:
stmt = (
select(Message)
.outerjoin(
MessageHidden,
(MessageHidden.message_id == Message.id) & (MessageHidden.user_id == user_id),
)
.where(
Message.chat_id == chat_id,
MessageHidden.id.is_(None),
)
.order_by(Message.id.desc())
.limit(1)
)
result = await db.execute(stmt)
return result.scalar_one_or_none()
async def get_chat_notification_setting(
db: AsyncSession, *, chat_id: int, user_id: int
) -> ChatNotificationSetting | None:

View File

@@ -43,6 +43,7 @@ from app.chats.service import (
update_chat_title_for_user,
)
from app.database.session import get_db
from app.realtime.service import realtime_gateway
from app.users.models import User
router = APIRouter(prefix="/chats", tags=["chats"])
@@ -94,6 +95,7 @@ async def create_chat(
current_user: User = Depends(get_current_user),
) -> ChatRead:
chat = await create_chat_for_user(db, creator_id=current_user.id, payload=payload)
realtime_gateway.add_chat_subscription(chat_id=chat.id, user_id=current_user.id)
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
@@ -104,6 +106,8 @@ async def join_chat(
current_user: User = Depends(get_current_user),
) -> ChatRead:
chat = await join_public_chat_for_user(db, chat_id=chat_id, user_id=current_user.id)
realtime_gateway.add_chat_subscription(chat_id=chat.id, user_id=current_user.id)
await realtime_gateway.publish_chat_updated(chat_id=chat.id)
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
@@ -131,6 +135,7 @@ async def update_chat_title(
current_user: User = Depends(get_current_user),
) -> ChatRead:
chat = await update_chat_title_for_user(db, chat_id=chat_id, user_id=current_user.id, payload=payload)
await realtime_gateway.publish_chat_updated(chat_id=chat.id)
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
@@ -151,7 +156,10 @@ async def add_chat_member(
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
) -> ChatMemberRead:
return await add_chat_member_for_user(db, chat_id=chat_id, actor_user_id=current_user.id, target_user_id=payload.user_id)
member = await add_chat_member_for_user(db, chat_id=chat_id, actor_user_id=current_user.id, target_user_id=payload.user_id)
realtime_gateway.add_chat_subscription(chat_id=chat_id, user_id=payload.user_id)
await realtime_gateway.publish_chat_updated(chat_id=chat_id)
return member
@router.patch("/{chat_id}/members/{user_id}/role", response_model=ChatMemberRead)
@@ -162,13 +170,15 @@ async def update_chat_member_role(
db: AsyncSession = Depends(get_db),
current_user: User = Depends(get_current_user),
) -> ChatMemberRead:
return await update_chat_member_role_for_user(
member = await update_chat_member_role_for_user(
db,
chat_id=chat_id,
actor_user_id=current_user.id,
target_user_id=user_id,
role=payload.role,
)
await realtime_gateway.publish_chat_updated(chat_id=chat_id)
return member
@router.delete("/{chat_id}/members/{user_id}", status_code=status.HTTP_204_NO_CONTENT)
@@ -179,6 +189,8 @@ async def remove_chat_member(
current_user: User = Depends(get_current_user),
) -> None:
await remove_chat_member_for_user(db, chat_id=chat_id, actor_user_id=current_user.id, target_user_id=user_id)
realtime_gateway.remove_chat_subscription(chat_id=chat_id, user_id=user_id)
await realtime_gateway.publish_chat_updated(chat_id=chat_id)
@router.post("/{chat_id}/leave", status_code=status.HTTP_204_NO_CONTENT)
@@ -188,6 +200,8 @@ async def leave_chat(
current_user: User = Depends(get_current_user),
) -> None:
await leave_chat_for_user(db, chat_id=chat_id, user_id=current_user.id)
realtime_gateway.remove_chat_subscription(chat_id=chat_id, user_id=current_user.id)
await realtime_gateway.publish_chat_updated(chat_id=chat_id)
@router.delete("/{chat_id}", status_code=status.HTTP_204_NO_CONTENT)
@@ -217,6 +231,7 @@ async def pin_chat_message(
current_user: User = Depends(get_current_user),
) -> ChatRead:
chat = await pin_chat_message_for_user(db, chat_id=chat_id, user_id=current_user.id, payload=payload)
await realtime_gateway.publish_chat_updated(chat_id=chat.id)
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
@@ -300,4 +315,6 @@ async def join_by_invite(
current_user: User = Depends(get_current_user),
) -> ChatRead:
chat = await join_chat_by_invite_for_user(db, user_id=current_user.id, payload=payload)
realtime_gateway.add_chat_subscription(chat_id=chat.id, user_id=current_user.id)
await realtime_gateway.publish_chat_updated(chat_id=chat.id)
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)

View File

@@ -3,6 +3,7 @@ from datetime import datetime
from pydantic import BaseModel, ConfigDict, Field
from app.chats.models import ChatMemberRole, ChatType
from app.messages.models import MessageType
class ChatRead(BaseModel):
@@ -29,6 +30,9 @@ class ChatRead(BaseModel):
counterpart_username: str | None = None
counterpart_is_online: bool | None = None
counterpart_last_seen_at: datetime | None = None
last_message_text: str | None = None
last_message_type: MessageType | None = None
last_message_created_at: datetime | None = None
my_role: ChatMemberRole | None = None
created_at: datetime

View File

@@ -67,6 +67,7 @@ async def serialize_chat_for_user(db: AsyncSession, *, user_id: int, chat: Chat)
subscribers_count = members_count
unread_count = await repository.get_unread_count_for_chat(db, chat_id=chat.id, user_id=user_id)
last_message = await repository.get_last_visible_message_for_user(db, chat_id=chat.id, user_id=user_id)
user_setting = await repository.get_chat_user_setting(db, chat_id=chat.id, user_id=user_id)
archived = bool(user_setting and user_setting.archived)
pinned = bool(user_setting and user_setting.pinned)
@@ -94,6 +95,9 @@ async def serialize_chat_for_user(db: AsyncSession, *, user_id: int, chat: Chat)
"counterpart_username": counterpart_username,
"counterpart_is_online": counterpart_is_online,
"counterpart_last_seen_at": counterpart_last_seen_at,
"last_message_text": last_message.text if last_message else None,
"last_message_type": last_message.type if last_message else None,
"last_message_created_at": last_message.created_at if last_message else None,
"my_role": my_role,
"created_at": chat.created_at,
}

View File

@@ -37,6 +37,7 @@ class ChatAttachmentRead(BaseModel):
id: int
message_id: int
sender_id: int
message_type: str
message_created_at: datetime
file_url: str
file_type: str

View File

@@ -157,6 +157,7 @@ async def list_attachments_for_chat(
id=attachment.id,
message_id=attachment.message_id,
sender_id=message.sender_id,
message_type=message.type.value if hasattr(message.type, "value") else str(message.type),
message_created_at=message.created_at,
file_url=attachment.file_url,
file_type=attachment.file_type,

View File

@@ -17,6 +17,7 @@ RealtimeEventName = Literal[
"message_delivered",
"user_online",
"user_offline",
"chat_updated",
"pong",
"error",
]

View File

@@ -144,6 +144,24 @@ class RealtimeGateway:
async def load_user_chat_ids(self, db: AsyncSession, user_id: int) -> list[int]:
return await list_user_chat_ids(db, user_id=user_id)
def add_chat_subscription(self, *, chat_id: int, user_id: int) -> None:
self._chat_subscribers[chat_id].add(user_id)
def remove_chat_subscription(self, *, chat_id: int, user_id: int) -> None:
subscribers = self._chat_subscribers.get(chat_id)
if not subscribers:
return
subscribers.discard(user_id)
if not subscribers:
self._chat_subscribers.pop(chat_id, None)
async def publish_chat_updated(self, *, chat_id: int) -> None:
await self._publish_chat_event(
chat_id,
event="chat_updated",
payload={"chat_id": chat_id},
)
async def _handle_redis_event(self, channel: str, payload: dict) -> None:
chat_id = self._extract_chat_id(channel)
if chat_id is None: