New getRecentStoryReplies function to clean up replies in multiple convos

This commit is contained in:
Scott Nonnenberg 2023-07-21 15:10:32 -07:00 committed by GitHub
parent ca84d637ae
commit 716f852970
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 356 additions and 63 deletions

View file

@ -93,6 +93,7 @@ import type {
GetAllStoriesResultType,
GetConversationRangeCenteredOnMessageResultType,
GetKnownMessageAttachmentsResultType,
GetRecentStoryRepliesOptionsType,
GetUnreadByConversationAndMarkReadResultType,
IdentityKeyIdType,
StoredIdentityKeyType,
@ -251,6 +252,7 @@ const dataInterface: ServerInterface = {
getMessageCount,
getStoryCount,
getRecentStoryReplies,
saveMessage,
saveMessages,
removeMessage,
@ -2530,6 +2532,53 @@ enum AdjacentDirection {
Newer = 'Newer',
}
async function getRecentStoryReplies(
storyId: string,
options?: GetRecentStoryRepliesOptionsType
): Promise<Array<MessageTypeUnhydrated>> {
return getRecentStoryRepliesSync(storyId, options);
}
// This function needs to pull story replies from all conversations, because when we send
// a story to one or more distribution lists, each reply to it will be in the sender's
// 1:1 conversation with us.
function getRecentStoryRepliesSync(
storyId: string,
{
limit = 100,
messageId,
receivedAt = Number.MAX_VALUE,
sentAt = Number.MAX_VALUE,
}: GetRecentStoryRepliesOptionsType = {}
): Array<MessageTypeUnhydrated> {
const db = getInstance();
const timeFilters = {
first: sqlFragment`received_at = ${receivedAt} AND sent_at < ${sentAt}`,
second: sqlFragment`received_at < ${receivedAt}`,
};
const createQuery = (timeFilter: QueryFragment): QueryFragment => sqlFragment`
SELECT json FROM messages WHERE
(${messageId} IS NULL OR id IS NOT ${messageId}) AND
isStory IS 0 AND
storyId IS ${storyId} AND
(
${timeFilter}
)
ORDER BY received_at DESC, sent_at DESC
`;
const template = sqlFragment`
SELECT first.json FROM (${createQuery(timeFilters.first)}) as first
UNION ALL
SELECT second.json FROM (${createQuery(timeFilters.second)}) as second
`;
const [query, params] = sql`${template} LIMIT ${limit}`;
return db.prepare(query).all(params);
}
function getAdjacentMessagesByConversationSync(
direction: AdjacentDirection,
{