feat(api): /api/customers 加 q 文本搜索(昵称/备注/ID/手机号,LIKE转义) + /api/groups 加 q 群名搜索
Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -2,8 +2,9 @@
|
||||
|
||||
* ``GET /api/groups`` -> ``Page[GroupOut]`` filtered by ``channel`` /
|
||||
``owner_account_id`` / ``user_id`` (the latter mapping to the
|
||||
``owner_user_id`` column), ordered by ``last_chat_time desc`` (nulls last)
|
||||
so recently-active groups surface first.
|
||||
``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).
|
||||
"""
|
||||
@@ -36,20 +37,26 @@ async def list_groups(
|
||||
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`` filters.
|
||||
``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 single-
|
||||
row lookup (chat page group-name resolution).
|
||||
row lookup (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:
|
||||
|
||||
Reference in New Issue
Block a user