Skip to content

Commit

Permalink
Merge(hynsoo/chat): 채팅 초대 기능 구현 #92
Browse files Browse the repository at this point in the history
Feature(hynsoo/chat): 채팅 초대 기능 구현 #92
  • Loading branch information
Kimsoo0119 authored Nov 9, 2022
2 parents 9d28883 + df29c87 commit f830392
Show file tree
Hide file tree
Showing 9 changed files with 357 additions and 43 deletions.
106 changes: 103 additions & 3 deletions main-project/src/chats/chats-controller.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
import { BadRequestException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ChatRoomList } from './interface/chat.interface';
import { async } from 'rxjs';
import { InsertResult } from 'typeorm';
import { ChatLog } from './entity/chat-log.entity';
import {
ChatRoomList,
ChatUserInfo,
PreviousChatLog,
} from './interface/chat.interface';
import { ChatListRepository } from './repository/chat-list.repository';
import { ChatLogRepository } from './repository/chat-log.repository';
import { ChatUsersRepository } from './repository/chat-users.repository';

@Injectable()
export class ChatsControllerService {
constructor(
@InjectRepository(ChatUsersRepository)
private readonly chatUsersRepository: ChatUsersRepository,

@InjectRepository(ChatLogRepository)
private readonly chatLogRepository: ChatLogRepository,

@InjectRepository(ChatListRepository)
private readonly chatListRepository: ChatListRepository,
) {}

async getChatRoomListByUserNo(userNo): Promise<ChatRoomList[]> {
Expand All @@ -19,8 +34,93 @@ export class ChatsControllerService {
}

return chatList;
} catch (err) {
throw err;
} catch (error) {
throw error;
}
}

async getChatLog({
userNo,
chatRoomNo,
currentChatLogNo,
}: PreviousChatLog): Promise<ChatLog[]> {
try {
await this.checkChatRoom({ userNo, chatRoomNo });

const chatLog = await this.chatLogRepository.getPreviousChatLog(
chatRoomNo,
currentChatLogNo,
);

return chatLog;
} catch (error) {
throw error;
}
}

async getRecentChatLog({ userNo, chatRoomNo }: PreviousChatLog) {
try {
await this.checkChatRoom({ userNo, chatRoomNo });

const chatLog = await this.chatLogRepository.getRecentChatLog(chatRoomNo);

return chatLog;
} catch (error) {
throw error;
}
}

async inviteUser(userNo, chatRoomNo): Promise<void> {
try {
const user = await this.checkUserInChatRoom({ userNo, chatRoomNo });
if (user) {
throw new BadRequestException('이미 채팅방에 존재하는 유저입니다.');
}

const insertId: InsertResult =
await this.chatUsersRepository.inviteUserByUserNo({
userNo,
chatRoomNo,
});
if (!insertId) {
throw new BadRequestException('채팅방 초대에 실패했습니다.');
}
} catch (error) {
throw error;
}
}

private async checkChatRoom(chatUserInfo: ChatUserInfo): Promise<void> {
try {
const { chatRoomNo } = chatUserInfo;
const chatRoom = await this.chatListRepository.checkRoomExistByChatNo(
chatRoomNo,
);

if (!chatRoom) {
throw new BadRequestException('존재하지 않는 채팅방입니다.');
}

const user = await this.checkUserInChatRoom(chatUserInfo);
if (!user) {
throw new BadRequestException(`채팅방에 존재하지 않는 사용자 입니다.`);
}
} catch (error) {
throw error;
}
}

