2022-02-23 18:48:40 +00:00
|
|
|
// Copyright 2019-2022 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
|
|
|
|
2021-11-01 18:43:02 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2019-05-31 22:42:01 +00:00
|
|
|
import { cleanSearchTerm } from '../../util/cleanSearchTerm';
|
2022-04-07 18:47:12 +00:00
|
|
|
import { filterAndSortConversationsByRecent } from '../../util/filterAndSortConversations';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type {
|
2021-04-05 22:18:19 +00:00
|
|
|
ClientSearchResultMessageType,
|
|
|
|
ClientInterface,
|
|
|
|
} from '../../sql/Interface';
|
2020-04-01 18:59:11 +00:00
|
|
|
import dataInterface from '../../sql/Client';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { makeLookup } from '../../util/makeLookup';
|
|
|
|
|
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-08-09 00:46:49 +00:00
|
|
|
MessageType,
|
2019-01-14 21:49:58 +00:00
|
|
|
RemoveAllConversationsActionType,
|
|
|
|
SelectedConversationChangedActionType,
|
2019-11-07 21:36:16 +00:00
|
|
|
ShowArchivedConversationsActionType,
|
2019-01-14 21:49:58 +00:00
|
|
|
} from './conversations';
|
2021-11-01 18:43:02 +00:00
|
|
|
import { 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';
|
2022-06-16 19:12:50 +00:00
|
|
|
import { SELECTED_CONVERSATION_CHANGED } from './conversations';
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2020-04-01 18:59:11 +00:00
|
|
|
const {
|
|
|
|
searchMessages: dataSearchMessages,
|
|
|
|
searchMessagesInConversation,
|
2021-04-05 22:18:19 +00:00
|
|
|
}: ClientInterface = dataInterface;
|
2020-04-01 18:59:11 +00:00
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
// State
|
|
|
|
|
2019-08-09 00:46:49 +00:00
|
|
|
export type MessageSearchResultType = MessageType & {
|
2021-06-17 17:15:10 +00:00
|
|
|
snippet?: string;
|
2019-08-09 00:46:49 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type MessageSearchResultLookupType = {
|
|
|
|
[id: string]: MessageSearchResultType;
|
|
|
|
};
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
export type SearchStateType = {
|
2019-11-07 21:36:16 +00:00
|
|
|
startSearchCounter: number;
|
2019-08-09 23:12:29 +00:00
|
|
|
searchConversationId?: string;
|
2021-02-23 20:34:28 +00:00
|
|
|
contactIds: Array<string>;
|
|
|
|
conversationIds: Array<string>;
|
2019-01-14 21:49:58 +00:00
|
|
|
query: string;
|
2019-08-09 00:46:49 +00:00
|
|
|
messageIds: Array<string>;
|
|
|
|
// We do store message data to pass through the selector
|
|
|
|
messageLookup: MessageSearchResultLookupType;
|
2019-01-14 21:49:58 +00:00
|
|
|
selectedMessage?: string;
|
2019-09-04 14:46:28 +00:00
|
|
|
// Loading state
|
|
|
|
discussionsLoading: boolean;
|
|
|
|
messagesLoading: boolean;
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2019-09-04 14:46:28 +00:00
|
|
|
type SearchMessagesResultsFulfilledActionType = {
|
|
|
|
type: 'SEARCH_MESSAGES_RESULTS_FULFILLED';
|
2021-11-01 18:43:02 +00:00
|
|
|
payload: {
|
|
|
|
messages: Array<MessageSearchResultType>;
|
|
|
|
query: string;
|
|
|
|
};
|
2019-09-04 14:46:28 +00:00
|
|
|
};
|
|
|
|
type SearchDiscussionsResultsFulfilledActionType = {
|
|
|
|
type: 'SEARCH_DISCUSSIONS_RESULTS_FULFILLED';
|
2021-11-01 18:43:02 +00:00
|
|
|
payload: {
|
|
|
|
conversationIds: Array<string>;
|
|
|
|
contactIds: Array<string>;
|
|
|
|
query: string;
|
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
type UpdateSearchTermActionType = {
|
|
|
|
type: 'SEARCH_UPDATE';
|
|
|
|
payload: {
|
|
|
|
query: string;
|
|
|
|
};
|
|
|
|
};
|
2019-11-07 21:36:16 +00:00
|
|
|
type StartSearchActionType = {
|
|
|
|
type: 'SEARCH_START';
|
|
|
|
payload: null;
|
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
type ClearSearchActionType = {
|
|
|
|
type: 'SEARCH_CLEAR';
|
|
|
|
payload: null;
|
|
|
|
};
|
2019-08-09 23:12:29 +00:00
|
|
|
type ClearConversationSearchActionType = {
|
|
|
|
type: 'CLEAR_CONVERSATION_SEARCH';
|
|
|
|
payload: null;
|
|
|
|
};
|
|
|
|
type SearchInConversationActionType = {
|
|
|
|
type: 'SEARCH_IN_CONVERSATION';
|
2021-11-01 18:43:02 +00:00
|
|
|
payload: { searchConversationId: string };
|
2019-08-09 23:12:29 +00:00
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2020-09-09 22:50:44 +00:00
|
|
|
export type SearchActionType =
|
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
|
2019-01-14 21:49:58 +00:00
|
|
|
| ClearSearchActionType
|
2019-08-09 23:12:29 +00:00
|
|
|
| ClearConversationSearchActionType
|
|
|
|
| SearchInConversationActionType
|
2019-05-31 22:42:01 +00:00
|
|
|
| MessageDeletedActionType
|
2019-01-14 21:49:58 +00:00
|
|
|
| RemoveAllConversationsActionType
|
2019-11-07 21:36:16 +00:00
|
|
|
| SelectedConversationChangedActionType
|
|
|
|
| ShowArchivedConversationsActionType
|
|
|
|
| ConversationUnloadedActionType;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
2019-11-07 21:36:16 +00:00
|
|
|
startSearch,
|
2019-01-14 21:49:58 +00:00
|
|
|
clearSearch,
|
2019-08-09 23:12:29 +00:00
|
|
|
clearConversationSearch,
|
|
|
|
searchInConversation,
|
2019-01-14 21:49:58 +00:00
|
|
|
updateSearchTerm,
|
|
|
|
};
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
function startSearch(): StartSearchActionType {
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_START',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
2019-01-14 21:49:58 +00:00
|
|
|
function clearSearch(): ClearSearchActionType {
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_CLEAR',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
2019-08-09 23:12:29 +00:00
|
|
|
function clearConversationSearch(): ClearConversationSearchActionType {
|
|
|
|
return {
|
|
|
|
type: 'CLEAR_CONVERSATION_SEARCH',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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 },
|
|
|
|
});
|
|
|
|
|
|
|
|
const state = getState();
|
2022-02-23 18:48:40 +00:00
|
|
|
const ourConversationId = getUserConversationId(state);
|
|
|
|
strictAssert(
|
|
|
|
ourConversationId,
|
|
|
|
'updateSearchTerm our conversation is missing'
|
|
|
|
);
|
2021-11-01 18:43:02 +00:00
|
|
|
|
|
|
|
doSearch({
|
|
|
|
dispatch,
|
2022-04-07 18:47:12 +00:00
|
|
|
allConversations: getAllConversations(state),
|
|
|
|
regionCode: getRegionCode(state),
|
2021-11-01 18:43:02 +00:00
|
|
|
noteToSelf: getIntl(state)('noteToSelf').toLowerCase(),
|
2022-02-23 18:48:40 +00:00
|
|
|
ourConversationId,
|
2021-11-01 18:43:02 +00:00
|
|
|
query: getQuery(state),
|
|
|
|
searchConversationId: getSearchConversation(state)?.id,
|
|
|
|
});
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2021-11-01 18:43:02 +00:00
|
|
|
const doSearch = debounce(
|
|
|
|
({
|
|
|
|
dispatch,
|
2022-04-07 18:47:12 +00:00
|
|
|
allConversations,
|
|
|
|
regionCode,
|
2021-11-01 18:43:02 +00:00
|
|
|
noteToSelf,
|
|
|
|
ourConversationId,
|
|
|
|
query,
|
|
|
|
searchConversationId,
|
|
|
|
}: Readonly<{
|
|
|
|
dispatch: ThunkDispatch<
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
| SearchMessagesResultsFulfilledActionType
|
|
|
|
| SearchDiscussionsResultsFulfilledActionType
|
|
|
|
>;
|
2022-04-07 18:47:12 +00:00
|
|
|
allConversations: ReadonlyArray<ConversationType>;
|
2021-11-01 18:43:02 +00:00
|
|
|
noteToSelf: string;
|
2022-04-07 18:47:12 +00:00
|
|
|
regionCode: string | undefined;
|
2021-11-01 18:43:02 +00:00
|
|
|
ourConversationId: string;
|
|
|
|
query: string;
|
|
|
|
searchConversationId: undefined | string;
|
|
|
|
}>) => {
|
|
|
|
if (!query) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
(async () => {
|
|
|
|
dispatch({
|
|
|
|
type: 'SEARCH_MESSAGES_RESULTS_FULFILLED',
|
|
|
|
payload: {
|
|
|
|
messages: await queryMessages(query, searchConversationId),
|
|
|
|
query,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
})();
|
|
|
|
|
|
|
|
if (!searchConversationId) {
|
|
|
|
(async () => {
|
2021-11-11 22:43:05 +00:00
|
|
|
const { conversationIds, contactIds } =
|
|
|
|
await queryConversationsAndContacts(query, {
|
|
|
|
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
|
|
|
|
);
|
|
|
|
|
2021-04-05 22:18:19 +00:00
|
|
|
async function queryMessages(
|
|
|
|
query: string,
|
|
|
|
searchConversationId?: string
|
|
|
|
): Promise<Array<ClientSearchResultMessageType>> {
|
2019-05-31 22:42:01 +00:00
|
|
|
try {
|
|
|
|
const normalized = cleanSearchTerm(query);
|
2021-04-08 10:32:12 +00:00
|
|
|
if (normalized.length === 0) {
|
|
|
|
return [];
|
|
|
|
}
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
if (searchConversationId) {
|
|
|
|
return searchMessagesInConversation(normalized, searchConversationId);
|
|
|
|
}
|
|
|
|
|
2019-09-04 14:46:28 +00:00
|
|
|
return dataSearchMessages(normalized);
|
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: {
|
|
|
|
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>;
|
|
|
|
}> {
|
2022-04-07 18:47:12 +00:00
|
|
|
const { ourConversationId, noteToSelf, regionCode, allConversations } =
|
|
|
|
options;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2022-04-07 18:47:12 +00:00
|
|
|
const searchResults: Array<ConversationType> =
|
|
|
|
filterAndSortConversationsByRecent(allConversations, query, regionCode);
|
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
|
|
|
}
|
|
|
|
|
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: '',
|
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
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2019-11-07 21:36:16 +00:00
|
|
|
if (action.type === 'SHOW_ARCHIVED_CONVERSATIONS') {
|
|
|
|
return getEmptyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SEARCH_START') {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
searchConversationId: undefined,
|
|
|
|
startSearchCounter: state.startSearchCounter + 1,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
if (action.type === 'SEARCH_CLEAR') {
|
2022-12-09 18:03:32 +00:00
|
|
|
return {
|
|
|
|
...getEmptyState(),
|
|
|
|
startSearchCounter: state.startSearchCounter,
|
|
|
|
};
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SEARCH_UPDATE') {
|
|
|
|
const { payload } = action;
|
|
|
|
const { query } = payload;
|
|
|
|
|
2021-04-14 10:52:37 +00:00
|
|
|
const hasQuery = Boolean(query);
|
2019-09-05 15:59:21 +00:00
|
|
|
const isWithinConversation = Boolean(state.searchConversationId);
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
query,
|
2019-09-05 15:59:21 +00:00
|
|
|
messagesLoading: hasQuery,
|
|
|
|
...(hasQuery
|
|
|
|
? {
|
|
|
|
messageIds: [],
|
|
|
|
messageLookup: {},
|
|
|
|
discussionsLoading: !isWithinConversation,
|
2021-02-23 20:34:28 +00:00
|
|
|
contactIds: [],
|
|
|
|
conversationIds: [],
|
2019-09-05 15:59:21 +00:00
|
|
|
}
|
|
|
|
: {}),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
return {
|
|
|
|
...getEmptyState(),
|
|
|
|
searchConversationId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
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) {
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2022-06-16 19:12:50 +00:00
|
|
|
if (action.type === SELECTED_CONVERSATION_CHANGED) {
|
2019-01-14 21:49:58 +00:00
|
|
|
const { payload } = action;
|
2019-08-09 23:12:29 +00:00
|
|
|
const { id, messageId } = payload;
|
|
|
|
const { searchConversationId } = state;
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-08-09 23:12:29 +00:00
|
|
|
if (searchConversationId && searchConversationId !== id) {
|
|
|
|
return getEmptyState();
|
2019-01-14 21:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
selectedMessage: messageId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-11-07 21:36:16 +00:00
|
|
|
if (action.type === 'CONVERSATION_UNLOADED') {
|
|
|
|
const { payload } = action;
|
|
|
|
const { id } = payload;
|
|
|
|
const { searchConversationId } = state;
|
|
|
|
|
|
|
|
if (searchConversationId && searchConversationId === id) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|