Skip to content

Commit

Permalink
Always provide isGroup/storyId to message-fetching functions
Browse files Browse the repository at this point in the history
  • Loading branch information
scottnonnenberg-signal committed May 11, 2022
1 parent e1392a2 commit 69d0ed3
Show file tree
Hide file tree
Showing 6 changed files with 74 additions and 37 deletions.
16 changes: 11 additions & 5 deletions ts/models/conversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MessageModel> = await this.cleanModels(messages);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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];

Expand Down Expand Up @@ -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,
}
);

Expand Down
13 changes: 8 additions & 5 deletions ts/sql/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
26 changes: 14 additions & 12 deletions ts/sql/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Array<MessageTypeUnhydrated>>;
getNewerMessagesByConversation: (
conversationId: string,
options?: {
isGroup?: boolean;
options: {
isGroup: boolean;
limit?: number;
receivedAt?: number;
sentAt?: number;
storyId?: UUIDStringType;
storyId: UUIDStringType | undefined;
}
) => Promise<Array<MessageTypeUnhydrated>>;
getConversationRangeCenteredOnMessage: (options: {
conversationId: string;
isGroup: boolean;
limit?: number;
messageId: string;
receivedAt: number;
sentAt?: number;
storyId?: UUIDStringType;
storyId: UUIDStringType | undefined;
}) => Promise<{
older: Array<MessageTypeUnhydrated>;
newer: Array<MessageTypeUnhydrated>;
Expand Down Expand Up @@ -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<Array<MessageAttributesType>>;
getNewerMessagesByConversation: (
conversationId: string,
options: {
isGroup?: boolean;
isGroup: boolean;
limit?: number;
receivedAt?: number;
sentAt?: number;
storyId?: UUIDStringType;
storyId: UUIDStringType | undefined;
}
) => Promise<Array<MessageAttributesType>>;
getConversationRangeCenteredOnMessage: (options: {
conversationId: string;
isGroup: boolean;
limit?: number;
messageId: string;
receivedAt: number;
sentAt?: number;
storyId?: UUIDStringType;
storyId: UUIDStringType | undefined;
}) => Promise<{
older: Array<MessageAttributesType>;
newer: Array<MessageAttributesType>;
Expand Down
29 changes: 17 additions & 12 deletions ts/sql/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2352,13 +2352,13 @@ async function _removeAllReactions(): Promise<void> {

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<Array<MessageTypeUnhydrated>> {
return getOlderMessagesByConversationSync(conversationId, options);
Expand All @@ -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<MessageTypeUnhydrated> {
const db = getInstance();

Expand Down Expand Up @@ -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<Array<MessageTypeUnhydrated>> {
return getNewerMessagesByConversationSync(conversationId, options);
Expand All @@ -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<MessageTypeUnhydrated> {
const db = getInstance();
const rows: JSONRows = db
Expand Down Expand Up @@ -2830,18 +2831,20 @@ function getMessageMetricsForConversationSync(

async function getConversationRangeCenteredOnMessage({
conversationId,
isGroup,
limit,
messageId,
receivedAt,
sentAt,
storyId,
}: {
conversationId: string;
isGroup: boolean;
limit?: number;
messageId: string;
receivedAt: number;
sentAt?: number;
storyId?: UUIDStringType;
storyId: UUIDStringType | undefined;
}): Promise<{
older: Array<MessageTypeUnhydrated>;
newer: Array<MessageTypeUnhydrated>;
Expand All @@ -2852,13 +2855,15 @@ async function getConversationRangeCenteredOnMessage({
return db.transaction(() => {
return {
older: getOlderMessagesByConversationSync(conversationId, {
isGroup,
limit,
messageId,
receivedAt,
sentAt,
storyId,
}),
newer: getNewerMessagesByConversationSync(conversationId, {
isGroup,
limit,
receivedAt,
sentAt,
Expand Down
7 changes: 5 additions & 2 deletions ts/state/ducks/stories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -133,10 +135,11 @@ function loadStoryReplies(
conversationId: string,
messageId: string
): ThunkAction<void, RootStateType, unknown, LoadStoryRepliesActionType> {
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({
Expand Down

0 comments on commit 69d0ed3

Please sign in to comment.