From 003e65086eab77612e78644a5218c7fd9312d05d Mon Sep 17 00:00:00 2001 From: kaireewu Date: Sun, 1 Nov 2026 17:57:34 +0800 Subject: [PATCH] fix(web): up list search matches up name only, not video titles --- reaction_board/web/queries.py | 15 +++++++++++++-- tests/test_web_queries.py | 13 +++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/reaction_board/web/queries.py b/reaction_board/web/queries.py index 2122c01..6134548 100644 --- a/reaction_board/web/queries.py +++ b/reaction_board/web/queries.py @@ -46,6 +46,8 @@ UP_SORTS = { "views", "count", "avg_views", "interaction", "avg_interaction", "latest", "efficiency", "interaction_efficiency", "duration", } +# 搜索字段白名单:视频搜索匹配标题或 UP 名,UP 搜索只匹配 UP 名。 +_Q_FIELDS = {"title": "v.title", "up_name": "v.up_name"} CAPACITY = 1000 CAPACITY_WARNING = 950 TRACK_FANS_NAMES = {37754047: "咻咻满"} @@ -284,6 +286,7 @@ class DashboardQueries: statuses: Sequence[str] = TRACKED_STATUSES, folder_id: int | None = None, q: str | None = None, + q_fields: Sequence[str] | None = None, within_field: str | None = None, within_start: str | None = None, within_end: str | None = None, @@ -299,10 +302,17 @@ class DashboardQueries: params.append(int(up_mid)) if q: pattern = _like_pattern(q) + fields = tuple(q_fields) if q_fields else ("title", "up_name") + columns = [] + for field in fields: + column = _Q_FIELDS.get(field) + if column is None: + raise ValueError(f"不支持的搜索字段: {field}") + columns.append(column) clauses.append( - "(v.title LIKE ? ESCAPE '\\' OR v.up_name LIKE ? ESCAPE '\\')" + "(" + " OR ".join(f"{col} LIKE ? ESCAPE '\\'" for col in columns) + ")" ) - params.extend([pattern, pattern]) + params.extend([pattern] * len(columns)) if within_field: if within_field not in {"pubdate", "first_seen"}: raise ValueError("within_field 必须是 pubdate 或 first_seen") @@ -1060,6 +1070,7 @@ class DashboardQueries: videos = self._videos( conn, q=q, + q_fields=("up_name",) if q else None, within_field=within_field, within_start=within_start, within_end=within_end, diff --git a/tests/test_web_queries.py b/tests/test_web_queries.py index b622224..d4fe29f 100644 --- a/tests/test_web_queries.py +++ b/tests/test_web_queries.py @@ -203,6 +203,19 @@ def test_up_queries(): assert blacklisted["blacklisted"] is True assert blacklisted["video_count"] == 1 assert q.up_detail(999999) is None + + # UP 搜索只匹配 UP 名,不匹配视频标题 + title_only = q.list_ups(q="视频") + assert title_only["total"] == 0 + name_match = q.list_ups(q="UP A") + assert name_match["total"] == 1 + assert name_match["items"][0]["up_mid"] == 1 + assert name_match["items"][0]["video_count"] == 2 + # 大小写不敏感 + lower = q.list_ups(q="up b") + assert lower["total"] == 1 and lower["items"][0]["up_mid"] == 2 + # 命中多个 UP + assert q.list_ups(q="UP")["total"] == 2 finally: db.close()