Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[backend] Prevent sending or receiving friend requests between users who have blocked each other #294

Merged
merged 2 commits into from
Mar 4, 2024
Merged
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
16 changes: 16 additions & 0 deletions backend/src/friend-request/friend-request.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export class FriendRequestService {
await this.expectNotRequesting(recipientId, user, tx);
await this.expectNotFriends(recipientId, user, tx);
await this.expectNotBlockedBy(recipientId, user, tx);
await this.expectNotBlocking(recipientId, user, tx);
return tx.user
.update({
where: { id: user.id },
Expand Down Expand Up @@ -100,6 +101,19 @@ export class FriendRequestService {
}
}

private async expectNotBlocking(blockedId: number, user: User, tx) {
const blocking = await tx.user
.findFirstOrThrow({
where: { id: user.id },
})
.blocking({
where: { id: blockedId },
});
if (blocking.length > 0) {
throw new ConflictException('Blocking user');
}
lim396 marked this conversation as resolved.
Show resolved Hide resolved
}

private async expectNotFriends(friendId: number, user: User, tx) {
const u = tx.user.findFirstOrThrow({
where: {
Expand All @@ -121,6 +135,8 @@ export class FriendRequestService {
return this.prisma.$transaction(async (tx) => {
// Check if user is requested by requester
await this.expectRequestedBy(requesterId, user, tx);
await this.expectNotBlockedBy(requesterId, user, tx);
await this.expectNotBlocking(requesterId, user, tx);

// Remove the friend request and add the requester to friends
return tx.user.update({
Expand Down
34 changes: 34 additions & 0 deletions backend/test/user.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,40 @@ describe('UserController (e2e)', () => {
.expect(409);
});

it('Should not send request to blocking user', async () => {
// user1 blocks user2
await app.blockUser(user1.id, user2.id, user1.accessToken).expect(200);

// user1 sends request to user2
await app
.sendFriendRequest(user1.id, user2.id, user1.accessToken)
.expect(409);
});

it('Should not accept request from blocked user', async () => {
await app
.sendFriendRequest(user1.id, user2.id, user1.accessToken)
.expect(201);

// user1 blocks user2
await app.blockUser(user1.id, user2.id, user1.accessToken).expect(200);
await app
.acceptFriendRequest(user2.id, user1.id, user2.accessToken)
.expect(409);
});

it('Should not accept request from blocking user', async () => {
await app
.sendFriendRequest(user1.id, user2.id, user1.accessToken)
.expect(201);

// user2 blocks user1
await app.blockUser(user2.id, user1.id, user2.accessToken).expect(200);
await app
.acceptFriendRequest(user2.id, user1.id, user2.accessToken)
.expect(409);
});

it('Should not send requests to self', async () => {
await app
.sendFriendRequest(user1.id, user1.id, user1.accessToken)
Expand Down
Loading