loadAndScroll: Use one transaction to pull all data: old/new/metrics

This commit is contained in:
Scott Nonnenberg 2021-12-20 13:05:13 -08:00 committed by GitHub
parent 60a53656af
commit 0163ef203b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 132 additions and 16 deletions

View file

@ -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<MessageModel> = await this.cleanModels(all);

View file

@ -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

View file

@ -451,6 +451,7 @@ export type DataInterface = {
conversationId: string,
storyId?: UUIDStringType
) => Promise<ConversationMetricsType>;
// 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<Array<MessageTypeUnhydrated>>;
getConversationRangeCenteredOnMessage: (options: {
conversationId: string;
limit?: number;
messageId: string;
receivedAt: number;
sentAt?: number;
storyId?: UUIDStringType;
}) => Promise<{
older: Array<MessageTypeUnhydrated>;
newer: Array<MessageTypeUnhydrated>;
metrics: ConversationMetricsType;
}>;
// Server-only
@ -681,6 +694,18 @@ export type ClientInterface = DataInterface & {
storyId?: UUIDStringType;
}
) => Promise<Array<MessageAttributesType>>;
getConversationRangeCenteredOnMessage: (options: {
conversationId: string;
limit?: number;
messageId: string;
receivedAt: number;
sentAt?: number;
storyId?: UUIDStringType;
}) => Promise<{
older: Array<MessageAttributesType>;
newer: Array<MessageAttributesType>;
metrics: ConversationMetricsType;
}>;
// Client-side only

View file

@ -236,6 +236,7 @@ const dataInterface: ServerInterface = {
getNewerMessagesByConversation,
getTotalUnreadForConversation,
getMessageMetricsForConversation,
getConversationRangeCenteredOnMessage,
getLastConversationMessages,
hasGroupCallHistoryMessage,
migrateConversationMessages,
@ -2304,6 +2305,18 @@ async function _removeAllReactions(): Promise<void> {
}
async function getOlderMessagesByConversation(
conversationId: string,
options?: {
limit?: number;
messageId?: string;
receivedAt?: number;
sentAt?: number;
storyId?: UUIDStringType;
}
): Promise<Array<MessageTypeUnhydrated>> {
return getOlderMessagesByConversationSync(conversationId, options);
}
function getOlderMessagesByConversationSync(
conversationId: string,
{
limit = 100,
@ -2318,7 +2331,7 @@ async function getOlderMessagesByConversation(
sentAt?: number;
storyId?: UUIDStringType;
} = {}
): Promise<Array<MessageTypeUnhydrated>> {
): Array<MessageTypeUnhydrated> {
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<Array<MessageTypeUnhydrated>> {
return getNewerMessagesByConversationSync(conversationId, options);
}
function getNewerMessagesByConversationSync(
conversationId: string,
{
limit = 100,
@ -2402,7 +2426,7 @@ async function getNewerMessagesByConversation(
sentAt?: number;
storyId?: UUIDStringType;
} = {}
): Promise<Array<MessageTypeUnhydrated>> {
): Array<MessageTypeUnhydrated> {
const db = getInstance();
const rows: JSONRows = db
.prepare<Query>(
@ -2609,6 +2633,12 @@ async function getTotalUnreadForConversation(
conversationId: string,
storyId?: UUIDStringType
): Promise<number> {
return getTotalUnreadForConversationSync(conversationId, storyId);
}
function getTotalUnreadForConversationSync(
conversationId: string,
storyId?: UUIDStringType
): number {
const db = getInstance();
const row = db
.prepare<Query>(
@ -2638,13 +2668,19 @@ async function getMessageMetricsForConversation(
conversationId: string,
storyId?: UUIDStringType
): Promise<ConversationMetricsType> {
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<MessageTypeUnhydrated>;
newer: Array<MessageTypeUnhydrated>;
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