Allow fetching conversation messages by a specific type.

pull/33/head
Mikunj 7 years ago
parent 4b7a94c7d0
commit 1150f0f915

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

@ -237,7 +237,10 @@
// Go through the messages and check for any pending friend requests // Go through the messages and check for any pending friend requests
const messages = await window.Signal.Data.getMessagesByConversation( const messages = await window.Signal.Data.getMessagesByConversation(
this.id, this.id,
{ MessageCollection: Whisper.MessageCollection } {
type: 'friend-request',
MessageCollection: Whisper.MessageCollection,
}
); );
for (const message of messages.models) { for (const message of messages.models) {
@ -250,7 +253,10 @@
// Theoretically all ouur messages could be friend requests, thus we have to unfortunately go through each one :( // Theoretically all ouur messages could be friend requests, thus we have to unfortunately go through each one :(
const messages = await window.Signal.Data.getMessagesByConversation( const messages = await window.Signal.Data.getMessagesByConversation(
this.id, this.id,
{ MessageCollection: Whisper.MessageCollection } {
type: 'friend-request',
MessageCollection: Whisper.MessageCollection,
}
); );
// We are most likely to find the friend request in the more recent conversations first // We are most likely to find the friend request in the more recent conversations first

@ -776,11 +776,12 @@ async function getUnreadByConversation(conversationId, { MessageCollection }) {
async function getMessagesByConversation( async function getMessagesByConversation(
conversationId, conversationId,
{ limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection } { limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection, type = null }
) { ) {
const messages = await channels.getMessagesByConversation(conversationId, { const messages = await channels.getMessagesByConversation(conversationId, {
limit, limit,
receivedAt, receivedAt,
type,
}); });
return new MessageCollection(messages); return new MessageCollection(messages);

Loading…
Cancel
Save