Further optimize getConversationMessageStats
This commit is contained in:
parent
1c22fe653c
commit
efc54e44c4
1 changed files with 24 additions and 15 deletions
|
@ -2735,35 +2735,44 @@ function getLastConversationPreview({
|
||||||
conversationId: string;
|
conversationId: string;
|
||||||
includeStoryReplies: boolean;
|
includeStoryReplies: boolean;
|
||||||
}): MessageType | undefined {
|
}): MessageType | undefined {
|
||||||
|
type Row = Readonly<{
|
||||||
|
json: string;
|
||||||
|
received_at: number;
|
||||||
|
sent_at: number;
|
||||||
|
}>;
|
||||||
|
|
||||||
const db = getInstance();
|
const db = getInstance();
|
||||||
const row = prepare(
|
|
||||||
db,
|
const queryTemplate = (extraClause: string): string => {
|
||||||
`
|
return `
|
||||||
SELECT json FROM messages
|
SELECT json, received_at, sent_at FROM messages
|
||||||
INDEXED BY messages_preview
|
INDEXED BY messages_preview
|
||||||
WHERE
|
WHERE
|
||||||
conversationId IS $conversationId AND
|
conversationId IS $conversationId AND
|
||||||
shouldAffectPreview IS 1 AND
|
shouldAffectPreview IS 1 AND
|
||||||
isGroupLeaveEventFromOther IS 0 AND
|
isGroupLeaveEventFromOther IS 0 AND
|
||||||
${includeStoryReplies ? '' : 'storyId IS NULL AND'}
|
${includeStoryReplies ? '' : 'storyId IS NULL AND'}
|
||||||
(
|
${extraClause}
|
||||||
expiresAt IS NULL
|
ORDER BY received_at DESC, sent_at DESC
|
||||||
OR
|
LIMIT 1
|
||||||
expiresAt > $now
|
`;
|
||||||
)
|
};
|
||||||
|
|
||||||
|
const row: Row | undefined = prepare(
|
||||||
|
db,
|
||||||
|
`
|
||||||
|
SELECT * FROM (${queryTemplate('expiresAt IS NULL')})
|
||||||
|
UNION ALL
|
||||||
|
SELECT * FROM (${queryTemplate('expiresAt > $now')})
|
||||||
ORDER BY received_at DESC, sent_at DESC
|
ORDER BY received_at DESC, sent_at DESC
|
||||||
LIMIT 1;
|
LIMIT 1;
|
||||||
`
|
`
|
||||||
).get({
|
).get({
|
||||||
conversationId,
|
conversationId,
|
||||||
now: Date.now(),
|
now: Date.now(),
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!row) {
|
return row ? jsonToObject(row.json) : undefined;
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
return jsonToObject(row.json);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getConversationMessageStats({
|
async function getConversationMessageStats({
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue