Commit 4a8c418

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-07-19 17:38:29
fix(history): handle segmented keywords correctly
1 parent 53d2b51
Changed files (1)
src
history
src/history/query.py
@@ -1,6 +1,7 @@
 #!/usr/bin/env python
 # -*- coding: utf-8 -*-
 import re
+import string
 from io import BytesIO
 
 from loguru import logger
@@ -8,7 +9,7 @@ from pyrogram.client import Client
 from pyrogram.parser.markdown import BLOCKQUOTE_EXPANDABLE_DELIM, BLOCKQUOTE_EXPANDABLE_END_DELIM
 from pyrogram.types import Message
 
-from config import PREFIX, cutter
+from config import PREFIX, TZ, cutter
 from database.turso import turso_exec, turso_parse_resp
 from history.turso import get_turso_chatinfo, save_chatinfo_to_turso
 from history.utils import TURSO_KWARGS, check_save_history, get_chat, get_uid_by_username, is_admin, list_chat_ids
@@ -19,7 +20,7 @@ from messages.sender import send2tg
 from messages.utils import blockquote, equal_prefix, smart_split, startswith_prefix
 from others.emoji import MTYPE_EMOJI
 from publish import publish_telegraph
-from utils import myself, slim_cid, strings_list
+from utils import myself, nowstr, slim_cid, strings_list
 
 HELP = f"""🗣**查询当前对话聊天记录**
 `/hist` 使用说明:
@@ -182,10 +183,13 @@ def parse_queries(texts: str, qtype: str) -> tuple[str, str, str, str, str]:
 
 async def query_turso(client: Client, cinfo: dict[str, str], match_time: str, user: str, keyword: str) -> dict:
     """Query chat history from Turso."""
-    segmented = " ".join(cutter.cutword(keyword))
-    texts_to_match = keyword if segmented == keyword else f'"{keyword}" OR "{segmented}"'  # must use double quotes for inner part
+    keyword = keyword.replace(",", " ")  # comma can not be used in query
+    segmented = [x for x in cutter.cutword(keyword) if x not in string.whitespace]
+    texts_to_match = " ".join(segmented)
     sql = f"SELECT T.mid, T.mtype, T.time, T.fullname, T.content FROM '{cinfo['tablename']}' AS T JOIN fts_{cinfo['cid']} AS FTS ON T.mid = FTS.rowid WHERE FTS.segmented MATCH '{texts_to_match}'"
     if match_time:
+        begin = "1970-01-01 00:00:00"
+        end = nowstr(TZ)
         if len(match_time) == 4:  # 2025
             begin = f"{match_time}-01-01 00:00:00"
             end = f"{match_time}-12-31 23:59:59"