directs have good names and cannot leave server side

This commit is contained in:
simplonco
2023-01-12 11:25:14 +01:00
parent 8e99a764f7
commit 6be1f32fa8
8 changed files with 60 additions and 43 deletions

View File

@@ -49,7 +49,6 @@ export class ChatController {
console.log("- in getAllRooms controller");
const rooms: roomDto[] = await this.chatService.getAllOtherRoomsAndUsers(req.user.username)
console.log("--- rooms:", rooms);
res.status(HttpStatus.OK).json({ rooms: rooms });
console.log("- out getAllRooms controller");
@@ -96,6 +95,8 @@ export class ChatController {
throw new HttpException(`Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY);
}
room.users = [req.user.username];
room.owner = req.user.username;
await this.chatService.addUserToNewRoom(req.user.username, room);
res.status(HttpStatus.OK).json({ room: room });
@@ -117,13 +118,15 @@ export class ChatController {
room.type = 'direct';
room.users = [room.name, req.user.username];
room.name += ` + ${req.user.username}`;
room.owner = req.user.username;
await this.chatService.addUserToNewRoom(req.user.username, room);
}
else
await this.chatService.addUserToRoom(req.user.username, room.name);
room = await this.chatService.addUserToRoom(req.user.username, room.name);
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
await this.chatService.socketJoinRoom(socket, room.name);
res.status(HttpStatus.OK).json({ room: room });
console.log("- out joinRoom controller");
@@ -146,10 +149,15 @@ export class ChatController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('leave')
async leaveRoom(@Body() body)
@Delete('leave')
async leaveRoom(@Req() req, @Res() res): Promise<void>
{
console.log("- in leaveRoom controller");
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
let response = await this.chatService.removeUserFromRoom(req.user.username, room_name);
res.status(HttpStatus.OK).json({ message: response });
console.log("- out leaveRoom controller");
}
@@ -181,19 +189,5 @@ export class ChatController {
console.log("- out getRoomUsers controller");
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Delete('removeuser')
async removeUser(@Req() req, @Res() res): Promise<void>
{
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);
res.status(HttpStatus.OK).json({ message: response });
console.log("- out removeUser controller");
}
}

View File

@@ -82,6 +82,7 @@ export class ChatService {
const rooms = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.type != :type', { type: 'private' })
.andWhere('chatroom.type != :type', { type: 'direct' })
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
.getMany();
console.log("--- rooms:", rooms);
@@ -200,17 +201,15 @@ export class ChatService {
const newChatroom = new Chatroom();
newChatroom.name = room.name;
newChatroom.type = room.type;
newChatroom.owner = username;
newChatroom.users = [username];
if (room.type === 'direct')
newChatroom.users = room.users;
newChatroom.owner = room.owner;
newChatroom.users = room.users;
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${room.name}` }];
await this.chatroomRepository.save(newChatroom);
console.log("-- out addUserToNewRoom service");
}
async addUserToRoom(username: string, room_name: string): Promise<void>
async addUserToRoom(username: string, room_name: string): Promise<roomDto>
{
console.log("-- in addUserToRoom service");
@@ -223,6 +222,7 @@ export class ChatService {
this.chatroomRepository.save(room);
console.log("-- out addUserToRoom service");
return room;
}
async addMessageToRoom(room_name: string, username: string, message: string): Promise<void>
@@ -251,6 +251,8 @@ export class ChatService {
const room = await this.getRoomByName(room_name);
if (!room.users.includes(username))
throw new HttpException(`your are not in this room`, HttpStatus.CONFLICT);
if (room.type === "direct")
throw new HttpException(`you cannot leave a direct messages conversation`, HttpStatus.CONFLICT);
// delete user from room
room.users.push(username);

View File

@@ -11,6 +11,14 @@ export class roomDto
@IsIn(["public", "protected", "private", "direct", "user"])
type: string;
@IsString()
@IsOptional()
owner?: string;
@IsString()
@IsOptional()
client_name?: string;
@IsArray()
@IsString({ each: true })
@IsOptional()