Commit 796395a

benny-dou <60535774+benny-dou@users.noreply.github.com>
2025-07-02 03:16:58
fix(turso): correctly parse types in turso response
1 parent 18777cf
Changed files (3)
src
database
history
src/database/turso.py
@@ -262,6 +262,16 @@ def insert_statement(table_name: str, records: dict, update_on_conflict: str = "
 
 def turso_parse_resp(resp: dict) -> list[dict]:
     """Parse turso SELECT response."""
+
+    def correct_type(decltype: str, value: str):
+        if str(value) == "":
+            return ""
+        if decltype in ["INT", "INTEGER"]:
+            return int(value)
+        if decltype in ["FLOAT", "REAL"]:
+            return float(value)
+        return value
+
     cols = glom(resp, "results.0.response.result.cols", default=[])
     rows = glom(resp, "results.0.response.result.rows", default=[])
-    return [{col["name"]: x.get("value", "") for x, col in zip(row, cols, strict=True)} for row in rows]
+    return [{col["name"]: correct_type(col["decltype"], x.get("value", "")) for x, col in zip(row, cols, strict=True)} for row in rows]
src/history/d1.py
@@ -69,12 +69,10 @@ async def backup_chat_history_to_d1(
         return
     if await i_am_bot(client):
         return
-    chatinfo = await get_d1_chatinfo(chat_id)
-    if not chatinfo:  # this chat is never synced
-        chat = await get_chat(client, to_int(chat_id))
-        chatinfo = await save_chatinfo_to_d1(client, parse_chat(chat))
-    if not chatinfo:  # chat is deleted
+    chat = await get_chat(client, to_int(chat_id))
+    if chat.id == 0:  # chat is not accessible
         return
+    chatinfo = await save_chatinfo_to_d1(client, parse_chat(chat))
     if true(os.getenv(f"HISTORY_IGNORE_{chatinfo['cid']}")):
         return
     table_name = chatinfo["tablename"]
src/history/turso.py
@@ -69,12 +69,10 @@ async def backup_chat_history_to_turso(
         return
     if await i_am_bot(client):
         return
-    chatinfo = await get_turso_chatinfo(chat_id)
-    if not chatinfo:  # this chat is never synced
-        chat = await get_chat(client, to_int(chat_id))
-        chatinfo = await save_chatinfo_to_turso(client, parse_chat(chat))
-    if not chatinfo:  # chat is deleted
+    chat = await get_chat(client, to_int(chat_id))
+    if chat.id == 0:  # chat is not accessible
         return
+    chatinfo = await save_chatinfo_to_turso(client, parse_chat(chat))
     if true(os.getenv(f"HISTORY_IGNORE_{chatinfo['cid']}")):
         return
     table_name = chatinfo["tablename"]
@@ -297,7 +295,7 @@ async def save_chatinfo_to_turso(client: Client, minfo: dict) -> dict[str, str]:
             ctitle = parse_chat(chat)["ctitle"]
 
     records = {
-        "cid": cid,
+        "cid": int(cid),
         "ctype": minfo["ctype"],
         "ctitle": ctitle,
         "chandle": minfo["chandle"],
@@ -375,9 +373,9 @@ async def save_userinfo_to_turso(client: Client, minfo: dict) -> dict[str, str]:
         "handle": minfo["handle"],
         "tags": cached.get("tags", ""),
         "name": minfo["full_name"].replace(" ", ""),
-        "uid": str(uid),  #     # all caced values are str from turso,
-        "cid": str(cid),  # so we need to use str to compare with cached.
-        "id": primary_key,
+        "uid": int(uid),
+        "cid": int(cid),
+        "id": int(primary_key),
     }
     if cached != records:
         logger.info(f"Save user info: {records}")