Optimize getAdjacentMessagesByConversationSync
This commit is contained in:
parent
0032d49e23
commit
6e5d9f5dd8
1 changed files with 23 additions and 16 deletions
|
@ -2435,26 +2435,27 @@ function getAdjacentMessagesByConversationSync(
|
||||||
): Array<MessageTypeUnhydrated> {
|
): Array<MessageTypeUnhydrated> {
|
||||||
const db = getInstance();
|
const db = getInstance();
|
||||||
|
|
||||||
const timeFilter =
|
let timeFilters: { first: QueryFragment; second: QueryFragment };
|
||||||
direction === AdjacentDirection.Older
|
let timeOrder: QueryFragment;
|
||||||
? sqlFragment`
|
|
||||||
(received_at = ${receivedAt} AND sent_at < ${sentAt}) OR
|
|
||||||
received_at < ${receivedAt}
|
|
||||||
`
|
|
||||||
: sqlFragment`
|
|
||||||
(received_at = ${receivedAt} AND sent_at > ${sentAt}) OR
|
|
||||||
received_at > ${receivedAt}
|
|
||||||
`;
|
|
||||||
|
|
||||||
const timeOrder =
|
if (direction === AdjacentDirection.Older) {
|
||||||
direction === AdjacentDirection.Older
|
timeFilters = {
|
||||||
? sqlFragment`DESC`
|
first: sqlFragment`received_at = ${receivedAt} AND sent_at < ${sentAt}`,
|
||||||
: sqlFragment`ASC`;
|
second: sqlFragment`received_at < ${receivedAt}`,
|
||||||
|
};
|
||||||
|
timeOrder = sqlFragment`DESC`;
|
||||||
|
} else {
|
||||||
|
timeFilters = {
|
||||||
|
first: sqlFragment`received_at = ${receivedAt} AND sent_at > ${sentAt}`,
|
||||||
|
second: sqlFragment`received_at > ${receivedAt}`,
|
||||||
|
};
|
||||||
|
timeOrder = sqlFragment`ASC`;
|
||||||
|
}
|
||||||
|
|
||||||
const requireDifferentMessage =
|
const requireDifferentMessage =
|
||||||
direction === AdjacentDirection.Older || requireVisualMediaAttachments;
|
direction === AdjacentDirection.Older || requireVisualMediaAttachments;
|
||||||
|
|
||||||
let template = sqlFragment`
|
const createQuery = (timeFilter: QueryFragment): QueryFragment => sqlFragment`
|
||||||
SELECT json FROM messages WHERE
|
SELECT json FROM messages WHERE
|
||||||
conversationId = ${conversationId} AND
|
conversationId = ${conversationId} AND
|
||||||
${
|
${
|
||||||
|
@ -2472,7 +2473,13 @@ function getAdjacentMessagesByConversationSync(
|
||||||
(
|
(
|
||||||
${timeFilter}
|
${timeFilter}
|
||||||
)
|
)
|
||||||
ORDER BY received_at ${timeOrder}, sent_at ${timeOrder}
|
ORDER BY received_at ${timeOrder}, sent_at ${timeOrder}
|
||||||
|
`;
|
||||||
|
|
||||||
|
let template = sqlFragment`
|
||||||
|
SELECT first.json FROM (${createQuery(timeFilters.first)}) as first
|
||||||
|
UNION ALL
|
||||||
|
SELECT second.json FROM (${createQuery(timeFilters.second)}) as second
|
||||||
`;
|
`;
|
||||||
|
|
||||||
// See `filterValidAttachments` in ts/state/ducks/lightbox.ts
|
// See `filterValidAttachments` in ts/state/ducks/lightbox.ts
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue