Cleaned up SQL for getMessagesByConversation.

This commit is contained in:
Mikunj 2018-11-13 15:35:45 +11:00
parent f2dbdff548
commit 33ee45b819
2 changed files with 16 additions and 35 deletions

View File

@ -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;
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,
$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,
}
);
}
{
$conversationId: conversationId,
$received_at: receivedAt,
$limit: limit,
$type: type,
}
);
return map(rows, row => jsonToObject(row.json));
}

View File

@ -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,