From 33ee45b819133dae2a039f812ea54cc1e4f4cd3d Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 13 Nov 2018 15:35:45 +1100 Subject: [PATCH] Cleaned up SQL for getMessagesByConversation. --- app/sql.js | 49 ++++++++++++++-------------------------------- js/modules/data.js | 2 +- 2 files changed, 16 insertions(+), 35 deletions(-) diff --git a/app/sql.js b/app/sql.js index 15e23543f..e87df9130 100644 --- a/app/sql.js +++ b/app/sql.js @@ -1310,42 +1310,23 @@ async function getUnreadByConversation(conversationId) { async function getMessagesByConversation( conversationId, - { limit = 100, receivedAt = Number.MAX_VALUE, type = null } = {} + { limit = 100, receivedAt = Number.MAX_VALUE, type = '%' } = {} ) { - let rows = []; - // Filter by a specific type of message - if (type) { - rows = await db.all(` - SELECT json FROM messages WHERE - conversationId = $conversationId AND - received_at < $received_at AND - 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; + const rows = await db.all(` + SELECT json FROM messages WHERE + conversationId = $conversationId AND + received_at < $received_at AND + type LIKE $type + ORDER BY received_at DESC + LIMIT $limit; `, - { - $conversationId: conversationId, - $received_at: receivedAt, - $limit: limit, - } - ); - } - + { + $conversationId: conversationId, + $received_at: receivedAt, + $limit: limit, + $type: type, + } + ); return map(rows, row => jsonToObject(row.json)); } diff --git a/js/modules/data.js b/js/modules/data.js index 6e1d1c682..330e579a0 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -776,7 +776,7 @@ async function getUnreadByConversation(conversationId, { MessageCollection }) { async function getMessagesByConversation( conversationId, - { limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection, type = null } + { limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection, type = '%' } ) { const messages = await channels.getMessagesByConversation(conversationId, { limit,