diff --git a/lib/utils/message-ops-utils.js b/lib/utils/message-ops-utils.js --- a/lib/utils/message-ops-utils.js +++ b/lib/utils/message-ops-utils.js @@ -19,6 +19,9 @@ assertMessageType, type MessageStoreOperation, type ClientDBMessageStoreOperation, + type ClientDBThreadMessageInfo, + type ThreadMessageInfo, + type MessageStoreThreads, } from '../types/message-types.js'; import type { MediaMessageServerDBContent } from '../types/messages/media.js'; @@ -228,17 +231,48 @@ ); } +function translateThreadMessageInfoToClientDBThreadMessageInfo( + id: string, + threadMessageInfo: ThreadMessageInfo, +): ClientDBThreadMessageInfo { + const startReached = threadMessageInfo.startReached ? 1 : 0; + const lastNavigatedTo = threadMessageInfo.lastNavigatedTo ?? 0; + const lastPruned = threadMessageInfo.lastPruned ?? 0; + return { + id, + start_reached: startReached.toString(), + last_navigated_to: lastNavigatedTo.toString(), + last_pruned: lastPruned.toString(), + }; +} + function convertMessageStoreOperationsToClientDBOperations( messageStoreOperations: $ReadOnlyArray, ): $ReadOnlyArray { const convertedOperations = messageStoreOperations.map( messageStoreOperation => { - if ( - messageStoreOperation.type === 'replace_threads' || - messageStoreOperation.type === 'remove_threads' || - messageStoreOperation.type === 'remove_all_threads' - ) { - return undefined; + if (messageStoreOperation.type === 'replace_threads') { + const threadMessageInfo: MessageStoreThreads = + messageStoreOperation.payload.threads; + const dbThreadMessageInfos: ClientDBThreadMessageInfo[] = []; + for (const threadID in threadMessageInfo) { + dbThreadMessageInfos.push( + translateThreadMessageInfoToClientDBThreadMessageInfo( + threadID, + threadMessageInfo[threadID], + ), + ); + } + if (dbThreadMessageInfos.length === 0) { + return undefined; + } + + return { + type: 'replace_threads', + payload: { + threads: dbThreadMessageInfos, + }, + }; } if (messageStoreOperation.type !== 'replace') { return messageStoreOperation;