diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 78b1ac312..761d09dfe 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -121,6 +121,7 @@ const { } = window.Signal.Migrations; const { addStickerPackReference, + getConversationRangeCenteredOnMessage, getOlderMessagesByConversation, getMessageMetricsForConversation, getMessageById, @@ -1576,19 +1577,14 @@ export class ConversationModel extends window.Backbone const receivedAt = message.received_at; const sentAt = message.sent_at; - const older = await getOlderMessagesByConversation(conversationId, { - limit: MESSAGE_LOAD_CHUNK_SIZE, - receivedAt, - sentAt, - messageId, - }); - const newer = await getNewerMessagesByConversation(conversationId, { - limit: MESSAGE_LOAD_CHUNK_SIZE, - receivedAt, - sentAt, - }); - const metrics = await getMessageMetricsForConversation(conversationId); - + const { older, newer, metrics } = + await getConversationRangeCenteredOnMessage({ + conversationId, + limit: MESSAGE_LOAD_CHUNK_SIZE, + receivedAt, + sentAt, + messageId, + }); const all = [...older, message, ...newer]; const cleaned: Array = await this.cleanModels(all); diff --git a/ts/sql/Client.ts b/ts/sql/Client.ts index ec869912c..99f0e7a78 100644 --- a/ts/sql/Client.ts +++ b/ts/sql/Client.ts @@ -240,6 +240,7 @@ const dataInterface: ClientInterface = { getOlderStories, getNewerMessagesByConversation, getMessageMetricsForConversation, + getConversationRangeCenteredOnMessage, getLastConversationMessages, hasGroupCallHistoryMessage, migrateConversationMessages, @@ -1318,6 +1319,23 @@ async function getMessageMetricsForConversation( return result; } +async function getConversationRangeCenteredOnMessage(options: { + conversationId: string; + limit?: number; + messageId: string; + receivedAt: number; + sentAt?: number; + storyId?: UUIDStringType; +}) { + const result = await channels.getConversationRangeCenteredOnMessage(options); + + return { + ...result, + older: handleMessageJSON(result.older), + newer: handleMessageJSON(result.newer), + }; +} + function hasGroupCallHistoryMessage( conversationId: string, eraId: string diff --git a/ts/sql/Interface.ts b/ts/sql/Interface.ts index de3881cf6..6f73ea884 100644 --- a/ts/sql/Interface.ts +++ b/ts/sql/Interface.ts @@ -451,6 +451,7 @@ export type DataInterface = { conversationId: string, storyId?: UUIDStringType ) => Promise; + // getConversationRangeCenteredOnMessage is JSON on server, full message on client getLastConversationMessages: (options: { conversationId: string; ourUuid: UUIDStringType; @@ -622,6 +623,18 @@ export type ServerInterface = DataInterface & { storyId?: UUIDStringType; } ) => Promise>; + getConversationRangeCenteredOnMessage: (options: { + conversationId: string; + limit?: number; + messageId: string; + receivedAt: number; + sentAt?: number; + storyId?: UUIDStringType; + }) => Promise<{ + older: Array; + newer: Array; + metrics: ConversationMetricsType; + }>; // Server-only @@ -681,6 +694,18 @@ export type ClientInterface = DataInterface & { storyId?: UUIDStringType; } ) => Promise>; + getConversationRangeCenteredOnMessage: (options: { + conversationId: string; + limit?: number; + messageId: string; + receivedAt: number; + sentAt?: number; + storyId?: UUIDStringType; + }) => Promise<{ + older: Array; + newer: Array; + metrics: ConversationMetricsType; + }>; // Client-side only diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index e8264e402..5c69c1f96 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -236,6 +236,7 @@ const dataInterface: ServerInterface = { getNewerMessagesByConversation, getTotalUnreadForConversation, getMessageMetricsForConversation, + getConversationRangeCenteredOnMessage, getLastConversationMessages, hasGroupCallHistoryMessage, migrateConversationMessages, @@ -2304,6 +2305,18 @@ async function _removeAllReactions(): Promise { } async function getOlderMessagesByConversation( + conversationId: string, + options?: { + limit?: number; + messageId?: string; + receivedAt?: number; + sentAt?: number; + storyId?: UUIDStringType; + } +): Promise> { + return getOlderMessagesByConversationSync(conversationId, options); +} +function getOlderMessagesByConversationSync( conversationId: string, { limit = 100, @@ -2318,7 +2331,7 @@ async function getOlderMessagesByConversation( sentAt?: number; storyId?: UUIDStringType; } = {} -): Promise> { +): Array { const db = getInstance(); return db @@ -2390,6 +2403,17 @@ async function getOlderStories({ } async function getNewerMessagesByConversation( + conversationId: string, + options?: { + limit?: number; + receivedAt?: number; + sentAt?: number; + storyId?: UUIDStringType; + } +): Promise> { + return getNewerMessagesByConversationSync(conversationId, options); +} +function getNewerMessagesByConversationSync( conversationId: string, { limit = 100, @@ -2402,7 +2426,7 @@ async function getNewerMessagesByConversation( sentAt?: number; storyId?: UUIDStringType; } = {} -): Promise> { +): Array { const db = getInstance(); const rows: JSONRows = db .prepare( @@ -2609,6 +2633,12 @@ async function getTotalUnreadForConversation( conversationId: string, storyId?: UUIDStringType ): Promise { + return getTotalUnreadForConversationSync(conversationId, storyId); +} +function getTotalUnreadForConversationSync( + conversationId: string, + storyId?: UUIDStringType +): number { const db = getInstance(); const row = db .prepare( @@ -2638,13 +2668,19 @@ async function getMessageMetricsForConversation( conversationId: string, storyId?: UUIDStringType ): Promise { + return getMessageMetricsForConversationSync(conversationId, storyId); +} +function getMessageMetricsForConversationSync( + conversationId: string, + storyId?: UUIDStringType +): ConversationMetricsType { const oldest = getOldestMessageForConversation(conversationId, storyId); const newest = getNewestMessageForConversation(conversationId, storyId); const oldestUnread = getOldestUnreadMessageForConversation( conversationId, storyId ); - const totalUnread = await getTotalUnreadForConversation( + const totalUnread = getTotalUnreadForConversationSync( conversationId, storyId ); @@ -2659,6 +2695,47 @@ async function getMessageMetricsForConversation( }; } +async function getConversationRangeCenteredOnMessage({ + conversationId, + limit, + messageId, + receivedAt, + sentAt, + storyId, +}: { + conversationId: string; + limit?: number; + messageId: string; + receivedAt: number; + sentAt?: number; + storyId?: UUIDStringType; +}): Promise<{ + older: Array; + newer: Array; + metrics: ConversationMetricsType; +}> { + const db = getInstance(); + + return db.transaction(() => { + return { + older: getOlderMessagesByConversationSync(conversationId, { + limit, + messageId, + receivedAt, + sentAt, + storyId, + }), + newer: getNewerMessagesByConversationSync(conversationId, { + limit, + receivedAt, + sentAt, + storyId, + }), + metrics: getMessageMetricsForConversationSync(conversationId, storyId), + }; + })(); +} + async function hasGroupCallHistoryMessage( conversationId: string, eraId: string