private async checkUserInChatRoom(
chatUserInfo: ChatUserInfo,
): Promise<ChatUserInfo> {
try {
const chatUser = await this.chatUsersRepository.checkUserInChatRoom(
chatUserInfo,
);

return chatUser;
} catch (error) {
throw error;
}
}
}
63 changes: 52 additions & 11 deletions main-project/src/chats/chats-gateway.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,19 @@ import {
import { InjectRepository } from '@nestjs/typeorm';
import { MeetingInfoRepository } from 'src/meetings/repository/meeting-info.repository';
import { MeetingRepository } from 'src/meetings/repository/meeting.repository';
import { InsertResult } from 'typeorm';
import { ChatList } from './entity/chat-list.entity';
import {
ChatRoom,
ChatRoomList,
ChatRoomUsers,
ChatUserInfo,
CreateChat,
JoinChatRoom,
MessagePayload,
} from './interface/chat.interface';
import { ChatListRepository } from './repository/chat-list.repository';
import { ChatLogRepository } from './repository/chat-log.repository';
import { ChatUsersRepository } from './repository/chat-users.repository';

@Injectable()
Expand All @@ -26,6 +30,9 @@ export class ChatsGatewayService {
@InjectRepository(ChatUsersRepository)
private readonly chatUsersRepository: ChatUsersRepository,

@InjectRepository(ChatLogRepository)
private readonly chatLogRepository: ChatLogRepository,

@InjectRepository(MeetingRepository)
private readonly meetingRepository: MeetingRepository,

Expand Down Expand Up @@ -61,7 +68,9 @@ export class ChatsGatewayService {
);
}

const roomExist = await this.chatListRepository.checkRoomExist(meetingNo);
const roomExist = await this.chatListRepository.checkRoomExistByMeetingNo(
meetingNo,
);
if (roomExist) {
throw new BadRequestException('이미 생성된 채팅방 입니다.');
}
Expand Down Expand Up @@ -110,6 +119,7 @@ export class ChatsGatewayService {

socket.join(`${user.chatRoomNo}`);

//추후 로그 또는 삭제
socket.broadcast.to(`${user.chatRoomNo}`).emit('join-room', {
username: user.nickname,
msg: `${user.nickname}님이 접속하셨습니다.`,
Expand All @@ -136,12 +146,19 @@ export class ChatsGatewayService {
async sendChat(socket, messagePayload: MessagePayload): Promise<void> {
try {
const { userNo, chatRoomNo, message } = messagePayload;
const chatRoom: ChatList = await this.checkChatRoom(chatRoomNo);
if (!chatRoom) {
throw new BadRequestException('존재하지 않는 채팅방입니다.');
}

const user: ChatRoomUsers =
await this.chatListRepository.isUserInChatRoom(chatRoomNo, userNo);
if (!user) {
throw new BadRequestException('채팅방에 유저의 정보가 없습니다.');
}

await this.saveMessage(messagePayload);

socket.broadcast.to(`${chatRoomNo}`).emit('message', {
message,
userNo,
Expand All @@ -152,25 +169,37 @@ export class ChatsGatewayService {
}
}

private async saveMessage(messagePayload: MessagePayload): Promise<void> {
try {
const insertId = await this.chatLogRepository.saveMessage(messagePayload);
if (!insertId) {
throw new BadRequestException('매세지 저장 오류 입니다.');
}
} catch (err) {
throw err;
}
}

private async getUserByMeetingNo(meetingNo): Promise<ChatRoom> {
try {
const chatRoom: ChatRoom =
await this.meetingInfoRepository.getMeetingUser(meetingNo);
chatRoom.roomName =
chatRoom.guestUserNickname + ',' + chatRoom.hostUserNickname;
chatRoom.userNo = chatRoom.guestUserNo + ',' + chatRoom.hostUserNo;
const meetingInfo: ChatRoom =
await this.meetingInfoRepository.getMeetingUserByMeetingNo(meetingNo);

return chatRoom;
meetingInfo.roomName =
meetingInfo.guestUserNickname + ',' + meetingInfo.hostUserNickname;
meetingInfo.userNo =
meetingInfo.guestUserNo + ',' + meetingInfo.hostUserNo;

return meetingInfo;
} catch (err) {
throw err;
}
}

private async setRoomUsers(roomUsers): Promise<number> {
private async setRoomUsers(roomUsers): Promise<InsertResult> {
try {
const affectedRows: number = await this.chatUsersRepository.setRoomUsers(
roomUsers,
);
const affectedRows: InsertResult =
await this.chatUsersRepository.setRoomUsers(roomUsers);

return affectedRows;
} catch (err) {
Expand All @@ -189,4 +218,16 @@ export class ChatsGatewayService {
throw err;
}
}

private async checkChatRoom(chatRoomNo): Promise<ChatList> {
try {
const chatRoom = await this.chatListRepository.checkRoomExistByChatNo(
chatRoomNo,
);

return chatRoom;
} catch (error) {
throw error;
}
}
}
74 changes: 70 additions & 4 deletions main-project/src/chats/chats.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
import { Controller, Get, Param } from '@nestjs/common';
import {
Body,
Controller,
Delete,
Get,
Param,
ParseIntPipe,
Patch,
} from '@nestjs/common';
import { ApiOperation, ApiTags } from '@nestjs/swagger';
import { ChatsControllerService } from './chats-controller.service';
import { ChatLog } from './entity/chat-log.entity';

@Controller('chats')
@ApiTags('채팅 APi')
export class ChatsController {
constructor(private readonly chatControllerService: ChatsControllerService) {}

@Get('/chatroom/:userNo')
@Get('/:userNo')
@ApiOperation({
summary: '채팅 목록 API',
description: ' 채팅 목록 조회',
Expand All @@ -20,8 +29,65 @@ export class ChatsController {
success: true,
chatRoomList,
};
} catch (err) {
throw err;
} catch (error) {
throw error;
}
}

@Get('/join/:chatRoomNo')
@ApiOperation({
summary: '채팅방 입장시 대화내역 API',
description: '채팅방 입장 시 가장 최신 대화내역 출력',
})
async getRecentChatLog(
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Body('userNo', ParseIntPipe) userNo: number,
): Promise<any> {
try {
const chatLog = await this.chatControllerService.getRecentChatLog({
userNo,
chatRoomNo,
});

return chatLog;
} catch (error) {
throw error;
}
}

@Get('/:chatRoomNo/log')
async getChatLog(
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Body('userNo', ParseIntPipe) userNo: number,
@Body('currentChatLogNo', ParseIntPipe) currentChatLogNo: number,
): Promise<ChatLog[]> {
try {
const chatLog = await this.chatControllerService.getChatLog({
userNo,
chatRoomNo,
currentChatLogNo,
});

return chatLog;
} catch (error) {
throw error;
}
}

@Patch('/:chatRoomNo/invite')
async inviteUser(
@Param('chatRoomNo', ParseIntPipe) chatRoomNo: number,
@Body('userNo', ParseIntPipe) userNo: number,
): Promise<any> {
try {
await this.chatControllerService.inviteUser(userNo, chatRoomNo);

return {
success: true,
msg: '초대 성공',
};
} catch (error) {
throw error;
}
}
}
13 changes: 13 additions & 0 deletions main-project/src/chats/interface/chat.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,16 @@ export interface ChatRoomList {
roomName: string;
chatRoomNo: number;
}

export interface PreviousChatLog {
userNo: number;
currentChatLogNo?: number;
chatRoomNo: number;
message?: string;
timeStamp?: Date;
}

export interface ChatUserInfo {
userNo: number;
chatRoomNo: number;
}
12 changes: 0 additions & 12 deletions main-project/src/chats/path/to/serviceAccountKey.json

This file was deleted.

Loading

0 comments on commit f830392

Please sign in to comment.