From e86e00df87b6ab96d84abb783a89f4e9c4f735ff Mon Sep 17 00:00:00 2001 From: ylytdeng Date: Thu, 23 Apr 2026 13:55:51 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=B0=E8=81=94=E7=B3=BB=E4=BA=BA/?= =?UTF-8?q?=E6=96=B0=E7=BE=A4=E5=90=8D=E7=A7=B0=E4=B8=8D=E5=88=B7=E6=96=B0?= =?UTF-8?q?=EF=BC=88issue=20#46=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 之前的修复 load_contact_names() 读的是 decrypted/contact/contact.db 静态快照,新加联系人不在里面,所以"自动刷新"实际不生效。 现改为通过 db_cache 实时解密源 contact.db 再加载,确保新增联系人 即时可见。db_cache 内部靠 mtime 检测变化,微信写入后下次查询会触发 重新解密。 Co-Authored-By: Claude Opus 4.6 (1M context) --- monitor_web.py | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/monitor_web.py b/monitor_web.py index 69b95aa..cd5f276 100644 --- a/monitor_web.py +++ b/monitor_web.py @@ -435,10 +435,16 @@ def decrypt_wal_full(wal_path, out_path, enc_key): return patched, ms -def load_contact_names(): +def load_contact_names(db_path=None): + """加载联系人名字字典。 + + Args: + db_path: 指定的 contact.db 路径。None 则使用 CONTACT_CACHE(静态快照,可能过期)。 + 实时场景应传入 db_cache.get("contact/contact.db") 返回的路径,确保数据最新。 + """ names = {} try: - conn = sqlite3.connect(CONTACT_CACHE) + conn = sqlite3.connect(db_path or CONTACT_CACHE) for r in conn.execute("SELECT username, nick_name, remark FROM contact").fetchall(): names[r[0]] = r[2] if r[2] else r[1] if r[1] else r[0] conn.close() @@ -1365,11 +1371,20 @@ class SessionMonitor: if is_new: display = self.contact_names.get(username, username) is_group = '@chatroom' in username - # 新群/新联系人不在缓存中时,重新加载联系人 - if display == username and username not in self.contact_names: - refreshed = load_contact_names() + # 新群/新联系人不在缓存中时,通过 db_cache 实时解密 contact.db 后重新加载 + # (load_contact_names 默认读静态快照,新加的联系人不在里面,这里必须走实时解密) + if username not in self.contact_names: + fresh_contact_db = None + if self.db_cache: + try: + fresh_contact_db = self.db_cache.get(os.path.join("contact", "contact.db")) + except Exception as e: + print(f" [contact] 实时解密 contact.db 失败: {e}", flush=True) + refreshed = load_contact_names(fresh_contact_db) self.contact_names.update(refreshed) display = self.contact_names.get(username, username) + if username in refreshed: + print(f" [contact] 新增: {username} -> {display}", flush=True) sender = '' if is_group: sender = self.contact_names.get(curr['sender'], curr['sender_name'] or curr['sender'])