import { Logger } from 'besonders-logger' import { CID } from 'multiformats' import { ensureTsPvAndFinalizeApplog } from '../applog/applog-helpers.ts' import { EntityID } from '../applog/datom-types.ts' import { Thread } from '../thread.ts' const { WARN, LOG, DEBUG, VERBOSE, ERROR } = Logger.setup(Logger.INFO) // eslint-disable-line no-unused-vars export type PubPullData = { cid: CID thread: Thread } export function integratePub({ targetThread, agentHash, subID, pubData }: { targetThread: Thread agentHash: EntityID pubData: PubPullData subID?: EntityID }) { const newLogs = pubData.thread.applogs.filter(log => !targetThread.hasApplog(log, false)) DEBUG(`[integratePub] integrating ${newLogs.length} logs`, { targetThread, subID, pubData }) let toInsert = newLogs if (subID) { toInsert = toInsert.concat(ensureTsPvAndFinalizeApplog( { en: subID, at: 'subscription/cid', vl: pubData.cid.toString(), ag: agentHash }, targetThread, )) } targetThread.insertRaw(toInsert) return newLogs }