Cleaned up SQL for getMessagesByConversation.

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

@ -1310,16 +1310,13 @@ 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
if (type) {
rows = await db.all(`
SELECT json FROM messages WHERE SELECT json FROM messages WHERE
conversationId = $conversationId AND conversationId = $conversationId AND
received_at < $received_at AND received_at < $received_at AND
type = $type type LIKE $type
ORDER BY received_at DESC ORDER BY received_at DESC
LIMIT $limit; LIMIT $limit;
`, `,
@ -1330,22 +1327,6 @@ async function getMessagesByConversation(
$type: type, $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,
$received_at: receivedAt,
$limit: limit,
}
);
}
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