2021-03-11 21:29:31 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import Fuse from 'fuse.js';
|
2021-03-11 21:29:31 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from '../state/ducks/conversations';
|
2022-04-05 00:38:22 +00:00
|
|
|
import { parseAndFormatPhoneNumber } from './libphonenumberInstance';
|
2021-03-11 21:29:31 +00:00
|
|
|
|
2022-04-05 00:38:22 +00:00
|
|
|
const FUSE_OPTIONS: Fuse.IFuseOptions<ConversationType> = {
|
2021-03-11 21:29:31 +00:00
|
|
|
// A small-but-nonzero threshold lets us match parts of E164s better, and makes the
|
|
|
|
// search a little more forgiving.
|
2022-04-27 20:09:54 +00:00
|
|
|
threshold: 0.2,
|
2022-04-05 00:38:22 +00:00
|
|
|
useExtendedSearch: true,
|
2021-03-11 23:33:12 +00:00
|
|
|
keys: [
|
2021-04-27 22:35:35 +00:00
|
|
|
{
|
|
|
|
name: 'searchableTitle',
|
|
|
|
weight: 1,
|
|
|
|
},
|
2021-03-11 23:33:12 +00:00
|
|
|
{
|
|
|
|
name: 'title',
|
|
|
|
weight: 1,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: 'name',
|
|
|
|
weight: 1,
|
|
|
|
},
|
2021-11-12 01:17:29 +00:00
|
|
|
{
|
|
|
|
name: 'username',
|
|
|
|
weight: 1,
|
|
|
|
},
|
2021-03-11 23:33:12 +00:00
|
|
|
{
|
|
|
|
name: 'e164',
|
|
|
|
weight: 0.5,
|
|
|
|
},
|
|
|
|
],
|
2021-03-11 21:29:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const collator = new Intl.Collator();
|
|
|
|
|
2022-04-05 00:38:22 +00:00
|
|
|
const cachedIndices = new WeakMap<
|
|
|
|
ReadonlyArray<ConversationType>,
|
|
|
|
Fuse<ConversationType>
|
|
|
|
>();
|
|
|
|
|
2022-04-27 18:52:43 +00:00
|
|
|
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));
|
|
|
|
});
|
|
|
|
|
2022-04-05 00:38:22 +00:00
|
|
|
// See https://fusejs.io/examples.html#extended-search for
|
|
|
|
// extended search documentation.
|
2021-04-28 20:44:48 +00:00
|
|
|
function searchConversations(
|
|
|
|
conversations: ReadonlyArray<ConversationType>,
|
2022-04-05 00:38:22 +00:00
|
|
|
searchTerm: string,
|
|
|
|
regionCode: string | undefined
|
2021-04-28 20:44:48 +00:00
|
|
|
): Array<ConversationType> {
|
2022-04-27 18:52:43 +00:00
|
|
|
const maybeCommand = searchTerm.match(/^!([^\s]+):(.*)$/);
|
|
|
|
if (maybeCommand) {
|
|
|
|
const [, commandName, query] = maybeCommand;
|
|
|
|
|
|
|
|
const command = COMMANDS.get(commandName);
|
|
|
|
if (command) {
|
|
|
|
return command(conversations, query);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 00:38:22 +00:00
|
|
|
const phoneNumber = parseAndFormatPhoneNumber(searchTerm, regionCode);
|
|
|
|
|
|
|
|
// Escape the search term
|
|
|
|
let extendedSearchTerm = searchTerm;
|
|
|
|
|
|
|
|
// OR phoneNumber
|
|
|
|
if (phoneNumber) {
|
|
|
|
extendedSearchTerm += ` | ${phoneNumber.e164}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
let index = cachedIndices.get(conversations);
|
|
|
|
if (!index) {
|
|
|
|
index = new Fuse<ConversationType>(conversations, FUSE_OPTIONS);
|
|
|
|
cachedIndices.set(conversations, index);
|
|
|
|
}
|
|
|
|
|
|
|
|
const results = index.search(extendedSearchTerm);
|
|
|
|
return results.map(result => result.item);
|
2021-04-28 20:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export function filterAndSortConversationsByRecent(
|
|
|
|
conversations: ReadonlyArray<ConversationType>,
|
2022-04-05 00:38:22 +00:00
|
|
|
searchTerm: string,
|
|
|
|
regionCode: string | undefined
|
2021-04-28 20:44:48 +00:00
|
|
|
): Array<ConversationType> {
|
|
|
|
if (searchTerm.length) {
|
2022-04-05 00:38:22 +00:00
|
|
|
return searchConversations(conversations, searchTerm, regionCode);
|
2021-04-28 20:44:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return conversations.concat().sort((a, b) => {
|
|
|
|
if (a.activeAt && b.activeAt) {
|
|
|
|
return a.activeAt > b.activeAt ? -1 : 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return a.activeAt && !b.activeAt ? -1 : 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
export function filterAndSortConversationsByTitle(
|
2021-04-20 23:16:49 +00:00
|
|
|
conversations: ReadonlyArray<ConversationType>,
|
2022-04-05 00:38:22 +00:00
|
|
|
searchTerm: string,
|
|
|
|
regionCode: string | undefined
|
2021-03-11 21:29:31 +00:00
|
|
|
): Array<ConversationType> {
|
|
|
|
if (searchTerm.length) {
|
2022-04-05 00:38:22 +00:00
|
|
|
return searchConversations(conversations, searchTerm, regionCode);
|
2021-03-11 21:29:31 +00:00
|
|
|
}
|
2021-03-11 23:33:12 +00:00
|
|
|
|
2021-04-20 23:16:49 +00:00
|
|
|
return conversations.concat().sort((a, b) => {
|
2021-03-11 23:33:12 +00:00
|
|
|
const aHasName = hasName(a);
|
|
|
|
const bHasName = hasName(b);
|
|
|
|
|
|
|
|
if (aHasName === bHasName) {
|
|
|
|
return collator.compare(a.title, b.title);
|
|
|
|
}
|
|
|
|
|
|
|
|
return aHasName && !bHasName ? -1 : 1;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasName(contact: Readonly<ConversationType>): boolean {
|
|
|
|
return Boolean(contact.name || contact.profileName);
|
2021-03-11 21:29:31 +00:00
|
|
|
}
|