|
| 1 | +import { writeTextFile } from "@tauri-apps/plugin-fs"; |
| 2 | +import { createCustomPersister } from "tinybase/persisters/with-schemas"; |
| 3 | +import type { MergeableStore, OptionalSchemas } from "tinybase/with-schemas"; |
| 4 | + |
| 5 | +import type { ChatMessageStorage } from "@hypr/store"; |
| 6 | + |
| 7 | +import { |
| 8 | + ensureDirsExist, |
| 9 | + getChatDir, |
| 10 | + getDataDir, |
| 11 | + iterateTableRows, |
| 12 | + type TablesContent, |
| 13 | +} from "./utils"; |
| 14 | + |
| 15 | +type ChatGroupData = { |
| 16 | + id: string; |
| 17 | + user_id: string; |
| 18 | + created_at: string; |
| 19 | + title: string; |
| 20 | +}; |
| 21 | + |
| 22 | +type ChatMessageWithId = ChatMessageStorage & { id: string }; |
| 23 | + |
| 24 | +type ChatJson = { |
| 25 | + chat_group: ChatGroupData; |
| 26 | + messages: ChatMessageWithId[]; |
| 27 | +}; |
| 28 | + |
| 29 | +function collectMessagesByChatGroup( |
| 30 | + tables: TablesContent | undefined, |
| 31 | +): Map<string, { chatGroup: ChatGroupData; messages: ChatMessageWithId[] }> { |
| 32 | + const messagesByChatGroup = new Map< |
| 33 | + string, |
| 34 | + { chatGroup: ChatGroupData; messages: ChatMessageWithId[] } |
| 35 | + >(); |
| 36 | + |
| 37 | + const chatGroups = iterateTableRows(tables, "chat_groups"); |
| 38 | + const chatMessages = iterateTableRows(tables, "chat_messages"); |
| 39 | + |
| 40 | + const chatGroupMap = new Map<string, ChatGroupData>(); |
| 41 | + for (const group of chatGroups) { |
| 42 | + chatGroupMap.set(group.id, group); |
| 43 | + } |
| 44 | + |
| 45 | + for (const message of chatMessages) { |
| 46 | + const chatGroupId = message.chat_group_id; |
| 47 | + if (!chatGroupId) continue; |
| 48 | + |
| 49 | + const chatGroup = chatGroupMap.get(chatGroupId); |
| 50 | + if (!chatGroup) continue; |
| 51 | + |
| 52 | + const existing = messagesByChatGroup.get(chatGroupId); |
| 53 | + if (existing) { |
| 54 | + existing.messages.push(message); |
| 55 | + } else { |
| 56 | + messagesByChatGroup.set(chatGroupId, { |
| 57 | + chatGroup, |
| 58 | + messages: [message], |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + return messagesByChatGroup; |
| 64 | +} |
| 65 | + |
| 66 | +export function createChatPersister<Schemas extends OptionalSchemas>( |
| 67 | + store: MergeableStore<Schemas>, |
| 68 | +) { |
| 69 | + return createCustomPersister( |
| 70 | + store, |
| 71 | + async () => { |
| 72 | + return undefined; |
| 73 | + }, |
| 74 | + async (getContent) => { |
| 75 | + const [tables] = getContent() as [TablesContent | undefined, unknown]; |
| 76 | + const dataDir = await getDataDir(); |
| 77 | + |
| 78 | + const messagesByChatGroup = collectMessagesByChatGroup(tables); |
| 79 | + if (messagesByChatGroup.size === 0) { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + const dirs = new Set<string>(); |
| 84 | + const writeOperations: Array<{ path: string; content: string }> = []; |
| 85 | + |
| 86 | + for (const [ |
| 87 | + chatGroupId, |
| 88 | + { chatGroup, messages }, |
| 89 | + ] of messagesByChatGroup) { |
| 90 | + const chatDir = getChatDir(dataDir, chatGroupId); |
| 91 | + dirs.add(chatDir); |
| 92 | + |
| 93 | + const json: ChatJson = { |
| 94 | + chat_group: chatGroup, |
| 95 | + messages: messages.sort( |
| 96 | + (a, b) => |
| 97 | + new Date(a.created_at || 0).getTime() - |
| 98 | + new Date(b.created_at || 0).getTime(), |
| 99 | + ), |
| 100 | + }; |
| 101 | + writeOperations.push({ |
| 102 | + path: `${chatDir}/_messages.json`, |
| 103 | + content: JSON.stringify(json, null, 2), |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + await ensureDirsExist(dirs); |
| 109 | + } catch (e) { |
| 110 | + console.error("Failed to ensure dirs exist:", e); |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + for (const op of writeOperations) { |
| 115 | + try { |
| 116 | + await writeTextFile(op.path, op.content); |
| 117 | + } catch (e) { |
| 118 | + console.error(`Failed to write ${op.path}:`, e); |
| 119 | + } |
| 120 | + } |
| 121 | + }, |
| 122 | + (listener) => setInterval(listener, 1000), |
| 123 | + (interval) => clearInterval(interval), |
| 124 | + ); |
| 125 | +} |
0 commit comments