Cleaned up SQL for getMessagesByConversation.
This commit is contained in:
parent
f2dbdff548
commit
33ee45b819
49
app/sql.js
49
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;
|
||||
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));
|
||||
}
|
||||
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue