diff --git a/keyserver/src/endpoints.js b/keyserver/src/endpoints.js index 7cfc191be..81e08c0c3 100644 --- a/keyserver/src/endpoints.js +++ b/keyserver/src/endpoints.js @@ -1,541 +1,541 @@ // @flow import t from 'tcomb'; import type { TType } from 'tcomb'; import { baseLegalPolicies } from 'lib/facts/policies.js'; import type { PolicyType } from 'lib/facts/policies.js'; import type { Endpoint } from 'lib/types/endpoints.js'; import { calendarQueryValidator } from 'lib/types/entry-types.js'; import { sessionStateValidator } from 'lib/types/session-types.js'; import { endpointValidators } from 'lib/types/validators/endpoint-validators.js'; import { updateUserAvatarRequestValidator } from 'lib/utils/avatar-utils.js'; import { updateActivityResponder, threadSetUnreadStatusResponder, setThreadUnreadStatusValidator, updateActivityResponderInputValidator, } from './responders/activity-responders.js'; import { fetchCommunityInfosResponder, fetchAllCommunityInfosWithNamesResponder, } from './responders/community-responders.js'; import { deviceTokenUpdateResponder, deviceTokenUpdateRequestInputValidator, } from './responders/device-responders.js'; import { entryFetchResponder, entryRevisionFetchResponder, entryCreationResponder, entryUpdateResponder, entryDeletionResponder, entryRestorationResponder, calendarQueryUpdateResponder, createEntryRequestInputValidator, deleteEntryRequestInputValidator, entryQueryInputValidator, entryRevisionHistoryFetchInputValidator, restoreEntryRequestInputValidator, saveEntryRequestInputValidator, } from './responders/entry-responders.js'; import { createOrUpdateFarcasterChannelTagResponder, deleteFarcasterChannelTagResponder, createOrUpdateFarcasterChannelTagInputValidator, deleteFarcasterChannelTagInputValidator, } from './responders/farcaster-channel-tag-responders.js'; import type { JSONResponder } from './responders/handlers.js'; import { createJSONResponder } from './responders/handlers.js'; import { getOlmSessionInitializationDataResponder } from './responders/keys-responders.js'; import { createOrUpdatePublicLinkResponder, disableInviteLinkResponder, fetchPrimaryInviteLinksResponder, inviteLinkVerificationResponder, createOrUpdatePublicLinkInputValidator, disableInviteLinkInputValidator, inviteLinkVerificationRequestInputValidator, } from './responders/link-responders.js'; import { messageReportCreationResponder, messageReportCreationRequestInputValidator, } from './responders/message-report-responder.js'; import { textMessageCreationResponder, messageFetchResponder, multimediaMessageCreationResponder, reactionMessageCreationResponder, editMessageCreationResponder, fetchPinnedMessagesResponder, searchMessagesResponder, sendMultimediaMessageRequestInputValidator, sendReactionMessageRequestInputValidator, editMessageRequestInputValidator, sendTextMessageRequestInputValidator, fetchMessageInfosRequestInputValidator, fetchPinnedMessagesResponderInputValidator, searchMessagesResponderInputValidator, } from './responders/message-responders.js'; import { getInitialReduxStateResponder, initialReduxStateRequestValidator, } from './responders/redux-state-responders.js'; import { - updateRelationshipsResponder, - updateRelationshipInputValidator, + legacyUpdateRelationshipsResponder, + legacyUpdateRelationshipInputValidator, } from './responders/relationship-responders.js'; import { reportCreationResponder, reportMultiCreationResponder, errorReportFetchInfosResponder, reportCreationRequestInputValidator, fetchErrorReportInfosRequestInputValidator, reportMultiCreationRequestInputValidator, } from './responders/report-responders.js'; import { userSearchResponder, exactUserSearchResponder, exactUserSearchRequestInputValidator, userSearchRequestInputValidator, } from './responders/search-responders.js'; import { siweNonceResponder } from './responders/siwe-nonce-responders.js'; import { threadDeletionResponder, roleUpdateResponder, memberRemovalResponder, threadLeaveResponder, threadUpdateResponder, threadCreationResponder, threadFetchMediaResponder, threadJoinResponder, toggleMessagePinResponder, roleModificationResponder, roleDeletionResponder, newThreadRequestInputValidator, threadDeletionRequestInputValidator, joinThreadRequestInputValidator, leaveThreadRequestInputValidator, threadFetchMediaRequestInputValidator, removeMembersRequestInputValidator, roleChangeRequestInputValidator, toggleMessagePinRequestInputValidator, updateThreadRequestInputValidator, roleDeletionRequestInputValidator, roleModificationRequestInputValidator, } from './responders/thread-responders.js'; import { fetchPendingUpdatesResponder } from './responders/update-responders.js'; import { keyserverAuthRequestInputValidator, keyserverAuthResponder, userSubscriptionUpdateResponder, passwordUpdateResponder, sendVerificationEmailResponder, sendPasswordResetEmailResponder, logOutResponder, accountDeletionResponder, accountCreationResponder, logInResponder, siweAuthResponder, oldPasswordUpdateResponder, updateUserSettingsResponder, policyAcknowledgmentResponder, updateUserAvatarResponder, registerRequestInputValidator, logInRequestInputValidator, policyAcknowledgmentRequestInputValidator, accountUpdateInputValidator, resetPasswordRequestInputValidator, siweAuthRequestInputValidator, subscriptionUpdateRequestInputValidator, updatePasswordRequestInputValidator, updateUserSettingsInputValidator, claimUsernameResponder, claimUsernameRequestInputValidator, } from './responders/user-responders.js'; import { codeVerificationResponder, codeVerificationRequestInputValidator, } from './responders/verification-responders.js'; import { versionResponder } from './responders/version-responders.js'; import type { Viewer } from './session/viewer.js'; import { uploadMediaMetadataResponder, uploadDeletionResponder, UploadDeletionRequestInputValidator, uploadMediaMetadataInputValidator, } from './uploads/uploads.js'; const ignoredArgumentValidator = t.irreducible( 'Ignored argument', () => true, ); type EndpointData = { responder: (viewer: Viewer, input: any) => Promise<*>, inputValidator: TType<*>, policies: $ReadOnlyArray, }; const jsonEndpointsData: { +[id: Endpoint]: EndpointData } = { create_account: { responder: accountCreationResponder, inputValidator: registerRequestInputValidator, policies: [], }, create_entry: { responder: entryCreationResponder, inputValidator: createEntryRequestInputValidator, policies: baseLegalPolicies, }, create_error_report: { responder: reportCreationResponder, inputValidator: reportCreationRequestInputValidator, policies: [], }, create_message_report: { responder: messageReportCreationResponder, inputValidator: messageReportCreationRequestInputValidator, policies: baseLegalPolicies, }, create_multimedia_message: { responder: multimediaMessageCreationResponder, inputValidator: sendMultimediaMessageRequestInputValidator, policies: baseLegalPolicies, }, create_or_update_public_link: { responder: createOrUpdatePublicLinkResponder, inputValidator: createOrUpdatePublicLinkInputValidator, policies: baseLegalPolicies, }, create_reaction_message: { responder: reactionMessageCreationResponder, inputValidator: sendReactionMessageRequestInputValidator, policies: baseLegalPolicies, }, disable_invite_link: { responder: disableInviteLinkResponder, inputValidator: disableInviteLinkInputValidator, policies: baseLegalPolicies, }, edit_message: { responder: editMessageCreationResponder, inputValidator: editMessageRequestInputValidator, policies: baseLegalPolicies, }, create_report: { responder: reportCreationResponder, inputValidator: reportCreationRequestInputValidator, policies: [], }, create_reports: { responder: reportMultiCreationResponder, inputValidator: reportMultiCreationRequestInputValidator, policies: [], }, create_text_message: { responder: textMessageCreationResponder, inputValidator: sendTextMessageRequestInputValidator, policies: baseLegalPolicies, }, create_thread: { responder: threadCreationResponder, inputValidator: newThreadRequestInputValidator, policies: baseLegalPolicies, }, delete_account: { responder: accountDeletionResponder, inputValidator: ignoredArgumentValidator, policies: [], }, delete_entry: { responder: entryDeletionResponder, inputValidator: deleteEntryRequestInputValidator, policies: baseLegalPolicies, }, delete_community_role: { responder: roleDeletionResponder, inputValidator: roleDeletionRequestInputValidator, policies: baseLegalPolicies, }, delete_thread: { responder: threadDeletionResponder, inputValidator: threadDeletionRequestInputValidator, policies: baseLegalPolicies, }, delete_upload: { responder: uploadDeletionResponder, inputValidator: UploadDeletionRequestInputValidator, policies: baseLegalPolicies, }, exact_search_user: { responder: exactUserSearchResponder, inputValidator: exactUserSearchRequestInputValidator, policies: [], }, fetch_entries: { responder: entryFetchResponder, inputValidator: entryQueryInputValidator, policies: baseLegalPolicies, }, fetch_entry_revisions: { responder: entryRevisionFetchResponder, inputValidator: entryRevisionHistoryFetchInputValidator, policies: baseLegalPolicies, }, fetch_error_report_infos: { responder: errorReportFetchInfosResponder, inputValidator: fetchErrorReportInfosRequestInputValidator, policies: baseLegalPolicies, }, fetch_messages: { responder: messageFetchResponder, inputValidator: fetchMessageInfosRequestInputValidator, policies: baseLegalPolicies, }, fetch_pending_updates: { responder: fetchPendingUpdatesResponder, inputValidator: sessionStateValidator, policies: baseLegalPolicies, }, fetch_pinned_messages: { responder: fetchPinnedMessagesResponder, inputValidator: fetchPinnedMessagesResponderInputValidator, policies: baseLegalPolicies, }, fetch_primary_invite_links: { responder: fetchPrimaryInviteLinksResponder, inputValidator: ignoredArgumentValidator, policies: baseLegalPolicies, }, fetch_thread_media: { responder: threadFetchMediaResponder, inputValidator: threadFetchMediaRequestInputValidator, policies: baseLegalPolicies, }, get_initial_redux_state: { responder: getInitialReduxStateResponder, inputValidator: initialReduxStateRequestValidator, policies: [], }, join_thread: { responder: threadJoinResponder, inputValidator: joinThreadRequestInputValidator, policies: baseLegalPolicies, }, keyserver_auth: { responder: keyserverAuthResponder, inputValidator: keyserverAuthRequestInputValidator, policies: [], }, leave_thread: { responder: threadLeaveResponder, inputValidator: leaveThreadRequestInputValidator, policies: baseLegalPolicies, }, log_in: { responder: logInResponder, inputValidator: logInRequestInputValidator, policies: [], }, log_out: { responder: logOutResponder, inputValidator: ignoredArgumentValidator, policies: [], }, modify_community_role: { responder: roleModificationResponder, inputValidator: roleModificationRequestInputValidator, policies: baseLegalPolicies, }, policy_acknowledgment: { responder: policyAcknowledgmentResponder, inputValidator: policyAcknowledgmentRequestInputValidator, policies: [], }, remove_members: { responder: memberRemovalResponder, inputValidator: removeMembersRequestInputValidator, policies: baseLegalPolicies, }, restore_entry: { responder: entryRestorationResponder, inputValidator: restoreEntryRequestInputValidator, policies: baseLegalPolicies, }, search_messages: { responder: searchMessagesResponder, inputValidator: searchMessagesResponderInputValidator, policies: baseLegalPolicies, }, search_users: { responder: userSearchResponder, inputValidator: userSearchRequestInputValidator, policies: baseLegalPolicies, }, send_password_reset_email: { responder: sendPasswordResetEmailResponder, inputValidator: resetPasswordRequestInputValidator, policies: [], }, send_verification_email: { responder: sendVerificationEmailResponder, inputValidator: ignoredArgumentValidator, policies: [], }, set_thread_unread_status: { responder: threadSetUnreadStatusResponder, inputValidator: setThreadUnreadStatusValidator, policies: baseLegalPolicies, }, toggle_message_pin: { responder: toggleMessagePinResponder, inputValidator: toggleMessagePinRequestInputValidator, policies: baseLegalPolicies, }, update_account: { responder: passwordUpdateResponder, inputValidator: accountUpdateInputValidator, policies: baseLegalPolicies, }, update_activity: { responder: updateActivityResponder, inputValidator: updateActivityResponderInputValidator, policies: baseLegalPolicies, }, update_calendar_query: { responder: calendarQueryUpdateResponder, inputValidator: calendarQueryValidator, policies: baseLegalPolicies, }, update_user_settings: { responder: updateUserSettingsResponder, inputValidator: updateUserSettingsInputValidator, policies: baseLegalPolicies, }, update_device_token: { responder: deviceTokenUpdateResponder, inputValidator: deviceTokenUpdateRequestInputValidator, policies: [], }, update_entry: { responder: entryUpdateResponder, inputValidator: saveEntryRequestInputValidator, policies: baseLegalPolicies, }, update_password: { responder: oldPasswordUpdateResponder, inputValidator: updatePasswordRequestInputValidator, policies: baseLegalPolicies, }, update_relationships: { - responder: updateRelationshipsResponder, - inputValidator: updateRelationshipInputValidator, + responder: legacyUpdateRelationshipsResponder, + inputValidator: legacyUpdateRelationshipInputValidator, policies: baseLegalPolicies, }, update_role: { responder: roleUpdateResponder, inputValidator: roleChangeRequestInputValidator, policies: baseLegalPolicies, }, update_thread: { responder: threadUpdateResponder, inputValidator: updateThreadRequestInputValidator, policies: baseLegalPolicies, }, update_user_subscription: { responder: userSubscriptionUpdateResponder, inputValidator: subscriptionUpdateRequestInputValidator, policies: baseLegalPolicies, }, verify_code: { responder: codeVerificationResponder, inputValidator: codeVerificationRequestInputValidator, policies: baseLegalPolicies, }, verify_invite_link: { responder: inviteLinkVerificationResponder, inputValidator: inviteLinkVerificationRequestInputValidator, policies: baseLegalPolicies, }, siwe_nonce: { responder: siweNonceResponder, inputValidator: ignoredArgumentValidator, policies: [], }, siwe_auth: { responder: siweAuthResponder, inputValidator: siweAuthRequestInputValidator, policies: [], }, claim_username: { responder: claimUsernameResponder, inputValidator: claimUsernameRequestInputValidator, policies: [], }, update_user_avatar: { responder: updateUserAvatarResponder, inputValidator: updateUserAvatarRequestValidator, policies: baseLegalPolicies, }, upload_media_metadata: { responder: uploadMediaMetadataResponder, inputValidator: uploadMediaMetadataInputValidator, policies: baseLegalPolicies, }, get_olm_session_initialization_data: { responder: getOlmSessionInitializationDataResponder, inputValidator: ignoredArgumentValidator, policies: [], }, version: { responder: versionResponder, inputValidator: ignoredArgumentValidator, policies: [], }, fetch_community_infos: { responder: fetchCommunityInfosResponder, inputValidator: ignoredArgumentValidator, policies: baseLegalPolicies, }, fetch_all_community_infos_with_names: { responder: fetchAllCommunityInfosWithNamesResponder, inputValidator: ignoredArgumentValidator, policies: baseLegalPolicies, }, create_or_update_farcaster_channel_tag: { responder: createOrUpdateFarcasterChannelTagResponder, inputValidator: createOrUpdateFarcasterChannelTagInputValidator, policies: baseLegalPolicies, }, delete_farcaster_channel_tag: { responder: deleteFarcasterChannelTagResponder, inputValidator: deleteFarcasterChannelTagInputValidator, policies: baseLegalPolicies, }, }; function createJSONResponders(obj: { +[Endpoint]: EndpointData }): { +[Endpoint]: JSONResponder, } { const result: { [Endpoint]: JSONResponder } = {}; Object.keys(obj).forEach((endpoint: Endpoint) => { const responder = createJSONResponder( obj[endpoint].responder, obj[endpoint].inputValidator, endpointValidators[endpoint].validator, obj[endpoint].policies, endpoint, ); result[endpoint] = responder; }); return result; } const jsonEndpoints: { +[Endpoint]: JSONResponder } = createJSONResponders(jsonEndpointsData); export { jsonEndpoints }; diff --git a/keyserver/src/responders/relationship-responders.js b/keyserver/src/responders/relationship-responders.js index 7b02b4709..259d82f9b 100644 --- a/keyserver/src/responders/relationship-responders.js +++ b/keyserver/src/responders/relationship-responders.js @@ -1,39 +1,39 @@ // @flow import t, { type TInterface, type TUnion } from 'tcomb'; import { - type TraditionalRelationshipRequest, + type LegacyTraditionalRelationshipRequest, type RelationshipErrors, traditionalRelationshipActionsList, - type RelationshipRequest, - farcasterRelationshipRequestValidator, + type LegacyRelationshipRequest, + legacyFarcasterRelationshipRequestValidator, } from 'lib/types/relationship-types.js'; import { tShape, tUserID } from 'lib/utils/validation-utils.js'; import type { Viewer } from '../session/viewer.js'; import { updateRelationships } from '../updaters/relationship-updaters.js'; -export const traditionalRelationshipRequestValidator: TInterface = - tShape({ +const legacyTraditionalRelationshipRequestValidator: TInterface = + tShape({ action: t.enums.of( traditionalRelationshipActionsList, 'relationship action', ), userIDs: t.list(tUserID), }); -export const updateRelationshipInputValidator: TUnion = +export const legacyUpdateRelationshipInputValidator: TUnion = t.union([ - traditionalRelationshipRequestValidator, - farcasterRelationshipRequestValidator, + legacyTraditionalRelationshipRequestValidator, + legacyFarcasterRelationshipRequestValidator, ]); -async function updateRelationshipsResponder( +async function legacyUpdateRelationshipsResponder( viewer: Viewer, - request: RelationshipRequest, + request: LegacyRelationshipRequest, ): Promise { return await updateRelationships(viewer, request); } -export { updateRelationshipsResponder }; +export { legacyUpdateRelationshipsResponder }; diff --git a/keyserver/src/updaters/relationship-updaters.js b/keyserver/src/updaters/relationship-updaters.js index 9e47a9422..d4b270a96 100644 --- a/keyserver/src/updaters/relationship-updaters.js +++ b/keyserver/src/updaters/relationship-updaters.js @@ -1,455 +1,455 @@ // @flow import invariant from 'invariant'; import { sortUserIDs } from 'lib/shared/relationship-utils.js'; import { messageTypes } from 'lib/types/message-types-enum.js'; import { type RelationshipErrors, type UndirectedRelationshipRow, relationshipActions, undirectedStatus, directedStatus, - type RelationshipRequest, + type LegacyRelationshipRequest, } from 'lib/types/relationship-types.js'; import { threadTypes } from 'lib/types/thread-types-enum.js'; import type { NewThreadResponse } from 'lib/types/thread-types.js'; import { updateTypes } from 'lib/types/update-types-enum.js'; import { type UpdateData } from 'lib/types/update-types.js'; import { cartesianProduct } from 'lib/utils/array.js'; import { ServerError } from 'lib/utils/errors.js'; import { promiseAll } from 'lib/utils/promises.js'; import createMessages from '../creators/message-creator.js'; import { createThread } from '../creators/thread-creator.js'; import { createUpdates } from '../creators/update-creator.js'; import { dbQuery, SQL, mergeOrConditions } from '../database/database.js'; import { fetchFriendRequestRelationshipOperations } from '../fetchers/relationship-fetchers.js'; import { fetchUserInfos } from '../fetchers/user-fetchers.js'; import type { Viewer } from '../session/viewer.js'; import { findUserIdentities } from '../utils/identity-utils.js'; async function updateRelationships( viewer: Viewer, - request: RelationshipRequest, + request: LegacyRelationshipRequest, ): Promise { if (!viewer.loggedIn) { throw new ServerError('not_logged_in'); } let requestUserIDs; const viewerID = viewer.userID; if (request.action === relationshipActions.FARCASTER_MUTUAL) { requestUserIDs = Object.keys(request.userIDsToFID).filter( userID => userID !== viewerID, ); } else { requestUserIDs = request.userIDs; } const uniqueUserIDs = [...new Set(requestUserIDs)]; const users = await fetchUserInfos(uniqueUserIDs); let errors: RelationshipErrors = {}; const userIDs: string[] = []; for (const userID of uniqueUserIDs) { if (userID === viewer.userID || !users[userID].username) { const acc = errors.invalid_user || []; errors.invalid_user = [...acc, userID]; } else { userIDs.push(userID); } } if (!userIDs.length) { return Object.freeze({ ...errors }); } const updateIDs = []; if (request.action === relationshipActions.FRIEND) { // We have to create personal threads before setting the relationship // status. By doing that we make sure that failed thread creation is // reported to the caller and can be repeated - there should be only // one GENESIS_PERSONAL thread per a pair of users and we can safely // call it repeatedly. const threadIDPerUser = await createPersonalThreads( viewer, request, userIDs, ); const { userRelationshipOperations, errors: friendRequestErrors } = await fetchFriendRequestRelationshipOperations(viewer, userIDs); errors = { ...errors, ...friendRequestErrors }; const undirectedInsertRows = []; const directedInsertRows = []; const directedDeleteIDs = []; const messageDatas = []; const now = Date.now(); for (const userID in userRelationshipOperations) { const operations = userRelationshipOperations[userID]; const ids = sortUserIDs(viewer.userID, userID); if (operations.length) { updateIDs.push(userID); } for (const operation of operations) { if (operation === 'delete_directed') { directedDeleteIDs.push(userID); } else if (operation === 'friend') { const [user1, user2] = ids; const status = undirectedStatus.FRIEND; undirectedInsertRows.push({ user1, user2, status }); messageDatas.push({ type: messageTypes.LEGACY_UPDATE_RELATIONSHIP, threadID: threadIDPerUser[userID], creatorID: viewer.userID, targetID: userID, time: now, operation: 'request_accepted', }); } else if (operation === 'pending_friend') { const status = directedStatus.PENDING_FRIEND; directedInsertRows.push([viewer.userID, userID, status]); messageDatas.push({ type: messageTypes.LEGACY_UPDATE_RELATIONSHIP, threadID: threadIDPerUser[userID], creatorID: viewer.userID, targetID: userID, time: now, operation: 'request_sent', }); } else if (operation === 'know_of') { const [user1, user2] = ids; const status = undirectedStatus.KNOW_OF; undirectedInsertRows.push({ user1, user2, status }); } else { invariant(false, `unexpected relationship operation ${operation}`); } } } const promises: Array> = [ updateUndirectedRelationships(undirectedInsertRows), ]; if (directedInsertRows.length) { const directedInsertQuery = SQL` INSERT INTO relationships_directed (user1, user2, status) VALUES ${directedInsertRows} ON DUPLICATE KEY UPDATE status = VALUE(status) `; promises.push(dbQuery(directedInsertQuery)); } if (directedDeleteIDs.length) { const directedDeleteQuery = SQL` DELETE FROM relationships_directed WHERE (user1 = ${viewer.userID} AND user2 IN (${directedDeleteIDs})) OR (status = ${directedStatus.PENDING_FRIEND} AND user1 IN (${directedDeleteIDs}) AND user2 = ${viewer.userID}) `; promises.push(dbQuery(directedDeleteQuery)); } if (messageDatas.length > 0) { promises.push(createMessages(viewer, messageDatas, 'broadcast')); } await Promise.all(promises); } else if (request.action === relationshipActions.UNFRIEND) { updateIDs.push(...userIDs); const updateRows = userIDs.map(userID => { const [user1, user2] = sortUserIDs(viewer.userID, userID); return { user1, user2, status: undirectedStatus.KNOW_OF }; }); const deleteQuery = SQL` DELETE FROM relationships_directed WHERE status = ${directedStatus.PENDING_FRIEND} AND (user1 = ${viewer.userID} AND user2 IN (${userIDs}) OR user1 IN (${userIDs}) AND user2 = ${viewer.userID}) `; await Promise.all([ updateUndirectedRelationships(updateRows, false), dbQuery(deleteQuery), ]); } else if (request.action === relationshipActions.BLOCK) { updateIDs.push(...userIDs); const directedRows = []; const undirectedRows = []; for (const userID of userIDs) { directedRows.push([viewer.userID, userID, directedStatus.BLOCKED]); const [user1, user2] = sortUserIDs(viewer.userID, userID); undirectedRows.push({ user1, user2, status: undirectedStatus.KNOW_OF }); } const directedInsertQuery = SQL` INSERT INTO relationships_directed (user1, user2, status) VALUES ${directedRows} ON DUPLICATE KEY UPDATE status = VALUE(status) `; const directedDeleteQuery = SQL` DELETE FROM relationships_directed WHERE status = ${directedStatus.PENDING_FRIEND} AND user1 IN (${userIDs}) AND user2 = ${viewer.userID} `; await Promise.all([ dbQuery(directedInsertQuery), dbQuery(directedDeleteQuery), updateUndirectedRelationships(undirectedRows, false), ]); } else if (request.action === relationshipActions.UNBLOCK) { updateIDs.push(...userIDs); const query = SQL` DELETE FROM relationships_directed WHERE status = ${directedStatus.BLOCKED} AND user1 = ${viewer.userID} AND user2 IN (${userIDs}) `; await dbQuery(query); } else if (request.action === relationshipActions.FARCASTER_MUTUAL) { const { identities: userIdentities } = await findUserIdentities([ ...userIDs, viewerID, ]); const viewerFID = userIdentities[viewerID]?.farcasterID; if (!viewerFID) { throw new ServerError('viewer_fid_missing'); } const userIDsToFIDs = new Map(); for (const userID in userIdentities) { if (userID === viewerID) { continue; } const fid = userIdentities[userID].farcasterID; if (fid) { userIDsToFIDs.set(userID, fid); } } const userIDsWithFID = [...userIDsToFIDs.keys()]; // We have to create personal threads before setting the relationship // status. By doing that we make sure that failed thread creation is // reported to the caller and can be repeated - there should be only // one GENESIS_PERSONAL thread per a pair of users and we can safely // call it repeatedly. const threadIDPerUser = await createPersonalThreads( viewer, request, userIDsWithFID, ); const insertRows = userIDsWithFID.map(otherUserID => { const [user1, user2] = sortUserIDs(viewer.userID, otherUserID); return { user1, user2, status: undirectedStatus.FRIEND }; }); const updateDatas = await updateChangedUndirectedRelationships(insertRows); await createUpdates(updateDatas); const now = Date.now(); const messageDatas = [...userIDsToFIDs.entries()].map( ([otherUserID, otherUserFID]) => ({ type: messageTypes.UPDATE_RELATIONSHIP, threadID: threadIDPerUser[otherUserID], creatorID: viewer.userID, creatorFID: viewerFID, targetID: otherUserID, targetFID: otherUserFID, time: now, operation: 'farcaster_mutual', }), ); await createMessages(viewer, messageDatas, 'broadcast'); } else if (request.action === relationshipActions.ACKNOWLEDGE) { updateIDs.push(...userIDs); const insertRows = userIDs.map(userID => { const [user1, user2] = sortUserIDs(viewer.userID, userID); return { user1, user2, status: undirectedStatus.KNOW_OF }; }); const updateDatas = await updateChangedUndirectedRelationships(insertRows); await createUpdates(updateDatas); } else { invariant( false, `action ${request.action} is invalid or not supported currently`, ); } await createUpdates( updateDatasForUserPairs(cartesianProduct([viewer.userID], updateIDs)), ); return Object.freeze({ ...errors }); } function updateDatasForUserPairs( userPairs: $ReadOnlyArray<[string, string]>, ): UpdateData[] { const time = Date.now(); const updateDatas: Array = []; for (const [user1, user2] of userPairs) { updateDatas.push({ type: updateTypes.UPDATE_USER, userID: user1, time, updatedUserID: user2, }); updateDatas.push({ type: updateTypes.UPDATE_USER, userID: user2, time, updatedUserID: user1, }); } return updateDatas; } async function updateUndirectedRelationships( changeset: UndirectedRelationshipRow[], greatest: boolean = true, ) { if (!changeset.length) { return; } const rows = changeset.map(row => [row.user1, row.user2, row.status]); const query = SQL` INSERT INTO relationships_undirected (user1, user2, status) VALUES ${rows} `; if (greatest) { query.append( SQL`ON DUPLICATE KEY UPDATE status = GREATEST(status, VALUE(status))`, ); } else { query.append(SQL`ON DUPLICATE KEY UPDATE status = VALUE(status)`); } await dbQuery(query); } async function updateChangedUndirectedRelationships( changeset: UndirectedRelationshipRow[], ): Promise { if (changeset.length === 0) { return []; } const user2ByUser1: Map> = new Map(); for (const { user1, user2 } of changeset) { if (!user2ByUser1.has(user1)) { user2ByUser1.set(user1, new Set()); } user2ByUser1.get(user1)?.add(user2); } const selectQuery = SQL` SELECT user1, user2, status FROM relationships_undirected WHERE `; const conditions = []; for (const [user1, users] of user2ByUser1) { conditions.push(SQL`(user1 = ${user1} AND user2 IN (${[...users]}))`); } selectQuery.append(mergeOrConditions(conditions)); const [result] = await dbQuery(selectQuery); const existingStatuses = new Map(); for (const row of result) { existingStatuses.set(`${row.user1}|${row.user2}`, row.status); } const insertRows = []; for (const row of changeset) { const existingStatus = existingStatuses.get(`${row.user1}|${row.user2}`); if (!existingStatus || existingStatus < row.status) { insertRows.push([row.user1, row.user2, row.status]); } } if (insertRows.length === 0) { return []; } const insertQuery = SQL` INSERT INTO relationships_undirected (user1, user2, status) VALUES ${insertRows} ON DUPLICATE KEY UPDATE status = GREATEST(status, VALUE(status)) `; await dbQuery(insertQuery); return updateDatasForUserPairs( insertRows.map(([user1, user2]) => [user1, user2]), ); } async function createPersonalThreads( viewer: Viewer, - request: RelationshipRequest, + request: LegacyRelationshipRequest, userIDs: $ReadOnlyArray, ) { invariant( request.action === relationshipActions.FRIEND || request.action === relationshipActions.FARCASTER_MUTUAL, 'We should only create a GENESIS_PERSONAL threads when sending FRIEND or ' + 'FARCASTER_MUTUAL requests, but we tried to do that for ' + request.action, ); const threadIDPerUser: { [string]: string } = {}; const personalThreadsQuery = SQL` SELECT t.id AS threadID, m2.user AS user2 FROM threads t INNER JOIN memberships m1 ON m1.thread = t.id AND m1.user = ${viewer.userID} INNER JOIN memberships m2 ON m2.thread = t.id AND m2.user IN (${userIDs}) WHERE t.type = ${threadTypes.GENESIS_PERSONAL} AND m1.role > 0 AND m2.role > 0 `; const [personalThreadsResult] = await dbQuery(personalThreadsQuery); for (const row of personalThreadsResult) { const user2 = row.user2.toString(); threadIDPerUser[user2] = row.threadID.toString(); } const threadCreationPromises: { [string]: Promise } = {}; for (const userID of userIDs) { if (threadIDPerUser[userID]) { continue; } threadCreationPromises[userID] = createThread( viewer, { type: threadTypes.GENESIS_PERSONAL, initialMemberIDs: [userID], }, { forceAddMembers: true, updatesForCurrentSession: 'broadcast' }, ); } const personalThreadPerUser = await promiseAll(threadCreationPromises); for (const userID in personalThreadPerUser) { const newThread = personalThreadPerUser[userID]; threadIDPerUser[userID] = newThread.newThreadID; } return threadIDPerUser; } export { updateRelationships, updateDatasForUserPairs, updateUndirectedRelationships, updateChangedUndirectedRelationships, }; diff --git a/lib/actions/relationship-actions.js b/lib/actions/relationship-actions.js index 31c36d4aa..dd5592e92 100644 --- a/lib/actions/relationship-actions.js +++ b/lib/actions/relationship-actions.js @@ -1,37 +1,37 @@ // @flow import type { CallSingleKeyserverEndpoint } from '../keyserver-conn/call-single-keyserver-endpoint.js'; import type { RelationshipErrors, - RelationshipRequest, + LegacyRelationshipRequest, } from '../types/relationship-types.js'; import { ServerError } from '../utils/errors.js'; const updateRelationshipsActionTypes = Object.freeze({ started: 'UPDATE_RELATIONSHIPS_STARTED', success: 'UPDATE_RELATIONSHIPS_SUCCESS', failed: 'UPDATE_RELATIONSHIPS_FAILED', }); const updateRelationships = ( callSingleKeyserverEndpoint: CallSingleKeyserverEndpoint, - ): ((request: RelationshipRequest) => Promise) => + ): ((request: LegacyRelationshipRequest) => Promise) => async request => { const errors = await callSingleKeyserverEndpoint( 'update_relationships', request, ); const { invalid_user, already_friends, user_blocked } = errors; if (invalid_user) { throw new ServerError('invalid_user', errors); } else if (already_friends) { throw new ServerError('already_friends', errors); } else if (user_blocked) { throw new ServerError('user_blocked', errors); } return errors; }; export { updateRelationshipsActionTypes, updateRelationships }; diff --git a/lib/types/endpoints.js b/lib/types/endpoints.js index 5ec5f7a00..b19158e1c 100644 --- a/lib/types/endpoints.js +++ b/lib/types/endpoints.js @@ -1,143 +1,143 @@ // @flow export type APIRequest = { endpoint: Endpoint, input?: Object, }; export type SocketAPIHandler = (request: APIRequest) => Promise; export type Endpoint = | HTTPOnlyEndpoint | SocketOnlyEndpoint | HTTPPreferredEndpoint | SocketPreferredEndpoint; // Endpoints that can cause session changes should occur over HTTP, since the // socket code does not currently support changing sessions. In the future they // could be made to work for native, but cookie changes on web require HTTP // since websockets aren't able to Set-Cookie. Note that technically any // endpoint can cause a sessionChange, and in that case the server will close // the socket with a specific error code, and the client will proceed via HTTP. const sessionChangingEndpoints = Object.freeze({ LOG_OUT: 'log_out', DELETE_ACCOUNT: 'delete_account', CREATE_ACCOUNT: 'create_account', LOG_IN: 'log_in', UPDATE_PASSWORD: 'update_password', POLICY_ACKNOWLEDGMENT: 'policy_acknowledgment', KEYSERVER_AUTH: 'keyserver_auth', }); type SessionChangingEndpoint = $Values; // We do uploads over HTTP as well. This is because Websockets use TCP, which // guarantees ordering. That means that if we start an upload, any messages we // try to send the server after the upload starts will have to wait until the // upload ends. To avoid blocking other messages we upload using HTTP // multipart/form-data. const uploadEndpoints = Object.freeze({ UPLOAD_MULTIMEDIA: 'upload_multimedia', }); type UploadEndpoint = $Values; const largeDataFetchEndpoints = Object.freeze({ GET_INITIAL_REDUX_STATE: 'get_initial_redux_state', FETCH_PENDING_UPDATES: 'fetch_pending_updates', }); type LargeDataFetchEndpoint = $Values; type HTTPOnlyEndpoint = | SessionChangingEndpoint | UploadEndpoint | LargeDataFetchEndpoint; const socketOnlyEndpoints = Object.freeze({ UPDATE_ACTIVITY: 'update_activity', UPDATE_CALENDAR_QUERY: 'update_calendar_query', }); type SocketOnlyEndpoint = $Values; const socketPreferredEndpoints = Object.freeze({}); type SocketPreferredEndpoint = $Values; const httpPreferredEndpoints = Object.freeze({ CREATE_REPORT: 'create_report', CREATE_REPORTS: 'create_reports', CREATE_ENTRY: 'create_entry', CREATE_ERROR_REPORT: 'create_error_report', CREATE_MESSAGE_REPORT: 'create_message_report', CREATE_MULTIMEDIA_MESSAGE: 'create_multimedia_message', CREATE_OR_UPDATE_PUBLIC_LINK: 'create_or_update_public_link', CREATE_REACTION_MESSAGE: 'create_reaction_message', EDIT_MESSAGE: 'edit_message', CREATE_TEXT_MESSAGE: 'create_text_message', CREATE_THREAD: 'create_thread', DELETE_ENTRY: 'delete_entry', DELETE_COMMUNITY_ROLE: 'delete_community_role', DELETE_THREAD: 'delete_thread', DELETE_UPLOAD: 'delete_upload', DISABLE_INVITE_LINK: 'disable_invite_link', EXACT_SEARCH_USER: 'exact_search_user', FETCH_ENTRIES: 'fetch_entries', FETCH_ENTRY_REVISIONS: 'fetch_entry_revisions', FETCH_ERROR_REPORT_INFOS: 'fetch_error_report_infos', FETCH_MESSAGES: 'fetch_messages', FETCH_PINNED_MESSAGES: 'fetch_pinned_messages', FETCH_PRIMARY_INVITE_LINKS: 'fetch_primary_invite_links', FETCH_THREAD_MEDIA: 'fetch_thread_media', JOIN_THREAD: 'join_thread', LEAVE_THREAD: 'leave_thread', MODIFY_COMMUNITY_ROLE: 'modify_community_role', REMOVE_MEMBERS: 'remove_members', RESTORE_ENTRY: 'restore_entry', SEARCH_USERS: 'search_users', SEND_PASSWORD_RESET_EMAIL: 'send_password_reset_email', SEND_VERIFICATION_EMAIL: 'send_verification_email', SET_THREAD_UNREAD_STATUS: 'set_thread_unread_status', TOGGLE_MESSAGE_PIN: 'toggle_message_pin', UPDATE_ACCOUNT: 'update_account', UPDATE_USER_SETTINGS: 'update_user_settings', UPDATE_DEVICE_TOKEN: 'update_device_token', UPDATE_ENTRY: 'update_entry', - UPDATE_RELATIONSHIPS: 'update_relationships', + LEGACY_UPDATE_RELATIONSHIPS: 'update_relationships', UPDATE_ROLE: 'update_role', UPDATE_THREAD: 'update_thread', UPDATE_USER_SUBSCRIPTION: 'update_user_subscription', VERIFY_CODE: 'verify_code', VERIFY_INVITE_LINK: 'verify_invite_link', SIWE_NONCE: 'siwe_nonce', SIWE_AUTH: 'siwe_auth', CLAIM_USERNAME: 'claim_username', UPDATE_USER_AVATAR: 'update_user_avatar', UPLOAD_MEDIA_METADATA: 'upload_media_metadata', SEARCH_MESSAGES: 'search_messages', GET_OLM_SESSION_INITIALIZATION_DATA: 'get_olm_session_initialization_data', VERSION: 'version', FETCH_COMMUNITY_INFOS: 'fetch_community_infos', FETCH_ALL_COMMUNITY_INFOS_WITH_NAMES: 'fetch_all_community_infos_with_names', CREATE_OR_UPDATE_FARCASTER_CHANNEL_TAG: 'create_or_update_farcaster_channel_tag', DELETE_FARCASTER_CHANNEL_TAG: 'delete_farcaster_channel_tag', }); type HTTPPreferredEndpoint = $Values; const socketPreferredEndpointSet = new Set([ ...Object.values(socketOnlyEndpoints), ...Object.values(socketPreferredEndpoints), ]); export function endpointIsSocketPreferred(endpoint: Endpoint): boolean { return socketPreferredEndpointSet.has(endpoint); } const socketSafeEndpointSet = new Set([ ...Object.values(socketOnlyEndpoints), ...Object.values(socketPreferredEndpoints), ...Object.values(httpPreferredEndpoints), ]); export function endpointIsSocketSafe(endpoint: Endpoint): boolean { return socketSafeEndpointSet.has(endpoint); } const socketOnlyEndpointSet = new Set(Object.values(socketOnlyEndpoints)); export function endpointIsSocketOnly(endpoint: Endpoint): boolean { return socketOnlyEndpointSet.has(endpoint); } diff --git a/lib/types/relationship-types.js b/lib/types/relationship-types.js index 9308fe8ad..d63d75e9b 100644 --- a/lib/types/relationship-types.js +++ b/lib/types/relationship-types.js @@ -1,120 +1,120 @@ // @flow import type { TInterface, TRefinement } from 'tcomb'; import t from 'tcomb'; import type { AccountUserInfo } from './user-types.js'; import { values } from '../utils/objects.js'; import { tUserID, tNumEnum, tShape, tString, } from '../utils/validation-utils.js'; export const undirectedStatus = Object.freeze({ KNOW_OF: 0, FRIEND: 2, }); export type UndirectedStatus = $Values; export const directedStatus = Object.freeze({ PENDING_FRIEND: 1, BLOCKED: 3, }); export type DirectedStatus = $Values; export const userRelationshipStatus = Object.freeze({ REQUEST_SENT: 1, REQUEST_RECEIVED: 2, FRIEND: 3, BLOCKED_BY_VIEWER: 4, BLOCKED_VIEWER: 5, BOTH_BLOCKED: 6, }); export type UserRelationshipStatus = $Values; export const userRelationshipStatusValidator: TRefinement = tNumEnum( values(userRelationshipStatus), ); const traditionalRelationshipActions = Object.freeze({ FRIEND: 'friend', UNFRIEND: 'unfriend', BLOCK: 'block', UNBLOCK: 'unblock', ACKNOWLEDGE: 'acknowledge', }); const farcasterRelationshipActions = Object.freeze({ FARCASTER_MUTUAL: 'farcaster', }); export const relationshipActions = Object.freeze({ ...traditionalRelationshipActions, ...farcasterRelationshipActions, }); export type RelationshipAction = $Values; export const relationshipActionsList: $ReadOnlyArray = values(relationshipActions); export type TraditionalRelationshipAction = $Values< typeof traditionalRelationshipActions, >; export const traditionalRelationshipActionsList: $ReadOnlyArray = values(traditionalRelationshipActions); export const relationshipButtons = Object.freeze({ FRIEND: 'friend', UNFRIEND: 'unfriend', BLOCK: 'block', UNBLOCK: 'unblock', ACCEPT: 'accept', WITHDRAW: 'withdraw', REJECT: 'reject', }); export type RelationshipButton = $Values; -export type TraditionalRelationshipRequest = { +export type LegacyTraditionalRelationshipRequest = { +action: TraditionalRelationshipAction, +userIDs: $ReadOnlyArray, }; -export type FarcasterRelationshipRequest = { +export type LegacyFarcasterRelationshipRequest = { +action: 'farcaster', +userIDsToFID: { +[userID: string]: string }, }; -export type RelationshipRequest = - | TraditionalRelationshipRequest - | FarcasterRelationshipRequest; +export type LegacyRelationshipRequest = + | LegacyTraditionalRelationshipRequest + | LegacyFarcasterRelationshipRequest; -export const farcasterRelationshipRequestValidator: TInterface = - tShape({ +export const legacyFarcasterRelationshipRequestValidator: TInterface = + tShape({ action: tString('farcaster'), userIDsToFID: t.dict(tUserID, t.String), }); type SharedRelationshipRow = { user1: string, user2: string, }; export type DirectedRelationshipRow = { ...SharedRelationshipRow, status: DirectedStatus, }; export type UndirectedRelationshipRow = { ...SharedRelationshipRow, status: UndirectedStatus, }; export type RelationshipErrors = Partial<{ invalid_user: string[], already_friends: string[], user_blocked: string[], }>; export type UserRelationships = { +friends: $ReadOnlyArray, +blocked: $ReadOnlyArray, }; diff --git a/lib/types/relationship-types.test.js b/lib/types/relationship-types.test.js index f4837c755..e306003b6 100644 --- a/lib/types/relationship-types.test.js +++ b/lib/types/relationship-types.test.js @@ -1,27 +1,27 @@ // @flow -import { farcasterRelationshipRequestValidator } from './relationship-types.js'; +import { legacyFarcasterRelationshipRequestValidator } from './relationship-types.js'; describe('updateFarcasterRelationshipInputValidator', () => { test('SHOULD validate input with 2 userIDsToFID entries', () => { const input = { action: 'farcaster', userIDsToFID: { '256': 'f256', '512': 'f512', }, }; - expect(farcasterRelationshipRequestValidator.is(input)).toBe(true); + expect(legacyFarcasterRelationshipRequestValidator.is(input)).toBe(true); }); test('Should not validate if action is not farcaster', () => { const input = { action: 'NOT_FARCASTER', userIDsToFID: { '256': 'f256', '512': 'f512', }, }; - expect(farcasterRelationshipRequestValidator.is(input)).toBe(false); + expect(legacyFarcasterRelationshipRequestValidator.is(input)).toBe(false); }); }); diff --git a/native/profile/relationship-list-item.react.js b/native/profile/relationship-list-item.react.js index c2d2851c4..f8c4850f9 100644 --- a/native/profile/relationship-list-item.react.js +++ b/native/profile/relationship-list-item.react.js @@ -1,363 +1,363 @@ // @flow 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 { 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'; import { type TraditionalRelationshipAction, type RelationshipErrors, userRelationshipStatus, relationshipActions, - type RelationshipRequest, + type LegacyRelationshipRequest, } from 'lib/types/relationship-types.js'; import type { AccountUserInfo, GlobalAccountUserInfo, } from 'lib/types/user-types.js'; import { useDispatchActionPromise, type DispatchActionPromise, } from 'lib/utils/redux-promise-utils.js'; import type { RelationshipListNavigate } from './relationship-list.react.js'; import UserAvatar from '../avatars/user-avatar.react.js'; import PencilIcon from '../components/pencil-icon.react.js'; import SingleLine from '../components/single-line.react.js'; import { type KeyboardState, KeyboardContext, } from '../keyboard/keyboard-state.js'; import { OverlayContext, type OverlayContextType, } from '../navigation/overlay-context.js'; import type { NavigationRoute } from '../navigation/route-names.js'; import { UserRelationshipTooltipModalRouteName, FriendListRouteName, BlockListRouteName, } from '../navigation/route-names.js'; import { useSelector } from '../redux/redux-utils.js'; import { type Colors, useColors, useStyles } from '../themes/colors.js'; import type { VerticalBounds } from '../types/layout-types.js'; import { useNavigateToUserProfileBottomSheet } from '../user-profile/user-profile-utils.js'; import { unknownErrorAlertDetails } from '../utils/alert-messages.js'; import Alert from '../utils/alert.js'; const unboundStyles = { container: { flex: 1, flexDirection: 'row', paddingHorizontal: 24, paddingVertical: 10, backgroundColor: 'panelForeground', borderColor: 'panelForegroundBorder', }, borderBottom: { borderBottomWidth: 1, }, buttonContainer: { flexDirection: 'row', }, editButtonWithMargin: { marginLeft: 15, }, username: { color: 'panelForegroundSecondaryLabel', flex: 1, fontSize: 16, lineHeight: 20, marginLeft: 8, }, editButton: { paddingLeft: 10, }, blueAction: { color: 'link', fontSize: 16, paddingLeft: 6, }, redAction: { color: 'redText', fontSize: 16, paddingLeft: 6, }, }; type BaseProps = { +userInfo: AccountUserInfo, +lastListItem: boolean, +verticalBounds: ?VerticalBounds, +relationshipListRoute: NavigationRoute<'FriendList' | 'BlockList'>, +navigate: RelationshipListNavigate, +onSelect: (selectedUser: GlobalAccountUserInfo) => void, }; type Props = { ...BaseProps, // Redux state +removeUserLoadingStatus: LoadingStatus, +colors: Colors, +styles: $ReadOnly, // Redux dispatch functions +dispatchActionPromise: DispatchActionPromise, // async functions that hit server APIs +updateRelationships: ( - request: RelationshipRequest, + request: LegacyRelationshipRequest, ) => Promise, // withOverlayContext +overlayContext: ?OverlayContextType, // withKeyboardState +keyboardState: ?KeyboardState, +navigateToUserProfileBottomSheet: (userID: string) => mixed, }; class RelationshipListItem extends React.PureComponent { editButton: ReactRef> = React.createRef(); render(): React.Node { const { lastListItem, removeUserLoadingStatus, userInfo, relationshipListRoute, } = this.props; const relationshipsToEdit = { [FriendListRouteName]: [userRelationshipStatus.FRIEND], [BlockListRouteName]: [ userRelationshipStatus.BOTH_BLOCKED, userRelationshipStatus.BLOCKED_BY_VIEWER, ], }[relationshipListRoute.name]; const canEditFriendRequest = { [FriendListRouteName]: true, [BlockListRouteName]: false, }[relationshipListRoute.name]; const borderBottom = lastListItem ? null : this.props.styles.borderBottom; let editButton = null; if (removeUserLoadingStatus === 'loading') { editButton = ( ); } else if (relationshipsToEdit.includes(userInfo.relationshipStatus)) { editButton = ( ); } else if ( userInfo.relationshipStatus === userRelationshipStatus.REQUEST_RECEIVED && canEditFriendRequest ) { editButton = ( Accept Reject ); } else if ( userInfo.relationshipStatus === userRelationshipStatus.REQUEST_SENT && canEditFriendRequest ) { editButton = ( Cancel request ); } else { editButton = ( Add ); } return ( {this.props.userInfo.username} {editButton} ); } onPressUser = () => { this.props.navigateToUserProfileBottomSheet(this.props.userInfo.id); }; onSelect = () => { const { id, username } = this.props.userInfo; this.props.onSelect({ id, username }); }; visibleEntryIDs(): [string] { const { relationshipListRoute } = this.props; const id = { [FriendListRouteName]: 'unfriend', [BlockListRouteName]: 'unblock', }[relationshipListRoute.name]; return [id]; } onPressEdit = () => { if (this.props.keyboardState?.dismissKeyboardIfShowing()) { return; } const { editButton, props: { verticalBounds }, } = this; const { overlayContext, userInfo } = this.props; invariant( overlayContext, 'RelationshipListItem should have OverlayContext', ); overlayContext.setScrollBlockingModalStatus('open'); if (!editButton.current || !verticalBounds) { return; } const { relationshipStatus, ...restUserInfo } = userInfo; const relativeUserInfo = { ...restUserInfo, isViewer: false, }; editButton.current.measure((x, y, width, height, pageX, pageY) => { const coordinates = { x: pageX, y: pageY, width, height }; this.props.navigate<'UserRelationshipTooltipModal'>({ name: UserRelationshipTooltipModalRouteName, params: { presentedFrom: this.props.relationshipListRoute.key, initialCoordinates: coordinates, verticalBounds, visibleEntryIDs: this.visibleEntryIDs(), relativeUserInfo, tooltipButtonIcon: 'pencil', }, }); }); }; // We need to set onLayout in order to allow .measure() to be on the ref onLayout = () => {}; onPressFriendUser = () => { this.onPressUpdateFriendship(relationshipActions.FRIEND); }; onPressUnfriendUser = () => { this.onPressUpdateFriendship(relationshipActions.UNFRIEND); }; onPressUpdateFriendship(action: TraditionalRelationshipAction) { const { id } = this.props.userInfo; const customKeyName = `${updateRelationshipsActionTypes.started}:${id}`; void this.props.dispatchActionPromise( updateRelationshipsActionTypes, this.updateFriendship(action), { customKeyName }, ); } async updateFriendship( action: TraditionalRelationshipAction, ): Promise { try { return await this.props.updateRelationships({ action, userIDs: [this.props.userInfo.id], }); } catch (e) { Alert.alert( unknownErrorAlertDetails.title, unknownErrorAlertDetails.message, [{ text: 'OK' }], { cancelable: true, }, ); throw e; } } } const ConnectedRelationshipListItem: React.ComponentType = React.memo(function ConnectedRelationshipListItem( props: BaseProps, ) { const removeUserLoadingStatus = useSelector(state => createLoadingStatusSelector( updateRelationshipsActionTypes, `${updateRelationshipsActionTypes.started}:${props.userInfo.id}`, )(state), ); const colors = useColors(); const styles = useStyles(unboundStyles); const dispatchActionPromise = useDispatchActionPromise(); const boundUpdateRelationships = useLegacyAshoatKeyserverCall(updateRelationships); const overlayContext = React.useContext(OverlayContext); const keyboardState = React.useContext(KeyboardContext); const navigateToUserProfileBottomSheet = useNavigateToUserProfileBottomSheet(); return ( ); }); export default ConnectedRelationshipListItem;