2020-10-30 20:34:04 +00:00
|
|
|
// Copyright 2020 Signal Messenger, LLC
|
|
|
|
// SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
import type { ReadonlyDeep } from 'type-fest';
|
2023-04-07 17:46:00 +00:00
|
|
|
import type { ThunkAction } from 'redux-thunk';
|
2023-07-13 19:06:42 +00:00
|
|
|
import { omit } from 'lodash';
|
2023-04-07 17:46:00 +00:00
|
|
|
|
2023-11-01 22:55:30 +00:00
|
|
|
import { generateSafetyNumber } from '../../util/safetyNumber';
|
2023-07-13 19:06:42 +00:00
|
|
|
import type { SafetyNumberType } from '../../types/safetyNumber';
|
2021-10-26 19:15:33 +00:00
|
|
|
import type { ConversationType } from './conversations';
|
2020-06-26 00:08:58 +00:00
|
|
|
import {
|
|
|
|
reloadProfiles,
|
|
|
|
toggleVerification,
|
|
|
|
} from '../../shims/contactVerification';
|
2021-09-17 18:27:53 +00:00
|
|
|
import * as log from '../../logging/log';
|
2022-11-22 18:43:43 +00:00
|
|
|
import * as Errors from '../../types/errors';
|
2023-04-07 17:46:00 +00:00
|
|
|
import type { StateType as RootStateType } from '../reducer';
|
2024-03-13 20:44:13 +00:00
|
|
|
import type { BoundActionCreatorsMapObject } from '../../hooks/useBoundActions';
|
|
|
|
import { useBoundActions } from '../../hooks/useBoundActions';
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type SafetyNumberContactType = ReadonlyDeep<{
|
2023-11-01 22:55:30 +00:00
|
|
|
safetyNumber: SafetyNumberType;
|
2020-06-26 00:08:58 +00:00
|
|
|
safetyNumberChanged?: boolean;
|
|
|
|
verificationDisabled: boolean;
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type SafetyNumberStateType = ReadonlyDeep<{
|
2020-06-26 00:08:58 +00:00
|
|
|
contacts: {
|
|
|
|
[key: string]: SafetyNumberContactType;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
const CLEAR_SAFETY_NUMBER = 'safetyNumber/CLEAR_SAFETY_NUMBER';
|
2020-06-26 00:08:58 +00:00
|
|
|
const GENERATE_FULFILLED = 'safetyNumber/GENERATE_FULFILLED';
|
|
|
|
const TOGGLE_VERIFIED_FULFILLED = 'safetyNumber/TOGGLE_VERIFIED_FULFILLED';
|
|
|
|
const TOGGLE_VERIFIED_PENDING = 'safetyNumber/TOGGLE_VERIFIED_PENDING';
|
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
type ClearSafetyNumberActionType = ReadonlyDeep<{
|
|
|
|
type: 'safetyNumber/CLEAR_SAFETY_NUMBER';
|
|
|
|
payload: {
|
|
|
|
contactId: string;
|
|
|
|
};
|
|
|
|
}>;
|
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type GenerateFulfilledActionType = ReadonlyDeep<{
|
2020-06-26 00:08:58 +00:00
|
|
|
type: 'safetyNumber/GENERATE_FULFILLED';
|
|
|
|
payload: {
|
2023-04-07 17:46:00 +00:00
|
|
|
contact: ConversationType;
|
2023-11-01 22:55:30 +00:00
|
|
|
safetyNumber: SafetyNumberType;
|
2020-06-26 00:08:58 +00:00
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ToggleVerifiedPendingActionType = ReadonlyDeep<{
|
2020-06-26 00:08:58 +00:00
|
|
|
type: 'safetyNumber/TOGGLE_VERIFIED_PENDING';
|
2023-04-07 17:46:00 +00:00
|
|
|
payload: {
|
|
|
|
contact: ConversationType;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
type ToggleVerifiedFulfilledActionType = ReadonlyDeep<{
|
2020-06-26 00:08:58 +00:00
|
|
|
type: 'safetyNumber/TOGGLE_VERIFIED_FULFILLED';
|
2023-04-07 17:46:00 +00:00
|
|
|
payload: {
|
|
|
|
contact: ConversationType;
|
2023-11-01 22:55:30 +00:00
|
|
|
safetyNumber?: SafetyNumberType;
|
2023-04-07 17:46:00 +00:00
|
|
|
safetyNumberChanged?: boolean;
|
|
|
|
};
|
2023-01-13 20:07:26 +00:00
|
|
|
}>;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-01-13 20:07:26 +00:00
|
|
|
export type SafetyNumberActionType = ReadonlyDeep<
|
2023-07-13 19:06:42 +00:00
|
|
|
| ClearSafetyNumberActionType
|
2020-06-26 00:08:58 +00:00
|
|
|
| GenerateFulfilledActionType
|
|
|
|
| ToggleVerifiedPendingActionType
|
2023-01-13 20:07:26 +00:00
|
|
|
| ToggleVerifiedFulfilledActionType
|
|
|
|
>;
|
2020-06-26 00:08:58 +00:00
|
|
|
|
2023-07-13 19:06:42 +00:00
|
|
|
function clearSafetyNumber(contactId: string): ClearSafetyNumberActionType {
|
|
|
|
return {
|
|
|
|
type: CLEAR_SAFETY_NUMBER,
|
|
|
|
payload: { contactId },
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-07 17:46:00 +00:00
|
|
|
function generate(
|
2020-06-26 00:08:58 +00:00
|
|
|
contact: ConversationType
|
2023-04-07 17:46:00 +00:00
|
|
|
): ThunkAction<void, RootStateType, unknown, GenerateFulfilledActionType> {
|
2023-11-01 20:35:55 +00:00
|
|
|
return async dispatch => {
|
2023-04-07 17:46:00 +00:00
|
|
|
try {
|
2023-11-01 22:55:30 +00:00
|
|
|
const safetyNumber = await generateSafetyNumber(contact);
|
2023-04-07 17:46:00 +00:00
|
|
|
dispatch({
|
|
|
|
type: GENERATE_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
contact,
|
2023-11-01 22:55:30 +00:00
|
|
|
safetyNumber,
|
2023-04-07 17:46:00 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (error) {
|
|
|
|
log.error(
|
|
|
|
'failed to generate security number:',
|
|
|
|
Errors.toLogFormat(error)
|
|
|
|
);
|
|
|
|
}
|
2020-06-26 00:08:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2023-04-07 17:46:00 +00:00
|
|
|
function toggleVerified(
|
|
|
|
contact: ConversationType
|
|
|
|
): ThunkAction<
|
|
|
|
void,
|
|
|
|
RootStateType,
|
|
|
|
unknown,
|
|
|
|
ToggleVerifiedPendingActionType | ToggleVerifiedFulfilledActionType
|
|
|
|
> {
|
2023-11-01 20:35:55 +00:00
|
|
|
return async dispatch => {
|
2023-04-07 17:46:00 +00:00
|
|
|
dispatch({
|
|
|
|
type: TOGGLE_VERIFIED_PENDING,
|
|
|
|
payload: {
|
|
|
|
contact,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
await alterVerification(contact);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: TOGGLE_VERIFIED_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
contact,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
} catch (err) {
|
|
|
|
if (err.name === 'OutgoingIdentityKeyError') {
|
|
|
|
await reloadProfiles(contact.id);
|
2023-11-01 22:55:30 +00:00
|
|
|
const safetyNumber = await generateSafetyNumber(contact);
|
2023-04-07 17:46:00 +00:00
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: TOGGLE_VERIFIED_FULFILLED,
|
|
|
|
payload: {
|
|
|
|
contact,
|
2023-11-01 22:55:30 +00:00
|
|
|
safetyNumber,
|
2023-04-07 17:46:00 +00:00
|
|
|
safetyNumberChanged: true,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2020-06-26 00:08:58 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async function alterVerification(contact: ConversationType): Promise<void> {
|
|
|
|
try {
|
|
|
|
await toggleVerification(contact.id);
|
|
|
|
} catch (result) {
|
|
|
|
if (result instanceof Error) {
|
|
|
|
if (result.name === 'OutgoingIdentityKeyError') {
|
|
|
|
throw result;
|
|
|
|
} else {
|
2022-11-22 18:43:43 +00:00
|
|
|
log.error('failed to toggle verified:', Errors.toLogFormat(result));
|
2020-06-26 00:08:58 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const keyError = result.errors.find(
|
|
|
|
(error: Error) => error.name === 'OutgoingIdentityKeyError'
|
|
|
|
);
|
|
|
|
if (keyError) {
|
|
|
|
throw keyError;
|
|
|
|
} else {
|
|
|
|
result.errors.forEach((error: Error) => {
|
2022-11-22 18:43:43 +00:00
|
|
|
log.error('failed to toggle verified:', Errors.toLogFormat(error));
|
2020-06-26 00:08:58 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export const actions = {
|
2023-07-13 19:06:42 +00:00
|
|
|
clearSafetyNumber,
|
2020-06-26 00:08:58 +00:00
|
|
|
generateSafetyNumber: generate,
|
|
|
|
toggleVerified,
|
|
|
|
};
|
|
|
|
|
2024-03-13 20:44:13 +00:00
|
|
|
export const useSafetyNumberActions = (): BoundActionCreatorsMapObject<
|
|
|
|
typeof actions
|
|
|
|
> => useBoundActions(actions);
|
|
|
|
|
2022-02-23 18:48:40 +00:00
|
|
|
export function getEmptyState(): SafetyNumberStateType {
|
2020-06-26 00:08:58 +00:00
|
|
|
return {
|
|
|
|
contacts: {},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export function reducer(
|
2020-12-14 19:47:21 +00:00
|
|
|
state: Readonly<SafetyNumberStateType> = getEmptyState(),
|
|
|
|
action: Readonly<SafetyNumberActionType>
|
2020-06-26 00:08:58 +00:00
|
|
|
): SafetyNumberStateType {
|
2023-07-13 19:06:42 +00:00
|
|
|
if (action.type === CLEAR_SAFETY_NUMBER) {
|
|
|
|
const { contactId } = action.payload;
|
|
|
|
return {
|
|
|
|
contacts: omit(state.contacts, contactId),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-06-26 00:08:58 +00:00
|
|
|
if (action.type === TOGGLE_VERIFIED_PENDING) {
|
|
|
|
const { contact } = action.payload;
|
|
|
|
const { id } = contact;
|
|
|
|
const record = state.contacts[id];
|
|
|
|
return {
|
|
|
|
contacts: {
|
|
|
|
...state.contacts,
|
|
|
|
[id]: {
|
|
|
|
...record,
|
|
|
|
safetyNumberChanged: false,
|
|
|
|
verificationDisabled: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === TOGGLE_VERIFIED_FULFILLED) {
|
|
|
|
const { contact, ...restProps } = action.payload;
|
|
|
|
const { id } = contact;
|
|
|
|
const record = state.contacts[id];
|
|
|
|
return {
|
|
|
|
contacts: {
|
|
|
|
...state.contacts,
|
|
|
|
[id]: {
|
|
|
|
...record,
|
|
|
|
...restProps,
|
|
|
|
verificationDisabled: false,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
if (action.type === GENERATE_FULFILLED) {
|
2023-11-01 22:55:30 +00:00
|
|
|
const { contact, safetyNumber } = action.payload;
|
2020-06-26 00:08:58 +00:00
|
|
|
const { id } = contact;
|
|
|
|
const record = state.contacts[id];
|
|
|
|
return {
|
|
|
|
contacts: {
|
|
|
|
...state.contacts,
|
|
|
|
[id]: {
|
|
|
|
...record,
|
2023-11-01 22:55:30 +00:00
|
|
|
safetyNumber,
|
2020-06-26 00:08:58 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return state;
|
|
|
|
}
|