Skip to content

Commit

Permalink
Add filter commands for finding conversations
Browse files Browse the repository at this point in the history
Co-authored-by: Fedor Indutny <79877362+indutny-signal@users.noreply.github.com>
  • Loading branch information
automated-signal and indutny-signal committed Apr 27, 2022
1 parent f5b3785 commit 050a1bf
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ts/util/filterAndSortConversations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,46 @@ const cachedIndices = new WeakMap<
Fuse<ConversationType>
>();

type CommandRunnerType = (
conversations: ReadonlyArray<ConversationType>,
query: string
) => Array<ConversationType>;

const COMMANDS = new Map<string, CommandRunnerType>();

COMMANDS.set('uuidEndsWith', (conversations, query) => {
return conversations.filter(convo => convo.uuid?.endsWith(query));
});

COMMANDS.set('idEndsWith', (conversations, query) => {
return conversations.filter(convo => convo.id?.endsWith(query));
});

COMMANDS.set('e164EndsWith', (conversations, query) => {
return conversations.filter(convo => convo.e164?.endsWith(query));
});

COMMANDS.set('groupIdEndsWith', (conversations, query) => {
return conversations.filter(convo => convo.groupId?.endsWith(query));
});

// See https://fusejs.io/examples.html#extended-search for
// extended search documentation.
function searchConversations(
conversations: ReadonlyArray<ConversationType>,
searchTerm: string,
regionCode: string | undefined
): Array<ConversationType> {
const maybeCommand = searchTerm.match(/^!([^\s]+):(.*)$/);
if (maybeCommand) {
const [, commandName, query] = maybeCommand;

const command = COMMANDS.get(commandName);
if (command) {
return command(conversations, query);
}
}

const phoneNumber = parseAndFormatPhoneNumber(searchTerm, regionCode);

// Escape the search term
Expand Down

0 comments on commit 050a1bf

Please sign in to comment.