Skip to content

Commit

Permalink
fix: PROTECTED 채널에서 비밀번호 검증 함수에 await 추가 #16
Browse files Browse the repository at this point in the history
  • Loading branch information
yubinquitous committed Dec 15, 2023
1 parent 0a1a5e1 commit a2fc975
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 57 deletions.
27 changes: 12 additions & 15 deletions src/channels/channels.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
Controller,
Delete,
Get,
Logger,
Param,
ParseIntPipe,
Patch,
Expand All @@ -20,16 +19,16 @@ import { User } from 'src/users/entities/user.entity';
import { PositiveIntPipe } from '../common/pipes/positiveInt.pipe';
import { ChannelsGateway } from './channels.gateway';
import { ChannelsService } from './channels.service';
import { ChannelInvitationParamDto } from './dto/channel-Invitation.dto';
import { CreateChannelRequestDto } from './dto/creat-channel-request.dto';
import { CreateChannelUserParamDto } from './dto/create-channel-user-param.dto';
import { CreateInvitationParamDto } from './dto/create-invitation-param.dto';
import { CreateInvitationRequestDto } from './dto/create-invitation-request.dto';
import { DeleteChannelInvitationParamDto } from './dto/delete-invitation-param.dto';
import { JoinChannelRequestDto } from './dto/join-channel-request.dto';
import { UpdateChannelPwdParamDto } from './dto/update-channel-pwd-param.dto';
import { UpdateChannelPwdReqeustDto } from './dto/update-channel-pwd-reqeust.dto';
import { UpdateChannelUserRequestDto } from './dto/update-channel-user-request.dto';
import { DeleteChannelInvitationParamDto } from './dto/delete-invitation-param.dto';
import { ChannelInvitationParamDto } from './dto/channel-Invitation.dto';

@Controller('channels')
@ApiTags('channels')
Expand Down Expand Up @@ -59,15 +58,15 @@ export class ChannelsController {

// DM 채널인 경우 userId가 필수
if (channelInfo.channelType === ChannelType.DM && !channelInfo.userId) {
throw new BadRequestException(`userId is required`);
throw new BadRequestException(`DM을 위한 userId를 입력해주세요.`);
}

// DM 채널이 아닌데 channel name이 NULL인 경우 예외 처리
if (
channelInfo.channelType !== ChannelType.DM &&
channelInfo.name === null
) {
throw new BadRequestException(`channel name is required`);
throw new BadRequestException(`채널 이름을 입력해주세요.`);
}

const createChannelResponseDto =
Expand Down Expand Up @@ -148,15 +147,15 @@ export class ChannelsController {

const channelUsersResponseDto =
await this.channelsService.createChannelUser(channelUserParamDto);

if (user.channelSocketId) {
this.channelsGateway.joinChannelRoom(
channelId.toString(),
user.channelSocketId,
);
}
return channelUsersResponseDto ;

return channelUsersResponseDto;
}

@Patch('/exit')
Expand Down Expand Up @@ -312,12 +311,13 @@ export class ChannelsController {
})
async acceptInvitation(
@GetUser() user: User,
@Body('channelInvitationId', ParseIntPipe, PositiveIntPipe) invitationId: number,
@Body('channelInvitationId', ParseIntPipe, PositiveIntPipe)
invitationId: number,
) {
const createChannelUserParamDto: ChannelInvitationParamDto = {
invitedUserId: user.id,
invitedUserId: user.id,
invitationId: invitationId,
}
};
await this.channelsService.acceptInvitation(createChannelUserParamDto);
}

Expand All @@ -335,9 +335,6 @@ export class ChannelsController {
cancelingUserId: user.id,
invitationId: invitationId,
};
await this.channelsService.rejectInvitation(
deleteInvitationParamDto,
);
await this.channelsService.rejectInvitation(deleteInvitationParamDto);
}

}
85 changes: 45 additions & 40 deletions src/channels/channels.service.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,31 @@
import { ChannelsGateway } from './channels.gateway';
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import { BadRequestException, Injectable, Logger } from '@nestjs/common';
import * as bycrypt from 'bcrypt';
import { Redis } from 'ioredis';
import { MUTE_TIME } from 'src/common/constants';
import { ChannelType, ChannelUserType } from 'src/common/enum';
import { GatewayCreateChannelInvitationParamDto } from 'src/game/dto/gateway-create-channelInvitation-param-dto';
import { User } from 'src/users/entities/user.entity';
import { UsersRepository } from 'src/users/users.repository';
import { DBUpdateFailureException } from '../common/exception/custom-exception';
import { ChannelInvitationRepository } from './channel-invitation.repository';
import { ChannelUsersRepository } from './channel-users.repository';
import { ChannelsGateway } from './channels.gateway';
import { ChannelsRepository } from './channels.repository';
import { ChannelInvitationListResponseDto } from './dto/channel-Invitation-list-response.dto';
import { ChannelInvitationParamDto } from './dto/channel-Invitation.dto';
import { ChannelListResponseDto } from './dto/channel-list-response.dto';
import { ChannelListReturnDto } from './dto/channel-list-return.dto';
import { ChannelUserInfoReturnDto } from './dto/channel-user-info-return.dto';
import { ChannelUsersResponseDto } from './dto/channel-users-response.dto';
import { CreateChannelRequestDto } from './dto/creat-channel-request.dto';
import { CreateChannelResponseDto } from './dto/create-channel-response.dto';
import { CreateChannelUserParamDto } from './dto/create-channel-user-param.dto';
import { CreateInvitationParamDto } from './dto/create-invitation-param.dto';
import { DeleteChannelInvitationParamDto } from './dto/delete-invitation-param.dto';
import { DmChannelListResponseDto } from './dto/dmchannel-list-response.dto';
import { DmChannelListReturnDto } from './dto/dmchannel-list-return.dto';
import { UpdateChannelPwdParamDto } from './dto/update-channel-pwd-param.dto';
import { GatewayCreateChannelInvitationParamDto } from 'src/game/dto/gateway-create-channelInvitation-param-dto';
import { ChannelInvitationListResponseDto } from './dto/channel-Invitation-list-response.dto';
import { ChannelInvitationListDto } from './dto/channel-Invitation-list-return.dto';
import { DeleteChannelInvitationParamDto } from './dto/delete-invitation-param.dto';
import { ChannelInvitationParamDto } from './dto/channel-Invitation.dto';
import { ChannelUserInfoReturnDto } from './dto/channel-user-info-return.dto';

