2023-01-03 19:55:46 +00:00
|
|
|
// Copyright 2019 Signal Messenger, LLC
|
2020-10-30 20:34:04 +00:00
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-11-01 18:43:02 +00:00
|
|
|
import type { ThunkAction, ThunkDispatch } from 'redux-thunk';
|
|
|
|
import { debounce, omit, reject } from 'lodash';
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2021-11-01 18:43:02 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2024-03-18 23:31:42 +00:00
|
|
|
import { filterAndSortConversations } from '../../util/filterAndSortConversations';
|
2024-07-22 18:16:33 +00:00
|
|
|
import type { ClientSearchResultMessageType } from '../../sql/Interface';
|
|
|
|
import { DataReader } from '../../sql/Client';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { makeLookup } from '../../util/makeLookup';
|
2023-08-16 20:54:39 +00:00
|
|
|
import { isNotNil } from '../../util/isNotNil';
|
|
|
|
import type { ServiceIdString } from '../../types/ServiceId';
|
2023-06-27 18:38:20 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2022-04-07 18:47:12 +00:00
|
|
|
ConversationType,
|
2019-11-07 21:36:16 +00:00
|
|
|
ConversationUnloadedActionType,
|
2019-05-31 22:42:01 +00:00
|
|
|
MessageDeletedActionType,
|
2019-01-14 21:49:58 +00:00
|
|
|
RemoveAllConversationsActionType,
|
2023-03-20 22:23:53 +00:00
|
|
|
TargetedConversationChangedActionType,
|
2019-11-07 21:36:16 +00:00
|
|
|
ShowArchivedConversationsActionType,
|
2023-06-26 18:25:48 +00:00
|
|
|
MessageType,
|
2019-01-14 21:49:58 +00:00
|
|
|
} from './conversations';
|
2024-11-13 19:33:41 +00:00
|
|
|
import {
|
|
|
|
getFilterByUnread,
|
|
|
|
getIsActivelySearching,
|
|
|
|
getQuery,
|
|
|
|
getSearchConversation,
|
|
|
|
} from '../selectors/search';
|
2022-04-07 18:47:12 +00:00
|
|
|
import { getAllConversations } from '../selectors/conversations';
|
|
|
|
import {
|
|
|
|
getIntl,
|
|
|
|
getRegionCode,
|
|
|
|
getUserConversationId,
|
|
|
|
} from '../selectors/user';
|
2022-02-23 18:48:40 +00:00
|
|
|
import { strictAssert } from '../../util/assert';
|
2023-01-05 00:22:36 +00:00
|
|
|
import {
|
|
|
|
CONVERSATION_UNLOADED,
|
2023-03-20 22:23:53 +00:00
|
|
|
TARGETED_CONVERSATION_CHANGED,
|
2023-01-05 00:22:36 +00:00
|
|
|
} from './conversations';
|
2023-05-10 01:29:59 +00:00
|
|
|
import { removeDiacritics } from '../../util/removeDiacritics';
|
2023-06-21 22:02:07 +00:00
|
|
|
import * as log from '../../logging/log';
|
2023-06-26 18:25:48 +00:00
|
|
|
import { searchConversationTitles } from '../../util/searchConversationTitles';
|
|
|
|
import { isDirectConversation } from '../../util/whatTypeOfConversation';
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2024-07-22 18:16:33 +00:00
|
|
|
const { searchMessages: dataSearchMessages } = DataReader;
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
// State
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type MessageSearchResultType = ReadonlyDeep<
|
|
|
|
MessageType & {
|
|
|
|
snippet?: string;
|
|
|
|
}
|
|
|
|
>;
|
2019-08-09 00:46:49 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type MessageSearchResultLookupType = ReadonlyDeep<{
|
2019-08-09 00:46:49 +00:00
|
|
|
[id: string]: MessageSearchResultType;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-08-09 00:46:49 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type SearchStateType = ReadonlyDeep<{
|
2019-11-07 21:36:16 +00:00
|
|
|
startSearchCounter: number;
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId?: string;
|
2023-02-22 19:21:59 +00:00
|
|
|
globalSearch?: boolean;
|
2021-02-23 20:34:28 +00:00
|
|
|
contactIds: Array<string>;
|
|
|
|
conversationIds: Array<string>;
|
2019-01-14 21:49:58 +00:00
|
|
|
query: string;
|
2024-11-13 19:33:41 +00:00
|
|
|
filterByUnread: boolean;
|
2019-08-09 00:46:49 +00:00
|
|
|
messageIds: Array<string>;
|
|
|
|
// We do store message data to pass through the selector
|
|
|
|
messageLookup: MessageSearchResultLookupType;
|
2023-03-20 22:23:53 +00:00
|
|
|
targetedMessage?: string;
|
2019-09-04 14:46:28 +00:00
|
|
|
// Loading state
|
|
|
|
discussionsLoading: boolean;
|
|
|
|
messagesLoading: boolean;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type SearchMessagesResultsFulfilledActionType = ReadonlyDeep<{
|
2019-09-04 14:46:28 +00:00
|
|
|
type: 'SEARCH_MESSAGES_RESULTS_FULFILLED';
|
2021-11-01 18:43:02 +00:00
|
|
|
payload: {
|
|
|
|
messages: Array<MessageSearchResultType>;
|
|
|
|
query: string;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type SearchDiscussionsResultsFulfilledActionType = ReadonlyDeep<{
|
2019-09-04 14:46:28 +00:00
|
|
|
type: 'SEARCH_DISCUSSIONS_RESULTS_FULFILLED';
|
2021-11-01 18:43:02 +00:00
|
|
|
payload: {
|
|
|
|
conversationIds: Array<string>;
|
|
|
|
contactIds: Array<string>;
|
|
|
|
query: string;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type UpdateSearchTermActionType = ReadonlyDeep<{
|
2019-01-14 21:49:58 +00:00
|
|
|
type: 'SEARCH_UPDATE';
|
|
|
|
payload: {
|
|
|
|
query: string;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type StartSearchActionType = ReadonlyDeep<{
|
2019-11-07 21:36:16 +00:00
|
|
|
type: 'SEARCH_START';
|
2024-03-09 01:08:30 +00:00
|
|
|
payload: null;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2024-11-13 19:33:41 +00:00
|
|
|
type ClearSearchQueryActionType = ReadonlyDeep<{
|
|
|
|
type: 'SEARCH_QUERY_CLEAR';
|
2019-01-14 21:49:58 +00:00
|
|
|
payload: null;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
|
|
|
type ClearConversationSearchActionType = ReadonlyDeep<{
|
2019-08-09 23:12:29 +00:00
|
|
|
type: 'CLEAR_CONVERSATION_SEARCH';
|
|
|
|
payload: null;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2024-03-09 01:08:30 +00:00
|
|
|
type EndSearchActionType = ReadonlyDeep<{
|
|
|
|
type: 'SEARCH_END';
|
|
|
|
payload: null;
|
|
|
|
}>;
|
|
|
|
type EndConversationSearchActionType = ReadonlyDeep<{
|
|
|
|
type: 'END_CONVERSATION_SEARCH';
|
|
|
|
payload: null;
|
|
|
|
}>;
|
2023-01-13 20:07:26 +00:00
|
|
|
type SearchInConversationActionType = ReadonlyDeep<{
|
2019-08-09 23:12:29 +00:00
|
|
|
type: 'SEARCH_IN_CONVERSATION';
|
2021-11-01 18:43:02 +00:00
|
|
|
payload: { searchConversationId: string };
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2024-11-13 19:33:41 +00:00
|
|
|
type UpdateFilterByUnreadActionType = ReadonlyDeep<{
|
|
|
|
type: 'FILTER_BY_UNREAD_UPDATE';
|
|
|
|
payload: { enabled: boolean };
|
|
|
|
}>;
|
|
|
|
|
|
|
|
type RefreshSearchActionType = ReadonlyDeep<{
|
|
|
|
type: 'SEARCH_REFRESH';
|
|
|
|
payload: null;
|
|
|
|
}>;
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type SearchActionType = ReadonlyDeep<
|
2019-09-04 14:46:28 +00:00
|
|
|
| SearchMessagesResultsFulfilledActionType
|
|
|
|
| SearchDiscussionsResultsFulfilledActionType
|
2019-01-14 21:49:58 +00:00
|
|
|
| UpdateSearchTermActionType
|
2019-11-07 21:36:16 +00:00
|
|
|
| StartSearchActionType
|
2024-11-13 19:33:41 +00:00
|
|
|
| ClearSearchQueryActionType
|
2019-08-09 23:12:29 +00:00
|
|
|
| ClearConversationSearchActionType
|
2024-03-09 01:08:30 +00:00
|
|
|
| EndSearchActionType
|
|
|
|
| EndConversationSearchActionType
|
2019-08-09 23:12:29 +00:00
|
|
|
| SearchInConversationActionType
|
2019-05-31 22:42:01 +00:00
|
|
|
| MessageDeletedActionType
|
2019-01-14 21:49:58 +00:00
|
|
|
| RemoveAllConversationsActionType
|
2023-03-20 22:23:53 +00:00
|
|
|
| TargetedConversationChangedActionType
|
2019-11-07 21:36:16 +00:00
|
|
|
| ShowArchivedConversationsActionType
|
2023-01-13 20:07:26 +00:00
|
|
|
| ConversationUnloadedActionType
|
2024-11-13 19:33:41 +00:00
|
|
|
| UpdateFilterByUnreadActionType
|
|
|
|
| RefreshSearchActionType
|
2023-01-13 20:07:26 +00:00
|
|
|
>;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
2019-11-07 21:36:16 +00:00
|
|
|
startSearch,
|
2024-11-13 19:33:41 +00:00
|
|
|
clearSearchQuery,
|
2019-08-09 23:12:29 +00:00
|
|
|
clearConversationSearch,
|
2024-03-09 01:08:30 +00:00
|
|
|
endSearch,
|
|
|
|
endConversationSearch,
|
2019-08-09 23:12:29 +00:00
|
|
|
searchInConversation,
|
2019-01-14 21:49:58 +00:00
|
|
|
updateSearchTerm,
|
2024-11-13 19:33:41 +00:00
|
|
|
updateFilterByUnread,
|
|
|
|
refreshSearch,
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
|
2023-06-27 18:38:20 +00:00
|
|
|
export const useSearchActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
function startSearch(): StartSearchActionType {
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_START',
|
2024-03-09 01:08:30 +00:00
|
|
|
payload: null,
|
2019-11-07 21:36:16 +00:00
|
|
|
};
|
|
|
|
}
|
2024-11-13 19:33:41 +00:00
|
|
|
function clearSearchQuery(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
ClearSearchQueryActionType
|
|
|
|
> {
|
|
|
|
return async (dispatch, getState) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_QUERY_CLEAR',
|
|
|
|
payload: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
doSearch({
|
|
|
|
dispatch,
|
|
|
|
state: getState(),
|
|
|
|
});
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
2019-08-09 23:12:29 +00:00
|
|
|
function clearConversationSearch(): ClearConversationSearchActionType {
|
|
|
|
return {
|
|
|
|
type: 'CLEAR_CONVERSATION_SEARCH',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
2024-03-09 01:08:30 +00:00
|
|
|
function endSearch(): EndSearchActionType {
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_END',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function endConversationSearch(): EndConversationSearchActionType {
|
|
|
|
return {
|
|
|
|
type: 'END_CONVERSATION_SEARCH',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
2019-08-09 23:12:29 +00:00
|
|
|
function searchInConversation(
|
2021-11-01 18:43:02 +00:00
|
|
|
searchConversationId: string
|
2019-08-09 23:12:29 +00:00
|
|
|
): SearchInConversationActionType {
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_IN_CONVERSATION',
|
2021-11-01 18:43:02 +00:00
|
|
|
payload: { searchConversationId },
|
2019-08-09 23:12:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-11-13 19:33:41 +00:00
|
|
|
function refreshSearch(): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
RefreshSearchActionType
|
|
|
|
> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
const state = getState();
|
|
|
|
|
|
|
|
if (!getIsActivelySearching(state)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_REFRESH',
|
|
|
|
payload: null,
|
|
|
|
});
|
|
|
|
|
|
|
|
doSearch({
|
|
|
|
dispatch,
|
|
|
|
state,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function updateFilterByUnread(
|
|
|
|
filterByUnread: boolean
|
|
|
|
): ThunkAction<void, RootStateType, unknown, UpdateFilterByUnreadActionType> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'FILTER_BY_UNREAD_UPDATE',
|
|
|
|
payload: {
|
|
|
|
enabled: filterByUnread,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
doSearch({
|
|
|
|
dispatch,
|
|
|
|
state: getState(),
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:43:02 +00:00
|
|
|
function updateSearchTerm(
|
|
|
|
query: string
|
|
|
|
): ThunkAction<void, RootStateType, unknown, UpdateSearchTermActionType> {
|
|
|
|
return (dispatch, getState) => {
|
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_UPDATE',
|
|
|
|
payload: { query },
|
|
|
|
});
|
|
|
|
|
|
|
|
doSearch({
|
|
|
|
dispatch,
|
2024-11-13 19:33:41 +00:00
|
|
|
state: getState(),
|
2021-11-01 18:43:02 +00:00
|
|
|
});
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:43:02 +00:00
|
|
|
const doSearch = debounce(
|
|
|
|
({
|
|
|
|
dispatch,
|
2024-11-13 19:33:41 +00:00
|
|
|
state,
|
2021-11-01 18:43:02 +00:00
|
|
|
}: Readonly<{
|
|
|
|
dispatch: ThunkDispatch<
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
| SearchMessagesResultsFulfilledActionType
|
|
|
|
| SearchDiscussionsResultsFulfilledActionType
|
|
|
|
>;
|
2024-11-13 19:33:41 +00:00
|
|
|
state: RootStateType;
|
2021-11-01 18:43:02 +00:00
|
|
|
}>) => {
|
2024-11-13 19:33:41 +00:00
|
|
|
if (!getIsActivelySearching(state)) {
|
2021-11-01 18:43:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-11-13 19:33:41 +00:00
|
|
|
const query = getQuery(state);
|
|
|
|
const filterByUnread = getFilterByUnread(state);
|
|
|
|
const i18n = getIntl(state);
|
|
|
|
const allConversations = getAllConversations(state);
|
|
|
|
const regionCode = getRegionCode(state);
|
|
|
|
const noteToSelf = i18n('icu:noteToSelf').toLowerCase();
|
|
|
|
const ourConversationId = getUserConversationId(state);
|
|
|
|
const searchConversationId = getSearchConversation(state)?.id;
|
|
|
|
|
|
|
|
strictAssert(ourConversationId, 'doSearch our conversation is missing');
|
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
// Limit the number of contacts to something reasonable
|
|
|
|
const MAX_MATCHING_CONTACTS = 100;
|
|
|
|
|
2022-12-21 18:41:48 +00:00
|
|
|
void (async () => {
|
2024-11-13 19:33:41 +00:00
|
|
|
if (filterByUnread) {
|
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_MESSAGES_RESULTS_FULFILLED',
|
|
|
|
payload: {
|
|
|
|
messages: [],
|
|
|
|
query,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
2023-06-26 18:25:48 +00:00
|
|
|
const segmenter = new Intl.Segmenter([], { granularity: 'word' });
|
|
|
|
const queryWords = [...segmenter.segment(query)]
|
|
|
|
.filter(word => word.isWordLike)
|
|
|
|
.map(word => word.segment);
|
2023-08-16 20:54:39 +00:00
|
|
|
const contactServiceIdsMatchingQuery = searchConversationTitles(
|
2023-06-26 18:25:48 +00:00
|
|
|
allConversations,
|
|
|
|
queryWords
|
|
|
|
)
|
2023-08-16 20:54:39 +00:00
|
|
|
.filter(conversation => isDirectConversation(conversation))
|
|
|
|
.map(conversation => conversation.serviceId)
|
|
|
|
.filter(isNotNil)
|
2023-06-26 18:25:48 +00:00
|
|
|
.slice(0, MAX_MATCHING_CONTACTS);
|
|
|
|
|
|
|
|
const messages = await queryMessages({
|
|
|
|
query,
|
|
|
|
searchConversationId,
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery,
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
|
|
|
|
2021-11-01 18:43:02 +00:00
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_MESSAGES_RESULTS_FULFILLED',
|
|
|
|
payload: {
|
2023-06-26 18:25:48 +00:00
|
|
|
messages,
|
2021-11-01 18:43:02 +00:00
|
|
|
query,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
|
|
|
if (!searchConversationId) {
|
2022-12-21 18:41:48 +00:00
|
|
|
void (async () => {
|
2021-11-11 22:43:05 +00:00
|
|
|
const { conversationIds, contactIds } =
|
|
|
|
await queryConversationsAndContacts(query, {
|
2024-11-13 19:33:41 +00:00
|
|
|
filterByUnread,
|
2021-11-11 22:43:05 +00:00
|
|
|
ourConversationId,
|
|
|
|
noteToSelf,
|
2022-04-07 18:47:12 +00:00
|
|
|
regionCode,
|
|
|
|
allConversations,
|
2021-11-11 22:43:05 +00:00
|
|
|
});
|
2021-11-01 18:43:02 +00:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_DISCUSSIONS_RESULTS_FULFILLED',
|
|
|
|
payload: {
|
|
|
|
conversationIds,
|
|
|
|
contactIds,
|
|
|
|
query,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
200
|
|
|
|
);
|
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
async function queryMessages({
|
|
|
|
query,
|
|
|
|
searchConversationId,
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery,
|
2023-06-26 18:25:48 +00:00
|
|
|
}: {
|
|
|
|
query: string;
|
|
|
|
searchConversationId?: string;
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery?: Array<ServiceIdString>;
|
2023-06-26 18:25:48 +00:00
|
|
|
}): Promise<Array<ClientSearchResultMessageType>> {
|
2019-05-31 22:42:01 +00:00
|
|
|
try {
|
2024-11-13 19:33:41 +00:00
|
|
|
if (query.trim().length === 0) {
|
2021-04-08 10:32:12 +00:00
|
|
|
return [];
|
|
|
|
}
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
if (searchConversationId) {
|
2023-06-26 18:25:48 +00:00
|
|
|
return dataSearchMessages({
|
2023-12-04 17:38:40 +00:00
|
|
|
query,
|
2023-06-26 18:25:48 +00:00
|
|
|
conversationId: searchConversationId,
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery,
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
2019-08-09 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-26 18:25:48 +00:00
|
|
|
return dataSearchMessages({
|
2023-12-04 17:38:40 +00:00
|
|
|
query,
|
2023-08-16 20:54:39 +00:00
|
|
|
contactServiceIdsMatchingQuery,
|
2023-06-26 18:25:48 +00:00
|
|
|
});
|
2019-05-31 22:42:01 +00:00
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
async function queryConversationsAndContacts(
|
2022-04-07 18:47:12 +00:00
|
|
|
query: string,
|
2020-03-05 21:14:58 +00:00
|
|
|
options: {
|
2024-11-13 19:33:41 +00:00
|
|
|
filterByUnread: boolean;
|
2020-03-05 21:14:58 +00:00
|
|
|
ourConversationId: string;
|
|
|
|
noteToSelf: string;
|
2022-04-07 18:47:12 +00:00
|
|
|
regionCode: string | undefined;
|
|
|
|
allConversations: ReadonlyArray<ConversationType>;
|
2020-03-05 21:14:58 +00:00
|
|
|
}
|
2021-02-23 20:34:28 +00:00
|
|
|
): Promise<{
|
|
|
|
contactIds: Array<string>;
|
|
|
|
conversationIds: Array<string>;
|
|
|
|
}> {
|
2024-11-13 19:33:41 +00:00
|
|
|
const {
|
|
|
|
filterByUnread,
|
|
|
|
ourConversationId,
|
|
|
|
noteToSelf,
|
|
|
|
regionCode,
|
|
|
|
allConversations,
|
|
|
|
} = options;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2023-05-10 01:29:59 +00:00
|
|
|
const normalizedQuery = removeDiacritics(query);
|
|
|
|
|
2024-10-23 16:49:22 +00:00
|
|
|
const visibleConversations = allConversations.filter(conversation => {
|
2024-10-29 17:01:04 +00:00
|
|
|
const { activeAt, removalStage, isBlocked } = conversation;
|
|
|
|
|
2024-10-23 16:49:22 +00:00
|
|
|
if (isDirectConversation(conversation)) {
|
2024-10-29 17:01:04 +00:00
|
|
|
// if a conversation has messages (i.e. is not "deleted"), always show it
|
|
|
|
if (activeAt != null) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't show if conversation is empty and the contact is blocked
|
|
|
|
if (isBlocked) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't show if conversation is empty and the contact is removed
|
|
|
|
if (removalStage != null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, show it
|
|
|
|
return true;
|
2023-11-15 00:39:17 +00:00
|
|
|
}
|
2024-10-29 17:01:04 +00:00
|
|
|
|
2024-10-23 16:49:22 +00:00
|
|
|
// We don't show groups in search results that don't have any messages
|
|
|
|
return activeAt != null;
|
|
|
|
});
|
2023-11-15 00:39:17 +00:00
|
|
|
|
2024-03-18 23:31:42 +00:00
|
|
|
const searchResults: Array<ConversationType> = filterAndSortConversations(
|
|
|
|
visibleConversations,
|
|
|
|
normalizedQuery,
|
2024-11-13 19:33:41 +00:00
|
|
|
regionCode,
|
|
|
|
filterByUnread
|
2024-03-18 23:31:42 +00:00
|
|
|
);
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
// Split into two groups - active conversations and items just from address book
|
2021-02-23 20:34:28 +00:00
|
|
|
let conversationIds: Array<string> = [];
|
|
|
|
let contactIds: Array<string> = [];
|
2019-01-14 21:49:58 +00:00
|
|
|
const max = searchResults.length;
|
|
|
|
for (let i = 0; i < max; i += 1) {
|
|
|
|
const conversation = searchResults[i];
|
|
|
|
|
2022-04-07 18:47:12 +00:00
|
|
|
if (conversation.type === 'direct' && !conversation.lastMessage) {
|
2021-02-23 20:34:28 +00:00
|
|
|
contactIds.push(conversation.id);
|
2019-01-14 21:49:58 +00:00
|
|
|
} else {
|
2021-02-23 20:34:28 +00:00
|
|
|
conversationIds.push(conversation.id);
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject synthetic Note to Self entry if query matches localized 'Note to Self'
|
2022-04-07 18:47:12 +00:00
|
|
|
if (noteToSelf.indexOf(query.toLowerCase()) !== -1) {
|
2019-01-14 21:49:58 +00:00
|
|
|
// ensure that we don't have duplicates in our results
|
2021-02-23 20:34:28 +00:00
|
|
|
contactIds = contactIds.filter(id => id !== ourConversationId);
|
|
|
|
conversationIds = conversationIds.filter(id => id !== ourConversationId);
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2021-02-23 20:34:28 +00:00
|
|
|
contactIds.unshift(ourConversationId);
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
2024-11-13 19:33:41 +00:00
|
|
|
// Don't show contacts in the left pane if we're filtering by unread
|
|
|
|
if (filterByUnread) {
|
|
|
|
contactIds = [];
|
|
|
|
}
|
|
|
|
|
2021-02-23 20:34:28 +00:00
|
|
|
return { conversationIds, contactIds };
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
2021-01-06 15:41:43 +00:00
|
|
|
export function getEmptyState(): SearchStateType {
|
2019-01-14 21:49:58 +00:00
|
|
|
return {
|
2019-11-07 21:36:16 +00:00
|
|
|
startSearchCounter: 0,
|
2019-01-14 21:49:58 +00:00
|
|
|
query: '',
|
2024-11-13 19:33:41 +00:00
|
|
|
filterByUnread: false,
|
2019-08-09 00:46:49 +00:00
|
|
|
messageIds: [],
|
2019-01-14 21:49:58 +00:00
|
|
|
messageLookup: {},
|
2021-02-23 20:34:28 +00:00
|
|
|
conversationIds: [],
|
|
|
|
contactIds: [],
|
2019-09-04 14:46:28 +00:00
|
|
|
discussionsLoading: false,
|
|
|
|
messagesLoading: false,
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-11-13 19:33:41 +00:00
|
|
|
function handleSearchUpdate(
|
|
|
|
state: SearchStateType,
|
|
|
|
params: { query?: string; filterByUnread?: boolean }
|
|
|
|
): SearchStateType {
|
|
|
|
const { query, filterByUnread } = params;
|
|
|
|
|
|
|
|
// Determine the new state values, falling back to existing state if not provided
|
|
|
|
const newQuery = query ?? state.query;
|
|
|
|
const newFilterByUnread = filterByUnread ?? state.filterByUnread;
|
|
|
|
|
|
|
|
const isValidSearch = newQuery.length > 0 || newFilterByUnread;
|
|
|
|
const isWithinConversation = Boolean(state.searchConversationId);
|
|
|
|
|
|
|
|
if (isValidSearch) {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
query: newQuery,
|
|
|
|
filterByUnread: newFilterByUnread,
|
|
|
|
messagesLoading: true,
|
|
|
|
messageIds: [],
|
|
|
|
messageLookup: {},
|
|
|
|
discussionsLoading: !isWithinConversation,
|
|
|
|
contactIds: [],
|
|
|
|
conversationIds: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...getEmptyState(),
|
|
|
|
startSearchCounter: state.startSearchCounter,
|
|
|
|
searchConversationId: state.searchConversationId,
|
|
|
|
globalSearch: state.globalSearch,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
export function reducer(
|
2020-12-14 19:47:21 +00:00
|
|
|
state: Readonly<SearchStateType> = getEmptyState(),
|
|
|
|
action: Readonly<SearchActionType>
|
2019-01-14 21:49:58 +00:00
|
|
|
): SearchStateType {
|
2024-11-13 19:33:41 +00:00
|
|
|
if (action.type === 'FILTER_BY_UNREAD_UPDATE') {
|
|
|
|
return handleSearchUpdate(state, {
|
|
|
|
filterByUnread: action.payload.enabled,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
if (action.type === 'SHOW_ARCHIVED_CONVERSATIONS') {
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info('search: show archived conversations, clearing message lookup');
|
2019-11-07 21:36:16 +00:00
|
|
|
return getEmptyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SEARCH_START') {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
searchConversationId: undefined,
|
2023-02-22 19:21:59 +00:00
|
|
|
globalSearch: true,
|
2019-11-07 21:36:16 +00:00
|
|
|
startSearchCounter: state.startSearchCounter + 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-11-13 19:33:41 +00:00
|
|
|
if (action.type === 'SEARCH_QUERY_CLEAR') {
|
|
|
|
return handleSearchUpdate(state, { query: '' });
|
2024-03-09 01:08:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SEARCH_END') {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
globalSearch: Boolean(state.query) && !state.searchConversationId,
|
2022-12-09 18:03:32 +00:00
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SEARCH_UPDATE') {
|
2024-11-13 19:33:41 +00:00
|
|
|
return handleSearchUpdate(state, { query: action.payload.query });
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
if (action.type === 'SEARCH_IN_CONVERSATION') {
|
|
|
|
const { payload } = action;
|
2021-11-01 18:43:02 +00:00
|
|
|
const { searchConversationId } = payload;
|
2019-08-09 23:12:29 +00:00
|
|
|
|
|
|
|
if (searchConversationId === state.searchConversationId) {
|
2019-11-07 21:36:16 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
startSearchCounter: state.startSearchCounter + 1,
|
|
|
|
};
|
2019-08-09 23:12:29 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info('search: searching in new conversation, clearing message lookup');
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
return {
|
|
|
|
...getEmptyState(),
|
|
|
|
searchConversationId,
|
2019-11-07 21:36:16 +00:00
|
|
|
startSearchCounter: state.startSearchCounter + 1,
|
2019-08-09 23:12:29 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
if (action.type === 'CLEAR_CONVERSATION_SEARCH') {
|
2021-11-01 18:43:02 +00:00
|
|
|
const { searchConversationId } = state;
|
2019-08-09 23:12:29 +00:00
|
|
|
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info('search: cleared conversation search, clearing message lookup');
|
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
return {
|
|
|
|
...getEmptyState(),
|
|
|
|
searchConversationId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2024-03-09 01:08:30 +00:00
|
|
|
if (action.type === 'END_CONVERSATION_SEARCH') {
|
|
|
|
return {
|
|
|
|
...getEmptyState(),
|
|
|
|
startSearchCounter: state.startSearchCounter + 1,
|
|
|
|
globalSearch: true,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-09-04 14:46:28 +00:00
|
|
|
if (action.type === 'SEARCH_MESSAGES_RESULTS_FULFILLED') {
|
2019-01-14 21:49:58 +00:00
|
|
|
const { payload } = action;
|
2021-11-01 18:43:02 +00:00
|
|
|
const { messages, query } = payload;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
// Reject if the associated query is not the most recent user-provided query
|
|
|
|
if (state.query !== query) {
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info('search: query mismatch, ignoring message results');
|
2019-01-14 21:49:58 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info('search: got new messages, updating message lookup');
|
|
|
|
|
2019-08-09 00:46:49 +00:00
|
|
|
const messageIds = messages.map(message => message.id);
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2019-08-09 00:46:49 +00:00
|
|
|
query,
|
|
|
|
messageIds,
|
2019-01-14 21:49:58 +00:00
|
|
|
messageLookup: makeLookup(messages, 'id'),
|
2019-09-04 14:46:28 +00:00
|
|
|
messagesLoading: false,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SEARCH_DISCUSSIONS_RESULTS_FULFILLED') {
|
|
|
|
const { payload } = action;
|
2021-11-01 18:43:02 +00:00
|
|
|
const { contactIds, conversationIds, query } = payload;
|
|
|
|
|
|
|
|
// Reject if the associated query is not the most recent user-provided query
|
|
|
|
if (state.query !== query) {
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info('search: query mismatch, ignoring message results');
|
2021-11-01 18:43:02 +00:00
|
|
|
return state;
|
|
|
|
}
|
2019-09-04 14:46:28 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
2021-02-23 20:34:28 +00:00
|
|
|
contactIds,
|
|
|
|
conversationIds,
|
2019-09-04 14:46:28 +00:00
|
|
|
discussionsLoading: false,
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'CONVERSATIONS_REMOVE_ALL') {
|
|
|
|
return getEmptyState();
|
|
|
|
}
|
|
|
|
|
2023-03-20 22:23:53 +00:00
|
|
|
if (action.type === TARGETED_CONVERSATION_CHANGED) {
|
2019-01-14 21:49:58 +00:00
|
|
|
const { payload } = action;
|
2023-01-05 00:22:36 +00:00
|
|
|
const { conversationId, messageId } = payload;
|
2019-08-09 23:12:29 +00:00
|
|
|
const { searchConversationId } = state;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2023-01-05 00:22:36 +00:00
|
|
|
if (searchConversationId && searchConversationId !== conversationId) {
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info(
|
|
|
|
'search: targeted conversation changed, clearing message lookup'
|
|
|
|
);
|
2019-08-09 23:12:29 +00:00
|
|
|
return getEmptyState();
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
2023-03-20 22:23:53 +00:00
|
|
|
targetedMessage: messageId,
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-01-05 00:22:36 +00:00
|
|
|
if (action.type === CONVERSATION_UNLOADED) {
|
2019-11-07 21:36:16 +00:00
|
|
|
const { payload } = action;
|
2023-01-05 00:22:36 +00:00
|
|
|
const { conversationId } = payload;
|
2019-11-07 21:36:16 +00:00
|
|
|
const { searchConversationId } = state;
|
|
|
|
|
2023-01-05 00:22:36 +00:00
|
|
|
if (searchConversationId && searchConversationId === conversationId) {
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info(
|
|
|
|
'search: searched conversation unloaded, clearing message lookup'
|
|
|
|
);
|
2019-11-07 21:36:16 +00:00
|
|
|
return getEmptyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
if (action.type === 'MESSAGE_DELETED') {
|
2019-08-09 00:46:49 +00:00
|
|
|
const { messageIds, messageLookup } = state;
|
|
|
|
if (!messageIds || messageIds.length < 1) {
|
2019-01-14 21:49:58 +00:00
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { payload } = action;
|
|
|
|
const { id } = payload;
|
|
|
|
|
2023-06-21 22:02:07 +00:00
|
|
|
log.info('search: message deleted, removing from message lookup');
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2019-08-09 00:46:49 +00:00
|
|
|
messageIds: reject(messageIds, messageId => id === messageId),
|
2021-06-16 16:39:09 +00:00
|
|
|
messageLookup: omit(messageLookup, id),
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|