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

[Refactor] Rename socket events, functions, variables, etc. around match requests #281

Merged
merged 6 commits into from
Feb 24, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
64 changes: 37 additions & 27 deletions backend/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export class ChatGateway {

@SubscribeMessage('request-match')
async handleRequestMatch(
@MessageBody() data: { userId: number },
@MessageBody() data: { requestedUserId: number },
@ConnectedSocket() client: Socket,
) {
// Check if the requesting user is valid
Expand All @@ -88,27 +88,31 @@ export class ChatGateway {
return;
}
// Check if the requested user is connected
const requestedUserWsId = this.chatService.getWsFromUserId(data.userId)?.id;
const requestedUserWsId = this.chatService.getWsFromUserId(
data.requestedUserId,
)?.id;
if (!requestedUserWsId) {
this.logger.error('invalid requested user');
return;
}
// Check if the requesting user is blocked by the requested user
const blockings = await this.chatService.getUsersBlockedBy(data.userId);
const blockings = await this.chatService.getUsersBlockedBy(
data.requestedUserId,
);
if (blockings.some((user) => user.id === requestingUser.id)) return;
// Check if the requested user is blocked by the requesting user
const blocked = await this.chatService.getUsersBlockedBy(requestingUser.id);
if (blocked.some((user) => user.id === data.userId)) return;
if (blocked.some((user) => user.id === data.requestedUserId)) return;
// Send the request
this.server
.to(requestedUserWsId)
.emit('request-match', { userId: requestingUser.id });
.emit('request-match', { requestingUserId: requestingUser.id });
// Save the request
this.chatService.addMatchRequest(requestingUser.id, data.userId);
this.chatService.addMatchRequest(requestingUser.id, data.requestedUserId);
}

@SubscribeMessage('cancel-request-match')
handleCancelRequestMatch(@ConnectedSocket() client: Socket) {
@SubscribeMessage('cancel-match-request')
handleCancelMatchRequest(@ConnectedSocket() client: Socket) {
// Check if the requesting user is valid
const requestingUser = this.chatService.getUser(client);
if (!requestingUser) {
Expand All @@ -119,7 +123,9 @@ export class ChatGateway {
const requestedUser = this.chatService.getMatchRequest(requestingUser.id);
if (!requestedUser) {
this.logger.error('invalid requested user');
this.server.to(client.id).emit('error-pong', 'No pending invite found.');
this.server
.to(client.id)
.emit('invalid-request', 'No pending invite found.');
return;
}
// Cancel the request
Expand All @@ -133,54 +139,58 @@ export class ChatGateway {
// Send the cancel request
this.server
.to(requestedUserWsId)
.emit('cancel-request-match', requestingUser);
.emit('cancelled-match-request', requestingUser);
}

@SubscribeMessage('approve-pong')
async handleApprovePong(
@MessageBody() data: { userId: number },
@SubscribeMessage('approve-match-request')
async handleApproveMatchRequest(
@MessageBody() data: { approvedUserId: number },
@ConnectedSocket() client: Socket,
) {
const approvedUserWsId = this.chatService.getWsFromUserId(data.userId)?.id;
const approvedUserWsId = this.chatService.getWsFromUserId(
data.approvedUserId,
)?.id;
if (!approvedUserWsId) {
return;
} else {
if (
this.chatService.getMatchRequest(data.userId) !==
this.chatService.getMatchRequest(data.approvedUserId) !==
this.chatService.getUserId(client)
) {
this.server
.to(client.id)
.emit('error-pong', 'No pending invite found.');
.emit('invalid-request', 'No pending invite found.');
return;
}
const emitData = { roomId: v4() };
this.server.to(client.id).emit('match-pong', emitData);
this.server.to(approvedUserWsId).emit('match-pong', emitData);
this.chatService.removeMatchRequest(data.userId);
this.server.to(client.id).emit('approved-match-request', emitData);
this.server.to(approvedUserWsId).emit('approved-match-request', emitData);
this.chatService.removeMatchRequest(data.approvedUserId);
}
}

@SubscribeMessage('deny-pong')
handleDenyPong(
@MessageBody() data: { userId: number },
@SubscribeMessage('deny-match-request')
handleDenyMatchRequest(
@MessageBody() data: { deniedUserId: number },
@ConnectedSocket() client: Socket,
) {
const deniedUserWsId = this.chatService.getWsFromUserId(data.userId)?.id;
const deniedUserWsId = this.chatService.getWsFromUserId(
data.deniedUserId,
)?.id;
if (!deniedUserWsId) {
return;
} else {
if (
this.chatService.getMatchRequest(data.userId) !==
this.chatService.getMatchRequest(data.deniedUserId) !==
this.chatService.getUserId(client)
) {
this.server
.to(client.id)
.emit('error-pong', 'No pending invite found.');
.emit('invalid-request', 'No pending invite found.');
return;
}
this.server.to(deniedUserWsId).emit('deny-pong');
this.chatService.removeMatchRequest(data.userId);
this.server.to(deniedUserWsId).emit('denied-match-request');
this.chatService.removeMatchRequest(data.deniedUserId);
}
}

Expand Down
Loading
Loading