Commit e27bd42

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-06-10 02:36:45
refactor(history): split save history logic from handler
1 parent 8638584
Changed files (3)
src/handler.py
@@ -12,7 +12,6 @@ from config import ENABLE, PREFIX, PROXY
 from danmu.entrypoint import query_danmu
 from database.database import del_db
 from history.query import query_chat_history
-from history.sync import sync_chat_history
 from llm.gpt import gpt_response
 from llm.summary import ai_summary
 from messages.parser import parse_msg
@@ -49,7 +48,6 @@ async def handle_utilities(
     asr: bool = True,
     audio: bool = True,
     danmu: bool = True,
-    save_history: bool = True,
     google: bool = True,
     ocr: bool = True,
     history: bool = True,
@@ -89,12 +87,6 @@ async def handle_utilities(
         show_progress (bool, optional): Show a progress message on Telegram. Defaults to True.
         detail_progress (bool, optional): Show detailed progress (Only if show_proress is set to True). Defaults to False.
     """
-    if kwargs.get("only_history"):
-        await sync_chat_history(client, message)
-        return
-    if kwargs.get("disabled"):
-        return
-
     kwargs |= {"target_chat": target_chat, "reply_msg_id": reply_msg_id, "show_progress": show_progress, "detail_progress": detail_progress}
     info = parse_msg(message)
     kwargs |= params_from_msg_text(info["text"])  # merge the parameters from the message text
@@ -124,8 +116,6 @@ async def handle_utilities(
         await query_danmu(client, message, **kwargs)  # /danmu
     if raw_img:
         await convert_raw_img_file(client, message, **kwargs)
-    if save_history:
-        await sync_chat_history(client, message)
 
 
 async def handle_social_media(
@@ -164,8 +154,6 @@ async def handle_social_media(
         show_progress (bool, optional): Show a progress message on Telegram. Defaults to True.
         detail_progress (bool, optional): Show detailed progress (Only if show_proress is set to True). Defaults to False.
     """
-    if kwargs.get("only_history") or kwargs.get("disabled"):
-        return None
     kwargs |= {"target_chat": target_chat, "reply_msg_id": reply_msg_id, "show_progress": show_progress, "detail_progress": detail_progress}
     if not ENABLE.SEND_AS_REPLY:
         kwargs["reply_msg_id"] = -1
src/main.py
@@ -55,18 +55,24 @@ async def main():
     @app.on_message(filters.group)
     async def groups(client: Client, message: Message):
         permission = await check_permission(client, message)
+        if permission["disabled"]:
+            return
         await handle_utilities(client, message, **permission)
         await handle_social_media(client, message, **permission)
 
     @app.on_message(filters.channel)
     async def channels(client: Client, message: Message):
         permission = await check_permission(client, message)
+        if permission["disabled"]:
+            return
         await handle_utilities(client, message, **permission)
         await handle_social_media(client, message, **permission)
 
     @app.on_message(filters.bot)
     async def bots(client: Client, message: Message):
         permission = await check_permission(client, message)
+        if permission["disabled"]:
+            return
         parse_msg(message, verbose=True)
         await forward_social_media_results(client, message)
         await forward_ocr_results(client, message)
@@ -83,12 +89,15 @@ async def main():
             await bots(client, message)  # handle bot messages
             return
         permission = await check_permission(client, message)
+        if permission["disabled"]:
+            return
         parse_msg(message, verbose=True)
         await handle_utilities(client, message, **permission)
         await handle_social_media(client, message, **permission)
 
-    @app.on_edited_message()
-    async def edited(client: Client, message: Message):
+    @app.on_message(group=1)
+    @app.on_edited_message(group=1)
+    async def save_history(client: Client, message: Message):
         await sync_chat_history(client, message)
 
     if ENABLE.CRONTAB:
src/permission.py
@@ -23,14 +23,6 @@ async def check_permission(client: Client, message: Message) -> dict:
     # check permission per service
     permission |= check_service(cid=message.chat.id, ctype=ctype)
 
-    """Save message to D1 is different to the above functions.
-    Some times, we only need to save message to D1, but disable other tools.
-    """
-    # check if we should save this message to D1
-    save_history = check_save_history(ctype=ctype, cid=message.chat.id)
-    if permission["disabled"] and save_history:  # only save msg to D1, disable others
-        permission["only_history"] = True
-    permission["save_history"] = save_history
     return permission
 
 
@@ -95,7 +87,7 @@ async def check_category(client: Client, message: Message, ctype: str) -> dict:
 def check_service(cid: int | str, ctype: str) -> dict:
     if not cid or not ctype:
         return {}
-    cid = str(cid).removeprefix("-100")
+    cid = slim_cid(cid)
 
     permission = {
         # default to False