Skip to content

Commit

Permalink
Merge pull request #76 from tscenping/jangcho
Browse files Browse the repository at this point in the history
WIP: add channels accept, refuse logic
  • Loading branch information
yubinquitous authored Dec 15, 2023
2 parents 3cdb87a + 2f365d0 commit 0a1a5e1
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 17 deletions.
7 changes: 7 additions & 0 deletions src/channels/channel-invitation.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { ChannelInvitation } from './entities/channel-invitation.entity';
import { CreateInvitationParamDto } from './dto/create-invitation-param.dto';
import { DBUpdateFailureException } from '../common/exception/custom-exception';
import { DeleteChannelInvitationParamDto } from './dto/delete-invitation-param.dto';

export class ChannelInvitationRepository extends Repository<ChannelInvitation> {
constructor(
Expand All @@ -25,4 +26,10 @@ export class ChannelInvitationRepository extends Repository<ChannelInvitation> {

return result;
}

async deleteChannelInvitation(deleteChannelInvitation: DeleteChannelInvitationParamDto) {
const result = await this.softDelete(deleteChannelInvitation.invitationId);
if (result.affected !== 1)
throw DBUpdateFailureException('delete channel invitation failed');
}
}
40 changes: 40 additions & 0 deletions src/channels/channels.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
BadRequestException,
Body,
Controller,
Delete,
Get,
Logger,
Param,
Expand All @@ -27,6 +28,8 @@ 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 @@ -300,4 +303,41 @@ export class ChannelsController {
) {
return await this.channelsService.findDmChannels(user.id, page);
}

// 초대 수락
@Post('/accept')
@ApiOperation({
summary: '초대 수락',
description: '초대 수락',
})
async acceptInvitation(
@GetUser() user: User,
@Body('channelInvitationId', ParseIntPipe, PositiveIntPipe) invitationId: number,
) {
const createChannelUserParamDto: ChannelInvitationParamDto = {
invitedUserId: user.id,
invitationId: invitationId,
}
await this.channelsService.acceptInvitation(createChannelUserParamDto);
}

@Delete('/refuse/:channelInvitationId')
@ApiOperation({
summary: '초대 거절',
description: '초대 거절',
})
async rejectInvitation(
@GetUser() user: User,
@Param('channelinvitationId', ParseIntPipe, PositiveIntPipe)
invitationId: number,
) {
const deleteInvitationParamDto: DeleteChannelInvitationParamDto = {
cancelingUserId: user.id,
invitationId: invitationId,
};
await this.channelsService.rejectInvitation(
deleteInvitationParamDto,
);
}

}
42 changes: 26 additions & 16 deletions src/channels/channels.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BlocksRepository } from './../users/blocks.repository';
import { InjectRedis } from '@liaoliaots/nestjs-redis';
import { Logger, UseFilters, UsePipes, ValidationPipe } from '@nestjs/common';
import {
Expand All @@ -22,6 +23,7 @@ import { UsersRepository } from 'src/users/users.repository';
import { ChannelUsersRepository } from './channel-users.repository';
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 All @@ -34,6 +36,7 @@ export class ChannelsGateway
private readonly usersRepository: UsersRepository,
private readonly channelUsersRepository: ChannelUsersRepository,
private readonly friendsRepository: FriendsRepository,
private readonly BlocksRepository: BlocksRepository,
@InjectRedis() private readonly redis: Redis,
) {}

Expand Down Expand Up @@ -215,30 +218,37 @@ export class ChannelsGateway
}

