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
|
|
|
|
|
|
|
import * as Errors from '../../types/errors';
|
|
|
|
import * as log from '../../logging/log';
|
|
|
|
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2022-04-12 00:26:09 +00:00
|
|
|
import type { UUIDStringType } from '../../types/UUID';
|
2022-08-18 20:44:53 +00:00
|
|
|
import { getUuidsForE164s } from '../../util/getUuidsForE164s';
|
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
|
|
|
|
|
|
|
|
export type AccountsStateType = {
|
2022-04-12 00:26:09 +00:00
|
|
|
accounts: Record<string, UUIDStringType | undefined>;
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Actions
|
|
|
|
|
|
|
|
type AccountUpdateActionType = {
|
|
|
|
type: 'accounts/UPDATE';
|
|
|
|
payload: {
|
2022-04-12 00:26:09 +00:00
|
|
|
phoneNumber: string;
|
|
|
|
uuid?: UUIDStringType;
|
2021-06-17 17:15:10 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
export type AccountsActionType = AccountUpdateActionType;
|
|
|
|
|
|
|
|
// Action Creators
|
|
|
|
|
|
|
|
export const actions = {
|
|
|
|
checkForAccount,
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
|
|
|
if (conversation && conversation.get('uuid')) {
|
2022-06-28 00:37:05 +00:00
|
|
|
log.info(`checkForAccount: found ${phoneNumber} in existing contacts`);
|
2022-04-12 00:26:09 +00:00
|
|
|
const uuid = conversation.get('uuid');
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: 'accounts/UPDATE',
|
|
|
|
payload: {
|
|
|
|
phoneNumber,
|
|
|
|
uuid,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
let uuid: UUIDStringType | 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 {
|
2022-08-18 20:44:53 +00:00
|
|
|
const uuidLookup = await getUuidsForE164s(server, [phoneNumber]);
|
|
|
|
const maybePair = uuidLookup.get(phoneNumber);
|
|
|
|
|
|
|
|
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',
|
|
|
|
});
|
|
|
|
uuid = maybeMerged?.get('uuid');
|
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,
|
|
|
|
uuid,
|
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;
|
2022-04-12 00:26:09 +00:00
|
|
|
const { phoneNumber, uuid } = payload;
|
2021-06-17 17:15:10 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
accounts: {
|
|
|
|
...state.accounts,
|
2022-04-12 00:26:09 +00:00
|
|
|
[phoneNumber]: uuid,
|
2021-06-17 17:15:10 +00:00
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|