From 1150f0f915b44abb53a872d93d19b7aac236d265 Mon Sep 17 00:00:00 2001 From: Mikunj Date: Tue, 13 Nov 2018 15:06:03 +1100 Subject: [PATCH] Allow fetching conversation messages by a specific type. --- app/sql.js | 49 +++++++++++++++++++++++++++----------- js/models/conversations.js | 10 ++++++-- js/modules/data.js | 3 ++- 3 files changed, 45 insertions(+), 17 deletions(-) diff --git a/app/sql.js b/app/sql.js index d7360c9d7..15e23543f 100644 --- a/app/sql.js +++ b/app/sql.js @@ -1310,21 +1310,42 @@ async function getUnreadByConversation(conversationId) { async function getMessagesByConversation( conversationId, - { limit = 100, receivedAt = Number.MAX_VALUE } = {} + { limit = 100, receivedAt = Number.MAX_VALUE, type = null } = {} ) { - const 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, - } - ); - + 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; + `, + { + $conversationId: conversationId, + $received_at: receivedAt, + $limit: limit, + } + ); + } + return map(rows, row => jsonToObject(row.json)); } diff --git a/js/models/conversations.js b/js/models/conversations.js index 07a684be9..2264d987c 100644 --- a/js/models/conversations.js +++ b/js/models/conversations.js @@ -237,7 +237,10 @@ // Go through the messages and check for any pending friend requests const messages = await window.Signal.Data.getMessagesByConversation( this.id, - { MessageCollection: Whisper.MessageCollection } + { + type: 'friend-request', + MessageCollection: Whisper.MessageCollection, + } ); 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 :( const messages = await window.Signal.Data.getMessagesByConversation( 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 diff --git a/js/modules/data.js b/js/modules/data.js index c1edc5e7b..6e1d1c682 100644 --- a/js/modules/data.js +++ b/js/modules/data.js @@ -776,11 +776,12 @@ async function getUnreadByConversation(conversationId, { MessageCollection }) { async function getMessagesByConversation( conversationId, - { limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection } + { limit = 100, receivedAt = Number.MAX_VALUE, MessageCollection, type = null } ) { const messages = await channels.getMessagesByConversation(conversationId, { limit, receivedAt, + type, }); return new MessageCollection(messages);