MCPcopy Index your code
hub / github.com/ChatLab/ChatLab / writeMessages

Function writeMessages

packages/node-runtime/src/services/push-importer.ts:143–221  ·  view source on GitHub ↗
(
  db: DatabaseAdapter,
  messages: PushImportMessage[],
  existingPmids: Set<string>,
  existingKeys: Set<string>
)

Source from the content-addressed store, hash-verified

141}
142
143function writeMessages(
144 db: DatabaseAdapter,
145 messages: PushImportMessage[],
146 existingPmids: Set<string>,
147 existingKeys: Set<string>
148): {
149 writtenCount: number
150 duplicateCount: number
151 minWrittenTs: number
152 ftsEntries: Array<{ id: number; content: string | null }>
153} {
154 const insertMsg = db.prepare(
155 `INSERT INTO message (sender_id, sender_account_name, sender_group_nickname, ts, type, content, reply_to_message_id, platform_message_id)
156 VALUES (?, ?, ?, ?, ?, ?, ?, ?)`
157 )
158 const getMemberId = db.prepare('SELECT id FROM member WHERE platform_id = ?')
159 const insertMinimalMember = db.prepare('INSERT OR IGNORE INTO member (platform_id, account_name) VALUES (?, ?)')
160
161 const memberIdCache = new Map<string, number>()
162 let writtenCount = 0
163 let duplicateCount = 0
164 let minWrittenTs = Infinity
165 const ftsEntries: Array<{ id: number; content: string | null }> = []
166
167 const sorted = [...messages].sort((a, b) => a.timestamp - b.timestamp)
168
169 db.transaction(() => {
170 for (const msg of sorted) {
171 const key = generateMessageKey(msg.timestamp, msg.sender, msg.content ?? null)
172 if (msg.platformMessageId) {
173 if (existingPmids.has(msg.platformMessageId)) {
174 duplicateCount++
175 continue
176 }
177 if (existingKeys.has(key)) {
178 duplicateCount++
179 continue
180 }
181 existingPmids.add(msg.platformMessageId)
182 // Also register the content hash so a same-content no-pmid copy later
183 // in this batch is caught by the fallback dedup path.
184 existingKeys.add(key)
185 } else {
186 if (existingKeys.has(key)) {
187 duplicateCount++
188 continue
189 }
190 existingKeys.add(key)
191 }
192
193 let memberId = memberIdCache.get(msg.sender)
194 if (!memberId) {
195 insertMinimalMember.run(msg.sender, normalizeSenderAccountName(msg.sender, msg.accountName) || null)
196 const row = getMemberId.get(msg.sender) as { id: number } | undefined
197 if (row) {
198 memberId = row.id
199 memberIdCache.set(msg.sender, memberId)
200 }

Callers 1

incrementalImportFunction · 0.85

Calls 9

generateMessageKeyFunction · 0.90
setMethod · 0.80
prepareMethod · 0.65
transactionMethod · 0.65
getMethod · 0.65
runMethod · 0.65
addMethod · 0.45

Tested by

no test coverage detected