feat(chats): add per-user chat archive support
This commit is contained in:
@@ -75,3 +75,19 @@ class ChatNotificationSetting(Base):
|
||||
onupdate=func.now(),
|
||||
nullable=False,
|
||||
)
|
||||
|
||||
|
||||
class ChatUserSetting(Base):
|
||||
__tablename__ = "chat_user_settings"
|
||||
__table_args__ = (UniqueConstraint("chat_id", "user_id", name="uq_chat_user_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)
|
||||
archived: 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,
|
||||
)
|
||||
|
||||
@@ -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, ChatNotificationSetting, ChatType
|
||||
from app.chats.models import Chat, ChatMember, ChatMemberRole, ChatNotificationSetting, ChatType, ChatUserSetting
|
||||
from app.messages.models import Message, MessageHidden, MessageReceipt
|
||||
|
||||
|
||||
@@ -53,7 +53,15 @@ async def count_chat_members(db: AsyncSession, *, chat_id: int) -> int:
|
||||
|
||||
|
||||
def _user_chats_query(user_id: int, query: str | None = None) -> Select[tuple[Chat]]:
|
||||
stmt = select(Chat).join(ChatMember, ChatMember.chat_id == Chat.id).where(ChatMember.user_id == user_id)
|
||||
stmt = (
|
||||
select(Chat)
|
||||
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||
.outerjoin(
|
||||
ChatUserSetting,
|
||||
(ChatUserSetting.chat_id == Chat.id) & (ChatUserSetting.user_id == user_id),
|
||||
)
|
||||
.where(ChatMember.user_id == user_id, func.coalesce(ChatUserSetting.archived, False).is_(False))
|
||||
)
|
||||
if query and query.strip():
|
||||
q = f"%{query.strip()}%"
|
||||
stmt = stmt.where(
|
||||
@@ -80,6 +88,30 @@ async def list_user_chats(
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
async def list_archived_user_chats(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
user_id: int,
|
||||
limit: int = 50,
|
||||
before_id: int | None = None,
|
||||
) -> list[Chat]:
|
||||
stmt = (
|
||||
select(Chat)
|
||||
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||
.join(
|
||||
ChatUserSetting,
|
||||
(ChatUserSetting.chat_id == Chat.id) & (ChatUserSetting.user_id == user_id),
|
||||
)
|
||||
.where(ChatMember.user_id == user_id, ChatUserSetting.archived.is_(True))
|
||||
.order_by(Chat.id.desc())
|
||||
.limit(limit)
|
||||
)
|
||||
if before_id is not None:
|
||||
stmt = stmt.where(Chat.id < before_id)
|
||||
result = await db.execute(stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
async def get_chat_by_id(db: AsyncSession, chat_id: int) -> Chat | None:
|
||||
result = await db.execute(select(Chat).where(Chat.id == chat_id))
|
||||
return result.scalar_one_or_none()
|
||||
@@ -230,3 +262,28 @@ async def upsert_chat_notification_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)
|
||||
|
||||
|
||||
async def get_chat_user_setting(db: AsyncSession, *, chat_id: int, user_id: int) -> ChatUserSetting | None:
|
||||
result = await db.execute(
|
||||
select(ChatUserSetting).where(ChatUserSetting.chat_id == chat_id, ChatUserSetting.user_id == user_id)
|
||||
)
|
||||
return result.scalar_one_or_none()
|
||||
|
||||
|
||||
async def upsert_chat_archived_setting(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
chat_id: int,
|
||||
user_id: int,
|
||||
archived: bool,
|
||||
) -> ChatUserSetting:
|
||||
setting = await get_chat_user_setting(db, chat_id=chat_id, user_id=user_id)
|
||||
if setting:
|
||||
setting.archived = archived
|
||||
await db.flush()
|
||||
return setting
|
||||
setting = ChatUserSetting(chat_id=chat_id, user_id=user_id, archived=archived)
|
||||
db.add(setting)
|
||||
await db.flush()
|
||||
return setting
|
||||
|
||||
@@ -32,6 +32,7 @@ from app.chats.service import (
|
||||
remove_chat_member_for_user,
|
||||
serialize_chat_for_user,
|
||||
serialize_chats_for_user,
|
||||
set_chat_archived_for_user,
|
||||
update_chat_member_role_for_user,
|
||||
update_chat_notification_settings_for_user,
|
||||
update_chat_title_for_user,
|
||||
@@ -47,10 +48,18 @@ async def list_chats(
|
||||
limit: int = 50,
|
||||
before_id: int | None = None,
|
||||
query: str | None = None,
|
||||
archived: bool = False,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> list[ChatRead]:
|
||||
chats = await get_chats_for_user(db, user_id=current_user.id, limit=limit, before_id=before_id, query=query)
|
||||
chats = await get_chats_for_user(
|
||||
db,
|
||||
user_id=current_user.id,
|
||||
limit=limit,
|
||||
before_id=before_id,
|
||||
query=query,
|
||||
archived=archived,
|
||||
)
|
||||
return await serialize_chats_for_user(db, user_id=current_user.id, chats=chats)
|
||||
|
||||
|
||||
@@ -228,3 +237,23 @@ async def update_chat_notifications(
|
||||
user_id=current_user.id,
|
||||
payload=payload,
|
||||
)
|
||||
|
||||
|
||||
@router.post("/{chat_id}/archive", response_model=ChatRead)
|
||||
async def archive_chat(
|
||||
chat_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> ChatRead:
|
||||
chat = await set_chat_archived_for_user(db, chat_id=chat_id, user_id=current_user.id, archived=True)
|
||||
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
|
||||
|
||||
|
||||
@router.post("/{chat_id}/unarchive", response_model=ChatRead)
|
||||
async def unarchive_chat(
|
||||
chat_id: int,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> ChatRead:
|
||||
chat = await set_chat_archived_for_user(db, chat_id=chat_id, user_id=current_user.id, archived=False)
|
||||
return await serialize_chat_for_user(db, user_id=current_user.id, chat=chat)
|
||||
|
||||
@@ -17,6 +17,7 @@ class ChatRead(BaseModel):
|
||||
description: str | None = None
|
||||
is_public: bool = False
|
||||
is_saved: bool = False
|
||||
archived: bool = False
|
||||
unread_count: int = 0
|
||||
pinned_message_id: int | None = None
|
||||
members_count: int | None = None
|
||||
|
||||
@@ -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