feat: add search APIs and telegram-like chats sidebar flow
All checks were successful
CI / test (push) Successful in 24s
All checks were successful
CI / test (push) Successful in 24s
- implement chat query filtering and message search endpoints - add db indexes for search fields - activate chats search input in web - replace inline create panel with floating TG-style action menu
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from sqlalchemy import Select, func, select
|
||||
from sqlalchemy import Select, String, func, or_, select
|
||||
from sqlalchemy.orm import aliased
|
||||
from sqlalchemy.ext.asyncio import AsyncSession
|
||||
|
||||
@@ -28,20 +28,31 @@ async def count_chat_members(db: AsyncSession, *, chat_id: int) -> int:
|
||||
return int(result.scalar_one())
|
||||
|
||||
|
||||
def _user_chats_query(user_id: int) -> Select[tuple[Chat]]:
|
||||
return (
|
||||
select(Chat)
|
||||
.join(ChatMember, ChatMember.chat_id == Chat.id)
|
||||
.where(ChatMember.user_id == user_id)
|
||||
.order_by(Chat.id.desc())
|
||||
)
|
||||
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)
|
||||
if query and query.strip():
|
||||
q = f"%{query.strip()}%"
|
||||
stmt = stmt.where(
|
||||
or_(
|
||||
Chat.title.ilike(q),
|
||||
Chat.type.cast(String).ilike(q),
|
||||
)
|
||||
)
|
||||
return stmt.order_by(Chat.id.desc())
|
||||
|
||||
|
||||
async def list_user_chats(db: AsyncSession, *, user_id: int, limit: int = 50, before_id: int | None = None) -> list[Chat]:
|
||||
query = _user_chats_query(user_id).limit(limit)
|
||||
async def list_user_chats(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
user_id: int,
|
||||
limit: int = 50,
|
||||
before_id: int | None = None,
|
||||
query: str | None = None,
|
||||
) -> list[Chat]:
|
||||
query_stmt = _user_chats_query(user_id, query=query).limit(limit)
|
||||
if before_id is not None:
|
||||
query = query.where(Chat.id < before_id)
|
||||
result = await db.execute(query)
|
||||
query_stmt = query_stmt.where(Chat.id < before_id)
|
||||
result = await db.execute(query_stmt)
|
||||
return list(result.scalars().all())
|
||||
|
||||
|
||||
|
||||
@@ -31,10 +31,11 @@ router = APIRouter(prefix="/chats", tags=["chats"])
|
||||
async def list_chats(
|
||||
limit: int = 50,
|
||||
before_id: int | None = None,
|
||||
query: str | None = None,
|
||||
db: AsyncSession = Depends(get_db),
|
||||
current_user: User = Depends(get_current_user),
|
||||
) -> list[ChatRead]:
|
||||
return await get_chats_for_user(db, user_id=current_user.id, limit=limit, before_id=before_id)
|
||||
return await get_chats_for_user(db, user_id=current_user.id, limit=limit, before_id=before_id, query=query)
|
||||
|
||||
|
||||
@router.post("", response_model=ChatRead)
|
||||
|
||||
@@ -47,9 +47,16 @@ async def create_chat_for_user(db: AsyncSession, *, creator_id: int, payload: Ch
|
||||
return chat
|
||||
|
||||
|
||||
async def get_chats_for_user(db: AsyncSession, *, user_id: int, limit: int = 50, before_id: int | None = None) -> list[Chat]:
|
||||
async def get_chats_for_user(
|
||||
db: AsyncSession,
|
||||
*,
|
||||
user_id: int,
|
||||
limit: int = 50,
|
||||
before_id: int | None = None,
|
||||
query: str | None = None,
|
||||
) -> list[Chat]:
|
||||
safe_limit = max(1, min(limit, 100))
|
||||
return await repository.list_user_chats(db, user_id=user_id, limit=safe_limit, before_id=before_id)
|
||||
return await repository.list_user_chats(db, user_id=user_id, limit=safe_limit, before_id=before_id, query=query)
|
||||
|
||||
|
||||
async def get_chat_for_user(db: AsyncSession, *, chat_id: int, user_id: int) -> tuple[Chat, list]:
|
||||
|
||||
Reference in New Issue
Block a user