From 69d0ed33099fc0ed003e5b27f4db95c778c6eeb1 Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Wed, 11 May 2022 15:20:47 -0700 Subject: [PATCH] Always provide isGroup/storyId to message-fetching functions --- ts/models/conversations.ts | 16 +++++++---- ts/sql/Client.ts | 13 +++++---- ts/sql/Interface.ts | 26 ++++++++++-------- ts/sql/Server.ts | 29 ++++++++++++-------- ts/state/ducks/stories.ts | 7 +++-- ts/test-electron/sql/timelineFetches_test.ts | 20 +++++++++++++- 6 files changed, 74 insertions(+), 37 deletions(-) diff --git a/ts/models/conversations.ts b/ts/models/conversations.ts index 636acff33e..21642373bc 100644 --- a/ts/models/conversations.ts +++ b/ts/models/conversations.ts @@ -1490,6 +1490,7 @@ export class ConversationModel extends window.Backbone const messages = await getOlderMessagesByConversation(conversationId, { isGroup: isGroup(this.attributes), limit: MESSAGE_LOAD_CHUNK_SIZE, + storyId: undefined, }); const cleaned: Array = await this.cleanModels(messages); @@ -1541,10 +1542,11 @@ export class ConversationModel extends window.Backbone const sentAt = message.sent_at; const models = await getOlderMessagesByConversation(conversationId, { isGroup: isGroup(this.attributes), + limit: MESSAGE_LOAD_CHUNK_SIZE, + messageId: oldestMessageId, receivedAt, sentAt, - messageId: oldestMessageId, - limit: MESSAGE_LOAD_CHUNK_SIZE, + storyId: undefined, }); if (models.length < 1) { @@ -1595,9 +1597,10 @@ export class ConversationModel extends window.Backbone const sentAt = message.sent_at; const models = await getNewerMessagesByConversation(conversationId, { isGroup: isGroup(this.attributes), + limit: MESSAGE_LOAD_CHUNK_SIZE, receivedAt, sentAt, - limit: MESSAGE_LOAD_CHUNK_SIZE, + storyId: undefined, }); if (models.length < 1) { @@ -1651,10 +1654,12 @@ export class ConversationModel extends window.Backbone const { older, newer, metrics } = await getConversationRangeCenteredOnMessage({ conversationId, + isGroup: isGroup(this.attributes), limit: MESSAGE_LOAD_CHUNK_SIZE, + messageId, receivedAt, sentAt, - messageId, + storyId: undefined, }); const all = [...older, message, ...newer]; @@ -2035,9 +2040,10 @@ export class ConversationModel extends window.Backbone { isGroup: isGroup(this.attributes), limit: 100, + messageId: first ? first.id : undefined, receivedAt: first ? first.received_at : undefined, sentAt: first ? first.sent_at : undefined, - messageId: first ? first.id : undefined, + storyId: undefined, } ); diff --git a/ts/sql/Client.ts b/ts/sql/Client.ts index 5ce13818d1..f9ab6ee67b 100644 --- a/ts/sql/Client.ts +++ b/ts/sql/Client.ts @@ -1239,12 +1239,12 @@ async function getOlderMessagesByConversation( sentAt = Number.MAX_VALUE, storyId, }: { - isGroup?: boolean; + isGroup: boolean; limit?: number; messageId?: string; receivedAt?: number; sentAt?: number; - storyId?: string; + storyId: string | undefined; } ) { const messages = await channels.getOlderMessagesByConversation( @@ -1280,11 +1280,11 @@ async function getNewerMessagesByConversation( sentAt = 0, storyId, }: { - isGroup?: boolean; + isGroup: boolean; limit?: number; receivedAt?: number; sentAt?: number; - storyId?: UUIDStringType; + storyId: UUIDStringType | undefined; } ) { const messages = await channels.getNewerMessagesByConversation( @@ -1344,11 +1344,12 @@ async function getMessageMetricsForConversation( } async function getConversationRangeCenteredOnMessage(options: { conversationId: string; + isGroup: boolean; limit?: number; messageId: string; receivedAt: number; sentAt?: number; - storyId?: UUIDStringType; + storyId: UUIDStringType | undefined; }) { const result = await channels.getConversationRangeCenteredOnMessage(options); @@ -1390,6 +1391,8 @@ async function removeAllMessagesInConversation( // time so we don't use too much memory. messages = await getOlderMessagesByConversation(conversationId, { limit: chunkSize, + isGroup: true, + storyId: undefined, }); if (!messages.length) { diff --git a/ts/sql/Interface.ts b/ts/sql/Interface.ts index c5311bde1c..9493a51f05 100644 --- a/ts/sql/Interface.ts +++ b/ts/sql/Interface.ts @@ -628,32 +628,33 @@ export type ServerInterface = DataInterface & { getOlderMessagesByConversation: ( conversationId: string, - options?: { - isGroup?: boolean; + options: { + isGroup: boolean; limit?: number; messageId?: string; receivedAt?: number; sentAt?: number; - storyId?: string; + storyId: string | undefined; } ) => Promise>; getNewerMessagesByConversation: ( conversationId: string, - options?: { - isGroup?: boolean; + options: { + isGroup: boolean; limit?: number; receivedAt?: number; sentAt?: number; - storyId?: UUIDStringType; + storyId: UUIDStringType | undefined; } ) => Promise>; getConversationRangeCenteredOnMessage: (options: { conversationId: string; + isGroup: boolean; limit?: number; messageId: string; receivedAt: number; sentAt?: number; - storyId?: UUIDStringType; + storyId: UUIDStringType | undefined; }) => Promise<{ older: Array; newer: Array; @@ -701,31 +702,32 @@ export type ClientInterface = DataInterface & { getOlderMessagesByConversation: ( conversationId: string, options: { - isGroup?: boolean; + isGroup: boolean; limit?: number; messageId?: string; receivedAt?: number; sentAt?: number; - storyId?: string; + storyId: string | undefined; } ) => Promise>; getNewerMessagesByConversation: ( conversationId: string, options: { - isGroup?: boolean; + isGroup: boolean; limit?: number; receivedAt?: number; sentAt?: number; - storyId?: UUIDStringType; + storyId: UUIDStringType | undefined; } ) => Promise>; getConversationRangeCenteredOnMessage: (options: { conversationId: string; + isGroup: boolean; limit?: number; messageId: string; receivedAt: number; sentAt?: number; - storyId?: UUIDStringType; + storyId: UUIDStringType | undefined; }) => Promise<{ older: Array; newer: Array; diff --git a/ts/sql/Server.ts b/ts/sql/Server.ts index e7c6febbf5..1675d13863 100644 --- a/ts/sql/Server.ts +++ b/ts/sql/Server.ts @@ -2352,13 +2352,13 @@ async function _removeAllReactions(): Promise { async function getOlderMessagesByConversation( conversationId: string, - options?: { - isGroup?: boolean; + options: { + isGroup: boolean; limit?: number; messageId?: string; receivedAt?: number; sentAt?: number; - storyId?: string; + storyId: string | undefined; } ): Promise> { return getOlderMessagesByConversationSync(conversationId, options); @@ -2373,13 +2373,13 @@ function getOlderMessagesByConversationSync( sentAt = Number.MAX_VALUE, storyId, }: { - isGroup?: boolean; + isGroup: boolean; limit?: number; messageId?: string; receivedAt?: number; sentAt?: number; - storyId?: string; - } = {} + storyId: string | undefined; + } ): Array { const db = getInstance(); @@ -2453,11 +2453,12 @@ async function getOlderStories({ async function getNewerMessagesByConversation( conversationId: string, - options?: { + options: { + isGroup: boolean; limit?: number; receivedAt?: number; sentAt?: number; - storyId?: UUIDStringType; + storyId: UUIDStringType | undefined; } ): Promise> { return getNewerMessagesByConversationSync(conversationId, options); @@ -2471,12 +2472,12 @@ function getNewerMessagesByConversationSync( sentAt = 0, storyId, }: { - isGroup?: boolean; + isGroup: boolean; limit?: number; receivedAt?: number; sentAt?: number; - storyId?: UUIDStringType; - } = {} + storyId: UUIDStringType | undefined; + } ): Array { const db = getInstance(); const rows: JSONRows = db @@ -2830,6 +2831,7 @@ function getMessageMetricsForConversationSync( async function getConversationRangeCenteredOnMessage({ conversationId, + isGroup, limit, messageId, receivedAt, @@ -2837,11 +2839,12 @@ async function getConversationRangeCenteredOnMessage({ storyId, }: { conversationId: string; + isGroup: boolean; limit?: number; messageId: string; receivedAt: number; sentAt?: number; - storyId?: UUIDStringType; + storyId: UUIDStringType | undefined; }): Promise<{ older: Array; newer: Array; @@ -2852,6 +2855,7 @@ async function getConversationRangeCenteredOnMessage({ return db.transaction(() => { return { older: getOlderMessagesByConversationSync(conversationId, { + isGroup, limit, messageId, receivedAt, @@ -2859,6 +2863,7 @@ async function getConversationRangeCenteredOnMessage({ storyId, }), newer: getNewerMessagesByConversationSync(conversationId, { + isGroup, limit, receivedAt, sentAt, diff --git a/ts/state/ducks/stories.ts b/ts/state/ducks/stories.ts index af56994046..d4a5e9414f 100644 --- a/ts/state/ducks/stories.ts +++ b/ts/state/ducks/stories.ts @@ -33,6 +33,8 @@ import { import { useBoundActions } from '../../hooks/useBoundActions'; import { viewSyncJobQueue } from '../../jobs/viewSyncJobQueue'; import { viewedReceiptsJobQueue } from '../../jobs/viewedReceiptsJobQueue'; +import { isGroup } from '../../util/whatTypeOfConversation'; +import { getConversationSelector } from '../selectors/conversations'; export type StoryDataType = { attachment?: AttachmentType; @@ -133,10 +135,11 @@ function loadStoryReplies( conversationId: string, messageId: string ): ThunkAction { - return async dispatch => { + return async (dispatch, getState) => { + const conversation = getConversationSelector(getState())(conversationId); const replies = await dataInterface.getOlderMessagesByConversation( conversationId, - { limit: 9000, storyId: messageId } + { limit: 9000, storyId: messageId, isGroup: isGroup(conversation) } ); dispatch({ diff --git a/ts/test-electron/sql/timelineFetches_test.ts b/ts/test-electron/sql/timelineFetches_test.ts index 496dd6e7a6..6f04bb817d 100644 --- a/ts/test-electron/sql/timelineFetches_test.ts +++ b/ts/test-electron/sql/timelineFetches_test.ts @@ -93,7 +93,9 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 5); const messages = await getOlderMessagesByConversation(conversationId, { + isGroup: false, limit: 5, + storyId: undefined, }); assert.lengthOf(messages, 3); @@ -148,6 +150,7 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 3); const messages = await getOlderMessagesByConversation(conversationId, { + isGroup: true, limit: 5, storyId, }); @@ -203,6 +206,7 @@ describe('sql/timelineFetches', () => { const messages = await getOlderMessagesByConversation(conversationId, { isGroup: true, limit: 5, + storyId: undefined, }); assert.lengthOf(messages, 1); assert.strictEqual(messages[0].id, message3.id); @@ -251,9 +255,11 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 3); const messages = await getOlderMessagesByConversation(conversationId, { + isGroup: true, limit: 5, receivedAt: target, sentAt: target, + storyId: undefined, }); assert.lengthOf(messages, 1); assert.strictEqual(messages[0].id, message1.id); @@ -302,9 +308,11 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 3); const messages = await getOlderMessagesByConversation(conversationId, { + isGroup: true, limit: 5, receivedAt: target, sentAt: target, + storyId: undefined, }); assert.lengthOf(messages, 2); @@ -357,10 +365,12 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 3); const messages = await getOlderMessagesByConversation(conversationId, { + isGroup: true, limit: 5, + messageId: message2.id, receivedAt: target, sentAt: target, - messageId: message2.id, + storyId: undefined, }); assert.lengthOf(messages, 1); @@ -433,7 +443,9 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 5); const messages = await getNewerMessagesByConversation(conversationId, { + isGroup: false, limit: 5, + storyId: undefined, }); assert.lengthOf(messages, 3); @@ -487,6 +499,7 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 3); const messages = await getNewerMessagesByConversation(conversationId, { + isGroup: true, limit: 5, storyId, }); @@ -538,9 +551,11 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 3); const messages = await getNewerMessagesByConversation(conversationId, { + isGroup: true, limit: 5, receivedAt: target, sentAt: target, + storyId: undefined, }); assert.lengthOf(messages, 1); assert.strictEqual(messages[0].id, message3.id); @@ -593,6 +608,7 @@ describe('sql/timelineFetches', () => { const messages = await getNewerMessagesByConversation(conversationId, { isGroup: true, limit: 5, + storyId: undefined, receivedAt: target, sentAt: target, }); @@ -643,9 +659,11 @@ describe('sql/timelineFetches', () => { assert.lengthOf(await _getAllMessages(), 3); const messages = await getNewerMessagesByConversation(conversationId, { + isGroup: true, limit: 5, receivedAt: target, sentAt: target, + storyId: undefined, }); assert.lengthOf(messages, 2);