Skip to content

Commit

Permalink
[lib][web][native] Refactor actions in user-actions.js pt.1
Browse files Browse the repository at this point in the history
Summary:
issue: https://linear.app/comm/issue/ENG-4681/create-action-objects-for-all-actions
Some more actions from this file will be updated in the next diff

Test Plan: Tested that the actions work as expected

Reviewers: michal, kamil, ginsu

Reviewed By: michal

Subscribers: ashoat, tomek, wyilio

Differential Revision: https://phab.comm.dev/D9501
  • Loading branch information
InkaAlicja committed Nov 6, 2023
1 parent b227c0e commit 6fb06ac
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 41 deletions.
72 changes: 52 additions & 20 deletions lib/actions/user-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ import type {
SubscriptionUpdateResult,
} from '../types/subscription-types.js';
import type { UserInfo, PasswordUpdate } from '../types/user-types.js';
import { extractKeyserverIDFromID } from '../utils/action-utils.js';
import type {
CallServerEndpoint,
CallServerEndpointOptions,
} from '../utils/call-server-endpoint.js';
import { getConfig } from '../utils/config.js';
import type { CallKeyserverEndpoint } from '../utils/keyserver-call';
import { useKeyserverCall } from '../utils/keyserver-call.js';
import sleep from '../utils/sleep.js';
import { ashoatKeyserverID } from '../utils/validation-utils.js';

