Skip to content

Commit

Permalink
Improve the ordering of conversation search results.
Browse files Browse the repository at this point in the history
  • Loading branch information
greyson-signal committed Aug 25, 2022
1 parent cdad450 commit 497b38d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,7 @@ public Cursor getFilteredConversationList(@Nullable List<RecipientId> filter) {
selectionArgs[i++] = recipientId.serialize();
}

String query = createQuery(selection, 0);
String query = createQuery(selection, DATE + " DESC", 0, 0);
cursors.add(db.rawQuery(query, selectionArgs));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,12 @@
import org.thoughtcrime.securesms.util.concurrent.SerialExecutor;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
Expand Down Expand Up @@ -159,33 +163,46 @@ private List<Recipient> queryContacts(String query) {
return Collections.emptyList();
}

Set<RecipientId> recipientIds = new LinkedHashSet<>();

Set<RecipientId> filteredContacts = new LinkedHashSet<>();
try (Cursor cursor = SignalDatabase.recipients().queryAllContacts(query)) {
while (cursor != null && cursor.moveToNext()) {
filteredContacts.add(RecipientId.from(CursorUtil.requireString(cursor, RecipientDatabase.ID)));
}
}
recipientIds.addAll(filteredContacts);

Set<RecipientId> contactIds = new LinkedHashSet<>(filteredContacts);

if (noteToSelfTitle.toLowerCase().contains(query.toLowerCase())) {
contactIds.add(Recipient.self().getId());
}

Set<RecipientId> groupsByTitleIds = new LinkedHashSet<>();

GroupDatabase.GroupRecord record;
try (GroupDatabase.Reader reader = SignalDatabase.groups().queryGroupsByTitle(query, true, false, false)) {
while ((record = reader.getNext()) != null) {
recipientIds.add(record.getRecipientId());
groupsByTitleIds.add(record.getRecipientId());
}
}

Set<RecipientId> groupsByMemberIds = new LinkedHashSet<>();

try (GroupDatabase.Reader reader = SignalDatabase.groups().queryGroupsByMembership(filteredContacts, true, false, false)) {
while ((record = reader.getNext()) != null) {
recipientIds.add(record.getRecipientId());
groupsByMemberIds.add(record.getRecipientId());
}
}

if (noteToSelfTitle.toLowerCase().contains(query.toLowerCase())) {
recipientIds.add(Recipient.self().getId());
}
List<ThreadRecord> output = new ArrayList<>(contactIds.size() + groupsByTitleIds.size() + groupsByMemberIds.size());

output.addAll(getMatchingThreads(contactIds));
output.addAll(getMatchingThreads(groupsByTitleIds));
output.addAll(getMatchingThreads(groupsByMemberIds));

return output;
}

private List<ThreadRecord> getMatchingThreads(@NonNull Collection<RecipientId> recipientIds) {
try (Cursor cursor = threadDatabase.getFilteredConversationList(new ArrayList<>(recipientIds))) {
return readToList(cursor, new ThreadModelBuilder(threadDatabase));
}
Expand Down

0 comments on commit 497b38d

Please sign in to comment.