35bfebd3e3
- 9b0d0c133035: wechat_account.list_order 进迁移链(此前仅模型声明, 干净库 /api/accounts 即 500);sa.inspect 幂等守卫兼容已手动加列的 smoke.db - fe63c6789e94(验证中发现的预存缺口,超出原清单): Task 14 的 5 列模型 类型修正(4×URL String(1024)→Text, db_size Integer→BigInteger)从未落 链——纯 alembic 新库会复活 >1KB 链接截断;对齐后干净库 alembic check 全绿。SQLite message 表 batch 重建会掉 FTS 触发器+rowid 脱节(docstring 记明 ensure_fts+rebuild_fts 修复,smoke.db 已修复验证) - scrape_via_page: _iter_moments_pages 每页一行进度日志(镜像 _fetch_pages) - messages: MsgTypeParam=Literal[tuple(_MSG_TYPE_FILTER)] 派生,杜绝 双处罗列漂移(openapi enum 验证一致,492 绿含 422 行为) - 文档: ChatGroup 复合PK docstring/groups room_id 多行表述/测试名去 单行假设;Dockerfile 补 alembic.ini+migrations;DEPLOY 增「数据库 迁移」节(新库/旧库stamp路径/回填脚本显式DB_URL)+运维表升级行 Co-Authored-By: Claude <noreply@anthropic.com>
113 lines
4.3 KiB
Python
113 lines
4.3 KiB
Python
"""GET /api/groups, /api/groups/{room_id}/members — chat-group browse endpoints.
|
|
|
|
* ``GET /api/groups`` -> ``Page[GroupOut]`` filtered by ``channel`` /
|
|
``owner_account_id`` / ``user_id`` (the latter mapping to the
|
|
``owner_user_id`` column) and ``q`` (substring match on ``room_name``,
|
|
``%``/``_`` escaped as literals — the 通讯录群 tab search box), ordered by
|
|
``last_chat_time desc`` (nulls last) so recently-active groups surface first.
|
|
* ``GET /api/groups/{room_id}/members`` -> ``list[GroupMemberOut]`` — the
|
|
members of a single group (small set, no pagination needed).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Annotated
|
|
|
|
import sqlalchemy as sa
|
|
from fastapi import APIRouter, Depends, HTTPException, Query
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
|
|
from app.api.deps import get_session
|
|
from app.api.schemas import GroupMemberOut, GroupOut, Page
|
|
from app.db.models import ChatGroup, GroupMember
|
|
|
|
router = APIRouter(prefix="/api", tags=["groups"])
|
|
|
|
SessionDep = Annotated[AsyncSession, Depends(get_session)]
|
|
PageParam = Annotated[int, Query(ge=1)]
|
|
PageSizeParam = Annotated[int, Query(ge=1, le=100)]
|
|
|
|
|
|
@router.get("/groups")
|
|
async def list_groups(
|
|
session: SessionDep,
|
|
page: PageParam = 1,
|
|
page_size: PageSizeParam = 20,
|
|
channel: Annotated[str | None, Query()] = None,
|
|
owner_account_id: Annotated[str | None, Query()] = None,
|
|
user_id: Annotated[str | None, Query()] = None,
|
|
room_id: Annotated[str | None, Query()] = None,
|
|
q: Annotated[str | None, Query()] = None,
|
|
) -> Page[GroupOut]:
|
|
"""List chat groups with optional ``channel`` / ``owner_account_id`` /
|
|
``user_id`` / ``room_id`` / ``q`` filters.
|
|
|
|
``user_id`` maps to the ``ChatGroup.owner_user_id`` column (the staff
|
|
``userId`` who owns the chatroom account), mirroring the ``user_id``
|
|
filter on ``GET /api/customers``. ``room_id`` is an exact-match lookup
|
|
(composite PK: the same room may return one row per owner; chat page
|
|
group-name resolution). ``q`` is a substring match on ``room_name``.
|
|
"""
|
|
conditions = []
|
|
if channel is not None:
|
|
conditions.append(ChatGroup.channel == channel)
|
|
if owner_account_id is not None:
|
|
conditions.append(ChatGroup.owner_account_id == owner_account_id)
|
|
if q:
|
|
# LIKE 通配符转义: %/_ 按字面匹配(镜像 customers/messages 的 q 处理)
|
|
escaped = q.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_")
|
|
conditions.append(ChatGroup.room_name.like(f"%{escaped}%", escape="\\"))
|
|
if room_id is not None:
|
|
conditions.append(ChatGroup.room_id == room_id)
|
|
if user_id is not None:
|
|
conditions.append(ChatGroup.owner_user_id == user_id)
|
|
|
|
total_stmt = sa.select(sa.func.count()).select_from(ChatGroup)
|
|
if conditions:
|
|
total_stmt = total_stmt.where(*conditions)
|
|
total = (await session.execute(total_stmt)).scalar_one()
|
|
|
|
list_stmt = sa.select(ChatGroup)
|
|
if conditions:
|
|
list_stmt = list_stmt.where(*conditions)
|
|
list_stmt = (
|
|
list_stmt.order_by(
|
|
sa.nullslast(ChatGroup.last_chat_time.desc()),
|
|
ChatGroup.room_id.asc(),
|
|
)
|
|
.limit(page_size)
|
|
.offset((page - 1) * page_size)
|
|
)
|
|
rows = (await session.execute(list_stmt)).scalars().all()
|
|
items = [GroupOut.model_validate(row) for row in rows]
|
|
return Page(items=items, total=total, page=page, page_size=page_size)
|
|
|
|
|
|
@router.get("/groups/{room_id}/members")
|
|
async def list_group_members(
|
|
room_id: str, session: SessionDep
|
|
) -> list[GroupMemberOut]:
|
|
"""Return the members of a single group (plain list).
|
|
|
|
Ordered by ``member_id`` for stable rendering. Returns ``404`` when no such
|
|
group exists so the frontend can distinguish "missing room" from "empty
|
|
room".
|
|
"""
|
|
exists = (
|
|
await session.execute(
|
|
sa.select(sa.func.count())
|
|
.select_from(ChatGroup)
|
|
.where(ChatGroup.room_id == room_id)
|
|
)
|
|
).scalar_one()
|
|
if not exists:
|
|
raise HTTPException(status_code=404, detail="group not found")
|
|
|
|
stmt = (
|
|
sa.select(GroupMember)
|
|
.where(GroupMember.room_id == room_id)
|
|
.order_by(GroupMember.member_id.asc())
|
|
)
|
|
rows = (await session.execute(stmt)).scalars().all()
|
|
return [GroupMemberOut.model_validate(row) for row in rows]
|