feat(chats): add per-user chat archive support
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user