feat(chats): add per-user chat archive support
This commit is contained in:
@@ -62,6 +62,8 @@ 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)
|
||||
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)
|
||||
|
||||
return ChatRead.model_validate(
|
||||
{
|
||||
@@ -74,6 +76,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,
|
||||
"archived": archived,
|
||||
"unread_count": unread_count,
|
||||
"pinned_message_id": chat.pinned_message_id,
|
||||
"members_count": members_count,
|
||||
@@ -167,14 +170,18 @@ async def get_chats_for_user(
|
||||
limit: int = 50,
|
||||
before_id: int | None = None,
|
||||
query: str | None = None,
|
||||
archived: bool = False,
|
||||
) -> list[Chat]:
|
||||
safe_limit = max(1, min(limit, 100))
|
||||
chats = await repository.list_user_chats(db, user_id=user_id, limit=safe_limit, before_id=before_id, query=query)
|
||||
saved = await ensure_saved_messages_chat(db, user_id=user_id)
|
||||
if saved.id not in [c.id for c in chats]:
|
||||
chats = [saved, *chats]
|
||||
if archived:
|
||||
chats = await repository.list_archived_user_chats(db, user_id=user_id, limit=safe_limit, before_id=before_id)
|
||||
else:
|
||||
chats = [saved, *[c for c in chats if c.id != saved.id]]
|
||||
chats = await repository.list_user_chats(db, user_id=user_id, limit=safe_limit, before_id=before_id, query=query)
|
||||
saved = await ensure_saved_messages_chat(db, user_id=user_id)
|
||||
if saved.id not in [c.id for c in chats]:
|
||||
chats = [saved, *chats]
|
||||
else:
|
||||
chats = [saved, *[c for c in chats if c.id != saved.id]]
|
||||
return chats
|
||||
|
||||
|
||||
@@ -480,3 +487,17 @@ async def update_chat_notification_settings_for_user(
|
||||
user_id=user_id,
|
||||
muted=setting.muted,
|
||||
)
|
||||
|
||||
|
||||
async def set_chat_archived_for_user(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
chat_id: int,
|
||||
user_id: int,
|
||||
archived: bool,
|
||||
) -> Chat:
|
||||
chat, _membership = await _get_chat_and_membership(db, chat_id=chat_id, user_id=user_id)
|
||||
await repository.upsert_chat_archived_setting(db, chat_id=chat_id, user_id=user_id, archived=archived)
|
||||
await db.commit()
|
||||
await db.refresh(chat)
|
||||
return chat
|
||||
|
||||
Reference in New Issue
Block a user