Skip to content

Commit

Permalink
[lib][native][web] Introduce useUpdateRelationships
Browse files Browse the repository at this point in the history
Summary:
This diff updates the `updateRelationships` action function to use the new `update_relationships2` API, and introduces `useUpdateRelationships`, a new hook that abstracts some complexity and in following diffs will handle forking between thick/thread logic.

Depends on D13440

Test Plan: I tested this new endpoint as part of my testing of [ENG-9269](https://linear.app/comm/issue/ENG-9269/relationship-buttons-result-in-a-thin-thread-being-created)

Reviewers: tomek

Reviewed By: tomek

Differential Revision: https://phab.comm.dev/D13441
  • Loading branch information
Ashoat committed Sep 24, 2024
1 parent deeb875 commit bf4c664
Show file tree
Hide file tree
Showing 11 changed files with 100 additions and 127 deletions.
6 changes: 3 additions & 3 deletions lib/actions/relationship-actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import type { CallSingleKeyserverEndpoint } from '../keyserver-conn/call-single-keyserver-endpoint.js';
import type {
RelationshipErrors,
LegacyRelationshipRequest,
RelationshipRequest,
} from '../types/relationship-types.js';
import { ServerError } from '../utils/errors.js';

Expand All @@ -15,10 +15,10 @@ const updateRelationshipsActionTypes = Object.freeze({
const updateRelationships =
(
callSingleKeyserverEndpoint: CallSingleKeyserverEndpoint,
): ((request: LegacyRelationshipRequest) => Promise<RelationshipErrors>) =>
): ((request: RelationshipRequest) => Promise<RelationshipErrors>) =>
async request => {
const errors = await callSingleKeyserverEndpoint(
'update_relationships',
'update_relationships2',
request,
);

Expand Down
32 changes: 8 additions & 24 deletions lib/components/farcaster-data-handler.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,12 @@
import * as React from 'react';

import { setAuxUserFIDsActionType } from '../actions/aux-user-actions.js';
import {
updateRelationships as serverUpdateRelationships,
updateRelationshipsActionTypes,
} from '../actions/relationship-actions.js';
import { updateRelationshipsActionTypes } from '../actions/relationship-actions.js';
import { setSyncedMetadataEntryActionType } from '../actions/synced-metadata-actions.js';
import { useFindUserIdentities } from '../actions/user-actions.js';
import { NeynarClientContext } from '../components/neynar-client-provider.react.js';
import { useIsLoggedInToIdentityAndAuthoritativeKeyserver } from '../hooks/account-hooks.js';
import { useLegacyAshoatKeyserverCall } from '../keyserver-conn/legacy-keyserver-call.js';
import { useUpdateRelationships } from '../hooks/relationship-hooks.js';
import { IdentityClientContext } from '../shared/identity-client-context.js';
import { relationshipActions } from '../types/relationship-types.js';
import { syncedMetadataNames } from '../types/synced-metadata-types.js';
Expand Down Expand Up @@ -43,15 +40,11 @@ function FarcasterDataHandler(props: Props): React.Node {

const dispatch = useDispatch();
const dispatchActionPromise = useDispatchActionPromise();
const updateRelationships = useLegacyAshoatKeyserverCall(
serverUpdateRelationships,
);

const updateRelationships = useUpdateRelationships();
const createThreadsAndRobotextForFarcasterMutuals = React.useCallback(
(userIDsToFID: { +[userID: string]: string }) =>
updateRelationships({
action: relationshipActions.FARCASTER_MUTUAL,
userIDsToFID,
}),
(userIDs: $ReadOnlyArray<string>) =>
updateRelationships(relationshipActions.FARCASTER_MUTUAL, userIDs),
[updateRelationships],
);

Expand Down Expand Up @@ -100,24 +93,15 @@ function FarcasterDataHandler(props: Props): React.Node {
return;
}

const userIDsToFID: { +[userID: string]: string } = Object.fromEntries(
newCommUsers.map(({ userID, farcasterID }) => [userID, farcasterID]),
);

const userIDsToFIDIncludingCurrentUser: { +[userID: string]: string } = {
...userIDsToFID,
[(currentUserID: string)]: fid,
};
const newCommUserIDs = newCommUsers.map(({ userID }) => userID);

if (!loggedInRef.current) {
return;
}

void dispatchActionPromise(
updateRelationshipsActionTypes,
createThreadsAndRobotextForFarcasterMutuals(
userIDsToFIDIncludingCurrentUser,
),
createThreadsAndRobotextForFarcasterMutuals(newCommUserIDs),
);
}, [
isActive,
Expand Down
20 changes: 8 additions & 12 deletions lib/handlers/user-infos-handler.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@
import invariant from 'invariant';
import * as React from 'react';

import {
updateRelationships,
updateRelationshipsActionTypes,
} from '../actions/relationship-actions.js';
import { updateRelationshipsActionTypes } from '../actions/relationship-actions.js';
import {
useFindUserIdentities,
findUserIdentitiesActionTypes,
} from '../actions/user-actions.js';
import { useIsLoggedInToAuthoritativeKeyserver } from '../hooks/account-hooks.js';
import { useGetAndUpdateDeviceListsForUsers } from '../hooks/peer-list-hooks.js';
import { useLegacyAshoatKeyserverCall } from '../keyserver-conn/legacy-keyserver-call.js';
import { useUpdateRelationships } from '../hooks/relationship-hooks.js';
import { usersWithMissingDeviceListSelector } from '../selectors/user-selectors.js';
import { IdentityClientContext } from '../shared/identity-client-context.js';
import { useTunnelbroker } from '../tunnelbroker/tunnelbroker-context.js';
Expand Down Expand Up @@ -46,8 +43,7 @@ function UserInfosHandler(): React.Node {
const requestedIDsRef = React.useRef(new Set<string>());
const requestedAvatarsRef = React.useRef(new Set<string>());

const callUpdateRelationships =
useLegacyAshoatKeyserverCall(updateRelationships);
const updateRelationships = useUpdateRelationships();

const currentUserInfo = useSelector(state => state.currentUserInfo);

Expand Down Expand Up @@ -107,10 +103,10 @@ function UserInfosHandler(): React.Node {

const updateRelationshipsPromise = (async () => {
try {
return await callUpdateRelationships({
action: relationshipActions.ACKNOWLEDGE,
userIDs: userIDsWithoutOwnID,
});
return await updateRelationships(
relationshipActions.ACKNOWLEDGE,
userIDsWithoutOwnID,
);
} catch (e) {
if (e instanceof FetchTimeout) {
userIDsWithoutOwnID.forEach(id =>
Expand All @@ -129,7 +125,7 @@ function UserInfosHandler(): React.Node {
})();
}, [
getAuthMetadata,
callUpdateRelationships,
updateRelationships,
currentUserInfo?.id,
dispatchActionPromise,
findUserIdentities,
Expand Down
36 changes: 36 additions & 0 deletions lib/hooks/relationship-hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// @flow

import * as React from 'react';

import { updateRelationships as serverUpdateRelationships } from '../actions/relationship-actions.js';
import { useLegacyAshoatKeyserverCall } from '../keyserver-conn/legacy-keyserver-call.js';
import type {
RelationshipAction,
RelationshipErrors,
} from '../types/relationship-types.js';

function useUpdateRelationships(): (
action: RelationshipAction,
userIDs: $ReadOnlyArray<string>,
) => Promise<RelationshipErrors> {
const updateRelationships = useLegacyAshoatKeyserverCall(
serverUpdateRelationships,
);
return React.useCallback(
(action: RelationshipAction, userIDs: $ReadOnlyArray<string>) =>
updateRelationships({
action,
users: Object.fromEntries(
userIDs.map(userID => [
userID,
{
createRobotextInThinThread: true,
},
]),
),
}),
[updateRelationships],
);
}

export { useUpdateRelationships };
18 changes: 5 additions & 13 deletions lib/hooks/relationship-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,8 @@
import invariant from 'invariant';
import * as React from 'react';

import {
updateRelationships as serverUpdateRelationships,
updateRelationshipsActionTypes,
} from '../actions/relationship-actions.js';
import { useLegacyAshoatKeyserverCall } from '../keyserver-conn/legacy-keyserver-call.js';
import { updateRelationshipsActionTypes } from '../actions/relationship-actions.js';
import { useUpdateRelationships } from '../hooks/relationship-hooks.js';
import { getSingleOtherUser } from '../shared/thread-utils.js';
import type { ThreadInfo } from '../types/minimally-encoded-thread-permissions-types.js';
import {
Expand Down Expand Up @@ -84,9 +81,7 @@ function useRelationshipCallbacks(
const [isLoadingUnfriendUser, setIsLoadingUnfriendUser] =
React.useState(false);

const callUpdateRelationships = useLegacyAshoatKeyserverCall(
serverUpdateRelationships,
);
const updateRelationships = useUpdateRelationships();
const updateRelationship = React.useCallback(
async (
action: TraditionalRelationshipAction,
Expand All @@ -95,18 +90,15 @@ function useRelationshipCallbacks(
try {
setInProgress(true);
invariant(otherUserID, 'Other user info id should be present');
return await callUpdateRelationships({
action,
userIDs: [otherUserID],
});
return await updateRelationships(action, [otherUserID]);
} catch (e) {
onErrorCallback?.();
throw e;
} finally {
setInProgress(false);
}
},
[callUpdateRelationships, onErrorCallback, otherUserID],
[updateRelationships, onErrorCallback, otherUserID],
);

const dispatchActionPromise = useDispatchActionPromise();
Expand Down
18 changes: 5 additions & 13 deletions native/chat/settings/thread-settings-edit-relationship.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ import invariant from 'invariant';
import * as React from 'react';
import { Text, View } from 'react-native';

import {
updateRelationships as serverUpdateRelationships,
updateRelationshipsActionTypes,
} from 'lib/actions/relationship-actions.js';
import { updateRelationshipsActionTypes } from 'lib/actions/relationship-actions.js';
import { useENSNames } from 'lib/hooks/ens-cache.js';
import { useLegacyAshoatKeyserverCall } from 'lib/keyserver-conn/legacy-keyserver-call.js';
import { useUpdateRelationships } from 'lib/hooks/relationship-hooks.js';
import {
getRelationshipActionText,
getRelationshipDispatchAction,
Expand Down Expand Up @@ -49,16 +46,11 @@ const ThreadSettingsEditRelationship: React.ComponentType<Props> =

const [otherUserInfo] = useENSNames([otherUserInfoFromRedux]);

const callUpdateRelationships = useLegacyAshoatKeyserverCall(
serverUpdateRelationships,
);
const updateRelationships = useUpdateRelationships();
const updateRelationship = React.useCallback(
async (action: TraditionalRelationshipAction) => {
try {
return await callUpdateRelationships({
action,
userIDs: [otherUserInfo.id],
});
return await updateRelationships(action, [otherUserInfo.id]);
} catch (e) {
Alert.alert(
unknownErrorAlertDetails.title,
Expand All @@ -71,7 +63,7 @@ const ThreadSettingsEditRelationship: React.ComponentType<Props> =
throw e;
}
},
[callUpdateRelationships, otherUserInfo],
[updateRelationships, otherUserInfo],
);

const { relationshipButton } = props;
Expand Down
24 changes: 10 additions & 14 deletions native/profile/relationship-list-item.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import invariant from 'invariant';
import * as React from 'react';
import { View, Text, TouchableOpacity, ActivityIndicator } from 'react-native';

import {
updateRelationshipsActionTypes,
updateRelationships,
} from 'lib/actions/relationship-actions.js';
import { useLegacyAshoatKeyserverCall } from 'lib/keyserver-conn/legacy-keyserver-call.js';
import { updateRelationshipsActionTypes } from 'lib/actions/relationship-actions.js';
import { useUpdateRelationships } from 'lib/hooks/relationship-hooks.js';
import { createLoadingStatusSelector } from 'lib/selectors/loading-selectors.js';
import type { LoadingStatus } from 'lib/types/loading-types.js';
import type { ReactRef } from 'lib/types/react-types.js';
Expand All @@ -17,7 +14,7 @@ import {
type RelationshipErrors,
userRelationshipStatus,
relationshipActions,
type LegacyRelationshipRequest,
type RelationshipAction,
} from 'lib/types/relationship-types.js';
import type {
AccountUserInfo,
Expand Down Expand Up @@ -111,7 +108,8 @@ type Props = {
+dispatchActionPromise: DispatchActionPromise,
// async functions that hit server APIs
+updateRelationships: (
request: LegacyRelationshipRequest,
action: RelationshipAction,
userIDs: $ReadOnlyArray<string>,
) => Promise<RelationshipErrors>,
// withOverlayContext
+overlayContext: ?OverlayContextType,
Expand Down Expand Up @@ -306,10 +304,9 @@ class RelationshipListItem extends React.PureComponent<Props> {
action: TraditionalRelationshipAction,
): Promise<RelationshipErrors> {
try {
return await this.props.updateRelationships({
action,
userIDs: [this.props.userInfo.id],
});
return await this.props.updateRelationships(action, [
this.props.userInfo.id,
]);
} catch (e) {
Alert.alert(
unknownErrorAlertDetails.title,
Expand Down Expand Up @@ -337,8 +334,7 @@ const ConnectedRelationshipListItem: React.ComponentType<BaseProps> =
const colors = useColors();
const styles = useStyles(unboundStyles);
const dispatchActionPromise = useDispatchActionPromise();
const boundUpdateRelationships =
useLegacyAshoatKeyserverCall(updateRelationships);
const updateRelationships = useUpdateRelationships();
const overlayContext = React.useContext(OverlayContext);
const keyboardState = React.useContext(KeyboardContext);

Expand All @@ -352,7 +348,7 @@ const ConnectedRelationshipListItem: React.ComponentType<BaseProps> =
colors={colors}
styles={styles}
dispatchActionPromise={dispatchActionPromise}
updateRelationships={boundUpdateRelationships}
updateRelationships={updateRelationships}
overlayContext={overlayContext}
keyboardState={keyboardState}
navigateToUserProfileBottomSheet={navigateToUserProfileBottomSheet}
Expand Down
17 changes: 5 additions & 12 deletions native/profile/relationship-list.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ import * as React from 'react';
import { View, Text, Platform } from 'react-native';
import { FlatList } from 'react-native-gesture-handler';

import {
updateRelationshipsActionTypes,
updateRelationships,
} from 'lib/actions/relationship-actions.js';
import { updateRelationshipsActionTypes } from 'lib/actions/relationship-actions.js';
import { useENSNames } from 'lib/hooks/ens-cache.js';
import { useLegacyAshoatKeyserverCall } from 'lib/keyserver-conn/legacy-keyserver-call.js';
import { useUpdateRelationships } from 'lib/hooks/relationship-hooks.js';
import { registerFetchKey } from 'lib/reducers/loading-reducer.js';
import { useUserSearchIndex } from 'lib/selectors/nav-selectors.js';
import { userRelationshipsSelector } from 'lib/selectors/relationship-selectors.js';
Expand Down Expand Up @@ -200,19 +197,15 @@ function RelationshipList(props: Props): React.Node {
tagInputRef.current?.focus();
}, []);

const callUpdateRelationships =
useLegacyAshoatKeyserverCall(updateRelationships);
const updateRelationships = useUpdateRelationships();
const updateRelationshipsOnServer = React.useCallback(async () => {
const action = {
[FriendListRouteName]: relationshipActions.FRIEND,
[BlockListRouteName]: relationshipActions.BLOCK,
}[routeName];
const userIDs = currentTags.map(userInfo => userInfo.id);
try {
const result = await callUpdateRelationships({
action,
userIDs,
});
const result = await updateRelationships(action, userIDs);
setCurrentTags([]);
setSearchInputText('');
return result;
Expand All @@ -228,7 +221,7 @@ function RelationshipList(props: Props): React.Node {
}, [
routeName,
currentTags,
callUpdateRelationships,
updateRelationships,
onUnknownErrorAlertAcknowledged,
]);

Expand Down
Loading

0 comments on commit bf4c664

Please sign in to comment.