Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 4 additions & 30 deletions lib/hooks/peer-list-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import invariant from 'invariant';
import * as React from 'react';

import { useGetDeviceListsForUsers } from './use-get-device-lists-for-users.js';
import { setPeerDeviceListsActionType } from '../actions/aux-user-actions.js';
import { usePeerOlmSessionsCreatorContext } from '../components/peer-olm-session-creator-provider.react.js';
import {
Expand All @@ -14,23 +15,21 @@ import { usePeerToPeerCommunication } from '../tunnelbroker/peer-to-peer-context
import { useTunnelbroker } from '../tunnelbroker/tunnelbroker-context.js';
import { useResendPeerToPeerMessages } from '../tunnelbroker/use-resend-peer-to-peer-messages.js';
import type {
UsersRawDeviceLists,
UsersDevicesPlatformDetails,
SignedDeviceList,
RawDeviceList,
SignedDeviceList,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Concerning to see these moved around, makes me wonder if the prior PR would fail ESLint CI

UserDevicesPlatformDetails,
UsersRawDeviceLists,
} from '../types/identity-service-types.js';
import {
type DeviceListUpdated,
peerToPeerMessageTypes,
} from '../types/tunnelbroker/peer-to-peer-message-types.js';
import {
userActionsP2PMessageTypes,
type AccountDeletionP2PMessage,
userActionsP2PMessageTypes,
} from '../types/tunnelbroker/user-actions-peer-to-peer-message-types.js';
import { getConfig } from '../utils/config.js';
import { getContentSigningKey } from '../utils/crypto-utils.js';
import { convertSignedDeviceListsToRawDeviceLists } from '../utils/device-list-utils.js';
import { identityServiceQueryTimeout } from '../utils/identity-service.js';
import { values } from '../utils/objects.js';
import { useDispatch, useSelector } from '../utils/redux-utils.js';
Expand All @@ -42,30 +41,6 @@ export type PrimaryDeviceChange = {
+newPrimaryDeviceID: string,
};

function useGetDeviceListsForUsers(): (
userIDs: $ReadOnlyArray<string>,
) => Promise<{
+deviceLists: UsersRawDeviceLists,
+usersPlatformDetails: UsersDevicesPlatformDetails,
}> {
const client = React.useContext(IdentityClientContext);
const identityClient = client?.identityClient;
invariant(identityClient, 'Identity client should be set');
return React.useCallback(
async (userIDs: $ReadOnlyArray<string>) => {
const peersDeviceLists =
await identityClient.getDeviceListsForUsers(userIDs);
return {
deviceLists: convertSignedDeviceListsToRawDeviceLists(
peersDeviceLists.usersSignedDeviceLists,
),
usersPlatformDetails: peersDeviceLists.usersDevicesPlatformDetails,
};
},
[identityClient],
);
}

async function throwOnTimeout(userIDs: $ReadOnlyArray<string>) {
await sleep(identityServiceQueryTimeout);
throw new Error(`Device list fetch for ${JSON.stringify(userIDs)} timed out`);
Expand Down Expand Up @@ -350,7 +325,6 @@ function useResetRatchetState(): (userID: ?string) => Promise<void> {
}

export {
useGetDeviceListsForUsers,
useBroadcastDeviceListUpdates,
useGetAndUpdateDeviceListsForUsers,
useBroadcastAccountDeletion,
Expand Down
37 changes: 37 additions & 0 deletions lib/hooks/use-get-device-lists-for-users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// @flow

import invariant from 'invariant';
import * as React from 'react';

import { IdentityClientContext } from '../shared/identity-client-context.js';
import type {
UsersDevicesPlatformDetails,
UsersRawDeviceLists,
} from '../types/identity-service-types.js';
import { convertSignedDeviceListsToRawDeviceLists } from '../utils/device-list-utils.js';

function useGetDeviceListsForUsers(): (
userIDs: $ReadOnlyArray<string>,
) => Promise<{
+deviceLists: UsersRawDeviceLists,
+usersPlatformDetails: UsersDevicesPlatformDetails,
}> {
const client = React.useContext(IdentityClientContext);
const identityClient = client?.identityClient;
invariant(identityClient, 'Identity client should be set');
return React.useCallback(
async (userIDs: $ReadOnlyArray<string>) => {
const peersDeviceLists =
await identityClient.getDeviceListsForUsers(userIDs);
return {
deviceLists: convertSignedDeviceListsToRawDeviceLists(
peersDeviceLists.usersSignedDeviceLists,
),
usersPlatformDetails: peersDeviceLists.usersDevicesPlatformDetails,
};
},
[identityClient],
);
}

export { useGetDeviceListsForUsers };
32 changes: 26 additions & 6 deletions lib/hooks/user-identities-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,23 @@
import invariant from 'invariant';
import * as React from 'react';

import { useGetDeviceListsForUsers } from './use-get-device-lists-for-users.js';
import { useFindUserIdentities } from '../actions/find-user-identities-actions.js';
import { logTypes, useDebugLogs } from '../components/debug-logs-context.js';
import { extractFIDFromUserID } from '../shared/id-utils.js';
import { IdentityClientContext } from '../shared/identity-client-context.js';
import type { FarcasterUser } from '../types/identity-service-types.js';
import type { AccountUserInfo } from '../types/user-types.js';
import { getMessageForException } from '../utils/errors.js';
import { olmSupportRequired } from '../utils/olm-support-required.js';
import { useSelector } from '../utils/redux-utils.js';
import { useIsFarcasterDCsIntegrationEnabled } from '../utils/services-utils.js';

function useUsersSupportThickThreads(): (
userIDs: $ReadOnlyArray<string>,
) => Promise<$ReadOnlyMap<string, boolean | void>> {
const findUserIdentities = useFindUserIdentities();
const getDeviceListsForUsers = useGetDeviceListsForUsers();
const auxUserInfos = useSelector(state => state.auxUserStore.auxUserInfos);

return React.useCallback(
Expand All @@ -25,20 +28,37 @@ function useUsersSupportThickThreads(): (

const usersNeedingFetch = [];
for (const userID of userIDs) {
if (auxUserInfos[userID]?.deviceList) {
usersSupportingThickThreads.set(userID, true);
const peerDeviceList = auxUserInfos[userID]?.deviceList?.devices;
const peerPlatformDetails =
auxUserInfos[userID]?.devicesPlatformDetails;
if (peerDeviceList && peerPlatformDetails) {
const supportsVodozemac = peerDeviceList.every(
deviceID => !olmSupportRequired(peerPlatformDetails?.[deviceID]),
);
usersSupportingThickThreads.set(userID, supportsVodozemac);
} else {
usersNeedingFetch.push(userID);
}
}
if (usersNeedingFetch.length > 0) {
const { identities, reservedUserIdentifiers } =
await findUserIdentities(usersNeedingFetch);
const [
{ identities, reservedUserIdentifiers },
{ deviceLists, usersPlatformDetails },
] = await Promise.all([
findUserIdentities(usersNeedingFetch),
getDeviceListsForUsers(usersNeedingFetch),
]);

for (const userID of usersNeedingFetch) {
const isReserved = !!reservedUserIdentifiers[userID];
const doesNotExist = identities[userID] === undefined && !isReserved;
if (identities[userID]) {
usersSupportingThickThreads.set(userID, true);
const peerDeviceList = deviceLists[userID]?.devices ?? [];
const peerPlatformDetails = usersPlatformDetails[userID];
const supportsVodozemac = peerDeviceList.every(
deviceID => !olmSupportRequired(peerPlatformDetails?.[deviceID]),
);
usersSupportingThickThreads.set(userID, supportsVodozemac);
} else if (doesNotExist) {
usersSupportingThickThreads.set(userID, undefined);
} else {
Expand All @@ -48,7 +68,7 @@ function useUsersSupportThickThreads(): (
}
return usersSupportingThickThreads;
},
[auxUserInfos, findUserIdentities],
[auxUserInfos, findUserIdentities, getDeviceListsForUsers],
);
}

Expand Down