Cleaned up SQL for getMessagesByConversation.

pull/33/head
Mikunj 6 years ago
parent f2dbdff548
commit 33ee45b819

@ -1310,42 +1310,23 @@ async function getUnreadByConversation(conversationId) {
async function getMessagesByConversation( async function getMessagesByConversation(
conversationId, conversationId,
{ limit = 100, receivedAt = Number.MAX_VALUE, type = null } = {} { limit = 100, receivedAt = Number.MAX_VALUE, type = '%' } = {}
) { ) {
let rows = []; const rows = await db.all(`
// Filter by a specific type of message SELECT json FROM messages WHERE
if (type) { conversationId = $conversationId AND
rows = await db.all(` received_at < $received_at AND
SELECT json FROM messages WHERE type LIKE $type
conversationId = $conversationId AND ORDER BY received_at DESC
received_at < $received_at AND LIMIT $limit;
type = $type
ORDER BY received_at DESC
LIMIT $limit;
`,
{
$conversationId: conversationId,
$received_at: receivedAt,
$limit: limit,
$type: type,
}
);
} 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, $conversationId: conversationId,
$received_at: receivedAt, $received_at: receivedAt,
$limit: limit, $limit: limit,
} $type: type,
); }
} );
return map(rows, row => jsonToObject(row.json)); return map(rows, row => jsonToObject(row.json));
} }

@ -776,7 +776,7 @@ async function getUnreadByConversation(conversationId, { MessageCollection }) {
async function getMessagesByConversation( async function getMessagesByConversation(
conversationId, conversationId,
{ limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection, type = null } { limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection, type = '%' }
) { ) {
const messages = await channels.getMessagesByConversation(conversationId, { const messages = await channels.getMessagesByConversation(conversationId, {
limit, limit,

Loading…
Cancel
Save