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

WIP: /channels/accept 리턴 타입 수정 및 /channels/notice 구현 진행 중 #52 #78

Merged
merged 1 commit into from
Dec 15, 2023
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
6 changes: 4 additions & 2 deletions src/channels/channels.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,14 +311,16 @@ export class ChannelsController {
})
async acceptInvitation(
@GetUser() user: User,
@Body('channelInvitationId', ParseIntPipe, PositiveIntPipe)
@Body('invitationId', ParseIntPipe, PositiveIntPipe)
invitationId: number,
) {
const createChannelUserParamDto: ChannelInvitationParamDto = {
invitedUserId: user.id,
invitationId: invitationId,
};
await this.channelsService.acceptInvitation(createChannelUserParamDto);
return await this.channelsService.acceptInvitation(
createChannelUserParamDto,
);
}

@Delete('/refuse/:channelInvitationId')
Expand Down
46 changes: 32 additions & 14 deletions src/channels/channels.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { BlocksRepository } from './../users/blocks.repository';
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import { Logger, UseFilters, UsePipes, ValidationPipe } from '@nestjs/common';
import {
Expand All @@ -14,16 +13,17 @@ import {
import Redis from 'ioredis';
import { Server, Socket } from 'socket.io';
import { AuthService } from 'src/auth/auth.service';
import { UserStatus } from 'src/common/enum';
import { EVENT_GAME_INVITATION, EVENT_USER_STATUS } from 'src/common/events';
import { ChannelEventType, UserStatus } from 'src/common/enum';
import { EVENT_USER_STATUS } from 'src/common/events';
import { WSBadRequestException } from 'src/common/exception/custom-exception';
import { WsExceptionFilter } from 'src/common/exception/custom-ws-exception.filter';
import { GatewayCreateChannelInvitationParamDto } from 'src/game/dto/gateway-create-channelInvitation-param-dto';
import { FriendsRepository } from 'src/users/friends.repository';
import { UsersRepository } from 'src/users/users.repository';
import { BlocksRepository } from './../users/blocks.repository';
import { ChannelUsersRepository } from './channel-users.repository';
import { ChannelNoticeResponseDto } from './dto/channel-notice.response.dto';
import { EventMessageOnDto } from './dto/event-message-on.dto';
import { GatewayCreateGameInvitationParamDto } from '../game/dto/gateway-create-invitation-param.dto';
import { GatewayCreateChannelInvitationParamDto } from 'src/game/dto/gateway-create-channelInvitation-param-dto';

@WebSocketGateway({ namespace: 'channels' })
@UseFilters(WsExceptionFilter)
Expand Down Expand Up @@ -140,7 +140,7 @@ export class ChannelsGateway
throw WSBadRequestException('유저 정보가 일치하지 않습니다.'); // TODO: exception 발생해도 서버 죽지 않는지 확인
}

const { channelId, message, notice } = data;
const { channelId, message } = data;

// 채널유저 유효성 검사
const channelUser = await this.channelUsersRepository.findOne({
Expand All @@ -154,8 +154,8 @@ export class ChannelsGateway
nickname: user.nickname,
message,
channelId,
notice,
};

// mute된 유저의 socketId List를 가져온다.
const muteRedisKey = `mute:${channelId}:${user.id}`;
const isMutedChannelUser = await this.redis.exists(muteRedisKey);
Expand Down Expand Up @@ -209,16 +209,23 @@ export class ChannelsGateway
// 채널에 leave한다.
client.leave(channelId.toString());

this.server
.to(channelId.toString())
.emit('message', `${user.nickname}님이 퇴장하셨습니다.`);
// this.server
// .to(channelId.toString())
// .emit('message', `${user.nickname}님이 퇴장하셨습니다.`);
// 채널의 다른 유저들에게 퇴장을 알린다.
this.channelNoticeMessage(channelId, {
nickname: user.nickname,
eventType: ChannelEventType.EXIT,
});

// 어느 채널에 퇴장했는지 알려주기 위해 front에 emit한다.
client.emit('leaveChannelRoom', channelId.toString());
}

// 알람 구현을 위한 메소드(한명에게만 알람)
async PrivateAlert(gatewayInvitationDto : GatewayCreateChannelInvitationParamDto) {
async PrivateAlert(
gatewayInvitationDto: GatewayCreateChannelInvitationParamDto,
) {
const invitedUser = await this.usersRepository.findOne({
where: { id: gatewayInvitationDto.invitedUserId },
});
Expand All @@ -232,10 +239,11 @@ export class ChannelsGateway

const invitationEmitDto = {
invitationId: gatewayInvitationDto.invitationId,
invitingUserId: invitingUser.nickname
invitingUserId: invitingUser.nickname,
};
if (
!invitedUser || !invitingUser ||
!invitedUser ||
!invitingUser ||
invitedUser.status === UserStatus.OFFLINE ||
invitingUser.status === UserStatus.OFFLINE ||
!invitedUser.channelSocketId
Expand All @@ -250,5 +258,15 @@ export class ChannelsGateway
this.server
.to(invitedUser.channelSocketId)
.emit('privateAlert', invitationEmitDto);
}
}

// 채널에 소켓 전송
channelNoticeMessage(
channelId: number,
channelNoticeResponseDto: ChannelNoticeResponseDto,
) {
this.server
.to(channelId.toString())
.emit('notice', channelNoticeResponseDto);
}
}
6 changes: 6 additions & 0 deletions src/channels/dto/channel-notice.response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ChannelEventType } from 'src/common/enum';

export type ChannelNoticeResponseDto = {
nickname: string;
eventType: ChannelEventType;
};
5 changes: 0 additions & 5 deletions src/channels/dto/event-message-on.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,4 @@ export class EventMessageOnDto {
@IsPositive()
@IsNotEmpty()
channelId: number;

@IsString()
@IsNotEmpty()
// 따로 리터럴이 지정되지 않으면 디폴트로 'MESSAGE'가 들어간다.
notice: 'ADMIN' | 'BAN' | 'KICK' | 'MUTE'| 'JOIN' | 'EXIT' | 'MESSAGE' = 'MESSAGE';
}
9 changes: 9 additions & 0 deletions src/common/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,12 @@ export enum ChannelUserType {
ADMIN = 'ADMIN',
MEMBER = 'MEMBER',
}

export enum ChannelEventType {
JOIN = 'JOIN',
BAN = 'BAN',
EXIT = 'EXIT',
KICK = 'KICK',
MUTE = 'MUTE',
ADMIN = 'ADMIN',
}
Loading