signal-desktop/ts/state/ducks/username.ts

577 lines
15 KiB
TypeScript
Raw Normal View History

2022-10-18 17:12:02 +00:00
// Copyright 2022 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
import type { ThunkAction } from 'redux-thunk';
import type { ReadonlyDeep } from 'type-fest';
2022-10-18 17:12:02 +00:00
import type { UsernameReservationType } from '../../types/Username';
import {
ReserveUsernameError,
ConfirmUsernameResult,
} from '../../types/Username';
2022-10-18 17:12:02 +00:00
import * as usernameServices from '../../services/username';
2023-07-20 03:14:08 +00:00
import { storageServiceUploadJob } from '../../services/storage';
2022-10-18 17:12:02 +00:00
import type { ReserveUsernameResultType } from '../../services/username';
import { missingCaseError } from '../../util/missingCaseError';
import { sleep } from '../../util/sleep';
import { assertDev } from '../../util/assert';
import type { StateType as RootStateType } from '../reducer';
import type { PromiseAction } from '../util';
import { getMe } from '../selectors/conversations';
import {
UsernameEditState,
2023-07-20 03:14:08 +00:00
UsernameLinkState,
2022-10-18 17:12:02 +00:00
UsernameReservationState,
UsernameReservationError,
} from './usernameEnums';
import { showToast } from './toast';
import { ToastType } from '../../types/Toast';
2022-10-18 17:12:02 +00:00
import type { ToastActionType } from './toast';
export type UsernameReservationStateType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
state: UsernameReservationState;
reservation?: UsernameReservationType;
error?: UsernameReservationError;
abortController?: AbortController;
}>;
export type UsernameStateType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
// ProfileEditor
editState: UsernameEditState;
2023-07-20 03:14:08 +00:00
// UsernameLinkModalBody
linkState: UsernameLinkState;
2022-10-18 17:12:02 +00:00
// EditUsernameModalBody
usernameReservation: UsernameReservationStateType;
}>;
// Actions
const SET_USERNAME_EDIT_STATE = 'username/SET_USERNAME_EDIT_STATE';
const OPEN_USERNAME_RESERVATION_MODAL = 'username/OPEN_RESERVATION_MODAL';
const CLOSE_USERNAME_RESERVATION_MODAL = 'username/CLOSE_RESERVATION_MODAL';
const SET_USERNAME_RESERVATION_ERROR = 'username/SET_RESERVATION_ERROR';
const RESERVE_USERNAME = 'username/RESERVE_USERNAME';
const CONFIRM_USERNAME = 'username/CONFIRM_USERNAME';
const DELETE_USERNAME = 'username/DELETE_USERNAME';
2023-07-20 03:14:08 +00:00
const RESET_USERNAME_LINK = 'username/RESET_USERNAME_LINK';
2022-10-18 17:12:02 +00:00
type SetUsernameEditStateActionType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
type: typeof SET_USERNAME_EDIT_STATE;
payload: {
editState: UsernameEditState;
};
}>;
2022-10-18 17:12:02 +00:00
type OpenUsernameReservationModalActionType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
type: typeof OPEN_USERNAME_RESERVATION_MODAL;
}>;
2022-10-18 17:12:02 +00:00
type CloseUsernameReservationModalActionType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
type: typeof CLOSE_USERNAME_RESERVATION_MODAL;
}>;
2022-10-18 17:12:02 +00:00
type SetUsernameReservationErrorActionType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
type: typeof SET_USERNAME_RESERVATION_ERROR;
payload: {
error: UsernameReservationError | undefined;
};
}>;
2022-10-18 17:12:02 +00:00
type ReserveUsernameActionType = ReadonlyDeep<
PromiseAction<
typeof RESERVE_USERNAME,
ReserveUsernameResultType | undefined,
{ abortController: AbortController }
>
>;
type ConfirmUsernameActionType = ReadonlyDeep<
PromiseAction<typeof CONFIRM_USERNAME, ConfirmUsernameResult>
>;
type DeleteUsernameActionType = ReadonlyDeep<
PromiseAction<typeof DELETE_USERNAME, void>
2022-10-18 17:12:02 +00:00
>;
2023-07-20 03:14:08 +00:00
type ResetUsernameLinkActionType = ReadonlyDeep<
PromiseAction<typeof RESET_USERNAME_LINK, void>
>;
2022-10-18 17:12:02 +00:00
export type UsernameActionType = ReadonlyDeep<
2022-10-18 17:12:02 +00:00
| SetUsernameEditStateActionType
| OpenUsernameReservationModalActionType
| CloseUsernameReservationModalActionType
| SetUsernameReservationErrorActionType
| ReserveUsernameActionType
| ConfirmUsernameActionType
| DeleteUsernameActionType
2023-07-20 03:14:08 +00:00
| ResetUsernameLinkActionType
>;
2022-10-18 17:12:02 +00:00
export const actions = {
setUsernameEditState,
openUsernameReservationModal,
closeUsernameReservationModal,
setUsernameReservationError,
reserveUsername,
confirmUsername,
deleteUsername,
2023-07-20 03:14:08 +00:00
markCompletedUsernameOnboarding,
resetUsernameLink,
setUsernameLinkColor,
markCompletedUsernameLinkOnboarding,
2022-10-18 17:12:02 +00:00
};
export function setUsernameEditState(
editState: UsernameEditState
): SetUsernameEditStateActionType {
return {
type: SET_USERNAME_EDIT_STATE,
payload: { editState },
};
}
export function openUsernameReservationModal(): OpenUsernameReservationModalActionType {
return {
type: OPEN_USERNAME_RESERVATION_MODAL,
};
}
export function closeUsernameReservationModal(): CloseUsernameReservationModalActionType {
return {
type: CLOSE_USERNAME_RESERVATION_MODAL,
};
}
export function setUsernameReservationError(
error: UsernameReservationError | undefined
): SetUsernameReservationErrorActionType {
return {
type: SET_USERNAME_RESERVATION_ERROR,
payload: { error },
};
}
const INPUT_DELAY_MS = 500;
export type ReserveUsernameOptionsType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
doReserveUsername?: typeof usernameServices.reserveUsername;
delay?: number;
}>;
export function reserveUsername(
nickname: string,
{
doReserveUsername = usernameServices.reserveUsername,
delay = INPUT_DELAY_MS,
}: ReserveUsernameOptionsType = {}
): ThunkAction<
void,
RootStateType,
unknown,
ReserveUsernameActionType | SetUsernameReservationErrorActionType
> {
return (dispatch, getState) => {
if (!nickname) {
return;
}
const { username } = getMe(getState());
const abortController = new AbortController();
const { signal: abortSignal } = abortController;
const run = async () => {
try {
await sleep(delay, abortSignal);
} catch {
// Aborted
return;
}
return doReserveUsername({
previousUsername: username,
nickname,
abortSignal,
});
};
dispatch({
type: RESERVE_USERNAME,
payload: run(),
meta: { abortController },
});
};
}
export type ConfirmUsernameOptionsType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
doConfirmUsername?: typeof usernameServices.confirmUsername;
}>;
export function confirmUsername({
doConfirmUsername = usernameServices.confirmUsername,
}: ConfirmUsernameOptionsType = {}): ThunkAction<
void,
RootStateType,
unknown,
ConfirmUsernameActionType | SetUsernameReservationErrorActionType
> {
return (dispatch, getState) => {
const { reservation } = getState().username.usernameReservation;
if (reservation === undefined) {
assertDev(false, 'This should not happen');
dispatch(setUsernameReservationError(UsernameReservationError.General));
return;
}
dispatch({
type: CONFIRM_USERNAME,
payload: doConfirmUsername(reservation),
});
};
}
export type DeleteUsernameOptionsType = ReadonlyDeep<{
2022-10-18 17:12:02 +00:00
doDeleteUsername?: typeof usernameServices.deleteUsername;
// Only for testing
username?: string;
}>;
export function deleteUsername({
doDeleteUsername = usernameServices.deleteUsername,
username: defaultUsername,
}: DeleteUsernameOptionsType = {}): ThunkAction<
void,
RootStateType,
unknown,
DeleteUsernameActionType | ToastActionType
> {
return (dispatch, getState) => {
const me = getMe(getState());
const username = me.username ?? defaultUsername;
if (!username) {
return;
}
const run = async () => {
try {
await doDeleteUsername(username);
} catch {
dispatch(showToast({ toastType: ToastType.FailedToDeleteUsername }));
2022-10-18 17:12:02 +00:00
}
};
dispatch({
type: DELETE_USERNAME,
payload: run(),
});
};
}
2023-07-20 03:14:08 +00:00
export type ResetUsernameLinkOptionsType = ReadonlyDeep<{
doResetLink?: typeof usernameServices.resetLink;
}>;
export function resetUsernameLink({
doResetLink = usernameServices.resetLink,
}: ResetUsernameLinkOptionsType = {}): ThunkAction<
void,
RootStateType,
unknown,
ResetUsernameLinkActionType
> {
return dispatch => {
const me = window.ConversationController.getOurConversationOrThrow();
const username = me.get('username');
assertDev(username, 'Username is required for resetting link');
dispatch({
type: RESET_USERNAME_LINK,
payload: doResetLink(username),
});
};
}
function markCompletedUsernameOnboarding(): ThunkAction<
void,
RootStateType,
unknown,
never
> {
return async () => {
await window.storage.put('hasCompletedUsernameOnboarding', true);
const me = window.ConversationController.getOurConversationOrThrow();
me.captureChange('usernameOnboarding');
storageServiceUploadJob();
};
}
function markCompletedUsernameLinkOnboarding(): ThunkAction<
void,
RootStateType,
unknown,
never
> {
return async () => {
await window.storage.put('hasCompletedUsernameLinkOnboarding', true);
};
}
function setUsernameLinkColor(
color: number
): ThunkAction<void, RootStateType, unknown, never> {
return async () => {
await window.storage.put('usernameLinkColor', color);
const me = window.ConversationController.getOurConversationOrThrow();
me.captureChange('usernameLinkColor');
storageServiceUploadJob();
};
}
2022-10-18 17:12:02 +00:00
// Reducers
export function getEmptyState(): UsernameStateType {
return {
editState: UsernameEditState.Editing,
2023-07-20 03:14:08 +00:00
linkState: UsernameLinkState.Ready,
2022-10-18 17:12:02 +00:00
usernameReservation: {
state: UsernameReservationState.Closed,
},
};
}
export function reducer(
state: Readonly<UsernameStateType> = getEmptyState(),
action: Readonly<UsernameActionType>
): UsernameStateType {
const { usernameReservation } = state;
if (action.type === SET_USERNAME_EDIT_STATE) {
const { editState } = action.payload;
return {
...state,
editState,
};
}
if (action.type === OPEN_USERNAME_RESERVATION_MODAL) {
return {
...state,
usernameReservation: {
state: UsernameReservationState.Open,
},
};
}
if (action.type === CLOSE_USERNAME_RESERVATION_MODAL) {
return {
...state,
usernameReservation: {
state: UsernameReservationState.Closed,
},
};
}
if (action.type === SET_USERNAME_RESERVATION_ERROR) {
const { error } = action.payload;
return {
...state,
usernameReservation: {
...usernameReservation,
error,
reservation: undefined,
},
};
}
if (action.type === 'username/RESERVE_USERNAME_PENDING') {
usernameReservation.abortController?.abort();
const { meta } = action;
return {
...state,
usernameReservation: {
state: UsernameReservationState.Reserving,
abortController: meta.abortController,
},
};
}
if (action.type === 'username/RESERVE_USERNAME_FULFILLED') {
const { meta } = action;
// New reservation is pending
if (meta.abortController !== usernameReservation.abortController) {
return state;
}
assertDev(
usernameReservation.state === UsernameReservationState.Reserving,
'Must be reserving before resolving reservation'
);
const { payload } = action;
assertDev(
payload !== undefined,
'Payload can be undefined only when aborted'
);
if (!payload.ok) {
const { error } = payload;
let stateError: UsernameReservationError;
if (error === ReserveUsernameError.Unprocessable) {
stateError = UsernameReservationError.CheckCharacters;
} else if (error === ReserveUsernameError.Conflict) {
stateError = UsernameReservationError.UsernameNotAvailable;
} else if (error === ReserveUsernameError.NotEnoughCharacters) {
stateError = UsernameReservationError.NotEnoughCharacters;
} else if (error === ReserveUsernameError.TooManyCharacters) {
stateError = UsernameReservationError.TooManyCharacters;
} else if (error === ReserveUsernameError.CheckStartingCharacter) {
stateError = UsernameReservationError.CheckStartingCharacter;
} else if (error === ReserveUsernameError.CheckCharacters) {
stateError = UsernameReservationError.CheckCharacters;
2022-10-18 17:12:02 +00:00
} else {
throw missingCaseError(error);
}
return {
...state,
usernameReservation: {
state: UsernameReservationState.Open,
error: stateError,
},
};
}
const { reservation } = payload;
return {
...state,
usernameReservation: {
state: UsernameReservationState.Open,
reservation,
},
};
}
if (action.type === 'username/RESERVE_USERNAME_REJECTED') {
const { meta } = action;
// New reservation is pending
if (meta.abortController !== usernameReservation.abortController) {
return state;
}
assertDev(
usernameReservation.state === UsernameReservationState.Reserving,
'Must be reserving before rejecting reservation'
);
return {
...state,
usernameReservation: {
state: UsernameReservationState.Open,
error: UsernameReservationError.General,
},
};
}
if (action.type === 'username/CONFIRM_USERNAME_PENDING') {
assertDev(
usernameReservation.state === UsernameReservationState.Open,
'Must be open before confirmation'
);
return {
...state,
usernameReservation: {
reservation: usernameReservation.reservation,
state: UsernameReservationState.Confirming,
},
};
}
if (action.type === 'username/CONFIRM_USERNAME_FULFILLED') {
assertDev(
usernameReservation.state === UsernameReservationState.Confirming,
'Must be reserving before resolving confirmation'
);
const { payload } = action;
if (payload === ConfirmUsernameResult.Ok) {
return {
...state,
usernameReservation: {
state: UsernameReservationState.Closed,
},
};
}
if (payload === ConfirmUsernameResult.ConflictOrGone) {
return {
...state,
usernameReservation: {
2023-03-09 01:38:52 +00:00
reservation: state.usernameReservation.reservation,
state: UsernameReservationState.Open,
error: UsernameReservationError.ConflictOrGone,
},
};
}
throw missingCaseError(payload);
2022-10-18 17:12:02 +00:00
}
if (action.type === 'username/CONFIRM_USERNAME_REJECTED') {
assertDev(
usernameReservation.state === UsernameReservationState.Confirming,
'Must be reserving before rejecting reservation'
);
return {
...state,
usernameReservation: {
state: UsernameReservationState.Open,
error: UsernameReservationError.General,
},
};
}
if (action.type === 'username/DELETE_USERNAME_PENDING') {
return {
...state,
editState: UsernameEditState.Deleting,
};
}
if (action.type === 'username/DELETE_USERNAME_FULFILLED') {
return {
...state,
editState: UsernameEditState.Editing,
};
}
if (action.type === 'username/DELETE_USERNAME_REJECTED') {
assertDev(false, 'Should never reject');
return state;
}
2023-07-20 03:14:08 +00:00
if (action.type === 'username/RESET_USERNAME_LINK_PENDING') {
return {
...state,
linkState: UsernameLinkState.Updating,
};
}
if (action.type === 'username/RESET_USERNAME_LINK_FULFILLED') {
return {
...state,
linkState: UsernameLinkState.Ready,
};
}
if (action.type === 'username/RESET_USERNAME_LINK_REJECTED') {
return {
...state,
linkState: UsernameLinkState.Ready,
};
}
2022-10-18 17:12:02 +00:00
return state;
}