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 event name #278

Merged
merged 4 commits into from
Feb 21, 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
14 changes: 7 additions & 7 deletions backend/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,8 @@ export class ChatGateway {
);
}

@SubscribeMessage('invite-pong')
async handleInvitePong(
@SubscribeMessage('request-match')
async handleRequestMatch(
@MessageBody() data: { userId: number },
@ConnectedSocket() client: Socket,
) {
Expand All @@ -90,13 +90,13 @@ export class ChatGateway {
if (blocked.some((user) => user.id === data.userId)) return;
this.server
.to(invitedUserWsId)
.emit('invite-pong', { userId: inviteUser.id });
.emit('request-match', { userId: inviteUser.id });
this.chatService.addInvite(inviteUser.id, data.userId);
}
}

@SubscribeMessage('invite-cancel-pong')
handleInviteCancelPong(@ConnectedSocket() client: Socket) {
@SubscribeMessage('cancel-request-match')
handleCancelRequestMatch(@ConnectedSocket() client: Socket) {
const inviteUser = this.chatService.getUser(client);
const invitee = this.chatService.getInvite(inviteUser.id);
if (!invitee) {
Expand All @@ -105,7 +105,7 @@ export class ChatGateway {
}
const inviteeWsId = this.chatService.getWsFromUserId(invitee)?.id;
this.chatService.removeInvite(inviteUser.id);
this.server.to(inviteeWsId).emit('invite-cancel-pong', inviteUser);
this.server.to(inviteeWsId).emit('cancel-request-match', inviteUser);
}

@SubscribeMessage('approve-pong')
Expand Down Expand Up @@ -187,7 +187,7 @@ export class ChatGateway {

@OnEvent('room.leave', { async: true })
async handleLeave(event: RoomLeftEvent) {
this.server.in(event.roomId.toString()).emit('leave', event);
this.server.in(event.roomId.toString()).emit('leave-room', event);
this.chatService.removeUserFromRoom(event.roomId, event.userId);
}

Expand Down
62 changes: 31 additions & 31 deletions backend/test/chat-gateway.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,17 +516,17 @@ describe('ChatGateway and ChatController (e2e)', () => {

describe('Kick scenario', () => {
let ctx5: Promise<void[]>;
it('setup promises to recv leave event with user id', async () => {
it('setup promises to recv leave-room event with user id', async () => {
const expectedEvent = {
userId: kickedUser1.id,
roomId: room.id,
};
const promises = [ws1, ws2, ws3, ws4, ws5, ws6].map(
(ws) =>
new Promise<void>((resolve) => {
ws.on('leave', (data) => {
ws.on('leave-room', (data) => {
expect(data).toEqual(expectedEvent);
ws.off('leave');
ws.off('leave-room');
resolve();
});
}),
Expand All @@ -541,7 +541,7 @@ describe('ChatGateway and ChatController (e2e)', () => {
.expect(204);
});

it('all users (except kickedUser1) should receive leave event with kickedUser1 id', async () => {
it('all users (except kickedUser1) should receive leave-room event with kickedUser1 id', async () => {
await ctx5;
});

Expand Down Expand Up @@ -1436,11 +1436,11 @@ describe('ChatGateway and ChatController (e2e)', () => {
invited = userAndSockets[1];
notInvited = userAndSockets[2];
ctx1 = new Promise<any>((resolve) =>
invited.ws.on('invite-pong', (data) => resolve(data)),
invited.ws.on('request-match', (data) => resolve(data)),
);
notInvited.ws.on('invite-pong', mockCallback);
notInvited.ws.on('request-match', mockCallback);

invite.ws.emit('invite-pong', {
invite.ws.emit('request-match', {
userId: invited.user.id,
});
ctx1.then((data) => {
Expand Down Expand Up @@ -1475,8 +1475,8 @@ describe('ChatGateway and ChatController (e2e)', () => {
invitee.user.accessToken,
)
.expect(200);
invitee.ws.on('invite-pong', mockCallback);
blocked.ws.emit('invite-pong', {
invitee.ws.on('request-match', mockCallback);
blocked.ws.emit('request-match', {
lim396 marked this conversation as resolved.
Show resolved Hide resolved
userId: invitee.user.id,
});
});
Expand Down Expand Up @@ -1507,14 +1507,14 @@ describe('ChatGateway and ChatController (e2e)', () => {
invitee = userAndSockets[0];
inviter = userAndSockets[1];

invitee.ws.on('invite-pong', mockCallback);
inviter.ws.emit('invite-pong', {
invitee.ws.on('request-match', mockCallback);
inviter.ws.emit('request-match', {
userId: invitee.user.id,
});
inviter.ws.emit('invite-cancel-pong', {
inviter.ws.emit('cancel-request-match', {
userId: invitee.user.id,
});
inviter.ws.emit('invite-pong', {
inviter.ws.emit('request-match', {
lim396 marked this conversation as resolved.
Show resolved Hide resolved
userId: invitee.user.id,
});
});
Expand All @@ -1539,7 +1539,7 @@ describe('ChatGateway and ChatController (e2e)', () => {
const notInvited1 = userAndSockets[2];

const promiseToInvite = new Promise<any>((resolve) =>
invitee.ws.on('invite-pong', (data) => resolve(data)),
invitee.ws.on('request-match', (data) => resolve(data)),
);
PromiseToMatchByInviter = new Promise<any>((resolve) =>
inviter.ws.on('match-pong', (data) => resolve(data)),
Expand All @@ -1548,11 +1548,11 @@ describe('ChatGateway and ChatController (e2e)', () => {
invitee.ws.on('match-pong', (data) => resolve(data)),
);

notInvited1.ws.on('invite-pong', mockCallback1);
notInvited1.ws.on('request-match', mockCallback1);
notInvited1.ws.on('approve-pong', mockCallback1);
notInvited1.ws.on('match-pong', mockCallback1);

inviter.ws.emit('invite-pong', {
inviter.ws.emit('request-match', {
userId: invitee.user.id,
});
return promiseToInvite.then((data) => {
lim396 marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -1621,15 +1621,15 @@ describe('ChatGateway and ChatController (e2e)', () => {
emitter.ws.on('match-pong', mockToMatchByEmitter);

const PromiseToInvite = new Promise<any>((resolve) =>
listener.ws.on('invite-pong', (data) => resolve(data)),
listener.ws.on('request-match', (data) => resolve(data)),
);
listener.ws.on('match-pong', mockToMatchByListener);

emitter.ws.emit('invite-pong', {
emitter.ws.emit('request-match', {
lim396 marked this conversation as resolved.
Show resolved Hide resolved
userId: listener.user.id,
});
return PromiseToInvite.then((data) => {
emitter.ws.emit('invite-cancel-pong', {
emitter.ws.emit('cancel-request-match', {
userId: data.userId,
});
setTimeout(() => {
Expand Down Expand Up @@ -1660,13 +1660,13 @@ describe('ChatGateway and ChatController (e2e)', () => {
const invitee = userAndSockets[1];
const notInvited1 = userAndSockets[2];

notInvited1.ws.on('invite-pong', mockCallback1);
notInvited1.ws.on('request-match', mockCallback1);
notInvited1.ws.on('deny-pong', mockCallback1);

const promiseToInvite = new Promise<any>((resolve) =>
invitee.ws.on('invite-pong', (data) => resolve(data)),
invitee.ws.on('request-match', (data) => resolve(data)),
);
inviter.ws.emit('invite-pong', {
inviter.ws.emit('request-match', {
lim396 marked this conversation as resolved.
Show resolved Hide resolved
userId: invitee.user.id,
});
ctxToDeny = new Promise<any>((resolve) =>
Expand Down Expand Up @@ -1717,7 +1717,7 @@ describe('ChatGateway and ChatController (e2e)', () => {
));
});
});
describe('invite-cancel', () => {
describe('request-cancel', () => {
describe('success case', () => {
const mockCallback1 = jest.fn();
let ctxToCancel: Promise<any>;
Expand All @@ -1727,23 +1727,23 @@ describe('ChatGateway and ChatController (e2e)', () => {
const invitee = userAndSockets[1];
const notInvited1 = userAndSockets[2];

notInvited1.ws.on('invite-pong', mockCallback1);
notInvited1.ws.on('invite-cancel-pong', mockCallback1);
notInvited1.ws.on('request-match', mockCallback1);
notInvited1.ws.on('cancel-request-match', mockCallback1);

const promiseToInvite = new Promise<any>((resolve) =>
invitee.ws.on('invite-pong', (data) => resolve(data)),
invitee.ws.on('request-match', (data) => resolve(data)),
);
inviter.ws.emit('invite-pong', {
inviter.ws.emit('request-match', {
userId: invitee.user.id,
});
ctxToCancel = new Promise<any>((resolve) =>
invitee.ws.on('invite-cancel-pong', (data) => resolve(data)),
invitee.ws.on('cancel-request-match', (data) => resolve(data)),
);
return promiseToInvite.then(() => {
inviter.ws.emit('invite-cancel-pong');
inviter.ws.emit('cancel-request-match');
lim396 marked this conversation as resolved.
Show resolved Hide resolved
});
});
it('invitee should receive an invite-cancel message', () =>
it('invitee should receive an request-cancel message', () =>
ctxToCancel.then((data) => {
expect(data).toHaveProperty('id');
expect(data).toHaveProperty('avatarURL');
Expand All @@ -1764,7 +1764,7 @@ describe('ChatGateway and ChatController (e2e)', () => {
errorCtx = new Promise<any>((resolve) =>
canceler.ws.on('error-pong', (data) => resolve(data)),
);
canceler.ws.emit('invite-cancel-pong');
canceler.ws.emit('cancel-request-match');
lim396 marked this conversation as resolved.
Show resolved Hide resolved
});
it('should receive an error when canceling without an existing invite', () =>
errorCtx);
Expand Down
6 changes: 3 additions & 3 deletions frontend/app/lib/client-socket-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ export default function SocketProvider() {
} else if (
event === "delete-room" ||
event === "enter-room" ||
event === "leave" ||
event === "leave-room" ||
event === "mute" ||
event === "unmute" ||
event === "update-role"
) {
/* Nothing to do here */
} else if (event === "invite-pong") {
} else if (event === "request-match") {
showInvitePongToast(data);
} else if (event === "invite-cancel-pong") {
} else if (event === "cancel-request-match") {
showInviteCancelPongToast(data);
} else if (event === "match-pong") {
MatchPong(data);
Expand Down
4 changes: 2 additions & 2 deletions frontend/app/lib/hooks/useKick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ export function useKick(roomId: number, userId: number, meId: number) {
);

useEffect(() => {
socket.on("leave", handleLeaveRoomEvent);
socket.on("leave-room", handleLeaveRoomEvent);
return () => {
socket.off("leave", handleLeaveRoomEvent);
socket.off("leave-room", handleLeaveRoomEvent);
};
}, [handleLeaveRoomEvent]);

Expand Down
4 changes: 2 additions & 2 deletions frontend/app/lib/hooks/useRequestMatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ export const useRequestMatch = (userId: number) => {

const requestMatch = useCallback(async () => {
setSendRequestPending(true);
await socket.emit("invite-pong", { userId: userId });
await socket.emit("request-match", { userId: userId });
setIsRequestingMatch(true);
setSendRequestPending(false);
}, [userId]);
const cancelRequestMatch = useCallback(async () => {
setSendRequestPending(true);
await socket.emit("invite-cancel-pong", { userId: userId });
await socket.emit("cancel-request-match", { userId: userId });
setIsRequestingMatch(false);
setSendRequestPending(false);
}, [userId]);
Expand Down
Loading