diff --git a/keyserver/src/responders/relationship-responders.js b/keyserver/src/responders/relationship-responders.js --- a/keyserver/src/responders/relationship-responders.js +++ b/keyserver/src/responders/relationship-responders.js @@ -6,6 +6,7 @@ type TraditionalRelationshipRequest, type RelationshipErrors, traditionalRelationshipActionsList, + RelationshipRequest, } from 'lib/types/relationship-types.js'; import { tShape } from 'lib/utils/validation-utils.js'; @@ -30,7 +31,7 @@ async function updateRelationshipsResponder( viewer: Viewer, - request: TraditionalRelationshipRequest, + request: RelationshipRequest, ): Promise { return await updateRelationships(viewer, request); } diff --git a/keyserver/src/updaters/relationship-updaters.js b/keyserver/src/updaters/relationship-updaters.js --- a/keyserver/src/updaters/relationship-updaters.js +++ b/keyserver/src/updaters/relationship-updaters.js @@ -5,7 +5,6 @@ import { sortUserIDs } from 'lib/shared/relationship-utils.js'; import { messageTypes } from 'lib/types/message-types-enum.js'; import { - type TraditionalRelationshipRequest, type RelationshipErrors, type UndirectedRelationshipRow, relationshipActions, @@ -31,15 +30,17 @@ async function updateRelationships( viewer: Viewer, - request: TraditionalRelationshipRequest, + request: RelationshipRequest, ): Promise { - const { action } = request; - if (!viewer.loggedIn) { throw new ServerError('not_logged_in'); } - const uniqueUserIDs = [...new Set(request.userIDs)]; + const requestUserIDs = + request.action === relationshipActions.FARCASTER_MUTUAL + ? Object.keys(request.userIDsToFID) + : request.userIDs; + const uniqueUserIDs = [...new Set(requestUserIDs)]; const users = await fetchUserInfos(uniqueUserIDs); let errors: RelationshipErrors = {}; @@ -57,7 +58,7 @@ } const updateIDs = []; - if (action === relationshipActions.FRIEND) { + 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 @@ -143,7 +144,7 @@ } await Promise.all(promises); - } else if (action === relationshipActions.UNFRIEND) { + } else if (request.action === relationshipActions.UNFRIEND) { updateIDs.push(...userIDs); const updateRows = userIDs.map(userID => { @@ -163,7 +164,7 @@ updateUndirectedRelationships(updateRows, false), dbQuery(deleteQuery), ]); - } else if (action === relationshipActions.BLOCK) { + } else if (request.action === relationshipActions.BLOCK) { updateIDs.push(...userIDs); const directedRows = []; @@ -190,7 +191,7 @@ dbQuery(directedDeleteQuery), updateUndirectedRelationships(undirectedRows, false), ]); - } else if (action === relationshipActions.UNBLOCK) { + } else if (request.action === relationshipActions.UNBLOCK) { updateIDs.push(...userIDs); const query = SQL` @@ -199,21 +200,24 @@ user1 = ${viewer.userID} AND user2 IN (${userIDs}) `; await dbQuery(query); - } else if (action === relationshipActions.FARCASTER_MUTUAL) { + } else if (request.action === relationshipActions.FARCASTER_MUTUAL) { // 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 PERSONAL thread per a pair of users and we can safely call it // repeatedly. await createPersonalThreads(viewer, request); - const insertRows = request.userIDs.map(otherUserID => { + const insertRows = Object.keys(request.userIDsToFID).map(otherUserID => { const [user1, user2] = sortUserIDs(viewer.userID, otherUserID); return { user1, user2, status: undirectedStatus.KNOW_OF }; }); const updateDatas = await updateChangedUndirectedRelationships(insertRows); await createUpdates(updateDatas); } else { - invariant(false, `action ${action} is invalid or not supported currently`); + invariant( + false, + `action ${request.action} is invalid or not supported currently`, + ); } await createUpdates(