From 1462f861698363b60a65a8434e9fad9edc3c5f83 Mon Sep 17 00:00:00 2001 From: ll9999345 Date: Tue, 3 Nov 2026 16:39:31 +0800 Subject: [PATCH 1/3] feat: add opt-in local retention Add independent per-account archives, opt-in capture services, searchable pages and synthetic regression tests. Preserve existing trigger-based features. --- README.md | 1 + docs/local-retention.md | 54 +++ electron/main.ts | 26 +- electron/preload.ts | 11 + electron/services/config.ts | 4 + electron/services/messageRetentionService.ts | 230 ++++++++++ electron/services/messageRetentionStore.ts | 235 ++++++++++ electron/services/snsRetentionService.ts | 245 ++++++++++ electron/services/snsRetentionStore.ts | 238 ++++++++++ electron/services/snsService.ts | 444 ++++++++----------- package.json | 1 + scripts/test-message-retention.cjs | 318 +++++++++++++ scripts/test-sns-download.cjs | 218 +++++++++ scripts/test-sns-retention.cjs | 245 ++++++++++ src/App.tsx | 4 + src/components/Sidebar.tsx | 20 +- src/pages/MessageRetentionPage.scss | 81 ++++ src/pages/MessageRetentionPage.tsx | 226 ++++++++++ src/pages/SnsRetentionPage.scss | 101 +++++ src/pages/SnsRetentionPage.tsx | 219 +++++++++ src/types/electron.d.ts | 86 ++++ 21 files changed, 2751 insertions(+), 256 deletions(-) create mode 100644 docs/local-retention.md create mode 100644 electron/services/messageRetentionService.ts create mode 100644 electron/services/messageRetentionStore.ts create mode 100644 electron/services/snsRetentionService.ts create mode 100644 electron/services/snsRetentionStore.ts create mode 100644 scripts/test-message-retention.cjs create mode 100644 scripts/test-sns-download.cjs create mode 100644 scripts/test-sns-retention.cjs create mode 100644 src/pages/MessageRetentionPage.scss create mode 100644 src/pages/MessageRetentionPage.tsx create mode 100644 src/pages/SnsRetentionPage.scss create mode 100644 src/pages/SnsRetentionPage.tsx diff --git a/README.md b/README.md index 91a187a..5321bb5 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,7 @@ ## 主要功能 - 本地实时查看聊天记录 +- 本地消息与朋友圈留存(需主动开启,见[使用说明](docs/local-retention.md)) - 朋友圈图片、视频、**实况**的预览和解密 - 统计分析与群聊画像 - 年度报告与可视化概览 diff --git a/docs/local-retention.md b/docs/local-retention.md new file mode 100644 index 0000000..620dc21 --- /dev/null +++ b/docs/local-retention.md @@ -0,0 +1,54 @@ +# 本地消息与朋友圈留存 + +侧栏的「消息留存」和「朋友圈留存」用于保存独立的本地副本。两项功能默认关闭,分别由你主动开启;它们读取当前账号在本机可访问的内容,不修改微信源数据库,也不安装防撤回或防删除触发器。原有防撤回、防删除功能保持独立。 + +留存只能保住已经读到的内容:没有捕获到的消息、在首次读取前已经删除或不可见的朋友圈,无法靠留存恢复。它也不是手机或微信云端全部历史记录的备份。 + +## 使用前准备 + +1. 先完成 WeFlow 的数据库连接和账号选择,确认「聊天」或「朋友圈」能正常读取内容。图片等媒体还需要对应的[第三方组件](third-party-components.md)能够正常工作。 +2. 保持微信和 WeFlow 运行。退出 WeFlow 后不会继续捕获;再次运行时只能读取仍在本机可用的内容。 +3. 在对应留存页开启开关。关闭开关会停止后续留存,已有副本仍可查看。 + +## 消息留存 + +打开侧栏「消息留存」,开启「自动留存消息」。开始运行时会读取会话中近期可用的消息,之后持续检查更新,不会补齐全部历史聊天。 + +- **已留存原文**:消息在撤回前已被读到,会保留当时的副本。 +- **未捕获原文**:只读到了撤回提示,原消息已经不可用。该条记录表示发现过撤回,不表示恢复了原文。 +- **检测到撤回**:时间是 WeFlow 识别该提示的时间,可能晚于实际撤回时间;首次扫描也可能发现较早的撤回提示。 + +可用搜索查找会话、发送者或消息正文,也可勾选「只看撤回」。留存依赖扫描时机和数据库可读性,不能保证捕获每一条消息。 + +### 消息媒体范围 + +目前会尝试为可读取、可解密的本机图片,以及已有本地文件的表情保存独立副本。尚不保存视频、语音和附件的完整文件内容。看到了消息文字或类型提示,不代表对应媒体文件已经保存;以每条记录的媒体状态为准。 + +## 朋友圈留存 + +1. 在微信中打开、刷新朋友圈,让可见的新内容进入本机缓存;需要较早的动态时,也要先在微信中浏览到它们。 +2. 打开侧栏「朋友圈留存」,开启「自动留存朋友圈」。首次整理会分批读取本机已经存在的动态。 +3. 需要立即重新检查时,点击「立即扫描」。此按钮需要先开启自动留存;「刷新列表」不会重新扫描微信数据库。 + +文字通常会先出现,图片和视频在后台尝试保存。当前查看或搜索到的动态会优先处理媒体,但原件可能已过期、无法下载、格式不支持或超过大小限制,不能保证补齐。 + +动态正文、曾经读到的点赞和评论,以及不同读取结果的快照会独立留存。它们可能与微信当前显示不同;「最近读取」只是再次读到本机缓存的时间,不证明对方此刻仍公开这条动态。 + +### 朋友圈媒体状态 + +| 页面提示 | 含义 | +| --- | --- | +| 已保存图片/视频/实况照片 | 已有对应的本地原件副本;能否播放还取决于设备支持的格式。 | +| 仅保存缩略图 | 原件尚未保存,当前显示的是缩略图,不代表高清图片、完整视频或实况视频已经留存。 | +| 排队保存中 | 自动留存已开启,正在等待后台尝试;退出应用或关闭开关会暂停后续处理。 | +| 未保存 | 尚未获得可用文件,文字和其他成功保存的内容仍会保留。 | + +顶部「已保存原件」只统计完整原件;「原件未保存」包括仅有缩略图的项目。视频和实况视频不会自动播放。当前每个朋友圈媒体文件的留存上限为 128 MiB。 + +朋友圈媒体可能需要从微信媒体服务下载,因此获取媒体时需要网络。归档页面的图片和视频预览仅使用归档内的本地副本,不会直接加载动态中的远程图片地址,也不会把正文作为 HTML 执行。 + +## 保存位置与备份 + +点击对应页面的「打开保存目录」即可查看当前账号的归档。不同账号的留存相互隔离,媒体是独立文件,不依赖原缓存一直存在。关闭留存不会清空已保存数据,归档也不会按时间自动过期。 + +留存文件可能包含可直接读取的消息、评论及媒体,请按自己的隐私需要保管和备份。自动留存不是磁盘备份;手动删除归档、卸载时清理应用数据或磁盘损坏,仍可能造成丢失。备份前先关闭两项留存并退出 WeFlow,再复制完整保存目录。 diff --git a/electron/main.ts b/electron/main.ts index 462c3bc..7b02682 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -29,6 +29,8 @@ import { cloudControlService } from './services/cloudControlService' import { destroyNotificationWindow, registerNotificationHandlers, showNotification, setNotificationNavigateHandler } from './windows/notificationWindow' import { httpService } from './services/httpService' import { messagePushService } from './services/messagePushService' +import { messageRetentionService } from './services/messageRetentionService' +import { snsRetentionService } from './services/snsRetentionService' import { insightService } from './services/insightService' import { insightRecordService } from './services/insightRecordService' import { insightProfileService } from './services/insightProfileService' @@ -2141,6 +2143,13 @@ const runLegacySnsCacheMigration = async ( // 注册 IPC 处理器 function registerIpcHandlers() { + ipcMain.handle('snsRetention:status', () => snsRetentionService.status()) + ipcMain.handle('snsRetention:setEnabled', (_, enabled: boolean) => snsRetentionService.setEnabled(enabled === true)) + ipcMain.handle('snsRetention:list', (_, options?: { query?: string; username?: string; limit?: number; offset?: number }) => snsRetentionService.list(options)) + ipcMain.handle('snsRetention:scanNow', () => snsRetentionService.scanNow()) + ipcMain.handle('messageRetention:status', () => messageRetentionService.status()) + ipcMain.handle('messageRetention:setEnabled', (_, enabled: boolean) => messageRetentionService.setEnabled(enabled === true)) + ipcMain.handle('messageRetention:list', (_, options?: { query?: string; onlyRevoked?: boolean; limit?: number; offset?: number }) => messageRetentionService.list(options)) const relayOneService = new RelayOneService(configService!) registerNotificationHandlers() ensureNotificationNavigateHandlerRegistered() @@ -2164,6 +2173,8 @@ function registerIpcHandlers() { applyAutoUpdateChannel('settings') } void messagePushService.handleConfigChanged(key) + messageRetentionService.handleConfigChanged(key) + snsRetentionService.handleConfigChanged(key) void insightService.handleConfigChanged(key) void groupSummaryService.handleConfigChanged(key) if (['dbPath', 'decryptKey', 'myAccountId'].includes(key)) { @@ -3027,6 +3038,8 @@ function registerIpcHandlers() { } configService?.clear() messagePushService.handleConfigCleared() + messageRetentionService.handleConfigChanged('dbPath') + snsRetentionService.handleConfigChanged('dbPath') insightService.handleConfigCleared() groupSummaryService.handleConfigCleared() snsService.clearMemoryCache() @@ -4008,7 +4021,12 @@ function registerIpcHandlers() { }) ipcMain.handle('sns:getTimeline', async (_, limit: number, offset: number, usernames?: string[], keyword?: string, startTime?: number, endTime?: number) => { - return snsService.getTimeline(limit, offset, usernames, keyword, startTime, endTime) + const accountScope = JSON.stringify([configService?.get('myAccountId'), configService?.get('dbPath')]) + const result = await snsService.getTimeline(limit, offset, usernames, keyword, startTime, endTime) + if (result.success && result.timeline && accountScope === JSON.stringify([configService?.get('myAccountId'), configService?.get('dbPath')])) { + snsRetentionService.captureVisible(result.timeline) + } + return result }) ipcMain.handle('sns:getSnsUsernames', async () => { @@ -5568,6 +5586,8 @@ app.whenReady().then(async () => { }) chatService.addDbMonitorListener((type, json) => { messagePushService.handleDbMonitorChange(type, json) + messageRetentionService.handleDbMonitorChange(type, json) + snsRetentionService.handleDbMonitorChange(type, json) insightService.handleDbMonitorChange(type, json) }) @@ -5648,6 +5668,8 @@ app.whenReady().then(async () => { // 依赖数据库的后台服务在窗口显示后再启动,避免与启动预热争抢数据库 worker messagePushService.start() + messageRetentionService.start() + snsRetentionService.start() insightService.start() groupSummaryService.start() if (configService.get('autoDownloadHighRes')) { @@ -5687,6 +5709,8 @@ const shutdownAppServices = async (): Promise => { // 通知窗使用 hide 而非 close,退出时主动销毁,避免残留窗口阻塞进程退出。 destroyNotificationWindow() messagePushService.stop() + messageRetentionService.stop() + snsRetentionService.stop() insightService.stop() groupSummaryService.stop() // before-quit 不会等待后续 await,先同步落盘刚生成的语音转写。 diff --git a/electron/preload.ts b/electron/preload.ts index 5d7d89b..354a0e8 100644 --- a/electron/preload.ts +++ b/electron/preload.ts @@ -7,6 +7,17 @@ type CloseConfirmPayload = { // 暴露给渲染进程的 API contextBridge.exposeInMainWorld('electronAPI', { + snsRetention: { + status: () => ipcRenderer.invoke('snsRetention:status'), + setEnabled: (enabled: boolean) => ipcRenderer.invoke('snsRetention:setEnabled', enabled), + list: (options?: { query?: string; username?: string; limit?: number; offset?: number }) => ipcRenderer.invoke('snsRetention:list', options), + scanNow: () => ipcRenderer.invoke('snsRetention:scanNow') + }, + messageRetention: { + status: () => ipcRenderer.invoke('messageRetention:status'), + setEnabled: (enabled: boolean) => ipcRenderer.invoke('messageRetention:setEnabled', enabled), + list: (options?: { query?: string; onlyRevoked?: boolean; limit?: number; offset?: number }) => ipcRenderer.invoke('messageRetention:list', options) + }, // 配置 config: { get: (key: string) => ipcRenderer.invoke('config:get', key), diff --git a/electron/services/config.ts b/electron/services/config.ts index 9bbdc16..5f13e04 100644 --- a/electron/services/config.ts +++ b/electron/services/config.ts @@ -53,6 +53,8 @@ interface ConfigSchema { cachePath: string lastOpenedDb: string lastSession: string + messageRetentionEnabled: boolean + snsRetentionEnabled: boolean // 界面相关 theme: 'light' | 'dark' | 'system' @@ -229,6 +231,8 @@ export class ConfigService { cachePath: '', lastOpenedDb: '', lastSession: '', + messageRetentionEnabled: false, + snsRetentionEnabled: false, theme: 'system', themeId: 'cloud-dancer', language: 'zh-CN', diff --git a/electron/services/messageRetentionService.ts b/electron/services/messageRetentionService.ts new file mode 100644 index 0000000..52ce07f --- /dev/null +++ b/electron/services/messageRetentionService.ts @@ -0,0 +1,230 @@ +import { app } from 'electron' +import { existsSync } from 'node:fs' +import { join } from 'node:path' +import { ConfigService } from './config' +import { chatService, type ChatSession, type Message } from './chatService' +import { imageDecryptService } from './imageDecryptService' +import { MessageRetentionStore, retentionAccountHash, type RetainedMessage, type RetentionListOptions } from './messageRetentionStore' + +export type { RetainedMessage, RetentionListOptions } from './messageRetentionStore' +export interface RetentionStatus { + enabled: boolean; running: boolean; totalMessages: number; revokedMessages: number + lastScanAt: number | null; error?: string; archivePath?: string +} +type MessagePosition = { createTime?: number; localId?: number; sortSeq?: number; serverIdRaw?: string } +type SessionState = { signature: string; scannedAt: number; highWater: number; pendingMin?: number; pendingCursor?: MessagePosition } +type MediaJob = { generation: number; store: MessageRetentionStore; item: RetainedMessage; message: Message; sessionId: string } + +/** Best-effort capture of locally visible messages while WeFlow is running. */ +class MessageRetentionService { + private config: ConfigService | null = null + private store: MessageRetentionStore | null = null + private identity = '' + private started = false + private generation = 0 + private timer: ReturnType | null = null + private timerDueAt = 0 + private busy = false + private wakeRequested = false + private error: string | undefined + private sessions = new Map() + private mediaQueue: MediaJob[] = [] + private mediaRunning = false + private mediaAttempts = new Map() + + private getConfig(): ConfigService { return this.config || (this.config = new ConfigService()) } + private enabled(): boolean { return this.getConfig().get('messageRetentionEnabled') === true } + private accountIdentity(): string { + const cfg = this.getConfig() + const account = String(cfg.get('myAccountId') || '').trim() + const path = String(cfg.get('dbPath') || '').trim() + if (!account || !path) return '' + const directory = cfg.getAccountDir(path, account) + return directory ? account + '\0' + directory : '' + } + private ensureStore(create: boolean): MessageRetentionStore | null { + const identity = this.accountIdentity() + if (identity !== this.identity) { + this.generation++; this.store?.close(); this.store = null; this.identity = identity + this.sessions.clear(); this.mediaQueue = []; this.mediaAttempts.clear() + } + if (!identity) return null + if (!this.store) { + const root = app.getPath('userData') + if (!create && !existsSync(join(root, 'Retention', retentionAccountHash(identity), 'messages.sqlite'))) return null + this.store = new MessageRetentionStore(root, identity) + } + return this.store + } + start(): void { + if (!this.enabled()) return + this.started = true + this.schedule(0) + } + stop(): void { + this.started = false; this.generation++; this.wakeRequested = false + if (this.timer) clearTimeout(this.timer) + this.timer = null; this.timerDueAt = 0; this.mediaQueue = [] + this.store?.close(); this.store = null + this.sessions.clear(); this.mediaAttempts.clear() + } + setEnabled(enabled: boolean): RetentionStatus { + this.getConfig().set('messageRetentionEnabled', enabled === true) + if (enabled === true) this.start(); else this.stop() + return this.status() + } + handleConfigChanged(key: string): void { + if (key === 'messageRetentionEnabled') { if (this.enabled()) this.start(); else this.stop(); return } + if (['myAccountId', 'dbPath', 'decryptKey', 'accountConfigs'].includes(key)) { + this.stop(); this.identity = ''; this.error = undefined + if (this.enabled()) this.start() + } + } + handleDbMonitorChange(type: string, json: string): void { + if (!this.started || !this.enabled()) return + try { + const event = JSON.parse(json || '{}') + const marker = `${type} ${event.action || ''} ${event.table || ''}`.toLowerCase() + if (/message|msg|session/.test(marker)) this.schedule(150) + } catch { this.schedule(150) } + } + status(): RetentionStatus { + const enabled = this.enabled() + try { + const store = this.ensureStore(false) + return { enabled, running: enabled && this.started, totalMessages: 0, revokedMessages: 0, lastScanAt: null, + ...store?.stats(), error: this.error, archivePath: store?.directory } + } catch { + return { enabled, running: false, totalMessages: 0, revokedMessages: 0, lastScanAt: null, error: '无法读取本地消息留存库' } + } + } + list(options: RetentionListOptions = {}): { items: RetainedMessage[]; total: number } { + return this.ensureStore(false)?.list(options) || { items: [], total: 0 } + } + private schedule(delay: number): void { + if (!this.started) return + if (this.busy) { this.wakeRequested = true; return } + const dueAt = Date.now() + delay + // Coalesce events at the earliest deadline. A stream of writes must not keep + // postponing capture until the database becomes quiet. + if (this.timer && this.timerDueAt <= dueAt) return + if (this.timer) clearTimeout(this.timer) + this.timerDueAt = dueAt + this.timer = setTimeout(() => { this.timer = null; this.timerDueAt = 0; void this.scan() }, delay) + this.timer.unref?.() + } + private isCurrent(generation: number, store: MessageRetentionStore): boolean { + return this.started && this.enabled() && generation === this.generation && this.store === store && this.accountIdentity() === this.identity + } + private async scan(): Promise { + if (!this.started || this.busy || !this.enabled()) return + this.busy = true; this.wakeRequested = false + try { + const store = this.ensureStore(true) + if (!store) { this.error = '请先连接微信数据库并选择账号'; return } + const generation = this.generation + const result = await chatService.getSessions() + if (!this.isCurrent(generation, store)) return + if (!result.success || !result.sessions) { this.error = '微信数据库暂不可读,等待下次重试'; return } + const now = Date.now() + const signature = (session: ChatSession) => [session.lastTimestamp, session.lastMsgType, session.lastMsgSender, session.summary, session.messageCountHint].join('|') + // Poll recently active/changed conversations first, and rotate through the rest. + const changed = result.sessions.filter(s => { + const state = this.sessions.get(s.username) + return state ? state.signature !== signature(s) || state.pendingMin !== undefined : now / 1000 - s.lastTimestamp < 300 + }).sort((a, b) => b.lastTimestamp - a.lastTimestamp) + const active = result.sessions.filter(s => now / 1000 - s.lastTimestamp < 180) + .sort((a, b) => (this.sessions.get(a.username)?.scannedAt || 0) - (this.sessions.get(b.username)?.scannedAt || 0)) + const rotation = [...result.sessions].sort((a, b) => (this.sessions.get(a.username)?.scannedAt || 0) - (this.sessions.get(b.username)?.scannedAt || 0)) + const queue = [...new Map([...changed.slice(0, 12), ...active.slice(0, 8), ...rotation.slice(0, 8)].map(s => [s.username, s])).values()] + let failed = false + for (const session of queue) { + if (!this.isCurrent(generation, store)) return + const old = this.sessions.get(session.username) + // Track attempts separately from successful signatures/watermarks. Failed + // conversations must rotate out so they cannot monopolize the baseline. + this.sessions.set(session.username, { signature: '', highWater: 0, ...old, scannedAt: now }) + // The independent latest-page query leaves the chat UI's paging cursor alone. + const latest = await chatService.getLatestMessages(session.username, old ? 80 : 20) + if (!this.isCurrent(generation, store)) return + if (!latest.success || !latest.messages) { failed = true; continue } + let highWater = old?.highWater || 0 + this.captureBatch(store, generation, session, latest.messages) + for (const message of latest.messages) { + highWater = Math.max(highWater, message.createTime) + } + let pendingMin: number | undefined + let pendingCursor: MessagePosition | undefined + if (old?.highWater && (highWater > old.highWater || old.pendingMin !== undefined)) { + // Fill gaps in a busy conversation rather than only retaining its last 80 rows. + const minTime = old.pendingMin ?? Math.max(0, old.highWater - 2) + let cursor: MessagePosition = old.pendingCursor || { createTime: minTime, localId: 0, sortSeq: 0, serverIdRaw: '0' } + pendingMin = minTime; pendingCursor = cursor + for (let page = 0; page < 8; page++) { + const batch = await chatService.getNewMessages(session.username, minTime, 200, cursor) + if (!this.isCurrent(generation, store)) return + if (!batch.success || !batch.messages) { failed = true; break } + this.captureBatch(store, generation, session, batch.messages) + if (batch.messages.length < 200) { pendingMin = undefined; pendingCursor = undefined; break } + const next = batch.messages[batch.messages.length - 1] + const position = { createTime: next.createTime, localId: next.localId, sortSeq: next.sortSeq, serverIdRaw: next.serverIdRaw } + if (JSON.stringify(cursor) === JSON.stringify(position)) { failed = true; break } + cursor = position; pendingCursor = position + } + } + this.sessions.set(session.username, { signature: signature(session), scannedAt: now, highWater, pendingMin, pendingCursor }) + // Yield between conversations so capture cannot starve the desktop UI. + await new Promise(resolve => setTimeout(resolve, 5)) + } + if (this.isCurrent(generation, store)) { + store.markScanned(); this.error = failed ? '部分会话暂不可读,已保存可读取的消息并将自动重试' : undefined + } + } catch (error) { + // Keep diagnostics useful without logging chat content or private database paths. + console.warn('[MessageRetention] scan failed', { name: error instanceof Error ? error.name : typeof error }) + this.error = '消息留存扫描暂时失败,将自动重试' + } finally { + this.busy = false + if (this.started && this.enabled()) this.schedule(this.wakeRequested ? 200 : 1500) + } + } + private captureBatch(store: MessageRetentionStore, generation: number, session: ChatSession, messages: Message[]): void { + const items = store.captureMany(session.username, session.displayName || session.username, messages.map(message => ({ ...message, + senderDisplayName: message.isSend ? (message.senderDisplayName || '我') + : !session.username.endsWith('@chatroom') ? (session.displayName || message.senderDisplayName || message.senderUsername || '') + : (message.senderDisplayName || message.senderUsername || '') }))) + items.forEach((item, index) => this.queueMedia(store, generation, session.username, messages[index], item)) + } + private queueMedia(store: MessageRetentionStore, generation: number, sessionId: string, message: Message, item: RetainedMessage | null): void { + if (!item || item.originalMissing || item.mediaState === 'saved' || ![3, 47].includes(message.localType)) return + const key = store.archivePath + ':' + item.id + if (Date.now() - (this.mediaAttempts.get(key) || 0) < 60_000 || this.mediaQueue.length >= 300) return + this.mediaAttempts.set(key, Date.now()) + this.mediaQueue.push({ generation, store, item, message, sessionId }) + void this.drainMedia() + } + private async drainMedia(): Promise { + if (this.mediaRunning) return + this.mediaRunning = true + try { + while (this.mediaQueue.length) { + const job = this.mediaQueue.shift()! + if (!this.isCurrent(job.generation, job.store)) continue + try { + let source = job.message.emojiLocalPath + if (job.message.localType === 3) { + const result = await imageDecryptService.decryptImage({ sessionId: job.sessionId, + imageMd5: job.message.imageMd5, imageDatName: job.message.imageDatName, + createTime: job.message.createTime, preferFilePath: true, hardlinkOnly: true, + allowFilesystemScan: false, suppressEvents: true }) + if (result.success) source = result.localPath + } + if (!this.isCurrent(job.generation, job.store)) continue + if (source) await job.store.preserveMedia(job.item.id, source) + else job.store.markMediaUnavailable(job.item.id) + } catch { job.store.markMediaUnavailable(job.item.id) } + } + } finally { this.mediaRunning = false } + } +} +export const messageRetentionService = new MessageRetentionService() diff --git a/electron/services/messageRetentionStore.ts b/electron/services/messageRetentionStore.ts new file mode 100644 index 0000000..0b557f9 --- /dev/null +++ b/electron/services/messageRetentionStore.ts @@ -0,0 +1,235 @@ +import { DatabaseSync } from 'node:sqlite' +import { createHash, randomUUID } from 'node:crypto' +import { mkdirSync, chmodSync, existsSync, promises as fs } from 'node:fs' +import { join, extname, isAbsolute } from 'node:path' +import { fileURLToPath } from 'node:url' + +export interface RetentionInput { + messageKey?: string; localId: number; serverId?: number | string; serverIdRaw?: string + localType: number; createTime: number; isSend: number | null; senderUsername?: string | null + senderDisplayName?: string; parsedContent: string; rawContent?: string + _db_path?: string; _db_name?: string; _table_name?: string +} +export interface RetainedMessage { + id: string; sessionId: string; sessionName: string; senderName: string; content: string + rawContent: string; createTime: number; localId: number; serverId: string; localType: number + isSend: boolean; recalledAt: number | null; originalMissing: boolean; mediaPath?: string + mediaState: 'none' | 'pending' | 'saved' | 'unavailable'; capturedAt: number +} +export interface RetentionListOptions { query?: string; onlyRevoked?: boolean; limit?: number; offset?: number } +export const retentionAccountHash = (identity: string): string => createHash('sha256').update(identity).digest('hex') +const digest = retentionAccountHash +const MAX_MEDIA_BYTES = 64 * 1024 * 1024 +export function retentionLocalMediaPath(source: string): string { + const path = source.startsWith('file:') ? fileURLToPath(source) : source + // Use native path semantics: Windows drive paths do not start with a slash. + if (!isAbsolute(path)) throw new Error('仅允许复制本地媒体文件') + return path +} +const finite = (n: unknown): number => Number.isFinite(Number(n)) ? Number(n) : 0 +const decimalId = (v: unknown): string => { + const s = typeof v === 'bigint' ? v.toString() : typeof v === 'string' ? v.trim() + : typeof v === 'number' && Number.isSafeInteger(v) ? String(v) : '' + return /^\d+$/.test(s) && !/^0+$/.test(s) ? s.replace(/^0+/, '') : '' +} +export function retentionServerId(message: RetentionInput): string { + // A rounded JS number cannot be repaired. Keep it empty rather than storing a false ID. + return decimalId(message.serverIdRaw) || decimalId(message.serverId) +} +function xmlId(xml: string, tag: string): string { + const value = xml.match(new RegExp(`<${tag}\\b[^>]*>\\s*(?:)?\\s*`, 'i'))?.[1] + return decimalId(value) +} +export function revocationInfo(message: RetentionInput): { serverId: string; serverCandidates: string[]; strongServerCandidates: string[]; localId: string } | null { + // XML pasted or quoted in an ordinary chat is content, not a system event. + if (![10000, 10002].includes(Number(message.localType))) return null + const raw = String(message.rawContent || '') + const xml = /]*\btype\s*=\s*["']revokemsg["']/i.test(raw) + const systemNotice = [10000, 10002].includes(Number(message.localType)) && + /撤回了?一条消息|撤回了一条訊息|recalled a message|retracted a message/i.test(message.parsedContent || raw) + if (!xml && !systemNotice) return null + const msgId = xmlId(raw, 'msgid') + // Some versions use msgid for the server ID, others for a small local row ID. + const localId = msgId && BigInt(msgId) <= 0xffffffffn ? msgId : '' + const strongServerCandidates = [...new Set([xmlId(raw, 'newmsgid'), xmlId(raw, 'oldmsgid'), !localId ? msgId : ''].filter(Boolean))] + const serverCandidates = [...new Set([...strongServerCandidates, msgId].filter(Boolean))] + return { serverId: serverCandidates[0] || '', serverCandidates, strongServerCandidates, localId } +} + +/** An independent local archive. It never opens or modifies a WeChat database. */ +export class MessageRetentionStore { + readonly archivePath: string + readonly directory: string + private db: DatabaseSync + private closed = false + constructor(userDataPath: string, accountIdentity: string) { + if (!accountIdentity.trim()) throw new Error('未选择留存账号') + const root = join(userDataPath, 'Retention') + this.directory = join(root, retentionAccountHash(accountIdentity)) + mkdirSync(root, { recursive: true, mode: 0o700 }); chmodSync(root, 0o700) + mkdirSync(this.directory, { recursive: true, mode: 0o700 }); chmodSync(this.directory, 0o700) + this.archivePath = join(this.directory, 'messages.sqlite') + this.db = new DatabaseSync(this.archivePath) + chmodSync(this.archivePath, 0o600) + this.db.exec(`PRAGMA journal_mode=DELETE; PRAGMA synchronous=FULL; PRAGMA busy_timeout=3000; + CREATE TABLE IF NOT EXISTS messages ( + id TEXT PRIMARY KEY, sessionId TEXT NOT NULL, sessionName TEXT NOT NULL, + senderName TEXT NOT NULL, content TEXT NOT NULL, rawContent TEXT NOT NULL, + createTime INTEGER NOT NULL, localId INTEGER NOT NULL, serverId TEXT NOT NULL, + source TEXT NOT NULL, localType INTEGER NOT NULL, isSend INTEGER NOT NULL, + recalledAt INTEGER, originalMissing INTEGER NOT NULL DEFAULT 0, + revokeContent TEXT NOT NULL DEFAULT '', mediaPath TEXT, mediaState TEXT NOT NULL DEFAULT 'none', + capturedAt INTEGER NOT NULL, lastSeenAt INTEGER NOT NULL); + CREATE INDEX IF NOT EXISTS retention_server ON messages(sessionId,serverId); + CREATE INDEX IF NOT EXISTS retention_local ON messages(sessionId,localId,source); + CREATE INDEX IF NOT EXISTS retention_recalled ON messages(recalledAt); + CREATE TABLE IF NOT EXISTS metadata (key TEXT PRIMARY KEY, value TEXT NOT NULL);`) + } + close(): void { if (!this.closed) { this.closed = true; this.db.close() } } + private map(row: Record): RetainedMessage { + return { id: String(row.id), sessionId: String(row.sessionId), sessionName: String(row.sessionName), + senderName: String(row.senderName), content: String(row.content), rawContent: String(row.rawContent), + createTime: finite(row.createTime), localId: finite(row.localId), serverId: String(row.serverId), + localType: finite(row.localType), isSend: Boolean(row.isSend), recalledAt: row.recalledAt == null ? null : finite(row.recalledAt), + originalMissing: Boolean(row.originalMissing), mediaPath: row.mediaPath ? String(row.mediaPath) : undefined, + mediaState: String(row.mediaState) as RetainedMessage['mediaState'], capturedAt: finite(row.capturedAt) } + } + captureMany(sessionId: string, sessionName: string, messages: RetentionInput[], now = Date.now()): Array { + if (this.closed) return [] + this.db.exec('BEGIN IMMEDIATE') + try { + const rows = messages.map(message => this.capture(sessionId, sessionName, message, now)) + this.db.exec('COMMIT') + return rows + } catch (error) { this.db.exec('ROLLBACK'); throw error } + } + private findUniqueLocal(sessionId: string, localId: number, source: string): Record | undefined { + // A row number alone is not an identity across shards. Missing shard metadata + // or several retained rows at that position make a local-only match ambiguous. + if (!localId || source.startsWith('|')) return undefined + const rows = this.db.prepare('SELECT * FROM messages WHERE sessionId=? AND localId=? AND source=? LIMIT 2').all(sessionId, localId, source) + return rows.length === 1 ? rows[0] : undefined + } + capture(sessionId: string, sessionName: string, message: RetentionInput, now = Date.now()): RetainedMessage | null { + if (this.closed || !sessionId) return null + const serverId = retentionServerId(message) + const localId = finite(message.localId) + const source = [message._db_name || message._db_path || '', message._table_name || ''].join('|') + const revoke = revocationInfo(message) + let existing: Record | undefined + if (revoke) { + // newmsgid is a 64-bit decimal string. Never pass it through Number. + // If explicit target server IDs exist, a short msgid is not an alternative + // server identity: it can refer to an unrelated local row. + const candidates = revoke.strongServerCandidates.length ? revoke.strongServerCandidates : revoke.serverCandidates + for (const candidate of candidates) { + existing = revoke.strongServerCandidates.length + ? this.db.prepare('SELECT * FROM messages WHERE sessionId=? AND serverId=? LIMIT 1').get(sessionId, candidate) + : !source.startsWith('|') ? this.db.prepare('SELECT * FROM messages WHERE sessionId=? AND serverId=? AND source=? LIMIT 1').get(sessionId, candidate, source) : undefined + if (existing) break + } + if (!existing && !revoke.strongServerCandidates.length && revoke.localId) { + existing = this.findUniqueLocal(sessionId, Number(revoke.localId), source) + } + // Only a target-less notice can be inferred to replace its own row. Require + // the same shard, row, timestamp and non-conflicting server identity. + if (!existing && !revoke.serverCandidates.length) { + const replacement = this.findUniqueLocal(sessionId, localId, source) + if (replacement && finite(replacement.createTime) === finite(message.createTime) && + (!serverId || !replacement.serverId || replacement.serverId === serverId)) existing = replacement + } + if (existing) { + if (existing.recalledAt == null) this.db.prepare('UPDATE messages SET recalledAt=?,revokeContent=?,lastSeenAt=? WHERE id=?').run(now, String(message.rawContent || message.parsedContent || ''), now, String(existing.id)) + return this.get(String(existing.id)) + } + const target = revoke.strongServerCandidates[0] || '' + const id = digest(`${sessionId}\0${target ? 'server:' + target : 'revoke-local:' + source + ':' + (revoke.localId || localId) + ':' + finite(message.createTime)}`) + this.db.prepare(`INSERT OR IGNORE INTO messages + (id,sessionId,sessionName,senderName,content,rawContent,createTime,localId,serverId,source,localType,isSend,recalledAt,originalMissing,revokeContent,capturedAt,lastSeenAt) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,1,?,?,?)`).run(id, sessionId, sessionName, message.senderDisplayName || message.senderUsername || '', + '原文未捕获,无法恢复', '', finite(message.createTime), revoke.localId ? Number(revoke.localId) : localId, target, source, finite(message.localType), message.isSend ? 1 : 0, now, + String(message.rawContent || message.parsedContent || ''), now, now) + return this.get(id) + } + if (serverId) existing = this.db.prepare('SELECT * FROM messages WHERE sessionId=? AND serverId=? LIMIT 1').get(sessionId, serverId) + if (!existing) { + const local = this.findUniqueLocal(sessionId, localId, source) + if (local && (!serverId || !local.serverId || local.serverId === serverId) && + (local.originalMissing || finite(local.createTime) === finite(message.createTime))) existing = local + } + const id = existing ? String(existing.id) : digest(`${sessionId}\0${serverId ? 'server:' + serverId : 'local:' + source + ':' + localId + ':' + finite(message.createTime)}`) + if (!existing) existing = this.db.prepare('SELECT * FROM messages WHERE id=?').get(id) + if (existing && !existing.originalMissing) { + // Outgoing rows may be captured before WeChat assigns a server ID. Enrich + // the identifier later without replacing the retained original content. + if (serverId && !existing.serverId) this.db.prepare("UPDATE messages SET serverId=? WHERE id=? AND serverId=''").run(serverId, id) + return this.get(id) + } + const mediaState = [3, 47].includes(Number(message.localType)) ? 'pending' : 'none' + if (existing) { + this.db.prepare(`UPDATE messages SET senderName=?,content=?,rawContent=?,createTime=?,localId=?,serverId=?,source=?,localType=?,isSend=?,originalMissing=0,mediaState=?,lastSeenAt=? WHERE id=?`).run( + message.senderDisplayName || message.senderUsername || '', message.parsedContent || '', message.rawContent || '', finite(message.createTime), localId, + serverId, source, finite(message.localType), message.isSend ? 1 : 0, mediaState, now, id) + } else { + this.db.prepare(`INSERT INTO messages + (id,sessionId,sessionName,senderName,content,rawContent,createTime,localId,serverId,source,localType,isSend,mediaState,capturedAt,lastSeenAt) + VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)`).run(id, sessionId, sessionName, message.senderDisplayName || message.senderUsername || '', + message.parsedContent || '', message.rawContent || '', finite(message.createTime), localId, serverId, source, finite(message.localType), message.isSend ? 1 : 0, mediaState, now, now) + } + return this.get(id) + } + get(id: string): RetainedMessage | null { const row = this.db.prepare('SELECT * FROM messages WHERE id=?').get(id); return row ? this.map(row) : null } + list(options: RetentionListOptions = {}): { items: RetainedMessage[]; total: number } { + const conditions: string[] = []; const args: string[] = [] + if (options.onlyRevoked) conditions.push('recalledAt IS NOT NULL') + const query = String(options.query || '').trim().slice(0, 300) + if (query) { conditions.push('(instr(content,?)>0 OR instr(sessionName,?)>0 OR instr(senderName,?)>0)'); args.push(query, query, query) } + const where = conditions.length ? ' WHERE ' + conditions.join(' AND ') : '' + const total = Number(this.db.prepare('SELECT COUNT(*) AS n FROM messages' + where).get(...args)?.n || 0) + const limit = Math.min(200, Math.max(1, Math.floor(finite(options.limit) || 50))) + const offset = Math.max(0, Math.floor(finite(options.offset))) + const rows = this.db.prepare('SELECT * FROM messages' + where + ' ORDER BY COALESCE(recalledAt,createTime*1000) DESC,id LIMIT ? OFFSET ?').all(...args, limit, offset) + return { items: rows.map(row => this.map(row)), total } + } + stats(): { totalMessages: number; revokedMessages: number; lastScanAt: number | null } { + const row = this.db.prepare('SELECT COUNT(*) AS total,SUM(recalledAt IS NOT NULL) AS revoked FROM messages').get() + const scan = this.db.prepare("SELECT value FROM metadata WHERE key='lastScanAt'").get() + return { totalMessages: Number(row?.total || 0), revokedMessages: Number(row?.revoked || 0), lastScanAt: scan ? Number(scan.value) : null } + } + markScanned(now = Date.now()): void { this.db.prepare("INSERT OR REPLACE INTO metadata(key,value) VALUES('lastScanAt',?)").run(String(now)) } + markMediaUnavailable(id: string): void { if (!this.closed) this.db.prepare("UPDATE messages SET mediaState='unavailable' WHERE id=? AND mediaState!='saved'").run(id) } + async preserveMedia(id: string, source: string): Promise { + if (this.closed) return undefined + const message = this.get(id) + if (!message || message.originalMissing) return undefined + if (message.mediaPath && existsSync(message.mediaPath)) return message.mediaPath + const dir = join(this.directory, 'media'); await fs.mkdir(dir, { recursive: true, mode: 0o700 }); await fs.chmod(dir, 0o700) + let suffix = '.bin'; let data: Buffer | undefined; let sourcePath: string | undefined + if (source.startsWith('data:')) { + const match = source.match(/^data:image\/(png|jpeg|gif|webp);base64,([\s\S]+)$/) + if (!match || match[2].length > 90_000_000) throw new Error('不支持的媒体内容') + suffix = '.' + (match[1] === 'jpeg' ? 'jpg' : match[1]); data = Buffer.from(match[2], 'base64') + if (!data.length || data.length > MAX_MEDIA_BYTES) throw new Error('媒体文件不可用或超过64MB') + } else { + sourcePath = retentionLocalMediaPath(source) + const info = await fs.stat(sourcePath) + if (!info.isFile() || !info.size || info.size > MAX_MEDIA_BYTES) throw new Error('媒体文件不可用或超过64MB') + const ext = extname(sourcePath).toLowerCase(); if (/^\.[a-z0-9]{1,8}$/.test(ext)) suffix = ext + } + const destination = join(dir, digest(id) + suffix) + const temp = destination + '.' + randomUUID() + '.tmp' + try { + if (this.closed) return undefined + if (data) await fs.writeFile(temp, data, { mode: 0o600 }) + else await fs.copyFile(sourcePath!, temp) + const copied = await fs.stat(temp) + if (copied.size > MAX_MEDIA_BYTES) throw new Error('媒体文件不可用或超过64MB') + await fs.chmod(temp, 0o600) + if (this.closed) return undefined + await fs.rename(temp, destination) + if (this.closed) { await fs.unlink(destination).catch(() => {}); return undefined } + this.db.prepare("UPDATE messages SET mediaPath=?,mediaState='saved' WHERE id=?").run(destination, id) + return destination + } finally { await fs.unlink(temp).catch(() => {}) } + } +} diff --git a/electron/services/snsRetentionService.ts b/electron/services/snsRetentionService.ts new file mode 100644 index 0000000..59514d3 --- /dev/null +++ b/electron/services/snsRetentionService.ts @@ -0,0 +1,245 @@ +import { app } from 'electron' +import { existsSync, promises as fs } from 'node:fs' +import { join, isAbsolute, relative, sep } from 'node:path' +import { fileURLToPath } from 'node:url' +import { ConfigService } from './config' +import { snsService, type SnsPost } from './snsService' +import { SnsRetentionStore, snsRetentionAccountHash, SNS_RETENTION_MAX_BYTES, type RetainedSnsPost, type SnsRetentionListOptions, type SnsMediaJob } from './snsRetentionStore' + +export type { RetainedSnsPost, RetainedSnsMedia, SnsRetentionListOptions } from './snsRetentionStore' +export interface SnsRetentionStatus { + enabled: boolean; running: boolean; totalPosts: number; totalSnapshots: number; savedMedia: number; pendingMedia: number; failedMedia: number + lastScanAt: number | null; baselineComplete: boolean; baselineOffset: number; error?: string; archivePath?: string +} +/** Never fetch arbitrary URLs embedded in a post or follow link-card destinations. */ +export function isTrustedSnsMediaUrl(value: string): boolean { + if (!value || value.length > 32768) return false + try { + const url = new URL(value); const host = url.hostname.toLowerCase() + return url.protocol === 'https:' && !url.username && !url.password && (!url.port || url.port === '443') && + (host === 'qpic.cn' || host.endsWith('.qpic.cn') || host === 'qlogo.cn' || host.endsWith('.qlogo.cn') || + host === 'video.qq.com' || host.endsWith('.video.qq.com') || host === 'wxapp.tc.qq.com' || host.endsWith('.wxapp.tc.qq.com') || + host === 'vweixinthumb.tc.qq.com' || host.endsWith('.vweixinthumb.tc.qq.com')) + } catch { return false } +} +export async function trustedSnsLocalPath(value: string, roots: string[]): Promise { + try { + const path = value.startsWith('file:') ? fileURLToPath(value) : value + if (!isAbsolute(path)) return null + const real = await fs.realpath(path) + for (const root of roots) { + const realRoot = await fs.realpath(root).catch(() => '') + if (!realRoot) continue + const child = relative(realRoot, real) + if (child && child !== '..' && !child.startsWith('..' + sep) && !isAbsolute(child)) return real + } + } catch { /* Missing files and symlinks outside cache roots are not retained. */ } + return null +} + +class SnsRetentionService { + private config: ConfigService | null = null + private store: SnsRetentionStore | null = null + private identity = '' + private generation = 0 + private started = false + private timer: ReturnType | null = null + private dueAt = 0 + private scanPromise: Promise | null = null + private wakeRequested = false + private lastRecentScan = 0 + private lastFullScan = 0 + private workers = 0 + private error: string | undefined + private cfg(): ConfigService { return this.config || (this.config = new ConfigService()) } + private enabled(): boolean { return Boolean(this.cfg().get('snsRetentionEnabled')) } + private accountIdentity(): string { + const account = String(this.cfg().get('myAccountId') || '').trim(); const path = String(this.cfg().get('dbPath') || '').trim() + const directory = account && path ? this.cfg().getAccountDir(path, account) : null + return directory ? account + '\0' + directory : '' + } + private ensureStore(create: boolean): SnsRetentionStore | null { + const identity = this.accountIdentity() + if (identity !== this.identity) { this.generation++; this.store?.close(); this.store = null; this.identity = identity; this.lastRecentScan = 0; this.lastFullScan = 0 } + if (!identity) return null + if (!this.store) { + const userData = app.getPath('userData') + if (!create && !existsSync(join(userData, 'SnsRetention', snsRetentionAccountHash(identity), 'posts.sqlite'))) return null + this.store = new SnsRetentionStore(userData, identity) + } + return this.store + } + private current(store: SnsRetentionStore, generation: number): boolean { + return this.started && this.enabled() && this.store === store && this.generation === generation && this.accountIdentity() === this.identity + } + start(): void { + if (!this.enabled() || this.started) return + this.started = true + try { + const store = this.ensureStore(true) + // Resume incomplete baselines; rescan completed baselines once per new start. + if (store?.stats().baselineComplete) store.setBaseline(0, false) + } catch { this.error = '无法打开朋友圈留存库,将自动重试' } + this.schedule(0) + } + stop(): void { + this.started = false; this.generation++; this.wakeRequested = false + if (this.timer) clearTimeout(this.timer) + this.timer = null; this.dueAt = 0; this.lastRecentScan = 0; this.lastFullScan = 0 + this.store?.close(); this.store = null + } + setEnabled(enabled: boolean): SnsRetentionStatus { + this.cfg().set('snsRetentionEnabled', Boolean(enabled)) + if (enabled) this.start(); else this.stop() + return this.status() + } + handleConfigChanged(key: string): void { + if (key === 'snsRetentionEnabled') { if (this.enabled()) this.start(); else this.stop(); return } + if (['myAccountId', 'dbPath', 'decryptKey', 'accountConfigs'].includes(key)) { + this.stop(); this.identity = ''; this.error = undefined; if (this.enabled()) this.start() + } + } + handleDbMonitorChange(type: string, json: string): void { + if (!this.started) return + try { + const event = JSON.parse(json || '{}') + if (/sns|moments/i.test(`${type} ${event.action || ''} ${event.table || ''} ${event.dbPath || ''}`)) { + // Restart only a completed sweep. Repeated events must not reset a sweep + // that is already progressing through older locally fetched posts. + if (this.store?.stats().baselineComplete) this.store.setBaseline(0, false) + this.lastRecentScan = 0; this.schedule(500) + } + } catch { /* The regular sweep remains active for unknown monitor protocols. */ } + } + captureVisible(posts: SnsPost[]): void { + if (!this.enabled()) return + try { + const store = this.ensureStore(true) + if (store && Array.isArray(posts)) { store.capturePosts(posts); this.kickMedia() } + } catch { this.error = '朋友圈已读取,但本地留存写入失败;原浏览功能不受影响' } + } + status(): SnsRetentionStatus { + const base: SnsRetentionStatus = { enabled: this.enabled(), running: Boolean(this.scanPromise) && this.enabled(), totalPosts: 0, totalSnapshots: 0, + savedMedia: 0, pendingMedia: 0, failedMedia: 0, lastScanAt: null, baselineComplete: false, baselineOffset: 0 } + try { const store = this.ensureStore(false); return { ...base, ...store?.stats(), archivePath: store?.directory, error: this.error } } + catch { return { ...base, running: false, error: '无法读取朋友圈留存库' } } + } + list(options: SnsRetentionListOptions = {}): { items: RetainedSnsPost[]; total: number } { + const store = this.ensureStore(false) + const result = store?.list(options) || { items: [], total: 0 } + if (store && this.enabled()) { + store.prioritizePostIds(result.items.map(post => post.id)) + this.kickMedia() + } + return result + } + async scanNow(): Promise { + if (!this.enabled()) return this.status() + if (!this.started) this.start() + if (this.scanPromise) await this.scanPromise + const store = this.ensureStore(true); store?.setBaseline(0, false); store?.retryFailed() + this.lastRecentScan = 0 + await this.runScan() + return this.status() + } + private schedule(delay: number): void { + if (!this.started) return + if (this.scanPromise) { this.wakeRequested = true; return } + const due = Date.now() + delay + if (this.timer && this.dueAt <= due) return + if (this.timer) clearTimeout(this.timer) + this.dueAt = due + this.timer = setTimeout(() => { this.timer = null; this.dueAt = 0; void this.runScan() }, delay) + this.timer.unref?.() + } + private runScan(): Promise { + if (this.scanPromise) return this.scanPromise + if (!this.started || !this.enabled()) return Promise.resolve() + this.wakeRequested = false + this.scanPromise = this.scan().finally(() => { + this.scanPromise = null + if (this.started && this.enabled()) this.schedule(this.wakeRequested ? 500 : this.store?.stats().baselineComplete ? 30_000 : 5000) + }) + return this.scanPromise + } + private async scan(): Promise { + try { + const store = this.ensureStore(true); if (!store) { this.error = '请先连接微信数据库并选择账号'; return } + const generation = this.generation + if (Date.now() - this.lastRecentScan >= 30_000) { + const recent = await snsService.getTimeline(100, 0) + if (!this.current(store, generation)) return + if (!recent.success || !recent.timeline) { this.error = '本机朋友圈数据库暂不可读,将自动重试'; return } + store.capturePosts(recent.timeline); this.lastRecentScan = Date.now(); this.kickMedia() + } + let state = store.stats() + // Some backends do not emit SNS notifications. Periodic full sweeps also + // discover historical posts fetched into the local cache below the top 100. + if (state.baselineComplete && Date.now() - this.lastFullScan >= 30 * 60_000) { + store.setBaseline(0, false); state = store.stats() + } + if (!state.baselineComplete) { + const page = await snsService.getTimeline(100, state.baselineOffset) + if (!this.current(store, generation)) return + if (!page.success || !page.timeline) { this.error = '历史朋友圈分页暂不可读,将从当前位置重试'; return } + store.capturePosts(page.timeline) + // Small overlap absorbs new rows inserted while the baseline is paged. + store.setBaseline(state.baselineOffset + Math.max(1, page.timeline.length - 10), page.timeline.length < 100) + if (page.timeline.length < 100) this.lastFullScan = Date.now() + } + store.markScanned(); this.error = undefined; this.kickMedia() + } catch { this.error = '朋友圈留存扫描暂时失败,将自动重试' } + } + private cacheRoots(store: SnsRetentionStore): string[] { + const cache = String(this.cfg().get('cachePath') || '').trim() || join(app.getPath('documents'), 'WeFlow') + return [join(cache, 'sns_cache'), store.directory] + } + private async obtain(source: string, key: string, store: SnsRetentionStore, kind: 'image' | 'video'): Promise<{ data?: Buffer; path?: string }> { + const local = source.startsWith('file:') || isAbsolute(source) + if (local) { + const path = await trustedSnsLocalPath(source, this.cacheRoots(store)) + if (!path) throw new Error('blocked') + return { path } + } + if (!isTrustedSnsMediaUrl(source)) throw new Error('blocked') + // The SNS downloader rejects redirects, caps decoded bytes and enforces a deadline. + const result = await snsService.downloadImage(source, key || undefined, { maxBytes: SNS_RETENTION_MAX_BYTES, timeoutMs: 30_000, kind }) + if (!result.success) throw new Error('unavailable') + if (result.cachePath) { + const path = await trustedSnsLocalPath(result.cachePath, this.cacheRoots(store)) + if (path) return { path } + } + if (result.data && result.data.length <= SNS_RETENTION_MAX_BYTES) return { data: result.data } + throw new Error('unavailable') + } + private kickMedia(): void { + if (!this.started || !this.enabled()) return + const available = Math.max(0, 2 - this.workers) + for (let index = 0; index < available; index++) { this.workers++; void this.drainMedia() } + } + private async drainMedia(): Promise { + try { + while (this.started && this.enabled()) { + const store = this.store; if (!store) break + const generation = this.generation; const job: SnsMediaJob | null = store.nextMedia(); if (!job) break + let blocked = false + try { + const original = await this.obtain(job.sourceUrl, job.key, store, job.kind === 'image' ? 'image' : 'video') + if (!this.current(store, generation)) continue + await store.saveMedia(job.id, original, 'original') + continue + } catch (error) { blocked = error instanceof Error && error.message === 'blocked' } + if (!this.current(store, generation)) continue + store.deferMedia(job.id, blocked ? '来源不在微信媒体白名单或本地缓存范围内' : '原媒体暂不可读取,稍后可重试', blocked) + if (job.thumbUrl && job.thumbUrl !== job.sourceUrl) { + try { + const thumbnail = await this.obtain(job.thumbUrl, job.key, store, 'image') + if (this.current(store, generation)) await store.saveMedia(job.id, thumbnail, 'thumbnail') + } catch { /* Text, snapshots and any previously retained media stay intact. */ } + } + } + } catch { this.error = '部分朋友圈媒体暂不可留存,文字与已保存副本仍保留' } + finally { this.workers-- } + } +} +export const snsRetentionService = new SnsRetentionService() diff --git a/electron/services/snsRetentionStore.ts b/electron/services/snsRetentionStore.ts new file mode 100644 index 0000000..2b394b8 --- /dev/null +++ b/electron/services/snsRetentionStore.ts @@ -0,0 +1,238 @@ +import { DatabaseSync } from 'node:sqlite' +import { createHash, randomUUID } from 'node:crypto' +import { mkdirSync, chmodSync, promises as fs } from 'node:fs' +import { join } from 'node:path' +import type { SnsPost } from './snsService' + +export const SNS_RETENTION_MAX_BYTES = 128 * 1024 * 1024 +export const snsRetentionAccountHash = (value: string): string => createHash('sha256').update(value).digest('hex') +const hash = snsRetentionAccountHash +export interface RetainedSnsMedia { + id: string; mediaIndex: number; kind: 'image' | 'video' | 'livephoto'; localPath?: string; thumbnailPath?: string + status: 'pending' | 'saved' | 'failed' | 'blocked'; quality: 'original' | 'thumbnail' | 'none' + contentType?: string; bytes?: number; error?: string +} +export interface RetainedSnsPost { + id: string; username: string; nickname: string; createTime: number; contentDesc: string; type?: number + likes: string[]; comments: SnsPost['comments']; location?: SnsPost['location']; linkTitle?: string; linkUrl?: string + capturedAt: number; lastSeenAt: number; snapshotCount: number; media: RetainedSnsMedia[] +} +export interface SnsRetentionListOptions { query?: string; username?: string; limit?: number; offset?: number } +export interface SnsMediaJob extends RetainedSnsMedia { + postId: string; sourceUrl: string; thumbUrl: string; key: string; attempts: number +} +const finite = (value: unknown): number => Number.isFinite(Number(value)) ? Number(value) : 0 +const stringId = (value: unknown): string => typeof value === 'string' ? value.trim() + : typeof value === 'bigint' ? value.toString() : typeof value === 'number' && Number.isSafeInteger(value) ? String(value) : '' +const json = (value: unknown): string => JSON.stringify(value) +function stable(value: unknown): string { + if (Array.isArray(value)) return '[' + value.map(stable).join(',') + ']' + if (value && typeof value === 'object') return '{' + Object.keys(value).sort().filter(key => (value as Record)[key] !== undefined) + .map(key => json(key) + ':' + stable((value as Record)[key])).join(',') + '}' + return json(value) || 'null' +} +function stableUrl(value: string): string { + try { const url = new URL(value); url.searchParams.delete('token'); url.searchParams.delete('idx'); return url.toString() } catch { return value } +} +function mediaKind(post: SnsPost, url: string): 'image' | 'video' { + if (post.type === 15) return 'video' + try { + const pathname = new URL(url).pathname + if (/\.(?:mp4|m4v|mov)$/i.test(pathname) || /(?:^|\/)(?:video|snsvideo)(?:\/|$)/i.test(pathname)) return 'video' + } catch { /* A local cache path is classified by its extension below. */ } + return /\.(?:mp4|m4v|mov)$/i.test(url) ? 'video' : 'image' +} +function normalized(post: SnsPost): SnsPost | null { + const id = stringId(post.id) + if (!id) return null + return { ...post, id, tid: stringId(post.tid) || undefined, username: String(post.username || ''), nickname: String(post.nickname || ''), + createTime: finite(post.createTime), contentDesc: String(post.contentDesc || ''), + media: Array.isArray(post.media) ? post.media : [], likes: Array.isArray(post.likes) ? post.likes.map(String) : [], + comments: Array.isArray(post.comments) ? post.comments.map(comment => ({ ...comment, id: stringId(comment.id), refCommentId: stringId(comment.refCommentId), nickname: String(comment.nickname || ''), content: String(comment.content || '') })) : [] } +} +function mergePosts(previous: SnsPost, observed: SnsPost): SnsPost { + const comments = new Map(previous.comments.map(comment => [comment.id || hash(stable(comment)), comment])) + for (const comment of observed.comments) { + const key = comment.id || hash(stable(comment)); const old = comments.get(key) + comments.set(key, { ...old, ...comment, content: comment.content || old?.content || '' }) + } + // The current archive is a non-destructive view; exact observations remain in snapshots. + return { ...previous, ...observed, contentDesc: observed.contentDesc || previous.contentDesc, + createTime: observed.createTime || previous.createTime, type: observed.type ?? previous.type, + nickname: observed.nickname || previous.nickname, username: observed.username || previous.username, + rawXml: observed.rawXml || previous.rawXml, linkTitle: observed.linkTitle || previous.linkTitle, + linkUrl: observed.linkUrl || previous.linkUrl, location: observed.location || previous.location, + media: observed.media.length ? observed.media : previous.media, + likes: [...new Set([...previous.likes, ...observed.likes])], comments: [...comments.values()] } +} +export function snsMediaSignature(header: Buffer): { contentType: string; extension: string } | null { + if (header.length >= 3 && header[0] === 255 && header[1] === 216 && header[2] === 255) return { contentType: 'image/jpeg', extension: '.jpg' } + if (header.subarray(0, 8).equals(Buffer.from('89504e470d0a1a0a', 'hex'))) return { contentType: 'image/png', extension: '.png' } + if (/^GIF8[79]a/.test(header.subarray(0, 6).toString('ascii'))) return { contentType: 'image/gif', extension: '.gif' } + if (header.length >= 2 && header[0] === 0x42 && header[1] === 0x4d) return { contentType: 'image/bmp', extension: '.bmp' } + if (header.subarray(0, 4).toString() === 'RIFF' && header.subarray(8, 12).toString() === 'WEBP') return { contentType: 'image/webp', extension: '.webp' } + if (header.length >= 12 && header.subarray(4, 8).toString() === 'ftyp') { + const brand = header.subarray(8, 12).toString() + if (['avif', 'avis'].includes(brand)) return { contentType: 'image/avif', extension: '.avif' } + if (['heic', 'heix', 'hevc', 'hevx', 'mif1', 'msf1'].includes(brand)) return { contentType: 'image/heic', extension: '.heic' } + if (['isom', 'iso2', 'mp41', 'mp42', 'M4V ', 'qt ', 'avc1', 'dash', 'msdh', 'MSNV'].includes(brand)) return { contentType: brand === 'qt ' ? 'video/quicktime' : 'video/mp4', extension: brand === 'qt ' ? '.mov' : '.mp4' } + } + return null +} + +/** Persistent independent copies of visible Moments data. There is no TTL or source deletion propagation. */ +export class SnsRetentionStore { + readonly directory: string + readonly archivePath: string + private db: DatabaseSync + private closed = false + constructor(userData: string, identity: string) { + if (!identity.trim()) throw new Error('未选择朋友圈留存账号') + const root = join(userData, 'SnsRetention') + this.directory = join(root, hash(identity)) + mkdirSync(root, { recursive: true, mode: 0o700 }); chmodSync(root, 0o700) + mkdirSync(this.directory, { recursive: true, mode: 0o700 }); chmodSync(this.directory, 0o700) + this.archivePath = join(this.directory, 'posts.sqlite') + this.db = new DatabaseSync(this.archivePath); chmodSync(this.archivePath, 0o600) + this.db.exec(`PRAGMA journal_mode=DELETE; PRAGMA synchronous=FULL; PRAGMA busy_timeout=3000; + CREATE TABLE IF NOT EXISTS posts(id TEXT PRIMARY KEY, username TEXT NOT NULL,nickname TEXT NOT NULL,contentDesc TEXT NOT NULL, + createTime INTEGER NOT NULL,mergedJson TEXT NOT NULL,capturedAt INTEGER NOT NULL,lastSeenAt INTEGER NOT NULL); + CREATE TABLE IF NOT EXISTS snapshots(postId TEXT NOT NULL,hash TEXT NOT NULL,json TEXT NOT NULL,observedAt INTEGER NOT NULL,PRIMARY KEY(postId,hash)); + CREATE TABLE IF NOT EXISTS media(id TEXT PRIMARY KEY,postId TEXT NOT NULL,mediaIndex INTEGER NOT NULL,kind TEXT NOT NULL, + sourceUrl TEXT NOT NULL,thumbUrl TEXT NOT NULL,key TEXT NOT NULL,localPath TEXT,thumbnailPath TEXT,status TEXT NOT NULL DEFAULT 'pending', + quality TEXT NOT NULL DEFAULT 'none',contentType TEXT,bytes INTEGER,error TEXT,attempts INTEGER NOT NULL DEFAULT 0,retryAfter INTEGER NOT NULL DEFAULT 0,priorityUntil INTEGER NOT NULL DEFAULT 0); + CREATE INDEX IF NOT EXISTS sns_post_time ON posts(createTime DESC); + CREATE INDEX IF NOT EXISTS sns_media_post ON media(postId); + CREATE TABLE IF NOT EXISTS metadata(key TEXT PRIMARY KEY,value TEXT NOT NULL);`) + if (!this.db.prepare('PRAGMA table_info(media)').all().some(column => column.name === 'priorityUntil')) { + this.db.exec('ALTER TABLE media ADD COLUMN priorityUntil INTEGER NOT NULL DEFAULT 0') + } + } + close(): void { if (!this.closed) { this.closed = true; this.db.close() } } + capturePosts(input: SnsPost[], now = Date.now()): void { + if (this.closed || !input.length) return + this.db.exec('BEGIN IMMEDIATE') + try { + for (const value of input) { + const post = normalized(value); if (!post) continue + const previous = this.db.prepare('SELECT mergedJson FROM posts WHERE id=?').get(post.id) + const merged = previous ? mergePosts(JSON.parse(String(previous.mergedJson)), post) : post + this.db.prepare(`INSERT INTO posts(id,username,nickname,contentDesc,createTime,mergedJson,capturedAt,lastSeenAt) VALUES(?,?,?,?,?,?,?,?) + ON CONFLICT(id) DO UPDATE SET username=excluded.username,nickname=excluded.nickname,contentDesc=excluded.contentDesc,createTime=excluded.createTime,mergedJson=excluded.mergedJson,lastSeenAt=excluded.lastSeenAt`).run( + post.id, merged.username, merged.nickname, merged.contentDesc, merged.createTime, json(merged), now, now) + this.db.prepare('INSERT OR IGNORE INTO snapshots(postId,hash,json,observedAt) VALUES(?,?,?,?)').run(post.id, hash(stable(post)), json(post), now) + post.media.forEach((media, index) => { + this.captureMedia(post, index, mediaKind(post, media.url || ''), media) + if (media.livePhoto) this.captureMedia(post, index, 'livephoto', media.livePhoto) + }) + } + this.db.exec('COMMIT') + } catch (error) { this.db.exec('ROLLBACK'); throw error } + } + private captureMedia(post: SnsPost, mediaIndex: number, kind: RetainedSnsMedia['kind'], input: { url: string; thumb: string; key?: string; md5?: string }): void { + const sourceUrl = String(input.url || ''); const thumbUrl = String(input.thumb || '') + if (!sourceUrl && !thumbUrl) return + const key = typeof input.key === 'string' ? input.key : typeof input.key === 'number' && Number.isSafeInteger(input.key) ? String(input.key) : '' + const id = hash(post.id + '\0' + kind + '\0' + (input.md5 || stableUrl(sourceUrl || thumbUrl))) + const old = this.db.prepare('SELECT * FROM media WHERE id=?').get(id) + if (!old) { + this.db.prepare('INSERT INTO media(id,postId,mediaIndex,kind,sourceUrl,thumbUrl,key) VALUES(?,?,?,?,?,?,?)').run(id, post.id, mediaIndex, kind, sourceUrl, thumbUrl, key) + } else { + const changed = (sourceUrl && sourceUrl !== old.sourceUrl) || (thumbUrl && thumbUrl !== old.thumbUrl) || (key && key !== old.key) + const saved = old.status === 'saved' && old.quality === 'original' + this.db.prepare('UPDATE media SET sourceUrl=?,thumbUrl=?,key=?,mediaIndex=? WHERE id=?').run(sourceUrl || String(old.sourceUrl), thumbUrl || String(old.thumbUrl), key || String(old.key), mediaIndex, id) + if (changed && !saved) this.db.prepare("UPDATE media SET status='pending',attempts=0,retryAfter=0,error=NULL WHERE id=?").run(id) + } + } + private mediaRow(row: Record): RetainedSnsMedia { + return { id: String(row.id), mediaIndex: finite(row.mediaIndex), kind: String(row.kind) as RetainedSnsMedia['kind'], + localPath: row.localPath ? String(row.localPath) : undefined, thumbnailPath: row.thumbnailPath ? String(row.thumbnailPath) : undefined, + status: String(row.status) as RetainedSnsMedia['status'], quality: String(row.quality) as RetainedSnsMedia['quality'], + contentType: row.contentType ? String(row.contentType) : undefined, bytes: row.bytes ? finite(row.bytes) : undefined, error: row.error ? String(row.error) : undefined } + } + list(options: SnsRetentionListOptions = {}): { items: RetainedSnsPost[]; total: number } { + const clauses: string[] = []; const args: string[] = [] + if (options.username) { clauses.push('username=?'); args.push(options.username) } + const query = String(options.query || '').trim().slice(0, 300) + if (query) { clauses.push('(instr(contentDesc,?)>0 OR instr(nickname,?)>0)'); args.push(query, query) } + const where = clauses.length ? ' WHERE ' + clauses.join(' AND ') : '' + const total = Number(this.db.prepare('SELECT COUNT(*) AS n FROM posts' + where).get(...args)?.n || 0) + const rows = this.db.prepare('SELECT * FROM posts' + where + ' ORDER BY createTime DESC,id LIMIT ? OFFSET ?').all(...args, + Math.max(1, Math.min(100, Math.floor(finite(options.limit) || 30))), Math.max(0, Math.floor(finite(options.offset)))) + return { total, items: rows.map(row => { + const post: SnsPost = JSON.parse(String(row.mergedJson)) + return { id: String(row.id), username: post.username, nickname: post.nickname, createTime: post.createTime, + contentDesc: post.contentDesc, type: post.type, likes: post.likes, comments: post.comments, location: post.location, + linkTitle: post.linkTitle, linkUrl: post.linkUrl, capturedAt: finite(row.capturedAt), lastSeenAt: finite(row.lastSeenAt), + snapshotCount: Number(this.db.prepare('SELECT COUNT(*) AS n FROM snapshots WHERE postId=?').get(String(row.id))?.n || 0), + media: this.db.prepare('SELECT * FROM media WHERE postId=? ORDER BY mediaIndex,kind').all(String(row.id)).map(media => this.mediaRow(media)) } + }) } + } + stats(): { totalPosts: number; totalSnapshots: number; savedMedia: number; pendingMedia: number; failedMedia: number; lastScanAt: number | null; baselineComplete: boolean; baselineOffset: number } { + const media = this.db.prepare("SELECT SUM(status='saved' AND quality='original') AS saved,SUM(status='pending') AS pending,SUM(status IN('failed','blocked') OR quality='thumbnail') AS failed FROM media").get() + return { totalPosts: Number(this.db.prepare('SELECT COUNT(*) AS n FROM posts').get()?.n || 0), + totalSnapshots: Number(this.db.prepare('SELECT COUNT(*) AS n FROM snapshots').get()?.n || 0), + savedMedia: Number(media?.saved || 0), pendingMedia: Number(media?.pending || 0), failedMedia: Number(media?.failed || 0), + lastScanAt: Number(this.meta('lastScanAt') || 0) || null, baselineComplete: this.meta('baselineComplete') === '1', baselineOffset: Number(this.meta('baselineOffset') || 0) } + } + private meta(key: string): string { return String(this.db.prepare('SELECT value FROM metadata WHERE key=?').get(key)?.value || '') } + private setMeta(key: string, value: string): void { this.db.prepare('INSERT OR REPLACE INTO metadata(key,value) VALUES(?,?)').run(key, value) } + markScanned(now = Date.now()): void { if (!this.closed) this.setMeta('lastScanAt', String(now)) } + setBaseline(offset: number, complete: boolean): void { + if (!this.closed) { this.setMeta('baselineOffset', String(offset)); this.setMeta('baselineComplete', complete ? '1' : '0') } + } + retryFailed(): void { if (!this.closed) this.db.prepare("UPDATE media SET status='pending',attempts=0,retryAfter=0 WHERE status='failed' OR quality='thumbnail'").run() } + prioritizePostIds(ids: string[], now = Date.now()): void { + if (this.closed || !ids.length) return + this.db.exec('BEGIN IMMEDIATE') + try { + const statement = this.db.prepare("UPDATE media SET priorityUntil=MAX(priorityUntil,?) WHERE postId=? AND status='pending'") + for (const id of new Set(ids.slice(0, 100))) statement.run(now + 60_000, id) + this.db.exec('COMMIT') + } catch (error) { this.db.exec('ROLLBACK'); throw error } + } + nextMedia(now = Date.now()): SnsMediaJob | null { + if (this.closed) return null + const row = this.db.prepare(`SELECT m.* FROM media m JOIN posts p ON p.id=m.postId + WHERE (m.status='pending' OR (m.status='failed' OR m.quality='thumbnail') AND m.attempts<3) AND m.retryAfter<=? + ORDER BY (m.priorityUntil>?) DESC,p.createTime DESC,m.mediaIndex,m.kind LIMIT 1`).get(now, now) + if (row) this.db.prepare('UPDATE media SET retryAfter=? WHERE id=?').run(now + 60_000, String(row.id)) + return row ? { ...this.mediaRow(row), postId: String(row.postId), sourceUrl: String(row.sourceUrl), thumbUrl: String(row.thumbUrl), key: String(row.key), attempts: finite(row.attempts) } : null + } + deferMedia(id: string, error: string, blocked = false, now = Date.now()): void { + if (this.closed) return + // Preserve an existing thumbnail even when an upgrade attempt fails. + this.db.prepare(`UPDATE media SET status=CASE WHEN quality='thumbnail' THEN 'saved' ELSE ? END,error=?,attempts=CASE WHEN ? THEN 3 ELSE attempts+1 END,retryAfter=? WHERE id=? AND quality!='original'`).run( + blocked ? 'blocked' : 'failed', error.slice(0, 180), blocked ? 1 : 0, now + 5 * 60_000, id) + } + async saveMedia(id: string, input: { data?: Buffer; path?: string }, quality: 'original' | 'thumbnail'): Promise { + if (this.closed) return + const row = this.db.prepare('SELECT * FROM media WHERE id=?').get(id) + if (!row || (row.quality === 'original' && row.status === 'saved')) return + let header: Buffer; let bytes: number + if (input.path) { + const handle = await fs.open(input.path, 'r') + try { const info = await handle.stat(); bytes = info.size; header = Buffer.alloc(Math.min(32, bytes)); await handle.read(header, 0, header.length, 0) } finally { await handle.close() } + } else { bytes = input.data?.length || 0; header = input.data?.subarray(0, 32) || Buffer.alloc(0) } + if (bytes <= 0 || bytes > SNS_RETENTION_MAX_BYTES) throw new Error('媒体为空或超过128MiB留存上限') + const signature = snsMediaSignature(header) + if (!signature || (quality === 'thumbnail' && !signature.contentType.startsWith('image/')) || + (quality === 'original' && row.kind === 'image' && !signature.contentType.startsWith('image/')) || + (quality === 'original' && row.kind !== 'image' && !signature.contentType.startsWith('video/'))) throw new Error('媒体格式验证失败') + const directory = join(this.directory, 'media'); await fs.mkdir(directory, { recursive: true, mode: 0o700 }); await fs.chmod(directory, 0o700) + const destination = join(directory, id + '-' + quality + signature.extension) + const temporary = destination + '.' + randomUUID() + '.tmp' + try { + if (input.path) await fs.copyFile(input.path, temporary); else await fs.writeFile(temporary, input.data!, { mode: 0o600 }) + const copied = await fs.stat(temporary); if (copied.size > SNS_RETENTION_MAX_BYTES) throw new Error('媒体超过128MiB留存上限') + await fs.chmod(temporary, 0o600) + if (this.closed) return + await fs.rename(temporary, destination) + if (this.closed) return + this.db.prepare(`UPDATE media SET localPath=?,thumbnailPath=CASE WHEN ?='thumbnail' THEN ? ELSE thumbnailPath END, + status='saved',quality=?,contentType=?,bytes=?,error=?,retryAfter=? WHERE id=? AND quality!='original'`).run( + destination, quality, destination, quality, signature.contentType, copied.size, + quality === 'thumbnail' ? '原媒体暂不可用,仅保留缩略图' : null, quality === 'thumbnail' ? Date.now() + 5 * 60_000 : 0, id) + } finally { await fs.unlink(temporary).catch(() => {}) } + } +} diff --git a/electron/services/snsService.ts b/electron/services/snsService.ts index 61cba8a..ebabd79 100644 --- a/electron/services/snsService.ts +++ b/electron/services/snsService.ts @@ -3,7 +3,7 @@ import { ConfigService } from './config' import { ContactCacheService, buildStableAccountCacheScope } from './contactCacheService' import { app } from 'electron' import { existsSync, mkdirSync, unlinkSync, renameSync } from 'fs' -import { readFile, writeFile, mkdir } from 'fs/promises' +import { readFile, writeFile, mkdir, open } from 'fs/promises' import { basename, join } from 'path' import crypto from 'crypto' import { WasmService } from './wasmService' @@ -215,8 +215,24 @@ import { Isaac64 } from './isaac64' const extractVideoKey = (xml: string): string | undefined => { if (!xml) return undefined // 匹配 或 - const match = xml.match(/]*\bkey\s*=\s*["'](\d+)["']/i) + return match && !/^0+$/.test(match[1]) ? match[1] : undefined +} + +// The database fallback may label a video as type 1. Use the content object's +// own type, never a nested media/action type or the hostname of a thumbnail. +const snsXmlContentType = (xml: string): number | undefined => { + const content = xml.match(/]*>([\s\S]*?)<\/ContentObject>/i)?.[1] + if (!content) return undefined + const withoutMedia = content.replace(/]*>[\s\S]*?<\/mediaList>/gi, '') + const token = withoutMedia.match(/<(?:contentStyle|type)\b[^>]*>\s*(?:)?\s*<\/(?:contentStyle|type)>/i)?.[1] + return token === undefined ? undefined : Number(token) +} +const preciseSnsVideoUrl = (value: string): boolean => { + try { + const pathname = new URL(value).pathname + return /\.(?:mp4|m4v|mov)$/i.test(pathname) || /(?:^|\/)(?:video|snsvideo)(?:\/|$)/i.test(pathname) + } catch { return false } } /** @@ -355,6 +371,78 @@ const resolveSnsLocationText = (location?: SnsLocation): string => { return primary || region } + +export interface SnsDownloadOptions { maxBytes?: number; timeoutMs?: number; kind?: 'image' | 'video' } +const snsDownloadLimits = (options: SnsDownloadOptions = {}) => ({ + // Existing interactive/export callers keep their size policy and 15-second + // idle timeout. The retention worker explicitly opts into both hard limits. + maxBytes: Number.isSafeInteger(options.maxBytes) && options.maxBytes! > 0 ? options.maxBytes! : Infinity, + timeoutMs: Number.isSafeInteger(options.timeoutMs) && options.timeoutMs! > 0 ? options.timeoutMs! : undefined +}) +const validSnsVideo = (data: Buffer): boolean => data.length >= 12 && + data.subarray(4, 8).toString('ascii') === 'ftyp' && detectImageMime(data, '') === 'video/mp4' + +/** HTTPS download with opt-in byte/deadline limits; redirects are never followed. */ +export function fetchSnsMediaBounded(url: string, options: SnsDownloadOptions = {}): Promise<{ data: Buffer; contentType: string; encrypted: boolean }> { + const { maxBytes, timeoutMs } = snsDownloadLimits(options) + return new Promise((resolve, reject) => { + let req: any; let response: any; let body: any; let settled = false + const chunks: Buffer[] = []; let total = 0 + const finish = (error?: Error, value?: { data: Buffer; contentType: string; encrypted: boolean }) => { + if (settled) return + settled = true; clearTimeout(timer) + if (error) { + chunks.length = 0 + body?.destroy(); response?.destroy(); req?.destroy() + reject(error) + } else resolve(value!) + } + // This deadline is absolute, so trickling bytes cannot keep a download alive indefinitely. + const timer = timeoutMs === undefined ? undefined + : setTimeout(() => finish(new Error('SNS_MEDIA_TIMEOUT: 媒体下载超时')), timeoutMs) + try { + const parsed = new URL(url) + if (parsed.protocol !== 'https:' || parsed.username || parsed.password || (parsed.port && parsed.port !== '443')) { + throw new Error('SNS_MEDIA_URL: 仅支持标准 HTTPS 媒体地址') + } + req = require('https').request({ hostname: parsed.hostname, path: parsed.pathname + parsed.search, method: 'GET', + headers: { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36', 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate, br', 'Accept-Language': 'zh-CN,zh;q=0.9' } + }, (res: any) => { + response = res + if (settled) { res.destroy(); return } + if (res.statusCode !== 200 && res.statusCode !== 206) { + const error = new Error(`HTTP ${res.statusCode}`) as Error & { status?: number } + error.status = res.statusCode; finish(error); return + } + const length = Number(res.headers['content-length']) + if (Number.isFinite(length) && length > maxBytes) { + finish(new Error('SNS_MEDIA_TOO_LARGE: 媒体超过大小限制')); return + } + const encoding = String(res.headers['content-encoding'] || '').toLowerCase() + body = encoding === 'gzip' ? res.pipe(zlib.createGunzip()) : encoding === 'deflate' ? res.pipe(zlib.createInflate()) + : encoding === 'br' ? res.pipe(zlib.createBrotliDecompress()) : res + res.on('error', () => finish(new Error('SNS_MEDIA_NETWORK: 媒体传输失败'))) + res.on('aborted', () => finish(new Error('SNS_MEDIA_NETWORK: 媒体传输中断'))) + body.on('error', () => finish(new Error('SNS_MEDIA_NETWORK: 媒体解码或传输失败'))) + body.on('data', (chunk: Buffer) => { + if (settled) return + total += chunk.length + // Count decoded bytes, not only Content-Length (which may be compressed or absent). + if (total > maxBytes) { finish(new Error('SNS_MEDIA_TOO_LARGE: 媒体超过大小限制')); return } + chunks.push(chunk) + }) + body.on('end', () => { + if (!settled) finish(undefined, { data: Buffer.concat(chunks, total), + contentType: String(res.headers['content-type'] || ''), encrypted: String(res.headers['x-enc'] || '') === '1' }) + }) + }) + req.on('error', () => finish(new Error('SNS_MEDIA_NETWORK: 媒体请求失败'))) + req.setTimeout(15_000, () => finish(new Error('SNS_MEDIA_TIMEOUT: 媒体下载超时'))) + req.end() + } catch (error) { finish(error instanceof Error ? error : new Error('SNS_MEDIA_NETWORK: 媒体请求失败')) } + }) +} + class SnsService { private configService: ConfigService private runtimeConfig?: { dbPath?: string; myAccountId?: string } @@ -948,9 +1036,9 @@ class SnsService { } } - private getCacheFilePath(url: string): string { + private getCacheFilePath(url: string, kind?: 'image' | 'video'): string { const hash = crypto.createHash('md5').update(this.normalizeCacheUrl(url)).digest('hex') - const ext = isVideoUrl(url) ? '.mp4' : '.jpg' + const ext = (kind ? kind === 'video' : isVideoUrl(url)) ? '.mp4' : '.jpg' return join(this.getSnsCacheDir(), `${hash}${ext}`) } @@ -959,11 +1047,11 @@ class SnsService { * 改为规范化 URL 命名后会全部失配。这里按需将当前 URL 对应的旧命名文件 * 重命名为新命名,仅针对单个 URL、不遍历目录,迁移一次后不再触发,避免反复扫描。 */ - private migrateLegacyCacheFile(url: string, newPath: string): void { + private migrateLegacyCacheFile(url: string, newPath: string, kind?: 'image' | 'video'): void { try { if (existsSync(newPath)) return const legacyHash = crypto.createHash('md5').update(url).digest('hex') - const ext = isVideoUrl(url) ? '.mp4' : '.jpg' + const ext = (kind ? kind === 'video' : isVideoUrl(url)) ? '.mp4' : '.jpg' const legacyPath = join(this.getSnsCacheDir(), `${legacyHash}${ext}`) if (legacyPath !== newPath && existsSync(legacyPath)) { renameSync(legacyPath, newPath) @@ -1310,33 +1398,35 @@ class SnsService { const enrichedTimeline = result.timeline.map((post: any) => { const contact = this.contactCache.get(contactCacheScope, post.username) - const isVideoPost = post.type === 15 const rawXml = post.rawXml || '' + const isVideoPost = post.type === 15 || snsXmlContentType(rawXml) === 15 const videoKey = extractVideoKey(rawXml) const location = this.mergeLocation( this.normalizeLocation((post as { location?: unknown }).location), this.parseLocationFromXml(rawXml) ) - const fixedMedia = (post.media || []).map((m: any) => ({ - url: fixSnsUrl(m.url, m.token, isVideoPost), - thumb: fixSnsUrl(m.thumb, m.thumbToken || m.token, false), - md5: m.md5, - token: m.token, - thumbToken: m.thumbToken, - key: isVideoPost ? (videoKey || m.key) : m.key, - encIdx: m.encIdx || m.enc_idx, - livePhoto: m.livePhoto ? { - ...m.livePhoto, - url: fixSnsUrl(m.livePhoto.url, m.livePhoto.token, true), - thumb: fixSnsUrl(m.livePhoto.thumb, m.livePhoto.thumbToken || m.livePhoto.token, false), - token: m.livePhoto.token, - thumbToken: m.livePhoto.thumbToken, - key: videoKey || m.livePhoto.key || m.key, - encIdx: m.livePhoto.encIdx || m.livePhoto.enc_idx - } : undefined - })) - + const fixedMedia = (post.media || []).map((m: any) => { + const isVideoMedia = isVideoPost || preciseSnsVideoUrl(m.url || '') + return { + url: fixSnsUrl(m.url, m.token, isVideoMedia), + thumb: fixSnsUrl(m.thumb, m.thumbToken || m.token, false), + md5: m.md5, + token: m.token, + thumbToken: m.thumbToken, + key: isVideoMedia ? (videoKey || m.key) : m.key, + encIdx: m.encIdx || m.enc_idx, + livePhoto: m.livePhoto ? { + ...m.livePhoto, + url: fixSnsUrl(m.livePhoto.url, m.livePhoto.token, true), + thumb: fixSnsUrl(m.livePhoto.thumb, m.livePhoto.thumbToken || m.livePhoto.token, false), + token: m.livePhoto.token, + thumbToken: m.livePhoto.thumbToken, + key: videoKey || m.livePhoto.key || m.key, + encIdx: m.livePhoto.encIdx || m.livePhoto.enc_idx + } : undefined + } + }) //数据服务已返回完整评论数据(含 emojis、refNickname) // 如果数据服务评论缺少表情包信息,回退到从 rawXml 重新解析 const dllComments: any[] = post.comments || [] @@ -1356,6 +1446,7 @@ class SnsService { return { ...post, + type: isVideoPost ? 15 : post.type, avatarUrl: contact?.avatarUrl, nickname: post.nickname || contact?.displayName || post.username, media: fixedMedia, @@ -1459,8 +1550,8 @@ class SnsService { return { success: false, status: result.status, error: result.error } } - async downloadImage(url: string, key?: string | number): Promise<{ success: boolean; data?: Buffer; contentType?: string; cachePath?: string; error?: string }> { - return this.fetchAndDecryptImage(url, key) + async downloadImage(url: string, key?: string | number, options: SnsDownloadOptions = {}): Promise<{ success: boolean; data?: Buffer; contentType?: string; cachePath?: string; error?: string }> { + return this.fetchAndDecryptImage(url, key, options) } /** @@ -2138,241 +2229,86 @@ window.addEventListener('scroll',function(){document.getElementById('btt').class ` } - private async fetchAndDecryptImage(url: string, key?: string | number): Promise<{ success: boolean; data?: Buffer; contentType?: string; cachePath?: string; status?: number; error?: string }> { + private async fetchAndDecryptImage(url: string, key?: string | number, options: SnsDownloadOptions = {}): Promise<{ success: boolean; data?: Buffer; contentType?: string; cachePath?: string; status?: number; error?: string }> { if (!url) return { success: false, error: 'url 不能为空' } - - const isVideo = isVideoUrl(url) - const cachePath = this.getCacheFilePath(url) - this.migrateLegacyCacheFile(url, cachePath) - - // 1. 优先尝试从当前缓存目录读取 + const { maxBytes, timeoutMs } = snsDownloadLimits(options) + let isVideo = options.kind ? options.kind === 'video' : isVideoUrl(url) + const cachePath = this.getCacheFilePath(url, options.kind) + this.migrateLegacyCacheFile(url, cachePath, options.kind) + // Validate bounded cache reads too; old cache entries may be encrypted or incomplete. if (existsSync(cachePath)) { + let handle: Awaited> | undefined try { - // 对于视频,不读取整个文件到内存,只确认存在即可 - if (isVideo) { - return { success: true, cachePath, contentType: 'video/mp4' } - } - - const data = await readFile(cachePath) - if (!detectImageMime(data, '').startsWith('image/')) { - // 旧版本可能把未解密内容写入缓存;发现无效图片头时删除并重新拉取。 - try { unlinkSync(cachePath) } catch { } - } else { - const contentType = detectImageMime(data) - return { success: true, data, contentType, cachePath } + handle = await open(cachePath, 'r') + const info = await handle.stat() + if (!info.isFile() || info.size > maxBytes) return { success: false, error: 'SNS_MEDIA_TOO_LARGE: 缓存媒体超过大小限制' } + let data = Buffer.alloc(isVideo ? Math.min(info.size, 64) : info.size) + let offset = 0 + while (offset < data.length) { + const { bytesRead } = await handle.read(data, offset, data.length - offset, offset) + if (!bytesRead) break + offset += bytesRead } - } catch (e) { - console.warn(`[SnsService] 读取缓存失败: ${cachePath}`, e) - } - } - - if (isVideo) { - // 视频专用下载逻辑 (下载 -> 解密 -> 缓存) - return new Promise(async (resolve) => { - const tmpPath = join(require('os').tmpdir(), `sns_video_${Date.now()}_${Math.random().toString(36).slice(2)}.enc`) - - try { - const https = require('https') - const urlObj = new URL(url) - const fs = require('fs') - - const fileStream = fs.createWriteStream(tmpPath) - - const options = { - hostname: urlObj.hostname, - path: urlObj.pathname + urlObj.search, - method: 'GET', - headers: { - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36', - 'Accept': '*/*', - // 'Accept-Encoding': 'gzip, deflate, br', // 视频流通常不压缩,去掉以免 stream 处理复杂 - 'Connection': 'keep-alive' + // A video CDN also serves image thumbnails. With no explicit kind, + // trust a valid image header rather than a hostname heuristic. + if (!options.kind && isVideo && detectImageMime(data.subarray(0, offset), '').startsWith('image/')) { + isVideo = false + if (data.length < info.size) { + data = Buffer.alloc(info.size); offset = 0 + while (offset < data.length) { + const { bytesRead } = await handle.read(data, offset, data.length - offset, offset) + if (!bytesRead) break + offset += bytesRead } } - - const req = https.request(options, (res: any) => { - if (res.statusCode !== 200 && res.statusCode !== 206) { - fileStream.close() - fs.unlink(tmpPath, () => { }) // 删除临时文件 - resolve({ success: false, status: res.statusCode, error: `HTTP ${res.statusCode}` }) - return - } - - res.pipe(fileStream) - fileStream.on('finish', async () => { - fileStream.close() - - try { - const encryptedBuffer = await readFile(tmpPath) - const raw = encryptedBuffer // 引用,方便后续操作 - - - if (key && String(key).trim().length > 0) { - try { - const keyText = String(key).trim() - let keystream: Buffer - - try { - const wasmService = WasmService.getInstance() - // 只需要前 128KB (131072 bytes) 用于解密头部 - keystream = await wasmService.getKeystream(keyText, 131072) - } catch (wasmErr) { - // 打包漏带 wasm 或 wasm 初始化异常时,回退到纯 TS ISAAC64 - const isaac = new Isaac64(keyText) - keystream = isaac.generateKeystreamBE(131072) - } - - const decryptLen = Math.min(keystream.length, raw.length) - - // XOR 解密 - for (let i = 0; i < decryptLen; i++) { - raw[i] ^= keystream[i] - } - - // 验证 MP4 签名 ('ftyp' at offset 4) - const ftyp = raw.subarray(4, 8).toString('ascii') - if (ftyp !== 'ftyp') { - // 可以在此处记录解密可能失败的标记,但不打印详细 hex - } - } catch (err) { - console.error(`[SnsService] 视频解密出错: ${err}`) - } - } - - // 写入最终缓存 (覆盖) - await writeFile(cachePath, raw) - - // 删除临时文件 - try { await import('fs/promises').then(fs => fs.unlink(tmpPath)) } catch (e) { } - - resolve({ success: true, data: raw, contentType: 'video/mp4', cachePath }) - } catch (e: any) { - console.error(`[SnsService] 视频处理失败:`, e) - resolve({ success: false, error: e.message }) - } - }) - }) - - req.on('error', (e: any) => { - fs.unlink(tmpPath, () => { }) - resolve({ success: false, error: e.message }) - }) - - req.setTimeout(15000, () => { - req.destroy() - fs.unlink(tmpPath, () => { }) - resolve({ success: false, error: '请求超时' }) - }) - - req.end() - - } catch (e: any) { - resolve({ success: false, error: e.message }) } - }) + const read = data.subarray(0, offset) + if (isVideo ? validSnsVideo(read) : detectImageMime(read, '').startsWith('image/')) { + return isVideo ? { success: true, cachePath, contentType: 'video/mp4' } + : { success: true, data: read, contentType: detectImageMime(read), cachePath } + } + } catch { /* An unreadable or invalid cache falls back to a bounded download. */ } + finally { await handle?.close().catch(() => {}) } } - - // 图片逻辑 (保持流式处理) - return new Promise((resolve) => { - try { - const https = require('https') - const zlib = require('zlib') - const urlObj = new URL(url) - - const options = { - hostname: urlObj.hostname, - path: urlObj.pathname + urlObj.search, - method: 'GET', - headers: { - 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Safari/537.36', - 'Accept': '*/*', - 'Accept-Encoding': 'gzip, deflate, br', - 'Accept-Language': 'zh-CN,zh;q=0.9', - 'Connection': 'keep-alive' - } + try { + const result = await fetchSnsMediaBounded(url, { maxBytes, timeoutMs }) + const raw = result.data + let decoded = raw + if (!options.kind && isVideo && detectImageMime(raw, '').startsWith('image/')) isVideo = false + if (isVideo) { + if (!validSnsVideo(raw) && key !== undefined && key !== null && String(key).trim()) { + const keyText = String(key).trim() + let keystream: Buffer + try { keystream = await WasmService.getInstance().getKeystream(keyText, 131072) } + catch { keystream = new Isaac64(keyText).generateKeystreamBE(131072) } + decoded = Buffer.from(raw) + for (let i = 0; i < Math.min(keystream.length, decoded.length); i++) decoded[i] ^= keystream[i] } - - const req = https.request(options, (res: any) => { - if (res.statusCode !== 200 && res.statusCode !== 206) { - console.error(`[SnsService] CDN 请求失败: HTTP ${res.statusCode}`) - resolve({ success: false, status: res.statusCode, error: `HTTP ${res.statusCode}` }) - return - } - - const chunks: Buffer[] = [] - let stream = res - - const encoding = res.headers['content-encoding'] - if (encoding === 'gzip') stream = res.pipe(zlib.createGunzip()) - else if (encoding === 'deflate') stream = res.pipe(zlib.createInflate()) - else if (encoding === 'br') stream = res.pipe(zlib.createBrotliDecompress()) - - stream.on('data', (chunk: Buffer) => chunks.push(chunk)) - stream.on('end', async () => { - const raw = Buffer.concat(chunks) - const xEnc = String(res.headers['x-enc'] || '').trim() - - let decoded = raw - const rawMagicMime = detectImageMime(raw, '') - - // 图片逻辑 - const shouldDecrypt = (xEnc === '1' || !!key) && key !== undefined && key !== null && String(key).trim().length > 0 - if (shouldDecrypt) { - try { - const keyStr = String(key).trim() - if (/^\d+$/.test(keyStr)) { - // 使用 WASM 版本的 Isaac64 解密图片 - // 修正逻辑:使用带 reverse 且修正了 8字节对齐偏移的 getKeystream - const wasmService = WasmService.getInstance() - const keystream = await wasmService.getKeystream(keyStr, raw.length) - - const decrypted = Buffer.allocUnsafe(raw.length) - for (let i = 0; i < raw.length; i++) { - decrypted[i] = raw[i] ^ keystream[i] - } - - const decryptedMagicMime = detectImageMime(decrypted, '') - if (decryptedMagicMime.startsWith('image/')) { - decoded = decrypted - } else if (!rawMagicMime.startsWith('image/')) { - decoded = decrypted - } - } - } catch (e) { - console.error('[SnsService] TS Decrypt Error:', e) - } - } - - const decodedMagicMime = detectImageMime(decoded, '') - if (!decodedMagicMime.startsWith('image/')) { - console.error(`[SnsService] 图片解密失败: 原始mime=${rawMagicMime}, 解密后mime=${decodedMagicMime}, key=${key}`) - resolve({ success: false, error: '图片解密失败:无法识别图片格式' }) - return - } - - // 写入磁盘缓存 - try { - await writeFile(cachePath, decoded) - } catch (e) { - console.warn(`[SnsService] 写入缓存失败: ${cachePath}`, e) - } - - const contentType = detectImageMime(decoded, (res.headers['content-type'] || 'image/jpeg') as string) - resolve({ success: true, data: decoded, contentType, cachePath }) - }) - stream.on('error', (e: any) => resolve({ success: false, error: e.message })) - }) - - req.on('error', (e: any) => resolve({ success: false, error: e.message })) - req.setTimeout(15000, () => { - req.destroy() - resolve({ success: false, error: '请求超时' }) - }) - req.end() - } catch (e: any) { - resolve({ success: false, error: e.message }) + if (!validSnsVideo(decoded)) return { success: false, error: 'SNS_MEDIA_INVALID_VIDEO: 视频解密失败或 MP4 格式无效' } + } else { + const rawMime = detectImageMime(raw, '') + if (!rawMime.startsWith('image/') && key !== undefined && key !== null && /^\d+$/.test(String(key).trim())) { + try { + const keystream = await WasmService.getInstance().getKeystream(String(key).trim(), raw.length) + decoded = Buffer.allocUnsafe(raw.length) + for (let i = 0; i < raw.length; i++) decoded[i] = raw[i] ^ keystream[i] + } catch { return { success: false, error: 'SNS_MEDIA_DECRYPT: 图片解密失败' } } + } + if (!detectImageMime(decoded, '').startsWith('image/')) return { success: false, error: 'SNS_MEDIA_INVALID_IMAGE: 图片解密失败:无法识别图片格式' } } - }) + // Do not claim cache success until a complete, validated private file is atomically published. + const temporary = cachePath + '.' + crypto.randomUUID() + '.tmp' + try { + await writeFile(temporary, decoded, { mode: 0o600 }) + renameSync(temporary, cachePath) + } finally { if (existsSync(temporary)) { try { unlinkSync(temporary) } catch {} } } + return { success: true, data: decoded, contentType: isVideo ? 'video/mp4' : detectImageMime(decoded), cachePath } + } catch (error) { + const value = error as Error & { status?: number } + // Error strings from the network/decryptor can include private URLs or keys; expose only our bounded codes. + const safe = /^SNS_MEDIA_|^HTTP \d{3}$/.test(value.message || '') ? value.message : 'SNS_MEDIA_FAILED: 媒体下载或保存失败' + return { success: false, status: value.status, error: safe } + } } /** 判断 buffer 是否为有效图片头 */ diff --git a/package.json b/package.json index 6fb9827..b979004 100644 --- a/package.json +++ b/package.json @@ -17,6 +17,7 @@ "rebuild": "electron-rebuild", "dev": "node scripts/prepare-electron-runtime.cjs && vite", "test:wcdb": "node --test scripts/check-wcdb-optional-exports.cjs", + "test:retention": "node --test scripts/test-message-retention.cjs scripts/test-sns-retention.cjs scripts/test-sns-download.cjs", "typecheck:renderer": "tsc --noEmit -p tsconfig.json", "typecheck:electron": "tsc --noEmit -p tsconfig.node.json", "typecheck": "npm run typecheck:renderer && npm run typecheck:electron", diff --git a/scripts/test-message-retention.cjs b/scripts/test-message-retention.cjs new file mode 100644 index 0000000..03e6273 --- /dev/null +++ b/scripts/test-message-retention.cjs @@ -0,0 +1,318 @@ +// Synthetic fixtures only: no WeChat database, account, or network is accessed. +const assert = require('node:assert/strict') +const fs = require('node:fs') +const os = require('node:os') +const path = require('node:path') +const vm = require('node:vm') +const { test, after } = require('node:test') +const { transformSync } = require('esbuild') +const compiled = transformSync(fs.readFileSync(path.join(__dirname, '../electron/services/messageRetentionStore.ts'), 'utf8'), { + loader: 'ts', format: 'cjs', target: 'node24' +}).code +function loadStore(overrides = {}) { + const moduleObject = { exports: {} } + vm.runInNewContext(compiled, { module: moduleObject, exports: moduleObject.exports, require: name => overrides[name] || require(name), Buffer }) + return moduleObject.exports +} +const moduleObject = { exports: loadStore() } +const { MessageRetentionStore, retentionServerId } = moduleObject.exports +const root = fs.mkdtempSync(path.join(os.tmpdir(), 'weflow-retention-test-')) +const stores = []; const services = [] +const store = (account) => { const value = new MessageRetentionStore(root, account); stores.push(value); return value } +after(() => { for (const service of services) service.stop(); for (const s of stores) s.close(); fs.rmSync(root, { recursive: true, force: true }) }) +const original = (overrides = {}) => ({ messageKey: 'synthetic', localId: 41, serverId: 0, serverIdRaw: '9223372036854775806', + localType: 1, createTime: 1700000000, isSend: 0, senderUsername: 'synthetic-sender', senderDisplayName: '合成发送者', + parsedContent: '合成原文:应在撤回后保留', rawContent: '合成原始内容', _db_name: 'message_0.db', _table_name: 'Msg_synthetic', ...overrides }) +const recall = (tag = 'newmsgid', value = '9223372036854775806', overrides = {}) => original({ localId: 99, serverIdRaw: '9223372036854775807', + localType: 10002, parsedContent: '合成发送者撤回了一条消息', rawContent: `<${tag}>${value}`, ...overrides }) + +test('capture, revoke, restart retains original and exact 64-bit server ID', () => { + let db = store('account-restart') + db.capture('chat', '合成会话', original(), 1000) + db.capture('chat', '合成会话', recall(), 2000) + db.close() + db = store('account-restart') + const { items, total } = db.list({ onlyRevoked: true }) + assert.equal(total, 1) + assert.equal(items[0].content, original().parsedContent) + assert.equal(items[0].rawContent, original().rawContent) + assert.equal(items[0].serverId, '9223372036854775806') + assert.equal(items[0].originalMissing, false) + assert.equal(items[0].recalledAt, 2000) +}) +test('duplicate polls do not overwrite original text or first recall timestamp', () => { + const db = store('account-duplicate') + db.captureMany('chat', '合成会话', [original()], 1000) + db.capture('chat', '合成会话', recall(), 2000) + db.captureMany('chat', '合成会话', Array.from({ length: 200 }, () => original({ parsedContent: '不得覆盖', rawContent: '不得覆盖' })), 3000) + db.capture('chat', '合成会话', recall(), 4000) + const result = db.list() + assert.equal(result.total, 1) + assert.equal(result.items[0].content, original().parsedContent) + assert.equal(result.items[0].recalledAt, 2000) +}) +test('same-local-ID replacement preserves the previously captured original', () => { + const db = store('account-local') + db.capture('chat', '会话', original(), 1000) + db.capture('chat', '会话', recall('msgid', '0', { localId: 41, serverIdRaw: '', rawContent: '', localType: 10000 }), 2000) + assert.equal(db.list({ onlyRevoked: true }).items[0].rawContent, original().rawContent) +}) +test('uncaptured revocation is explicitly marked and not presented as recovered text', () => { + const db = store('account-missing') + db.capture('chat', '会话', recall(), 2000) + db.capture('chat', '会话', recall(), 3000) + const result = db.list({ onlyRevoked: true }) + assert.equal(result.total, 1) + assert.equal(result.items[0].originalMissing, true) + assert.equal(result.items[0].rawContent, '') + assert.match(result.items[0].content, /原文未捕获/) +}) +test('oldmsgid and long msgid match server strings; short msgid can identify a local row', () => { + for (const [index, tag, value] of [[1, 'oldmsgid', '9223372036854775806'], [2, 'msgid', '9223372036854775806'], [3, 'msgid', '41']]) { + const db = store('account-recall-variant-' + index) + db.capture('chat', '会话', original(), 1000) + db.capture('chat', '会话', recall(tag, value), 2000) + assert.equal(db.list().total, 1) + assert.equal(db.list({ onlyRevoked: true }).items[0].originalMissing, false) + } + assert.equal(retentionServerId(original({ serverIdRaw: undefined, serverId: Number('9223372036854775806') })), '') + assert.equal(retentionServerId(original({ serverIdRaw: undefined, serverId: 1234 })), '1234') +}) +test('accounts and conversations are isolated even when message IDs match', () => { + const one = store('account-one'); const two = store('account-two') + one.capture('chat-a', '甲', original()) + one.capture('chat-b', '乙', original()) + one.capture('chat-a', '甲', recall()) + assert.equal(one.list().total, 2) + assert.equal(one.list({ onlyRevoked: true }).total, 1) + assert.equal(two.list().total, 0) + assert.notEqual(one.archivePath, two.archivePath) + // Windows inherits the user's profile ACL; chmod does not expose POSIX modes. + if (process.platform !== 'win32') { + assert.equal(fs.statSync(one.directory).mode & 0o777, 0o700) + assert.equal(fs.statSync(one.archivePath).mode & 0o777, 0o600) + } +}) +test('media is an independent private copy that survives deletion of the source cache', async () => { + const db = store('account-media') + const item = db.capture('chat', '会话', original({ localType: 3 })) + const source = path.join(root, 'synthetic-cache.png') + const bytes = Buffer.from('89504e470d0a1a0a', 'hex') + fs.writeFileSync(source, bytes) + const destination = await db.preserveMedia(item.id, source) + assert.ok(destination.startsWith(db.directory + path.sep)) + if (process.platform !== 'win32') assert.notEqual(fs.statSync(source).ino, fs.statSync(destination).ino) + fs.unlinkSync(source) + assert.deepEqual(fs.readFileSync(destination), bytes) + if (process.platform !== 'win32') assert.equal(fs.statSync(destination).mode & 0o777, 0o600) + db.close() + const reopened = store('account-media') + assert.equal(reopened.list().items[0].mediaPath, destination) + assert.equal(reopened.list().items[0].mediaState, 'saved') +}) +test('an unmatched strong server ID cannot fall back to a colliding local ID in any shard', () => { + const db = store('account-conflicting-target') + db.capture('chat', '会话', original({ serverIdRaw: '100', _db_name: 'message_0.db' })) + db.capture('chat', '会话', original({ serverIdRaw: '200', _db_name: 'message_1.db' })) + db.capture('chat', '会话', recall('newmsgid', '300', { _db_name: 'message_1.db', + rawContent: '30041' })) + const rows = db.list().items + assert.equal(rows.length, 3) + assert.equal(rows.find(row => row.serverId === '100').recalledAt, null) + assert.equal(rows.find(row => row.serverId === '200').recalledAt, null) + assert.equal(rows.find(row => row.serverId === '300').originalMissing, true) +}) +test('short local msgid matching is confined to its shard and rejects ambiguity', () => { + const db = store('account-local-scope') + db.capture('chat', '会话', original({ serverIdRaw: '100', _db_name: 'message_0.db' })) + db.capture('chat', '会话', original({ serverIdRaw: '200', _db_name: 'message_1.db' })) + db.capture('chat', '会话', recall('msgid', '41', { _db_name: 'message_1.db' })) + assert.equal(db.list().items.find(row => row.serverId === '100').recalledAt, null) + assert.equal(db.list({ onlyRevoked: true }).items[0].serverId, '200') + const ambiguous = store('account-local-ambiguous') + ambiguous.capture('chat', '会话', original({ serverIdRaw: '100' })) + ambiguous.capture('chat', '会话', original({ serverIdRaw: '200' })) + ambiguous.capture('chat', '会话', recall('msgid', '41')) + assert.equal(ambiguous.list({ onlyRevoked: true }).items[0].originalMissing, true) + assert.equal(ambiguous.list().items.filter(row => row.recalledAt === null).length, 2) +}) +test('different exact server IDs at the same local position remain distinct originals', () => { + const db = store('account-server-conflict') + const first = db.capture('chat', '会话', original({ serverIdRaw: '100', parsedContent: '第一条' })) + const second = db.capture('chat', '会话', original({ serverIdRaw: '200', parsedContent: '第二条' })) + assert.notEqual(first.id, second.id) + assert.equal(db.list().total, 2) + assert.equal(db.get(first.id).content, '第一条') + assert.equal(db.get(second.id).content, '第二条') +}) +test('ordinary text containing revocation XML cannot mark another message revoked', () => { + const db = store('account-pasted-xml') + db.capture('chat', '会话', original()) + db.capture('chat', '会话', recall('newmsgid', original().serverIdRaw, { localType: 1 })) + assert.equal(db.list().total, 2) + assert.equal(db.list({ onlyRevoked: true }).total, 0) +}) +test('a later exact server ID enriches a locally captured message without replacing its original', () => { + const db = store('account-late-server-id') + const first = db.capture('chat', '会话', original({ serverIdRaw: '', serverId: 0 })) + db.capture('chat', '会话', original({ parsedContent: '不得覆盖已保存原文' })) + db.capture('chat', '会话', recall()) + const result = db.list({ onlyRevoked: true }) + assert.equal(result.total, 1) + assert.equal(result.items[0].id, first.id) + assert.equal(result.items[0].serverId, original().serverIdRaw) + assert.equal(result.items[0].content, original().parsedContent) + assert.equal(result.items[0].originalMissing, false) +}) +test('native path handling accepts Windows drive paths and file URLs as well as POSIX paths', () => { + const url = require('node:url') + const windows = loadStore({ 'node:path': { ...path, isAbsolute: path.win32.isAbsolute }, + 'node:url': { ...url, fileURLToPath: value => url.fileURLToPath(value, { windows: true }) } }) + assert.equal(windows.retentionLocalMediaPath('C:\\Users\\Synthetic\\cache\\image.png'), 'C:\\Users\\Synthetic\\cache\\image.png') + assert.equal(windows.retentionLocalMediaPath('file:///C:/Users/Synthetic/cache/image%20one.png'), 'C:\\Users\\Synthetic\\cache\\image one.png') + for (const source of ['C:relative.png', '..\\relative.png', 'https://example.invalid/image.png']) assert.throws(() => windows.retentionLocalMediaPath(source)) + const posix = loadStore({ 'node:path': { ...path, isAbsolute: path.posix.isAbsolute }, + 'node:url': { ...url, fileURLToPath: value => url.fileURLToPath(value, { windows: false }) } }) + assert.equal(posix.retentionLocalMediaPath('/tmp/image.png'), '/tmp/image.png') + assert.equal(posix.retentionLocalMediaPath('file:///tmp/image%20one.png'), '/tmp/image one.png') + assert.throws(() => posix.retentionLocalMediaPath('../relative.png')) +}) +test('closing an archive during media copying does not publish or retain a temporary file', async () => { + let entered; let release + const copying = new Promise(resolve => { entered = resolve }) + const resume = new Promise(resolve => { release = resolve }) + const Store = loadStore({ 'node:fs': { ...fs, promises: { ...fs.promises, copyFile: async (...args) => { + entered(); await resume; return fs.promises.copyFile(...args) + } } } }).MessageRetentionStore + const db = new Store(root, 'account-close-copy'); stores.push(db) + const item = db.capture('chat', '会话', original({ localType: 3 })) + const source = path.join(root, 'close-copy.png'); fs.writeFileSync(source, Buffer.from('89504e470d0a1a0a', 'hex')) + const pending = db.preserveMedia(item.id, source) + await copying; db.close(); release() + assert.equal(await pending, undefined) + assert.deepEqual(fs.readdirSync(path.join(db.directory, 'media')), []) +}) +test('a close racing the final rename removes only its unpublished media copy', async () => { + let db + const Store = loadStore({ 'node:fs': { ...fs, promises: { ...fs.promises, rename: async (...args) => { + await fs.promises.rename(...args); db.close() + } } } }).MessageRetentionStore + db = new Store(root, 'account-close-rename'); stores.push(db) + const item = db.capture('chat', '会话', original({ localType: 3 })) + const source = path.join(root, 'close-rename.png'); fs.writeFileSync(source, Buffer.from('89504e470d0a1a0a', 'hex')) + assert.equal(await db.preserveMedia(item.id, source), undefined) + assert.deepEqual(fs.readdirSync(path.join(db.directory, 'media')), []) + assert.ok(fs.existsSync(source)) +}) +test('media growth during copying cannot bypass the size limit or publish a partial archive', async () => { + const Store = loadStore({ 'node:fs': { ...fs, promises: { ...fs.promises, copyFile: async (source, destination) => { + await fs.promises.copyFile(source, destination); await fs.promises.truncate(destination, 64 * 1024 * 1024 + 1) + } } } }).MessageRetentionStore + const db = new Store(root, 'account-growing-media'); stores.push(db) + const item = db.capture('chat', '会话', original({ localType: 3 })) + const source = path.join(root, 'growing.png'); fs.writeFileSync(source, Buffer.from('89504e470d0a1a0a', 'hex')) + await assert.rejects(db.preserveMedia(item.id, source), /64MB/) + assert.deepEqual(fs.readdirSync(path.join(db.directory, 'media')), []) + assert.equal(db.get(item.id).mediaPath, undefined) +}) + +function makeService(chat = {}, options = {}) { + const moduleObject = { exports: {} } + let clock = 2000000000000 + const timers = new Set() + class FakeDate extends Date { static now() { return clock } } + const config = { messageRetentionEnabled: true, myAccountId: 'synthetic-account', dbPath: '/synthetic', ...options.config } + const dependencies = { + electron: { app: { getPath: () => path.join(root, 'service-fixture-' + stores.length + '-' + Math.random()) } }, + './config': { ConfigService: class { + get(key) { return config[key] } set(key, value) { config[key] = value } + getAccountDir(_path, account) { return '/synthetic/' + account } + } }, + './chatService': { chatService: chat }, + './imageDecryptService': { imageDecryptService: options.imageDecrypt || { decryptImage: async () => ({ success: false }) } }, + './messageRetentionStore': moduleObjectStore() + } + // Keep each fake app's data directory stable throughout that instance. + const directory = dependencies.electron.app.getPath() + dependencies.electron.app.getPath = () => directory + const source = transformSync(fs.readFileSync(path.join(__dirname, '../electron/services/messageRetentionService.ts'), 'utf8'), { + loader: 'ts', format: 'cjs', target: 'node24' + }).code + vm.runInNewContext(source, { module: moduleObject, exports: moduleObject.exports, Buffer, console, Date: FakeDate, + require: name => dependencies[name] || require(name), + setTimeout: (callback, delay) => { + const timer = { callback, at: clock + delay, unref() {} } + if (delay === 5) queueMicrotask(callback); else timers.add(timer) + return timer + }, clearTimeout: timer => timers.delete(timer) + }) + services.push(moduleObject.exports.messageRetentionService) + return { service: moduleObject.exports.messageRetentionService, config, directory, timers, setClock: value => { clock = value }, clock: () => clock } +} +function moduleObjectStore() { return moduleObject.exports } + +test('continuous database events retain the earliest scan deadline', () => { + const fixture = makeService() + const { service, timers } = fixture + let scans = 0 + service.scan = async () => { scans++ } + service.start() + const initial = [...timers][0]; timers.delete(initial); initial.callback() + const begin = fixture.clock() + service.schedule(1500) + service.handleDbMonitorChange('message_change', '{}') + for (const offset of [50, 100, 149]) { + fixture.setClock(begin + offset) + service.handleDbMonitorChange('message_change', '{}') + assert.equal(timers.size, 1) + assert.equal([...timers][0].at, begin + 150) + } + fixture.setClock(begin + 150) + const due = [...timers][0]; timers.delete(due); due.callback() + assert.equal(scans, 2) + service.stop() +}) +test('failed conversation reads rotate out and do not starve later conversations', async () => { + const attempts = [] + const sessions = Array.from({ length: 16 }, (_, index) => ({ username: 'chat-' + index, lastTimestamp: 1, lastMsgType: 1, summary: '' })) + const fixture = makeService({ + getSessions: async () => ({ success: true, sessions }), + getLatestMessages: async id => { + attempts.push(id) + return Number(id.split('-')[1]) < 8 ? { success: false, error: 'synthetic unavailable' } + : { success: true, messages: [original()] } + } + }) + const { service } = fixture + service.start() + await service.scan() + fixture.setClock(fixture.clock() + 1500) + await service.scan() + assert.ok(attempts.includes('chat-15')) + assert.equal(service.status().totalMessages, 8) + service.stop() +}) +test('capture is opt-in and malformed non-boolean settings fail closed', () => { + for (const enabled of [false, 'false', 'true', 1, null]) { + const { service, directory, timers } = makeService({}, { config: { messageRetentionEnabled: enabled } }) + service.start() + assert.equal(service.status().enabled, false) + assert.equal(timers.size, 0) + assert.equal(fs.existsSync(path.join(directory, 'Retention')), false) + service.stop() + } +}) +test('an account switch discards an in-flight query from the previous account', async () => { + let started; let finish + const waiting = new Promise(resolve => { started = resolve }) + const { service, config } = makeService({ + getSessions: async () => ({ success: true, sessions: [{ username: 'chat', lastTimestamp: 1, lastMsgType: 1, summary: '' }] }), + getLatestMessages: () => { started(); return new Promise(resolve => { finish = resolve }) } + }) + service.start(); const scan = service.scan(); await waiting + config.myAccountId = 'second-synthetic-account'; service.handleConfigChanged('myAccountId') + finish({ success: true, messages: [original()] }); await scan + assert.equal(service.status().totalMessages, 0) + assert.equal(service.list().total, 0) + service.stop() +}) diff --git a/scripts/test-sns-download.cjs b/scripts/test-sns-download.cjs new file mode 100644 index 0000000..2a413da --- /dev/null +++ b/scripts/test-sns-download.cjs @@ -0,0 +1,218 @@ +// Portable synthetic fixtures only: no account, real CDN, private files, or network. +const assert = require('node:assert/strict') +const fs = require('node:fs') +const path = require('node:path') +const os = require('node:os') +const vm = require('node:vm') +const { EventEmitter } = require('node:events') +const { PassThrough } = require('node:stream') +const zlib = require('node:zlib') +const { test } = require('node:test') +const { transformSync } = require('esbuild') + +const compiled = transformSync(fs.readFileSync(path.join(__dirname, '../electron/services/snsService.ts'), 'utf8'), { + loader: 'ts', format: 'cjs', target: 'node24' +}).code +const png = Buffer.concat([Buffer.from('89504e470d0a1a0a', 'hex'), Buffer.alloc(200)]) +const mp4 = Buffer.concat([Buffer.from([0, 0, 0, 24]), Buffer.from('ftypisom'), Buffer.alloc(12)]) +const fixtureUrl = name => `https://media.example.invalid/${name}` + +function runtime(t) { + const root = fs.mkdtempSync(path.join(os.tmpdir(), 'weflow-sns-download-test-')) + const state = { response: {}, timeline: [], requestCount: 0, destroyedRequests: 0, idleTimeoutMs: null } + const cleanup = new Set() + const https = { + request(_options, callback) { + state.requestCount++ + const plan = state.response + const request = new EventEmitter() + let timeout; let interval; let response; let closed = false + const stop = () => { clearTimeout(timeout); clearInterval(interval); cleanup.delete(stop) } + cleanup.add(stop) + request.destroy = () => { + if (!closed) { closed = true; state.destroyedRequests++ } + stop(); response?.destroy() + } + request.setTimeout = milliseconds => { state.idleTimeoutMs = milliseconds } + request.end = () => { + timeout = setTimeout(() => { + response = new PassThrough() + response.statusCode = plan.status || 200 + response.headers = plan.headers || {} + response.on('close', stop) + callback(response) + if (response.destroyed) return + if (plan.trickle) interval = setInterval(() => response.write(Buffer.from('x')), 5) + else response.end(plan.body || Buffer.alloc(0)) + }, 0) + } + return request + } + } + class Config { + get(name) { return name === 'cachePath' ? root : undefined } + getCacheBasePath() { return root } + } + const dependencies = { + https, + electron: { app: { getPath: () => root } }, + './config': { ConfigService: Config }, + './contactCacheService': { ContactCacheService: { getInstance: () => ({ get: () => undefined }) } }, + './wcdbService': { wcdbService: { getSnsTimeline: async () => ({ success: true, timeline: state.timeline }) } }, + './wasmService': { WasmService: { getInstance: () => ({ getKeystream: async (_key, length) => Buffer.alloc(length) }) } }, + './isaac64': { Isaac64: class { generateKeystreamBE(length) { return Buffer.alloc(length) } } }, + './export/utils/markdown': {} + } + const module = { exports: {} } + vm.runInNewContext(compiled, { module, exports: module.exports, + require: name => Object.hasOwn(dependencies, name) ? dependencies[name] : require(name), + Buffer, URL, setTimeout, clearTimeout, console }) + const { snsService, fetchSnsMediaBounded } = module.exports + snsService.getContactCacheScope = () => '' + t.after(() => { for (const stop of cleanup) stop(); fs.rmSync(root, { recursive: true, force: true }) }) + return { state, snsService, fetchSnsMediaBounded } +} + +test('limits decompressed bytes even when Content-Length is small', async t => { + const { state, fetchSnsMediaBounded } = runtime(t) + const compressed = zlib.gzipSync(Buffer.alloc(5000)) + state.response = { headers: { 'content-encoding': 'gzip', 'content-length': String(compressed.length) }, body: compressed } + await assert.rejects(fetchSnsMediaBounded(fixtureUrl('compressed'), { maxBytes: 100, timeoutMs: 1000 }), /SNS_MEDIA_TOO_LARGE/) + assert.equal(state.destroyedRequests, 1) +}) + +test('limits video bytes without trusting a missing Content-Length', async t => { + const { state, snsService } = runtime(t) + state.response = { body: Buffer.alloc(1000) } + const result = await snsService.downloadImage(fixtureUrl('large.mp4'), undefined, { maxBytes: 100, timeoutMs: 1000 }) + assert.equal(result.success, false) + assert.match(result.error, /SNS_MEDIA_TOO_LARGE/) +}) + +test('absolute timeout closes a response that keeps trickling bytes', async t => { + const { state, fetchSnsMediaBounded } = runtime(t) + state.response = { trickle: true } + await assert.rejects(fetchSnsMediaBounded(fixtureUrl('trickle'), { maxBytes: 10000, timeoutMs: 40 }), /SNS_MEDIA_TIMEOUT/) + assert.equal(state.destroyedRequests, 1) +}) + +test('invalid video does not become a successful cache entry', async t => { + const { state, snsService } = runtime(t) + state.response = { body: Buffer.from('synthetic invalid video') } + const url = fixtureUrl('invalid.mp4') + const result = await snsService.downloadImage(url) + assert.equal(result.success, false) + assert.match(result.error, /SNS_MEDIA_INVALID_VIDEO/) + assert.equal(fs.existsSync(snsService.getCacheFilePath(url)), false) +}) + +test('the cache cannot bypass an explicit size limit', async t => { + const { state, snsService } = runtime(t) + const url = fixtureUrl('cached.png') + fs.writeFileSync(snsService.getCacheFilePath(url), Buffer.alloc(1000)) + const result = await snsService.downloadImage(url, undefined, { maxBytes: 100 }) + assert.equal(result.success, false) + assert.match(result.error, /SNS_MEDIA_TOO_LARGE/) + assert.equal(state.requestCount, 0) +}) + +test('corrupt cached video is replaced only after format validation', async t => { + const { state, snsService } = runtime(t) + const url = fixtureUrl('retry.mp4') + fs.writeFileSync(snsService.getCacheFilePath(url), Buffer.from('invalid')) + state.response = { body: mp4 } + const result = await snsService.downloadImage(url) + assert.equal(result.success, true) + assert.equal(result.contentType, 'video/mp4') + assert.deepEqual(fs.readFileSync(result.cachePath), mp4) +}) + +test('rejects redirects without requesting their destination', async t => { + const { state, fetchSnsMediaBounded } = runtime(t) + state.response = { status: 302, headers: { location: 'https://127.0.0.1/private' } } + await assert.rejects(fetchSnsMediaBounded(fixtureUrl('redirect')), /HTTP 302/) + assert.equal(state.requestCount, 1) +}) + +test('a valid image is atomically cached with private POSIX permissions', async t => { + const { state, snsService } = runtime(t) + state.response = { body: png } + const result = await snsService.downloadImage(fixtureUrl('normal.png')) + assert.equal(result.success, true) + assert.equal(result.contentType, 'image/png') + assert.deepEqual(fs.readFileSync(result.cachePath), png) + if (process.platform !== 'win32') assert.equal(fs.statSync(result.cachePath).mode & 0o777, 0o600) + assert.equal(fs.readdirSync(path.dirname(result.cachePath)).some(name => name.endsWith('.tmp')), false) +}) + +test('explicit thumbnail kind overrides a video hostname', async t => { + const { state, snsService } = runtime(t) + state.response = { body: png } + const result = await snsService.downloadImage('https://video.example.invalid/thumbnail', undefined, { kind: 'image' }) + assert.equal(result.success, true) + assert.equal(result.contentType, 'image/png') + assert.ok(result.cachePath.endsWith('.jpg')) +}) + +function videoPost(tag) { + return { id: 'synthetic-video', username: 'synthetic-user', nickname: 'Synthetic', type: 1, + media: [{ url: fixtureUrl('opaque'), thumb: fixtureUrl('thumbnail'), key: '0' }], comments: [], likes: [], + rawXml: `<${tag}>156` } +} + +test('XML content type repairs a fallback video type and exact nonzero key', async t => { + const { state, snsService } = runtime(t) + for (const tag of ['type', 'contentStyle']) { + state.timeline = [videoPost(tag)] + const result = await snsService.getTimeline() + assert.equal(result.timeline[0].type, 15) + assert.equal(result.timeline[0].media[0].key, '1234567890') + } +}) + +test('nested media types or video hostnames do not redefine an image post', async t => { + const { state, snsService } = runtime(t) + state.timeline = [{ ...videoPost('type'), media: [{ url: 'https://video.example.invalid/thumbnail', thumb: '', key: '234' }], + rawXml: '115' }] + const result = await snsService.getTimeline() + assert.equal(result.timeline[0].type, 1) + assert.equal(result.timeline[0].media[0].key, '234') +}) + +test('automatic image detection also reads the complete cached thumbnail', async t => { + const { state, snsService } = runtime(t) + const url = 'https://video.example.invalid/automatic-thumbnail' + state.response = { body: png } + const first = await snsService.downloadImage(url) + const second = await snsService.downloadImage(url) + assert.equal(first.success, true) + assert.equal(second.success, true) + assert.equal(second.contentType, 'image/png') + assert.deepEqual(second.data, png) + assert.equal(state.requestCount, 1) +}) + +test('explicit video kind continues to reject image data', async t => { + const { state, snsService } = runtime(t) + state.response = { body: png } + const result = await snsService.downloadImage(fixtureUrl('forced.mp4'), undefined, { kind: 'video' }) + assert.equal(result.success, false) + assert.match(result.error, /SNS_MEDIA_INVALID_VIDEO/) +}) + + +test('default callers retain their size policy and original idle timeout', async t => { + const { state, snsService } = runtime(t) + state.response = { headers: { 'content-length': String(256 * 1024 * 1024) }, body: png } + const result = await snsService.downloadImage(fixtureUrl('default-policy.png')) + assert.equal(result.success, true, 'the optional retention ceiling must not reject an existing caller') + assert.equal(state.idleTimeoutMs, 15000) +}) + +test('retention opts into a hard ceiling before reading a large declared response', async t => { + const { state, snsService } = runtime(t) + state.response = { headers: { 'content-length': String(256 * 1024 * 1024) }, body: png } + const result = await snsService.downloadImage(fixtureUrl('retention-policy.png'), undefined, { maxBytes: 128 * 1024 * 1024, timeoutMs: 30000 }) + assert.equal(result.success, false) + assert.match(result.error, /SNS_MEDIA_TOO_LARGE/) +}) diff --git a/scripts/test-sns-retention.cjs b/scripts/test-sns-retention.cjs new file mode 100644 index 0000000..3ab9306 --- /dev/null +++ b/scripts/test-sns-retention.cjs @@ -0,0 +1,245 @@ +// All data and files are synthetic. No WeChat account or network is accessed. +const assert = require('node:assert/strict') +const fs = require('node:fs') +const path = require('node:path') +const os = require('node:os') +const vm = require('node:vm') +const { test, after } = require('node:test') +const { transformSync } = require('esbuild') +function load(filename, dependencies = {}, globals = {}) { + const compiled = transformSync(fs.readFileSync(path.join(__dirname, '../electron/services', filename), 'utf8'), { loader: 'ts', format: 'cjs', target: 'node24' }).code + const module = { exports: {} } + vm.runInNewContext(compiled, { module, exports: module.exports, require: name => dependencies[name] || require(name), + Buffer, URL, console, ...globals }) + return module.exports +} +const storeModule = load('snsRetentionStore.ts') +const { SnsRetentionStore, SNS_RETENTION_MAX_BYTES } = storeModule +const root = fs.mkdtempSync(path.join(os.tmpdir(), 'weflow-sns-retention-test-')) +const stores = []; const services = [] +after(() => { for (const service of services) service.stop(); for (const store of stores) store.close(); fs.rmSync(root, { recursive: true, force: true }) }) +function store(identity) { const value = new SnsRetentionStore(root, identity); stores.push(value); return value } +const post = (overrides = {}) => ({ id: '9223372036854775806', username: 'synthetic-friend', nickname: '合成昵称', createTime: 1700000000, + contentDesc: '合成朋友圈原文', media: [], likes: ['合成点赞'], comments: [{ id: '18446744073709551614', nickname: '合成评论者', content: '合成评论', refCommentId: '' }], ...overrides }) +const imagePost = (overrides = {}) => post({ media: [{ url: 'https://mmsns.qpic.cn/synthetic/0?token=old', thumb: 'https://mmsns.qpic.cn/synthetic/150', key: '18446744073709551614' }], ...overrides }) +const png = Buffer.from('89504e470d0a1a0a0000000d49484452', 'hex') + +test('blank or vanished source posts never delete the archived original or its snapshots', () => { + let db = store('disappeared') + db.capturePosts([post()], 1000) + db.capturePosts([post()], 1100) + assert.equal(db.stats().totalSnapshots, 1) + db.capturePosts([post({ contentDesc: '', likes: [], comments: [], createTime: 0 })], 2000) + db.capturePosts([]) + db.close(); db = store('disappeared') + const item = db.list().items[0] + assert.equal(item.contentDesc, '合成朋友圈原文') + assert.equal(item.likes[0], '合成点赞') + assert.equal(item.comments[0].content, '合成评论') + assert.equal(item.createTime, 1700000000) + assert.equal(item.snapshotCount, 2) + assert.equal(item.capturedAt, 1000) +}) +test('64-bit post and comment IDs remain strings and accounts are isolated', () => { + const one = store('account-a'); const two = store('account-b') + one.capturePosts([post(), post({ id: '9223372036854775807' })]) + assert.equal(one.list().total, 2) + assert.equal(one.list().items.find(item => item.id === '9223372036854775806').comments[0].id, '18446744073709551614') + assert.equal(two.list().total, 0) + one.capturePosts([post({ id: Number('9223372036854775806') })]) + assert.equal(one.list().total, 2, 'rounded numeric IDs must not invent an archive identity') + if (process.platform !== 'win32') { + assert.equal(fs.statSync(one.directory).mode & 0o777, 0o700) + assert.equal(fs.statSync(one.archivePath).mode & 0o777, 0o600) + } +}) +test('media copies survive source-cache deletion and restarting the archive', async () => { + let db = store('media-copy'); db.capturePosts([imagePost()]) + const job = db.nextMedia(); assert.equal(job.key, '18446744073709551614') + const source = path.join(root, 'synthetic-cache.png'); fs.writeFileSync(source, png) + await db.saveMedia(job.id, { path: source }, 'original') + const saved = db.list().items[0].media[0] + assert.ok(saved.localPath.startsWith(db.directory + path.sep)) + if (process.platform !== 'win32') assert.notEqual(fs.statSync(source).ino, fs.statSync(saved.localPath).ino) + fs.unlinkSync(source) + db.close(); db = store('media-copy') + assert.deepEqual(fs.readFileSync(db.list().items[0].media[0].localPath), png) + if (process.platform !== 'win32') assert.equal(fs.statSync(saved.localPath).mode & 0o777, 0o600) +}) +test('failed media backs off and new URL tokens retry without downgrading a saved original', async () => { + const db = store('retry'); const now = Date.now() + db.capturePosts([imagePost()]); const job = db.nextMedia(now) + db.deferMedia(job.id, 'synthetic unavailable', false, now) + assert.equal(db.nextMedia(now + 299999), null) + assert.equal(db.nextMedia(now + 300001).id, job.id) + db.capturePosts([imagePost({ media: [{ ...imagePost().media[0], url: 'https://mmsns.qpic.cn/synthetic/0?token=new' }] })]) + const refreshed = db.nextMedia(now); assert.equal(refreshed.id, job.id); assert.equal(refreshed.attempts, 0) + await db.saveMedia(job.id, { data: png }, 'original') + const originalPath = db.list().items[0].media[0].localPath + db.capturePosts([imagePost({ media: [{ ...imagePost().media[0], url: 'https://mmsns.qpic.cn/synthetic/0?token=newer' }] })]) + await db.saveMedia(job.id, { data: png }, 'thumbnail') + assert.equal(db.nextMedia(now + 999999), null) + assert.equal(db.list().items[0].media[0].quality, 'original') + assert.equal(db.list().items[0].media[0].localPath, originalPath) +}) +test('thumbnail fallback is explicit and preserved when an original upgrade succeeds', async () => { + const db = store('thumbnail'); db.capturePosts([imagePost()]); const job = db.nextMedia() + db.deferMedia(job.id, 'original missing') + await db.saveMedia(job.id, { data: png }, 'thumbnail') + let media = db.list().items[0].media[0] + assert.equal(media.quality, 'thumbnail'); assert.equal(db.stats().failedMedia, 1) + assert.equal(db.stats().savedMedia, 0, 'a thumbnail is not counted as a saved original') + const thumb = media.localPath + await db.saveMedia(job.id, { data: png }, 'original') + media = db.list().items[0].media[0] + assert.equal(media.quality, 'original'); assert.equal(media.thumbnailPath, thumb) + assert.equal(db.stats().savedMedia, 1); assert.equal(db.stats().failedMedia, 0) + assert.ok(fs.existsSync(thumb)); assert.notEqual(media.localPath, thumb) +}) +test('two workers claim different newest media and reject invalid or oversized content', async () => { + const db = store('claims') + db.capturePosts([imagePost(), imagePost({ id: 'newer', createTime: 1700000001 })]) + const first = db.nextMedia(); const second = db.nextMedia() + assert.equal(first.postId, 'newer'); assert.notEqual(first.id, second.id); assert.equal(db.nextMedia(), null) + await assert.rejects(db.saveMedia(first.id, { data: Buffer.from('not raster') }, 'original'), /格式/) + const large = path.join(root, 'oversized-sparse.png'); fs.writeFileSync(large, png); fs.truncateSync(large, SNS_RETENTION_MAX_BYTES + 1) + await assert.rejects(db.saveMedia(first.id, { path: large }, 'original'), /128MiB/) + fs.unlinkSync(large) +}) +test('visible older posts outrank background media without resetting failed-item backoff', () => { + const db = store('visible-priority'); const now = Date.now() + db.capturePosts([imagePost({ id: 'older', createTime: 1000 }), imagePost({ id: 'newer', createTime: 2000 })]) + db.prioritizePostIds(['older'], now) + const selected = db.nextMedia(now) + assert.equal(selected.postId, 'older') + db.deferMedia(selected.id, 'synthetic failure', false, now) + db.prioritizePostIds(['older'], now + 1000) + const background = db.nextMedia(now + 1000) + assert.equal(background.postId, 'newer') + assert.equal(db.nextMedia(now + 2000), null) + assert.equal(db.nextMedia(now + 300001).postId, 'newer', 'expired page priority does not permanently starve newer background work') +}) +test('existing archives migrate the priority column without losing posts or pending media', () => { + let db = store('priority-migration'); db.capturePosts([imagePost()]); const file = db.archivePath; db.close() + const { DatabaseSync } = require('node:sqlite') + const old = new DatabaseSync(file); old.exec('ALTER TABLE media DROP COLUMN priorityUntil'); old.close() + db = store('priority-migration') + assert.equal(db.stats().totalPosts, 1); assert.equal(db.stats().pendingMedia, 1) + db.prioritizePostIds([imagePost().id]); assert.equal(db.nextMedia().postId, imagePost().id) +}) + +function makeService(sns = {}) { + const fixture = fs.mkdtempSync(path.join(root, 'service-')) + const configuration = { snsRetentionEnabled: true, myAccountId: 'synthetic', dbPath: path.join(fixture, 'database'), cachePath: path.join(fixture, 'cache') } + const api = { getTimeline: async () => ({ success: true, timeline: [] }), downloadImage: async () => ({ success: false }), ...sns } + const module = load('snsRetentionService.ts', { + electron: { app: { getPath: name => name === 'documents' ? path.join(fixture, 'documents') : fixture } }, + './config': { ConfigService: class { get(key) { return configuration[key] } set(key, value) { configuration[key] = value } getAccountDir(_path, account) { return path.join(fixture, 'database', account) } } }, + './snsService': { snsService: api }, './snsRetentionStore': storeModule + }, { setTimeout: () => ({ unref() {} }), clearTimeout() {} }) + services.push(module.snsRetentionService) + return { ...module, configuration, fixture } +} +test('source validation blocks SSRF destinations, credentials, and escaping cache symlinks', async () => { + const { isTrustedSnsMediaUrl, trustedSnsLocalPath, fixture } = makeService() + for (const url of ['https://mmsns.qpic.cn/a', 'https://shzjwxsns.video.qq.com/a', 'https://wxapp.tc.qq.com/a', 'https://vweixinthumb.tc.qq.com/a']) assert.equal(isTrustedSnsMediaUrl(url), true) + for (const url of ['http://mmsns.qpic.cn/a', 'https://127.0.0.1/a', 'https://[::1]/a', 'https://user:pass@mmsns.qpic.cn/a', 'https://mmsns.qpic.cn:444/a', 'https://qpic.cn.evil.test/a', 'https://c6.y.qq.com/a', 'file:///etc/passwd']) assert.equal(isTrustedSnsMediaUrl(url), false) + const cache = path.join(fixture, 'cache'); fs.mkdirSync(cache) + const local = path.join(cache, 'allowed.png'); fs.writeFileSync(local, png) + const outside = path.join(fixture, 'outside.png'); fs.writeFileSync(outside, png) + const outsideDir = path.join(fixture, 'outside'); fs.mkdirSync(outsideDir) + fs.writeFileSync(path.join(outsideDir, 'secret.png'), png) + const symlink = path.join(cache, 'escape'); fs.symlinkSync(outsideDir, symlink, process.platform === 'win32' ? 'junction' : 'dir') + assert.equal(await trustedSnsLocalPath(local, [cache]), fs.realpathSync(local)) + assert.equal(await trustedSnsLocalPath(require('node:url').pathToFileURL(local).href, [cache]), fs.realpathSync(local)) + assert.equal(await trustedSnsLocalPath(path.join(symlink, 'secret.png'), [cache]), null) + assert.equal(await trustedSnsLocalPath(outside, [cache]), null) +}) +test('paced baseline retains all local pages without waiting for media or purging vanished posts', async () => { + let timeline = Array.from({ length: 205 }, (_, index) => post({ id: String(1000 + index), createTime: 1700001000 - index })) + const { snsRetentionService: service } = makeService({ getTimeline: async (limit, offset) => ({ success: true, timeline: timeline.slice(offset, offset + limit) }) }) + service.start(); await service.runScan(); await service.runScan(); await service.runScan() + assert.equal(service.status().totalPosts, 205); assert.equal(service.status().baselineComplete, true) + timeline = [] + await service.scanNow() + assert.equal(service.status().totalPosts, 205) + service.stop() +}) +test('archive write failures never throw into the ordinary timeline UI', () => { + const { snsRetentionService: service } = makeService() + assert.doesNotThrow(() => service.captureVisible([post({ rawXml: 1n })])) + assert.match(service.status().error, /留存写入失败/) + service.stop() +}) +test('an account switch during a timeline query cannot archive the old account into the new one', async () => { + let finish + const { snsRetentionService: service, configuration } = makeService({ getTimeline: () => new Promise(resolve => { finish = resolve }) }) + service.start(); const running = service.runScan() + configuration.myAccountId = 'second-account'; service.handleConfigChanged('myAccountId') + finish({ success: true, timeline: [post()] }); await running + assert.equal(service.status().totalPosts, 0) + service.stop() +}) +test('running means an active scan and returns to idle so manual scan stays available', async () => { + let finish + let defer = true + const { snsRetentionService: service } = makeService({ getTimeline: () => defer + ? new Promise(resolve => { finish = resolve }) : Promise.resolve({ success: true, timeline: [] }) }) + service.start(); assert.equal(service.status().running, false) + const operation = service.runScan(); assert.equal(service.status().running, true) + defer = false; finish({ success: true, timeline: [] }); await operation + assert.equal(service.status().running, false) + assert.equal(service.status().enabled, true) + await service.scanNow(); assert.equal(service.status().running, false) + service.stop() +}) +test('SNS events and periodic sweeps discover older rows without resetting in-progress pagination', async () => { + let timeline = [post()] + const { snsRetentionService: service } = makeService({ getTimeline: async (limit, offset) => ({ success: true, timeline: timeline.slice(offset, offset + limit) }) }) + service.start(); await service.runScan(); assert.equal(service.status().baselineComplete, true) + timeline = Array.from({ length: 205 }, (_, index) => post({ id: String(3000 + index), createTime: 1700001000 - index })) + service.handleDbMonitorChange('sns_change', '{}') + assert.equal(service.status().baselineOffset, 0) + await service.runScan(); assert.equal(service.status().baselineOffset, 90) + service.handleDbMonitorChange('sns_change', '{}') + assert.equal(service.status().baselineOffset, 90) + await service.runScan(); await service.runScan() + assert.equal(service.status().baselineComplete, true) + timeline.push(post({ id: 'newly-cached-old-post', createTime: 1000 })) + service.lastFullScan = Date.now() - 31 * 60_000 + await service.runScan(); assert.equal(service.status().baselineComplete, false) + await service.runScan(); await service.runScan() + assert.ok(service.list({ query: '合成朋友圈原文', limit: 100 }).total >= 207) + assert.equal(service.store.list({ username: 'synthetic-friend', limit: 100, offset: 200 }).items.some(item => item.id === 'newly-cached-old-post'), true) + service.stop() +}) + + +test('image media on video-named CDNs keeps its image kind', async () => { + const db = store('image-kind') + db.capturePosts([imagePost({ type: 1, media: [{ url: 'https://synthetic.video.qq.com/thumbnail', thumb: '' }] })]) + const job = db.nextMedia() + assert.equal(job.kind, 'image') + await db.saveMedia(job.id, { data: png }, 'original') + assert.equal(db.stats().savedMedia, 1) +}) + +test('HEIC and BMP output accepted by the downloader is also retainable', async () => { + const db = store('image-formats') + db.capturePosts([imagePost({ id: 'synthetic-heic' }), imagePost({ id: 'synthetic-bmp' })]) + const samples = [Buffer.concat([Buffer.from([0, 0, 0, 24]), Buffer.from('ftypheic'), Buffer.alloc(12)]), Buffer.from('424d00000000', 'hex')] + for (const bytes of samples) await db.saveMedia(db.nextMedia().id, { data: bytes }, 'original') + assert.equal(db.stats().savedMedia, 2) +}) + +test('retention is inert when disabled', async () => { + let reads = 0; let downloads = 0 + const { snsRetentionService: service, configuration } = makeService({ + getTimeline: async () => { reads++; return { success: true, timeline: [imagePost()] } }, + downloadImage: async () => { downloads++; return { success: false } } + }) + configuration.snsRetentionEnabled = false + service.start(); service.captureVisible([imagePost()]); await service.scanNow() + assert.equal(reads, 0); assert.equal(downloads, 0) + assert.equal(service.status().totalPosts, 0) +}) diff --git a/src/App.tsx b/src/App.tsx index d2f1bae..694bb2d 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -41,6 +41,8 @@ const ChatHistoryPage = lazy(() => import('./pages/ChatHistoryPage')) const NotificationWindow = lazy(() => import('./pages/NotificationWindow')) const AccountManagementPage = lazy(() => import('./pages/AccountManagementPage')) const BackupPage = lazy(() => import('./pages/BackupPage')) +const MessageRetentionPage = lazy(() => import('./pages/MessageRetentionPage')) +const SnsRetentionPage = lazy(() => import('./pages/SnsRetentionPage')) const InsightInboxPage = lazy(() => import('./pages/InsightInboxPage')) const AnalyticsPage = lazy(() => import('./pages/AnalyticsPage')) const GroupAnalyticsPage = lazy(() => import('./pages/GroupAnalyticsPage')) @@ -854,6 +856,8 @@ function App() { } /> } /> } /> + } /> + } /> } /> } /> diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 94b9eba..85a1d0c 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -1,6 +1,6 @@ import { useState, useEffect, useRef } from 'react' import { NavLink, useLocation, useNavigate } from 'react-router-dom' -import { Home, MessageSquare, MessageSquareMore, BarChart3, FileText, Settings, Download, Aperture, UserCircle, Lock, LockOpen, ChevronUp, FolderClosed, Footprints, Users, ArchiveRestore, Sparkles, CircleHelp } from 'lucide-react' +import { Home, MessageSquare, MessageSquareMore, BarChart3, FileText, Settings, Download, Aperture, UserCircle, Lock, LockOpen, ChevronUp, FolderClosed, Footprints, Users, ArchiveRestore, Sparkles, CircleHelp, ShieldCheck } from 'lucide-react' import { useAppStore } from '../stores/appStore' import * as configService from '../services/config' import { onExportSessionStatus, requestExportSessionStatus } from '../services/exportBridge' @@ -447,6 +447,24 @@ function Sidebar({ collapsed }: SidebarProps) { 数据库备份 + + + 消息留存 + + + + + 朋友圈留存 + + diff --git a/src/pages/MessageRetentionPage.scss b/src/pages/MessageRetentionPage.scss new file mode 100644 index 0000000..4cebafa --- /dev/null +++ b/src/pages/MessageRetentionPage.scss @@ -0,0 +1,81 @@ +.message-retention-page { + height: 100%; + overflow-y: auto; + padding: 24px; + color: var(--text-primary); + background: var(--bg-primary); + + &.is-embedded { height: auto; overflow: visible; padding: 0; background: transparent; } + *, *::before, *::after { box-sizing: border-box; } + h1, h2, h3, p { margin: 0; } + h1 { display: flex; align-items: center; gap: 10px; font-size: 23px; font-weight: 650; } + h1 svg { color: var(--primary); } + h2 { font-size: 15px; font-weight: 600; } + button, input { font: inherit; } + button:focus-visible, input:focus-visible { outline: 2px solid var(--primary); outline-offset: 3px; } + + .retention-header { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 22px; } + .retention-header p { margin-top: 8px; color: var(--text-secondary); font-size: 13px; line-height: 1.6; } + .retention-button { display: inline-flex; flex-shrink: 0; align-items: center; justify-content: center; gap: 7px; min-height: 34px; padding: 7px 11px; border: 1px solid var(--border-color); border-radius: 8px; background: var(--bg-secondary); color: var(--text-primary); font-size: 13px; cursor: pointer; } + .retention-button:hover:not(:disabled) { background: var(--bg-tertiary); } + .retention-button:disabled { cursor: not-allowed; opacity: .5; } + .retention-control { padding: 20px; border: 1px solid var(--border-color); border-radius: 12px; background: var(--bg-secondary); } + .retention-control-heading { display: flex; align-items: center; justify-content: space-between; gap: 18px; } + .retention-status { display: inline-flex; align-items: center; gap: 7px; margin-top: 8px; color: var(--text-secondary); font-size: 12px; } + .retention-status i { width: 7px; height: 7px; background: currentColor; border-radius: 50%; } + .retention-status.is-running { color: var(--primary); } + .retention-toggle { display: flex; align-items: center; gap: 9px; position: relative; flex-shrink: 0; cursor: pointer; font-size: 12px; } + .retention-toggle input { position: absolute; left: 0; top: 0; width: 44px; height: 25px; opacity: 0; cursor: pointer; } + .retention-toggle-track { width: 44px; height: 25px; border: 1px solid var(--border-color); background: var(--bg-tertiary); border-radius: 20px; pointer-events: none; transition: background .15s; } + .retention-toggle-track::after { display: block; content: ''; width: 17px; height: 17px; margin: 3px; border-radius: 50%; background: var(--text-secondary); transition: transform .15s; } + .retention-toggle input:checked + .retention-toggle-track { background: var(--primary); border-color: var(--primary); } + .retention-toggle input:checked + .retention-toggle-track::after { transform: translateX(19px); background: var(--on-primary, #fff); } + .retention-toggle input:focus-visible + .retention-toggle-track { outline: 2px solid var(--primary); outline-offset: 3px; } + .retention-toggle input:disabled ~ span { opacity: .5; cursor: not-allowed; } + .retention-explanation { margin-top: 16px; color: var(--text-secondary); font-size: 12px; line-height: 1.8; max-width: 850px; } + .retention-stats { display: grid; grid-template-columns: 1fr 1fr 1.4fr; gap: 18px; margin-top: 18px; padding-top: 17px; border-top: 1px solid var(--border-color); } + .retention-stats > div { display: flex; flex-direction: column; gap: 6px; } + .retention-stats span { color: var(--text-secondary); font-size: 12px; } + .retention-stats strong { font-size: 23px; font-weight: 600; font-variant-numeric: tabular-nums; } + .retention-last-scan strong { font-size: 13px; line-height: 29px; font-weight: 500; } + .retention-error { margin-top: 14px; padding: 12px 14px; border: 1px solid var(--danger, #cf625a); border-radius: 8px; color: var(--danger, #cf625a); font-size: 13px; line-height: 1.6; white-space: pre-wrap; overflow-wrap: anywhere; } + .retention-records { margin-top: 24px; } + .retention-toolbar { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; } + .retention-search { flex: 1; min-width: 180px; display: flex; align-items: center; gap: 9px; padding: 9px 11px; background: var(--bg-secondary); border: 1px solid var(--border-color); border-radius: 9px; color: var(--text-secondary); } + .retention-search input { border: 0; outline: none; background: transparent; color: var(--text-primary); width: 100%; min-width: 0; font-size: 13px; } + .retention-search:focus-within { border-color: var(--primary); } + .retention-search input::-webkit-search-cancel-button { display: none; } + .retention-search button { display: flex; padding: 0; border: 0; background: transparent; color: var(--text-secondary); cursor: pointer; } + .retention-filter { display: inline-flex; align-items: center; gap: 7px; font-size: 13px; cursor: pointer; } + .retention-filter input { accent-color: var(--primary); } + .retention-results-heading { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin: 22px 0 12px; } + .retention-results-heading > span { color: var(--text-secondary); font-size: 12px; } + .retention-empty { padding: 50px 20px; border: 1px dashed var(--border-color); border-radius: 10px; text-align: center; color: var(--text-secondary); } + .retention-empty h3 { margin-top: 12px; font-size: 15px; color: var(--text-primary); font-weight: 500; } + .retention-empty p { margin-top: 8px; font-size: 13px; line-height: 1.6; } + .retention-list { display: flex; flex-direction: column; gap: 12px; } + .retention-message { padding: 15px 17px; border: 1px solid var(--border-color); border-radius: 10px; background: var(--bg-secondary); } + .retention-message.is-recalled { border-left: 3px solid var(--primary); } + .retention-message > header { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; } + .retention-message-identity { display: flex; align-items: baseline; flex-wrap: wrap; gap: 10px; min-width: 0; font-size: 13px; overflow-wrap: anywhere; } + .retention-message-identity strong { font-weight: 600; } + .retention-message-identity > span { color: var(--text-secondary); font-size: 12px; } + .retention-message-meta { display: flex; align-items: center; flex-wrap: wrap; gap: 9px; color: var(--text-secondary); font-size: 11px; } + .retention-recalled-label { padding: 3px 7px; border-radius: 5px; color: var(--primary); background: var(--bg-tertiary); } + .retention-message-content { margin-top: 13px; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 14px; line-height: 1.7; max-height: 340px; overflow: auto; } + .retention-image { display: block; margin-top: 12px; max-width: min(100%, 360px); max-height: 320px; object-fit: contain; object-position: left center; border-radius: 7px; } + .retention-media-note { display: flex; gap: 8px; align-items: center; margin-top: 12px; color: var(--text-secondary); font-size: 12px; } + .retention-missing { margin-top: 13px; padding: 12px; background: var(--bg-tertiary); border-radius: 7px; } + .retention-missing strong { font-size: 13px; font-weight: 600; } + .retention-missing p { margin-top: 6px; color: var(--text-secondary); font-size: 12px; line-height: 1.6; } + .retention-message > footer { margin-top: 13px; color: var(--text-secondary); font-size: 11px; } + .retention-pagination { display: flex; align-items: center; justify-content: flex-end; flex-wrap: wrap; gap: 9px; margin-top: 18px; padding-bottom: 12px; } + .retention-pagination > span { margin-right: auto; color: var(--text-secondary); font-size: 12px; } + + @media (max-width: 760px) { + padding: 16px; + .retention-header { flex-wrap: wrap; } + .retention-stats { grid-template-columns: 1fr 1fr; } + .retention-last-scan { grid-column: 1 / -1; } + } +} diff --git a/src/pages/MessageRetentionPage.tsx b/src/pages/MessageRetentionPage.tsx new file mode 100644 index 0000000..c301982 --- /dev/null +++ b/src/pages/MessageRetentionPage.tsx @@ -0,0 +1,226 @@ +import { useEffect, useState } from 'react' +import { Archive, ChevronLeft, ChevronRight, FolderOpen, ImageOff, RefreshCw, Search, ShieldCheck, X } from 'lucide-react' +import type { RetainedMessage, RetentionStatus } from '../types/electron' +import './MessageRetentionPage.scss' + +const PAGE_SIZE = 30 + +function formatTime(value: number | null | undefined): string { + if (!value || !Number.isFinite(value)) return '尚未扫描' + const date = new Date(value) + return Number.isNaN(date.getTime()) ? '时间未知' : date.toLocaleString('zh-CN', { hour12: false }) +} + +// Media must be a local raster image inside the archive returned by the main +// process. Message content and rawContent are never interpreted as HTML/URLs. +function archiveImage(mediaPath: string | undefined, archivePath: string | undefined): { path: string; url: string } | null { + if (!mediaPath || !archivePath) return null + const normalize = (value: string): string | null => { + const result = value.replace(/\\/g, '/').replace(/\/+$/, '') + if (!result || result.includes('\0') || (!/^\/(?!\/)/.test(result) && !/^[a-z]:\//i.test(result))) return null + if (result.split('/').some(segment => segment === '.' || segment === '..')) return null + return result + } + const file = normalize(mediaPath) + const root = normalize(archivePath) + if (!file || !root || !/\.(?:jpe?g|png|gif|webp|bmp|avif)$/i.test(file)) return null + const windows = /^[a-z]:\//i.test(file) + const comparableFile = windows ? file.toLowerCase() : file + const comparableRoot = windows ? root.toLowerCase() : root + if (!comparableFile.startsWith(`${comparableRoot}/`)) return null + const encoded = file.split('/').map((segment, index) => windows && index === 0 ? segment : encodeURIComponent(segment)).join('/') + return { path: file, url: windows ? `file:///${encoded}` : `file://${encoded}` } +} + +function SavedImage({ item, archivePath, enabled }: { item: RetainedMessage; archivePath?: string; enabled: boolean }) { + const source = item.mediaState === 'saved' ? archiveImage(item.mediaPath, archivePath) : null + const [failed, setFailed] = useState(false) + useEffect(() => setFailed(false), [source?.url]) + + if (source && !failed) { + return 留存的图片 setFailed(true)} /> + } + if (item.mediaState === 'none') return null + return ( +
+ + {item.mediaState === 'pending' ? enabled ? '图片正在尝试保存' : '图片等待保存,自动留存已关闭' : item.mediaState === 'saved' ? '图片暂时无法显示,可打开保存目录查看' : '未能留存图片文件'} +
+ ) +} + +function MessageRetentionPage({ embedded = false }: { embedded?: boolean }) { + const [status, setStatus] = useState(null) + const [items, setItems] = useState([]) + const [total, setTotal] = useState(0) + const [search, setSearch] = useState('') + const [query, setQuery] = useState('') + const [onlyRevoked, setOnlyRevoked] = useState(false) + const [page, setPage] = useState(0) + const [refreshVersion, setRefreshVersion] = useState(0) + const [loading, setLoading] = useState(true) + const [changing, setChanging] = useState(false) + const [error, setError] = useState('') + const [actionError, setActionError] = useState('') + + useEffect(() => { + const timer = window.setTimeout(() => { + setQuery(search.trim()) + setPage(0) + }, 300) + return () => window.clearTimeout(timer) + }, [search]) + + useEffect(() => { + let cancelled = false + let pending = false + setLoading(true) + const refresh = async () => { + if (pending) return + pending = true + try { + const [nextStatus, result] = await Promise.all([ + window.electronAPI.messageRetention.status(), + window.electronAPI.messageRetention.list({ query, onlyRevoked, limit: PAGE_SIZE, offset: page * PAGE_SIZE }) + ]) + if (cancelled) return + setStatus(nextStatus) + setItems(result.items) + setTotal(result.total) + setError('') + const lastPage = Math.max(0, Math.ceil(result.total / PAGE_SIZE) - 1) + if (page > lastPage) setPage(lastPage) + } catch (cause) { + if (!cancelled) setError(cause instanceof Error ? cause.message : '读取留存记录失败,请重试。') + } finally { + pending = false + if (!cancelled) setLoading(false) + } + } + void refresh() + const timer = window.setInterval(() => { void refresh() }, 5000) + return () => { + cancelled = true + window.clearInterval(timer) + } + }, [query, onlyRevoked, page, refreshVersion]) + + const changeEnabled = async (enabled: boolean) => { + setChanging(true) + setActionError('') + try { + setStatus(await window.electronAPI.messageRetention.setEnabled(enabled)) + setRefreshVersion(value => value + 1) + } catch (cause) { + setActionError(cause instanceof Error ? cause.message : '无法更新留存设置,请重试。') + } finally { + setChanging(false) + } + } + + const openArchive = async () => { + if (!status?.archivePath) return + setActionError('') + try { + const result = await window.electronAPI.shell.openPath(status.archivePath) + if (result) setActionError('无法打开保存目录,请稍后重试。') + } catch { + setActionError('无法打开保存目录,请稍后重试。') + } + } + + const pageCount = Math.max(1, Math.ceil(total / PAGE_SIZE)) + const stateLabel = !status ? '正在读取状态' : !status.enabled ? '已关闭' : status.error ? '留存异常' : status.running ? '正在留存' : '等待连接' + const count = (value: number | undefined) => value == null ? '—' : value.toLocaleString('zh-CN') + + return ( +
+
+
+

消息留存

+

保存本机收到的消息,撤回后仍可查看已留存的副本。

+
+ +
+ +
+
+
+

自动留存消息

+ +