Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6bd8480e authored by Moez Bhatti's avatar Moez Bhatti
Browse files

Include archived conversations in search results

parent ca8b263f
Loading
Loading
Loading
Loading
+15 −5
Original line number Diff line number Diff line
@@ -112,17 +112,26 @@ class ConversationRepositoryImpl @Inject constructor(
    }

    override fun searchConversations(query: CharSequence): List<SearchResult> {
        val realm = Realm.getDefaultInstance()

        val normalizedQuery = query.removeAccents()
        val conversations = getConversationsSnapshot()
        val conversations = realm.copyFromRealm(realm
                .where(Conversation::class.java)
                .notEqualTo("id", 0L)
                .isNotNull("lastMessage")
                .equalTo("blocked", false)
                .isNotEmpty("recipients")
                .sort("pinned", Sort.DESCENDING, "lastMessage.date", Sort.DESCENDING)
                .findAll())

        val messagesByConversation = Realm.getDefaultInstance()
        val messagesByConversation = realm.copyFromRealm(realm
                .where(Message::class.java)
                .beginGroup()
                .contains("body", normalizedQuery, Case.INSENSITIVE)
                .or()
                .contains("parts.text", normalizedQuery, Case.INSENSITIVE)
                .endGroup()
                .findAll()
                .findAll())
                .asSequence()
                .groupBy { message -> message.threadId }
                .filter { (threadId, _) -> conversations.firstOrNull { it.id == threadId } != null }
@@ -131,10 +140,11 @@ class ConversationRepositoryImpl @Inject constructor(
                .sortedByDescending { result -> result.messages }
                .toList()

        realm.close()

        return conversations
                .filter { conversation -> conversationFilter.filter(conversation, normalizedQuery) }
                .map { conversation -> SearchResult(normalizedQuery, conversation, 0) }
                .plus(messagesByConversation)
                .map { conversation -> SearchResult(normalizedQuery, conversation, 0) } + messagesByConversation
    }

    override fun getBlockedConversations(): RealmResults<Conversation> {