Handle messages with the same received_at

This commit is contained in:
Scott Nonnenberg 2020-07-06 10:06:44 -07:00
parent 82bf517a69
commit e536929e35
5 changed files with 60 additions and 19 deletions

View file

@ -2598,21 +2598,45 @@ async function getOlderMessagesByConversation(
{
limit = 100,
receivedAt = Number.MAX_VALUE,
}: { limit?: number; receivedAt?: number } = {}
messageId,
}: { limit?: number; receivedAt?: number; messageId?: string } = {}
) {
if (receivedAt !== Number.MAX_VALUE && !messageId) {
throw new Error('If receivedAt is supplied, messageId should be as well');
}
const db = getInstance();
const rows = await db.all(
`SELECT json FROM messages WHERE
let rows;
if (messageId) {
rows = await db.all(
`SELECT json FROM messages WHERE
conversationId = $conversationId AND
received_at <= $received_at AND
id != $messageId
ORDER BY received_at DESC
LIMIT $limit;`,
{
$conversationId: conversationId,
$received_at: receivedAt,
$limit: limit,
$messageId: messageId,
}
);
} else {
rows = await db.all(
`SELECT json FROM messages WHERE
conversationId = $conversationId AND
received_at < $received_at
ORDER BY received_at DESC
LIMIT $limit;`,
{
$conversationId: conversationId,
$received_at: receivedAt,
$limit: limit,
}
);
{
$conversationId: conversationId,
$received_at: receivedAt,
$limit: limit,
}
);
}
return rows.reverse();
}