const logOutActionTypes = Object.freeze({
started: 'LOG_OUT_STARTED',
Expand Down Expand Up @@ -71,17 +75,24 @@ const claimUsernameActionTypes = Object.freeze({
const claimUsernameCallServerEndpointOptions = { timeout: 500 };
const claimUsername =
(
callServerEndpoint: CallServerEndpoint,
callKeyserverEndpoint: CallKeyserverEndpoint,
): (() => Promise<ClaimUsernameResponse>) =>
async () => {
const response = await callServerEndpoint(
'claim_username',
{},
{ ...claimUsernameCallServerEndpointOptions },
);
return response;
const requests = { [ashoatKeyserverID]: {} };
const responses = await callKeyserverEndpoint('claim_username', requests, {
...claimUsernameCallServerEndpointOptions,
});
const response = responses[ashoatKeyserverID];
return {
message: response.message,
signature: response.signature,
};
};

function useClaimUsername(): () => Promise<ClaimUsernameResponse> {
return useKeyserverCall(claimUsername);
}

const deleteAccountActionTypes = Object.freeze({
started: 'DELETE_ACCOUNT_STARTED',
success: 'DELETE_ACCOUNT_SUCCESS',
Expand Down Expand Up @@ -248,21 +259,31 @@ const updateSubscriptionActionTypes = Object.freeze({
});
const updateSubscription =
(
callServerEndpoint: CallServerEndpoint,
callKeyserverEndpoint: CallKeyserverEndpoint,
): ((
subscriptionUpdate: SubscriptionUpdateRequest,
input: SubscriptionUpdateRequest,
) => Promise<SubscriptionUpdateResult>) =>
async subscriptionUpdate => {
const response = await callServerEndpoint(
async input => {
const keyserverID = extractKeyserverIDFromID(input.threadID);
const requests = { [keyserverID]: input };

const responses = await callKeyserverEndpoint(
'update_user_subscription',
subscriptionUpdate,
requests,
);
const response = responses[keyserverID];
return {
threadID: subscriptionUpdate.threadID,
threadID: input.threadID,
subscription: response.threadSubscription,
};
};

function useUpdateSubscription(): (
input: SubscriptionUpdateRequest,
) => Promise<SubscriptionUpdateResult> {
return useKeyserverCall(updateSubscription);
}

const setUserSettingsActionTypes = Object.freeze({
started: 'SET_USER_SETTINGS_STARTED',
success: 'SET_USER_SETTINGS_SUCCESS',
Expand All @@ -271,12 +292,23 @@ const setUserSettingsActionTypes = Object.freeze({

const setUserSettings =
(
callServerEndpoint: CallServerEndpoint,
): ((userSettingsRequest: UpdateUserSettingsRequest) => Promise<void>) =>
async userSettingsRequest => {
await callServerEndpoint('update_user_settings', userSettingsRequest);
callKeyserverEndpoint: CallKeyserverEndpoint,
allKeyserverIDs: $ReadOnlyArray<string>,
): ((input: UpdateUserSettingsRequest) => Promise<void>) =>
async input => {
const requests = {};
for (const keyserverID of allKeyserverIDs) {
requests[keyserverID] = input;
}
await callKeyserverEndpoint('update_user_settings', requests);
};

function useSetUserSettings(): (
input: UpdateUserSettingsRequest,
) => Promise<void> {
return useKeyserverCall(setUserSettings);
}

const getSessionPublicKeys =
(
callServerEndpoint: CallServerEndpoint,
Expand Down Expand Up @@ -345,7 +377,7 @@ export {
changeUserPasswordActionTypes,
changeUserPassword,
claimUsernameActionTypes,
claimUsername,
useClaimUsername,
deleteAccount,
deleteAccountActionTypes,
getSessionPublicKeys,
Expand All @@ -362,9 +394,9 @@ export {
searchUsersActionTypes,
exactSearchUser,
exactSearchUserActionTypes,
setUserSettings,
useSetUserSettings,
setUserSettingsActionTypes,
updateSubscription,
useUpdateSubscription,
updateSubscriptionActionTypes,
policyAcknowledgment,
policyAcknowledgmentActionTypes,
Expand Down
9 changes: 3 additions & 6 deletions native/chat/settings/thread-settings-home-notifs.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,15 @@ import { View, Switch } from 'react-native';

import {
updateSubscriptionActionTypes,
updateSubscription,
useUpdateSubscription,
} from 'lib/actions/user-actions.js';
import type {
SubscriptionUpdateRequest,
SubscriptionUpdateResult,
} from 'lib/types/subscription-types.js';
import { type ThreadInfo } from 'lib/types/thread-types.js';
import type { DispatchActionPromise } from 'lib/utils/action-utils.js';
import {
useServerCall,
useDispatchActionPromise,
} from 'lib/utils/action-utils.js';
import { useDispatchActionPromise } from 'lib/utils/action-utils.js';

import SingleLine from '../../components/single-line.react.js';
import { useStyles } from '../../themes/colors.js';
Expand Down Expand Up @@ -105,7 +102,7 @@ const ConnectedThreadSettingsHomeNotifs: React.ComponentType<BaseProps> =
) {
const styles = useStyles(unboundStyles);
const dispatchActionPromise = useDispatchActionPromise();
const callUpdateSubscription = useServerCall(updateSubscription);
const callUpdateSubscription = useUpdateSubscription();
return (
<ThreadSettingsHomeNotifs
{...props}
Expand Down
9 changes: 3 additions & 6 deletions native/chat/settings/thread-settings-push-notifs.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,15 @@ import Linking from 'react-native/Libraries/Linking/Linking.js';

import {
updateSubscriptionActionTypes,
updateSubscription,
useUpdateSubscription,
} from 'lib/actions/user-actions.js';
import type {
SubscriptionUpdateRequest,
SubscriptionUpdateResult,
} from 'lib/types/subscription-types.js';
import { type ThreadInfo } from 'lib/types/thread-types.js';
import type { DispatchActionPromise } from 'lib/utils/action-utils.js';
import {
useServerCall,
useDispatchActionPromise,
} from 'lib/utils/action-utils.js';
import { useDispatchActionPromise } from 'lib/utils/action-utils.js';

import SingleLine from '../../components/single-line.react.js';
import SWMansionIcon from '../../components/swmansion-icon.react.js';
Expand Down Expand Up @@ -176,7 +173,7 @@ const ConnectedThreadSettingsPushNotifs: React.ComponentType<BaseProps> =
) {
const styles = useStyles(unboundStyles);
const dispatchActionPromise = useDispatchActionPromise();
const callUpdateSubscription = useServerCall(updateSubscription);
const callUpdateSubscription = useUpdateSubscription();
const hasPushPermissions = useSelector(
state => state.deviceToken !== null && state.deviceToken !== undefined,
);
Expand Down
5 changes: 2 additions & 3 deletions native/profile/default-notifications-preferences.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { View, Text, Platform } from 'react-native';
import { ScrollView } from 'react-native-gesture-handler';

import {
setUserSettings,
useSetUserSettings,
setUserSettingsActionTypes,
} from 'lib/actions/user-actions.js';
import { registerFetchKey } from 'lib/reducers/loading-reducer.js';
Expand All @@ -18,7 +18,6 @@ import {
} from 'lib/types/account-types.js';
import {
type DispatchActionPromise,
useServerCall,
useDispatchActionPromise,
} from 'lib/utils/action-utils.js';

Expand Down Expand Up @@ -185,7 +184,7 @@ const ConnectedDefaultNotificationPreferences: React.ComponentType<BaseProps> =
) {
const styles = useStyles(unboundStyles);
const dispatchActionPromise = useDispatchActionPromise();
const changeNotificationSettings = useServerCall(setUserSettings);
const changeNotificationSettings = useSetUserSettings();
const defaultNotification = userSettingsTypes.DEFAULT_NOTIFICATIONS;

const selectedDefaultNotification = useSelector<NotificationTypes>(
Expand Down
9 changes: 3 additions & 6 deletions web/modals/threads/notifications/notifications-modal.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import * as React from 'react';

import {
updateSubscription,
useUpdateSubscription,
updateSubscriptionActionTypes,
} from 'lib/actions/user-actions.js';
import { canPromoteSidebar } from 'lib/hooks/promote-sidebar.react.js';
import { threadInfoSelector } from 'lib/selectors/thread-selectors.js';
import { threadIsSidebar } from 'lib/shared/thread-utils.js';
import {
useServerCall,
useDispatchActionPromise,
} from 'lib/utils/action-utils.js';
import { useDispatchActionPromise } from 'lib/utils/action-utils.js';

import css from './notifications-modal.css';
import AllNotifsIllustration from '../../../assets/all-notifs.react.js';
Expand Down Expand Up @@ -169,7 +166,7 @@ function NotificationsModal(props: Props): React.Node {

const dispatchActionPromise = useDispatchActionPromise();

const callUpdateSubscription = useServerCall(updateSubscription);
const callUpdateSubscription = useUpdateSubscription();

const onClickSave = React.useCallback(() => {
dispatchActionPromise(
Expand Down

0 comments on commit 6fb06ac

Please sign in to comment.