// 알람 구현을 위한 메소드(한명에게만 알람)
async PrivateAlert(data: { invitedUserId: number; invitationId: number }) {
async PrivateAlert(gatewayInvitationDto : GatewayCreateChannelInvitationParamDto) {
const invitedUser = await this.usersRepository.findOne({
where: { id: data.invitedUserId },
where: { id: gatewayInvitationDto.invitedUserId },
});

const invitingUser = await this.usersRepository.findOne({
where: { id: gatewayInvitationDto.invitingUserId },
});
if (!invitingUser) {
throw WSBadRequestException('유저가 유효하지 않습니다.');
}

const invitationEmitDto = {
invitationId: data.invitationId,
invitationId: gatewayInvitationDto.invitationId,
invitingUserId: invitingUser.nickname
};

if (
!invitedUser ||
!invitedUser || !invitingUser ||
invitedUser.status === UserStatus.OFFLINE ||
invitingUser.status === UserStatus.OFFLINE ||
!invitedUser.channelSocketId
) {
if (
!invitedUser ||
invitedUser.status === UserStatus.OFFLINE ||
!invitedUser.channelSocketId
) {
throw WSBadRequestException('유저가 유효하지 않습니다.');
}
this.server
.to(invitedUser.channelSocketId)
.emit('privateAlert', invitationEmitDto);
throw WSBadRequestException('유저가 유효하지 않습니다.');
}
const isBlocked = await this.BlocksRepository.findOne({
where: { fromUserId: invitedUser.id, toUserId: invitingUser.id },
});
if (isBlocked) return;

this.server
.to(invitedUser.channelSocketId)
.emit('privateAlert', invitationEmitDto);
}
}
}
61 changes: 61 additions & 0 deletions src/channels/channels.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ 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 @@ -309,6 +314,7 @@ export class ChannelsService {

const gatewayInvitationParamDto: GatewayCreateChannelInvitationParamDto = {
invitationId: channelInvitation.id,
invitingUserId: invitingUserId,
invitedUserId: invitedUserId,
};
await this.ChannelsGateway.PrivateAlert(gatewayInvitationParamDto);
Expand Down Expand Up @@ -580,6 +586,61 @@ export class ChannelsService {
return { dmChannels, totalItemCount };
}

async acceptInvitation(
createChannelUserParamDto: ChannelInvitationParamDto
): Promise<ChannelInvitationListResponseDto> {
const channelInfo = await this.channelsRepository.findOne({
where: {
id: createChannelUserParamDto.invitationId,
},
});
if (!channelInfo || !channelInfo.name)
throw new BadRequestException(
`channel ${createChannelUserParamDto.invitationId} does not exist`,
);

const channelId = channelInfo.id;
const channelName = channelInfo.name;
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,
}));

return { channelId, channelName, channelUsers };
}

async rejectInvitation(
deleteInvitationParamDto: DeleteChannelInvitationParamDto,
) {
const invitation = await this.channelInvitationRepository.findOne({
where: {
id: deleteInvitationParamDto.invitationId,
invitedUserId: deleteInvitationParamDto.cancelingUserId,
},
});
if (!invitation)
throw new BadRequestException(
`해당하는 invitation id ${deleteInvitationParamDto.invitationId} 가 없습니다`,
);

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

await this.channelInvitationRepository.deleteChannelInvitation(
deleteChannelInvitationParamDto,
);

}

async validateDmChannel(
userId: number,
targetUserId: number,
Expand Down
7 changes: 7 additions & 0 deletions src/channels/dto/channel-Invitation-list-response.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ChannelInvitationListDto } from "./channel-Invitation-list-return.dto";

export type ChannelInvitationListResponseDto = {
channelId : number;
channelName : string;
channelUsers: ChannelInvitationListDto[];
};
19 changes: 19 additions & 0 deletions src/channels/dto/channel-Invitation-list-return.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { ApiProperty } from '@nestjs/swagger';
import { ChannelUserType } from 'src/common/enum';

export class ChannelInvitationListDto {
@ApiProperty({ description: '채널유저id' })
channelUserId: number;
@ApiProperty({ description: '유저id' })
userId : number;
@ApiProperty({ description: '유저닉네임' })
nickname : string;
@ApiProperty({ description: '유저아바타' })
avatar : string;
@ApiProperty({ description: '친구여부' })
isFriend : boolean;
@ApiProperty({ description: '차단여부' })
isBlocked : boolean;
@ApiProperty({ description: '채널유저타입' })
channelUserType : ChannelUserType;
}
4 changes: 4 additions & 0 deletions src/channels/dto/channel-Invitation.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class ChannelInvitationParamDto {
invitationId: number;
invitedUserId: number;
}
4 changes: 4 additions & 0 deletions src/channels/dto/delete-invitation-param.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class DeleteChannelInvitationParamDto {
cancelingUserId: number;
invitationId: number;
}
4 changes: 4 additions & 0 deletions src/channels/dto/delete-invitations-param.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export class DeleteChannelInvitationsParamDto {
invitingUserId: number;
invitedUserId: number;
}
2 changes: 1 addition & 1 deletion src/game/dto/gateway-create-channelInvitation-param-dto.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export class GatewayCreateChannelInvitationParamDto {
invitationId: number;

invitingUserId: number;
invitedUserId: number;
}

0 comments on commit 0a1a5e1

Please sign in to comment.