feat(web-core): implement unread counters and new-messages divider
Some checks failed
CI / test (push) Failing after 19s
Some checks failed
CI / test (push) Failing after 19s
backend: - add unread_count to ChatRead serialization - compute unread_count per chat using message_receipts and hidden messages web: - add unread badges in chat list - track unread boundary per chat in store - show 'New messages' divider in message list - update realtime flow to increment/clear unread on incoming events
This commit is contained in:
@@ -3,6 +3,7 @@ from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatType
|
||||
from app.messages.models import Message, MessageHidden, MessageReceipt
|
||||
|
||||
|
||||
async def create_chat(db: AsyncSession, *, chat_type: ChatType, title: str | None) -> Chat:
|
||||
@@ -165,3 +166,27 @@ async def get_private_counterpart_user_id(db: AsyncSession, *, chat_id: int, use
|
||||
stmt = select(ChatMember.user_id).where(ChatMember.chat_id == chat_id, ChatMember.user_id != user_id).limit(1)
|
||||
result = await db.execute(stmt)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def get_unread_count_for_chat(db: AsyncSession, *, chat_id: int, user_id: int) -> int:
|
||||
last_read_subquery = (
|
||||
select(MessageReceipt.last_read_message_id)
|
||||
.where(MessageReceipt.chat_id == chat_id, MessageReceipt.user_id == user_id)
|
||||
.limit(1)
|
||||
.scalar_subquery()
|
||||
)
|
||||
stmt = (
|
||||
select(func.count(Message.id))
|
||||
.outerjoin(
|
||||
MessageHidden,
|
||||
(MessageHidden.message_id == Message.id) & (MessageHidden.user_id == user_id),
|
||||
)
|
||||
.where(
|
||||
Message.chat_id == chat_id,
|
||||
Message.sender_id != user_id,
|
||||
MessageHidden.id.is_(None),
|
||||
Message.id > func.coalesce(last_read_subquery, 0),
|
||||
)
|
||||
)
|
||||
result = await db.execute(stmt)
|
||||
return int(result.scalar_one() or 0)
|
||||
|
||||
@@ -16,6 +16,7 @@ class ChatRead(BaseModel):
|
||||
description: str | None = None
|
||||
is_public: bool = False
|
||||
is_saved: bool = False
|
||||
unread_count: int = 0
|
||||
pinned_message_id: int | None = None
|
||||
created_at: datetime
|
||||
|
||||
|
||||
@@ -26,6 +26,8 @@ async def serialize_chat_for_user(db: AsyncSession, *, user_id: int, chat: Chat)
|
||||
if counterpart:
|
||||
display_title = counterpart.name or counterpart.username
|
||||
|
||||
unread_count = await repository.get_unread_count_for_chat(db, chat_id=chat.id, user_id=user_id)
|
||||
|
||||
return ChatRead.model_validate(
|
||||
{
|
||||
"id": chat.id,
|
||||
@@ -36,6 +38,7 @@ async def serialize_chat_for_user(db: AsyncSession, *, user_id: int, chat: Chat)
|
||||
"description": chat.description,
|
||||
"is_public": chat.is_public,
|
||||
"is_saved": chat.is_saved,
|
||||
"unread_count": unread_count,
|
||||
"pinned_message_id": chat.pinned_message_id,
|
||||
"created_at": chat.created_at,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user