2019-01-14 21:49:58 +00:00
|
|
|
import { omit, reject } from 'lodash';
|
|
|
|
|
|
|
|
import { normalize } from '../../types/PhoneNumber';
|
|
|
|
import { trigger } from '../../shims/events';
|
2019-05-31 22:42:01 +00:00
|
|
|
import { cleanSearchTerm } from '../../util/cleanSearchTerm';
|
|
|
|
import { searchConversations, searchMessages } from '../../../js/modules/data';
|
2019-01-14 21:49:58 +00:00
|
|
|
import { makeLookup } from '../../util/makeLookup';
|
|
|
|
|
|
|
|
import {
|
|
|
|
ConversationType,
|
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,
|
|
|
|
} from './conversations';
|
|
|
|
|
|
|
|
// State
|
|
|
|
|
2019-08-09 00:46:49 +00:00
|
|
|
export type MessageSearchResultType = MessageType & {
|
|
|
|
snippet: string;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type MessageSearchResultLookupType = {
|
|
|
|
[id: string]: MessageSearchResultType;
|
|
|
|
};
|
|
|
|
|
2019-01-14 21:49:58 +00:00
|
|
|
export type SearchStateType = {
|
2019-08-09 00:46:49 +00:00
|
|
|
// We store just ids of conversations, since that data is always cached in memory
|
|
|
|
contacts: Array<string>;
|
|
|
|
conversations: Array<string>;
|
2019-01-14 21:49:58 +00:00
|
|
|
query: string;
|
|
|
|
normalizedPhoneNumber?: 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;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
type SearchResultsPayloadType = {
|
|
|
|
query: string;
|
|
|
|
normalizedPhoneNumber?: string;
|
2019-03-20 17:42:28 +00:00
|
|
|
messages: Array<MessageSearchResultType>;
|
2019-01-14 21:49:58 +00:00
|
|
|
conversations: Array<string>;
|
|
|
|
contacts: Array<string>;
|
|
|
|
};
|
|
|
|
|
|
|
|
type SearchResultsKickoffActionType = {
|
|
|
|
type: 'SEARCH_RESULTS';
|
|
|
|
payload: Promise<SearchResultsPayloadType>;
|
|
|
|
};
|
|
|
|
type SearchResultsFulfilledActionType = {
|
|
|
|
type: 'SEARCH_RESULTS_FULFILLED';
|
|
|
|
payload: SearchResultsPayloadType;
|
|
|
|
};
|
|
|
|
type UpdateSearchTermActionType = {
|
|
|
|
type: 'SEARCH_UPDATE';
|
|
|
|
payload: {
|
|
|
|
query: string;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
type ClearSearchActionType = {
|
|
|
|
type: 'SEARCH_CLEAR';
|
|
|
|
payload: null;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SEARCH_TYPES =
|
|
|
|
| SearchResultsFulfilledActionType
|
|
|
|
| UpdateSearchTermActionType
|
|
|
|
| ClearSearchActionType
|
2019-05-31 22:42:01 +00:00
|
|
|
| MessageDeletedActionType
|
2019-01-14 21:49:58 +00:00
|
|
|
| RemoveAllConversationsActionType
|
|
|
|
| SelectedConversationChangedActionType;
|
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
search,
|
|
|
|
clearSearch,
|
|
|
|
updateSearchTerm,
|
|
|
|
startNewConversation,
|
|
|
|
};
|
|
|
|
|
|
|
|
function search(
|
|
|
|
query: string,
|
|
|
|
options: { regionCode: string; ourNumber: string; noteToSelf: string }
|
|
|
|
): SearchResultsKickoffActionType {
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_RESULTS',
|
|
|
|
payload: doSearch(query, options),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function doSearch(
|
|
|
|
query: string,
|
|
|
|
options: {
|
|
|
|
regionCode: string;
|
|
|
|
ourNumber: string;
|
|
|
|
noteToSelf: string;
|
|
|
|
}
|
|
|
|
): Promise<SearchResultsPayloadType> {
|
|
|
|
const { regionCode, ourNumber, noteToSelf } = options;
|
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
const [discussions, messages] = await Promise.all([
|
2019-01-14 21:49:58 +00:00
|
|
|
queryConversationsAndContacts(query, { ourNumber, noteToSelf }),
|
2019-05-31 22:42:01 +00:00
|
|
|
queryMessages(query),
|
2019-01-14 21:49:58 +00:00
|
|
|
]);
|
|
|
|
const { conversations, contacts } = discussions;
|
|
|
|
|
|
|
|
return {
|
|
|
|
query,
|
|
|
|
normalizedPhoneNumber: normalize(query, { regionCode }),
|
|
|
|
conversations,
|
|
|
|
contacts,
|
2019-05-31 22:42:01 +00:00
|
|
|
messages,
|
2019-01-14 21:49:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
function clearSearch(): ClearSearchActionType {
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_CLEAR',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function updateSearchTerm(query: string): UpdateSearchTermActionType {
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_UPDATE',
|
|
|
|
payload: {
|
|
|
|
query,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
function startNewConversation(
|
|
|
|
query: string,
|
|
|
|
options: { regionCode: string }
|
|
|
|
): ClearSearchActionType {
|
|
|
|
const { regionCode } = options;
|
|
|
|
const normalized = normalize(query, { regionCode });
|
|
|
|
if (!normalized) {
|
|
|
|
throw new Error('Attempted to start new conversation with invalid number');
|
|
|
|
}
|
|
|
|
trigger('showConversation', normalized);
|
|
|
|
|
|
|
|
return {
|
|
|
|
type: 'SEARCH_CLEAR',
|
|
|
|
payload: null,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
async function queryMessages(query: string) {
|
|
|
|
try {
|
|
|
|
const normalized = cleanSearchTerm(query);
|
2019-01-14 21:49:58 +00:00
|
|
|
|
2019-05-31 22:42:01 +00:00
|
|
|
return searchMessages(normalized);
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
2019-01-14 21:49:58 +00:00
|
|
|
|
|
|
|
async function queryConversationsAndContacts(
|
|
|
|
providedQuery: string,
|
|
|
|
options: { ourNumber: string; noteToSelf: string }
|
|
|
|
) {
|
|
|
|
const { ourNumber, noteToSelf } = options;
|
|
|
|
const query = providedQuery.replace(/[+-.()]*/g, '');
|
|
|
|
|
|
|
|
const searchResults: Array<ConversationType> = await searchConversations(
|
|
|
|
query
|
|
|
|
);
|
|
|
|
|
|
|
|
// Split into two groups - active conversations and items just from address book
|
|
|
|
let conversations: Array<string> = [];
|
|
|
|
let contacts: Array<string> = [];
|
|
|
|
const max = searchResults.length;
|
|
|
|
for (let i = 0; i < max; i += 1) {
|
|
|
|
const conversation = searchResults[i];
|
|
|
|
|
|
|
|
if (conversation.type === 'direct' && !Boolean(conversation.lastMessage)) {
|
|
|
|
contacts.push(conversation.id);
|
|
|
|
} else {
|
|
|
|
conversations.push(conversation.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Inject synthetic Note to Self entry if query matches localized 'Note to Self'
|
|
|
|
if (noteToSelf.indexOf(providedQuery.toLowerCase()) !== -1) {
|
|
|
|
// ensure that we don't have duplicates in our results
|
|
|
|
contacts = contacts.filter(id => id !== ourNumber);
|
|
|
|
conversations = conversations.filter(id => id !== ourNumber);
|
|
|
|
|
|
|
|
contacts.unshift(ourNumber);
|
|
|
|
}
|
|
|
|
|
|
|
|
return { conversations, contacts };
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
|
|
|
function getEmptyState(): SearchStateType {
|
|
|
|
return {
|
|
|
|
query: '',
|
2019-08-09 00:46:49 +00:00
|
|
|
messageIds: [],
|
2019-01-14 21:49:58 +00:00
|
|
|
messageLookup: {},
|
|
|
|
conversations: [],
|
|
|
|
contacts: [],
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
2019-05-16 22:32:11 +00:00
|
|
|
state: SearchStateType = getEmptyState(),
|
2019-01-14 21:49:58 +00:00
|
|
|
action: SEARCH_TYPES
|
|
|
|
): SearchStateType {
|
|
|
|
if (action.type === 'SEARCH_CLEAR') {
|
|
|
|
return getEmptyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SEARCH_UPDATE') {
|
|
|
|
const { payload } = action;
|
|
|
|
const { query } = payload;
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
query,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SEARCH_RESULTS_FULFILLED') {
|
|
|
|
const { payload } = action;
|
2019-08-09 00:46:49 +00:00
|
|
|
const {
|
|
|
|
contacts,
|
|
|
|
conversations,
|
|
|
|
messages,
|
|
|
|
normalizedPhoneNumber,
|
|
|
|
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
|
|
|
contacts,
|
|
|
|
conversations,
|
|
|
|
normalizedPhoneNumber,
|
|
|
|
query,
|
|
|
|
messageIds,
|
2019-01-14 21:49:58 +00:00
|
|
|
messageLookup: makeLookup(messages, 'id'),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'CONVERSATIONS_REMOVE_ALL') {
|
|
|
|
return getEmptyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'SELECTED_CONVERSATION_CHANGED') {
|
|
|
|
const { payload } = action;
|
|
|
|
const { messageId } = payload;
|
|
|
|
|
|
|
|
if (!messageId) {
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
selectedMessage: messageId,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
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),
|
2019-01-14 21:49:58 +00:00
|
|
|
messageLookup: omit(messageLookup, ['id']),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|