leave room

This commit is contained in:
hugogogo
2023-01-09 21:26:49 +01:00
parent eea631b18a
commit 5d20e1614c
7 changed files with 114 additions and 61 deletions

View File

@@ -1,4 +1,4 @@
import { Controller, UseGuards, HttpException, HttpStatus, Get, Post, Body, Req, Res } from '@nestjs/common';
import { Controller, UseGuards, HttpException, HttpStatus, Get, Post, Delete, Body, Req, Res } from '@nestjs/common';
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
import { ConnectedSocket } from '@nestjs/websockets';
import { ChatService } from './chat.service';
@@ -108,12 +108,24 @@ export class ChatController {
async getRoomUsers(@Req() req, @Res() res): Promise<object>
{
console.log("- in getRoomUsers controller");
const current_room = await this.chatService.getCurrentRoom(req.user.username);
const room = await this.chatService.getRoomByName(current_room);
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
const room = await this.chatService.getRoomByName(room_name);
const users = room.users;
console.log("- out getRoomUsers controller");
return res.status(HttpStatus.OK).json({ users: users });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Delete('removeuser')
async removeUser(@Req() req, @Res() res): Promise<object>
{
console.log("- in removeUser controller");
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
let response = await this.chatService.removeUserFromRoom(req.user.username, room_name);
console.log("- out removeUser controller");
return res.status(HttpStatus.OK).json({ message: response });
}
}

View File

@@ -61,7 +61,7 @@ export class ChatGateway
console.log('- in handleMessage gateway');
//let room_name = await this.chatService.getCurrentRoom(socket.username);
socket.to(socket.room).emit('message', socket.username, message);
let room_name = await this.chatService.getCurrentRoom(socket.username);
let room_name = await this.chatService.getCurrentRoomName(socket.username);
await this.chatService.addMessageToRoom(room_name, socket.username, message);
console.log('- out handleMessage gateway');

View File

@@ -80,14 +80,14 @@ export class ChatService {
return currentRoom.messages;
}
async getCurrentRoom(username: string)
async getCurrentRoomName(username: string)
{
console.log("-- in getCurrentRoom service");
console.log("-- in getCurrentRoomName service");
console.log('username:', username);
const user_db = await this.getUserByName(username);
//const user_db = await this.usersService.findOne(username);
console.log("-- out getCurrentRoom service");
console.log("-- out getCurrentRoomName service");
return user_db.currentRoom;
}
@@ -169,7 +169,7 @@ export class ChatService {
await this.setCurrentRoom(username, room_name);
console.log("-- out addUserToRoom service");
return "successfull joining room";
return "successfully joining room";
}
async addMessageToRoom(room_name: string, username: string, message: string)
@@ -196,8 +196,20 @@ export class ChatService {
async removeUserFromRoom(username: string, room_name: string)
{
console.log("-- in removeUserFromRoom service");
// get room
// remove user
const room = await this.getRoomByName(room_name);
if (!room.users.includes(username))
throw new HttpException(`your are not in this room`, HttpStatus.CONFLICT);
// delete user from room
room.users.push(username);
room.users = room.users.filter(name => name !== username);
this.chatroomRepository.save(room);
// set current room to nothing
await this.setCurrentRoom(username, "");
console.log("-- out removeUserFromRoom service");
return "successfully leaving room";
}