2021-06-17 17:15:10 +00:00
|
|
|
// Copyright 2021 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
2022-04-12 00:26:09 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2022-04-12 00:26:09 +00:00
|
|
|
import * as Errors from '../../types/errors';
|
|
|
|
import * as log from '../../logging/log';
|
|
|
|
|
2022-12-21 20:44:23 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2023-08-10 16:43:33 +00:00
|
|
|
import type { ServiceIdString } from '../../types/ServiceId';
|
|
|
|
import { getServiceIdsForE164s } from '../../util/getServiceIdsForE164s';
|
2022-12-21 20:44:23 +00:00
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { NoopActionType } from './noop';
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
// State
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type AccountsStateType = ReadonlyDeep<{
|
2023-08-10 16:43:33 +00:00
|
|
|
accounts: Record<string, ServiceIdString | undefined>;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type AccountUpdateActionType = ReadonlyDeep<{
|
2021-06-17 17:15:10 +00:00
|
|
|
type: 'accounts/UPDATE';
|
|
|
|
payload: {
|
2022-04-12 00:26:09 +00:00
|
|
|
phoneNumber: string;
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId?: ServiceIdString;
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type AccountsActionType = ReadonlyDeep<AccountUpdateActionType>;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
checkForAccount,
|
|
|
|
};
|
|
|
|
|
2022-12-21 20:44:23 +00:00
|
|
|
export const useAccountsActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
|
|
|
|
2021-06-17 17:15:10 +00:00
|
|
|
function checkForAccount(
|
2022-04-12 00:26:09 +00:00
|
|
|
phoneNumber: string
|
2021-06-17 17:15:10 +00:00
|
|
|
): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
AccountUpdateActionType | NoopActionType
|
|
|
|
> {
|
2022-04-12 00:26:09 +00:00
|
|
|
return async (dispatch, getState) => {
|
2022-08-18 20:44:53 +00:00
|
|
|
const { server } = window.textsecure;
|
|
|
|
if (!server) {
|
2021-06-17 17:15:10 +00:00
|
|
|
dispatch({
|
|
|
|
type: 'NOOP',
|
|
|
|
payload: null,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-04-12 00:26:09 +00:00
|
|
|
const conversation = window.ConversationController.get(phoneNumber);
|
2023-08-16 20:54:39 +00:00
|
|
|
if (conversation && conversation.getServiceId()) {
|
2022-06-28 00:37:05 +00:00
|
|
|
log.info(`checkForAccount: found ${phoneNumber} in existing contacts`);
|
2023-08-16 20:54:39 +00:00
|
|
|
const serviceId = conversation.getServiceId();
|
2022-04-12 00:26:09 +00:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'accounts/UPDATE',
|
|
|
|
payload: {
|
|
|
|
phoneNumber,
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2022-04-12 00:26:09 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = getState();
|
|
|
|
const existing = Object.prototype.hasOwnProperty.call(
|
|
|
|
state.accounts.accounts,
|
|
|
|
phoneNumber
|
|
|
|
);
|
|
|
|
if (existing) {
|
|
|
|
dispatch({
|
|
|
|
type: 'NOOP',
|
|
|
|
payload: null,
|
|
|
|
});
|
2022-08-18 20:44:53 +00:00
|
|
|
return;
|
2022-04-12 00:26:09 +00:00
|
|
|
}
|
|
|
|
|
2023-08-10 16:43:33 +00:00
|
|
|
let serviceId: ServiceIdString | undefined;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
2022-06-28 00:37:05 +00:00
|
|
|
log.info(`checkForAccount: looking ${phoneNumber} up on server`);
|
2021-06-17 17:15:10 +00:00
|
|
|
try {
|
2024-02-13 21:41:48 +00:00
|
|
|
const { entries: serviceIdLookup } = await getServiceIdsForE164s(server, [
|
2023-08-16 20:54:39 +00:00
|
|
|
phoneNumber,
|
|
|
|
]);
|
|
|
|
const maybePair = serviceIdLookup.get(phoneNumber);
|
2022-08-18 20:44:53 +00:00
|
|
|
|
|
|
|
if (maybePair) {
|
2022-12-05 22:46:54 +00:00
|
|
|
const { conversation: maybeMerged } =
|
|
|
|
window.ConversationController.maybeMergeContacts({
|
|
|
|
aci: maybePair.aci,
|
|
|
|
pni: maybePair.pni,
|
|
|
|
e164: phoneNumber,
|
|
|
|
reason: 'checkForAccount',
|
|
|
|
});
|
2023-08-16 20:54:39 +00:00
|
|
|
serviceId = maybeMerged.getServiceId();
|
2022-08-18 20:44:53 +00:00
|
|
|
}
|
2022-04-12 00:26:09 +00:00
|
|
|
} catch (error) {
|
|
|
|
log.error('checkForAccount:', Errors.toLogFormat(error));
|
2021-06-17 17:15:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'accounts/UPDATE',
|
|
|
|
payload: {
|
2022-04-12 00:26:09 +00:00
|
|
|
phoneNumber,
|
2023-08-10 16:43:33 +00:00
|
|
|
serviceId,
|
2021-06-17 17:15:10 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reducer
|
|
|
|
|
|
|
|
export function getEmptyState(): AccountsStateType {
|
|
|
|
return {
|
|
|
|
accounts: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
|
|
|
state: Readonly<AccountsStateType> = getEmptyState(),
|
|
|
|
action: Readonly<AccountsActionType>
|
|
|
|
): AccountsStateType {
|
|
|
|
if (!state) {
|
|
|
|
return getEmptyState();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === 'accounts/UPDATE') {
|
|
|
|
const { payload } = action;
|
2023-08-10 16:43:33 +00:00
|
|
|
const { phoneNumber, serviceId } = payload;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
accounts: {
|
|
|
|
...state.accounts,
|
2023-08-10 16:43:33 +00:00
|
|
|
[phoneNumber]: serviceId,
|
2021-06-17 17:15:10 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|