@Injectable()
export class ChannelsService {
Expand Down Expand Up @@ -239,17 +238,18 @@ export class ChannelsService {
// channel이 프로텍티드라면 비밀번호가 맞는지 확인
if (channel.channelType === ChannelType.PROTECTED) {
if (!password) {
throw new BadRequestException(
'this is protected channel. enter password',
);
throw new BadRequestException('비밀번호를 입력해주세요.');
}

if (!bycrypt.compare(password, channel.password!)) {
throw new BadRequestException(
'user cannot join this protected channel. check password',
);
const isPasswordMatching = await bycrypt.compare(
password,
channel.password!,
);
if (!isPasswordMatching) {
throw new BadRequestException('비밀번호가 일치하지 않습니다.');
}
}

// channel이 프라이빗이라면 초대 받은적이 있는지 확인
else if (channel.channelType === ChannelType.PRIVATE) {
const channelInvitation =
Expand Down Expand Up @@ -308,15 +308,17 @@ export class ChannelsService {
);
}

const channelInvitation = await this.channelInvitationRepository.createChannelInvitation(
createInvitationParamDto,
);
const channelInvitation =
await this.channelInvitationRepository.createChannelInvitation(
createInvitationParamDto,
);

const gatewayInvitationParamDto: GatewayCreateChannelInvitationParamDto = {
invitationId: channelInvitation.id,
invitingUserId: invitingUserId,
invitedUserId: invitedUserId,
};
const gatewayInvitationParamDto: GatewayCreateChannelInvitationParamDto =
{
invitationId: channelInvitation.id,
invitingUserId: invitingUserId,
invitedUserId: invitedUserId,
};
await this.ChannelsGateway.PrivateAlert(gatewayInvitationParamDto);
}

Expand Down Expand Up @@ -587,7 +589,7 @@ export class ChannelsService {
}

async acceptInvitation(
createChannelUserParamDto: ChannelInvitationParamDto
createChannelUserParamDto: ChannelInvitationParamDto,
): Promise<ChannelInvitationListResponseDto> {
const channelInfo = await this.channelsRepository.findOne({
where: {
Expand All @@ -601,17 +603,20 @@ export class ChannelsService {

const channelId = channelInfo.id;
const channelName = channelInfo.name;
const channelUsersRepo = await this.channelUsersRepository.findChannelUserInfoList(
createChannelUserParamDto.invitedUserId,
channelId,
);
const channelUsersRepo =
await this.channelUsersRepository.findChannelUserInfoList(
createChannelUserParamDto.invitedUserId,
channelId,
);

const channelUsers: ChannelUserInfoReturnDto[] = channelUsersRepo.map(user => ({
...user,
isFriend: user.isFriend,
isBlocked: user.isBlocked,
channelUserType: user.channelUserType,
}));
const channelUsers: ChannelUserInfoReturnDto[] = channelUsersRepo.map(
(user) => ({
...user,
isFriend: user.isFriend,
isBlocked: user.isBlocked,
channelUserType: user.channelUserType,
}),
);

return { channelId, channelName, channelUsers };
}
Expand All @@ -629,16 +634,16 @@ export class ChannelsService {
throw new BadRequestException(
`해당하는 invitation id ${deleteInvitationParamDto.invitationId} 가 없습니다`,
);

const deleteChannelInvitationParamDto: DeleteChannelInvitationParamDto = {
invitationId: invitation.id,
cancelingUserId: invitation.invitedUserId,
}


const deleteChannelInvitationParamDto: DeleteChannelInvitationParamDto =
{
invitationId: invitation.id,
cancelingUserId: invitation.invitedUserId,
};

await this.channelInvitationRepository.deleteChannelInvitation(
deleteChannelInvitationParamDto,
);

}

async validateDmChannel(
Expand Down
6 changes: 4 additions & 2 deletions src/channels/entities/channel.entity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { InternalServerErrorException } from '@nestjs/common';
import { BadRequestException } from '@nestjs/common';
import * as bycrypt from 'bcrypt';
import {
IsEnum,
Expand Down Expand Up @@ -51,7 +51,9 @@ export class Channel extends BaseEntity {
this.password = await bycrypt.hash(this.password, 10);
} catch (e) {
console.error(e);
throw new InternalServerErrorException();
throw new BadRequestException(
`비밀번호 암호화에 실패했습니다.`,
);
}
}
}
Expand Down

0 comments on commit a2fc975

Please sign in to comment.