A free and open source instant messaging and VoIP chat app built for friends, groups, and communities.
fix(api): befriend on send-back to an ignored request (#3836)
Hampus42d2fa9ef927
diff --git a/fluxer_api/src/api/user/services/UserRelationshipService.ts b/fluxer_api/src/api/user/services/UserRelationshipService.tsindex c177fe742..c9c1056e5 100644--- a/fluxer_api/src/api/user/services/UserRelationshipService.ts+++ b/fluxer_api/src/api/user/services/UserRelationshipService.ts@@ -165,13 +165,22 @@ export class UserRelationshipService {if (this.isDeletedUser(targetUserForAttempt)) {throw new FriendRequestBlockedError();}- const pendingIncoming = await this.userRepository.getRelationship(+ const pendingRequestFromTarget = await this.userRepository.getRelationship(targetId,userId,RelationshipTypes.OUTGOING_REQUEST,);- if (pendingIncoming) {- return this.acceptFriendRequest({userId, targetId, userCacheService, requestCache});+ if (pendingRequestFromTarget) {+ if (await this.userRepository.getRelationship(userId, targetId, RelationshipTypes.BLOCKED)) {+ throw new CannotSendFriendRequestToBlockedUserError();+ }+ return this.acceptFriendRequest({+ userId,+ targetId,+ userCacheService,+ requestCache,+ targetRequestConfirmed: true,+ });}const existingFriendship = await this.userRepository.getRelationship(userId, targetId, RelationshipTypes.FRIEND);const existingOutgoingRequest = await this.userRepository.getRelationship(@@ -212,11 +221,13 @@ export class UserRelationshipService {targetId,userCacheService,requestCache,+ targetRequestConfirmed = false,}: {userId: UserID;targetId: UserID;userCacheService: UserCacheService;requestCache: RequestCache;+ targetRequestConfirmed?: boolean;}): Promise<Relationship> {if ((await getInstanceConfigRepository().getInstancePolicyConfig()).friends_disabled) {throw new FriendsDisabledError();@@ -235,11 +246,13 @@ export class UserRelationshipService {targetId,RelationshipTypes.INCOMING_REQUEST,);- if (!incomingRequest) {+ if (!incomingRequest && !targetRequestConfirmed) {throw new UnknownUserError();}await this.validateRelationshipCounts({userId, targetId});- await this.userRepository.deleteRelationship(userId, targetId, RelationshipTypes.INCOMING_REQUEST);+ if (incomingRequest) {+ await this.userRepository.deleteRelationship(userId, targetId, RelationshipTypes.INCOMING_REQUEST);+ }await this.userRepository.deleteRelationship(targetId, userId, RelationshipTypes.OUTGOING_REQUEST);const [userRelationship, targetRelationship] = await this.upsertMutualFriendship(userId, targetId);await this.dispatchRelationshipUpdate({diff --git a/fluxer_api/src/api/user/tests/UserRelationshipStateTransitions.test.ts b/fluxer_api/src/api/user/tests/UserRelationshipStateTransitions.test.tsindex 04395386b..bfed654fd 100644--- a/fluxer_api/src/api/user/tests/UserRelationshipStateTransitions.test.ts+++ b/fluxer_api/src/api/user/tests/UserRelationshipStateTransitions.test.ts@@ -29,14 +29,18 @@ async function markUserScheduledForDeletion(harness: ApiTestHarness, userId: strawait markUserDeleted(harness, userId);}-async function markUserDeleted(harness: ApiTestHarness, userId: string): Promise<void> {+async function setFlags(harness: ApiTestHarness, userId: string, flags: bigint): Promise<void> {await createBuilder(harness, '').patch(`/test/users/${userId}/flags`)- .body({flags: UserFlags.DELETED.toString()})+ .body({flags: flags.toString()}).expect(HTTP_STATUS.OK).execute();}+async function markUserDeleted(harness: ApiTestHarness, userId: string): Promise<void> {+ await setFlags(harness, userId, UserFlags.DELETED);+}+describe('UserRelationshipStateTransitions', () => {let harness: ApiTestHarness;beforeAll(async () => {@@ -101,6 +105,46 @@ describe('UserRelationshipStateTransitions', () => {expect(aliceRels).toHaveLength(1);assertRelationshipType(aliceRels[0]!, RelationshipTypes.FRIEND);});+ test('sending back to an ignored sender makes both users friends', async () => {+ const alice = await createTestAccount(harness);+ const bob = await createTestAccount(harness);+ await sendFriendRequest(harness, alice.token, bob.userId);+ await removeRelationship(harness, bob.token, alice.userId);+ const {json: friendship} = await sendFriendRequest(harness, bob.token, alice.userId);+ assertRelationshipType(friendship, RelationshipTypes.FRIEND);+ const {json: aliceRels} = await listRelationships(harness, alice.token);+ expect(aliceRels).toHaveLength(1);+ assertRelationshipType(aliceRels[0]!, RelationshipTypes.FRIEND);+ const {json: bobRels} = await listRelationships(harness, bob.token);+ expect(bobRels).toHaveLength(1);+ assertRelationshipType(bobRels[0]!, RelationshipTypes.FRIEND);+ });+ test('sending back to a sender whose request was never delivered makes both users friends', async () => {+ const alice = await createTestAccount(harness);+ const bob = await createTestAccount(harness);+ await setFlags(harness, alice.userId, UserFlags.SPAMMER);+ await sendFriendRequest(harness, alice.token, bob.userId);+ const {json: bobBefore} = await listRelationships(harness, bob.token);+ expect(bobBefore).toHaveLength(0);+ await setFlags(harness, alice.userId, 0n);+ const {json: friendship} = await sendFriendRequest(harness, bob.token, alice.userId);+ assertRelationshipType(friendship, RelationshipTypes.FRIEND);+ const {json: aliceRels} = await listRelationships(harness, alice.token);+ expect(aliceRels).toHaveLength(1);+ assertRelationshipType(aliceRels[0]!, RelationshipTypes.FRIEND);+ });+ test('sending back to an ignored sender you blocked is rejected', async () => {+ const alice = await createTestAccount(harness);+ const bob = await createTestAccount(harness);+ await sendFriendRequest(harness, alice.token, bob.userId);+ await removeRelationship(harness, bob.token, alice.userId);+ await blockUser(harness, bob.token, alice.userId);+ await createBuilder(harness, bob.token)+ .post(`/users/@me/relationships/${alice.userId}`)+ .body({})+ .expect(HTTP_STATUS.BAD_REQUEST, 'CANNOT_SEND_FRIEND_REQUEST_TO_BLOCKED_USER')+ .execute();+ });test('friend request by tag creates outgoing request', async () => {const alice = await createTestAccount(harness);const bob = await createTestAccount(harness);