wip password
This commit is contained in:
@@ -18,6 +18,21 @@ export class ChatController {
|
||||
private chatGateway: ChatGateway,
|
||||
) {}
|
||||
|
||||
private format_room(room)
|
||||
{
|
||||
let new_room: roomDto = {
|
||||
name: room.name,
|
||||
type: room.type,
|
||||
};
|
||||
if (room.owner)
|
||||
new_room.owner = room.owner;
|
||||
if (room.users)
|
||||
new_room.users = room.users;
|
||||
if (room.protection)
|
||||
new_room.protection = room.protection;
|
||||
return new_room;
|
||||
}
|
||||
|
||||
// don't allow '+' because it's used in direct rooms name
|
||||
private allowed_chars = '-#!?_';
|
||||
private escape_chars(str)
|
||||
@@ -36,7 +51,8 @@ export class ChatController {
|
||||
let fields = ["name", "type", "users"];
|
||||
const rooms = await this.chatService.getMyRooms(req.user.username, fields);
|
||||
|
||||
res.status(HttpStatus.OK).json({ rooms: rooms });
|
||||
const ret_rooms = rooms.map(room => this.format_room(room));
|
||||
res.status(HttpStatus.OK).json({ rooms: ret_rooms });
|
||||
|
||||
console.log("- out getMyRooms controller");
|
||||
}
|
||||
@@ -49,7 +65,9 @@ export class ChatController {
|
||||
console.log("- in getAllRooms controller");
|
||||
|
||||
const rooms: roomDto[] = await this.chatService.getAllOtherRoomsAndUsers(req.user.username)
|
||||
res.status(HttpStatus.OK).json({ rooms: rooms });
|
||||
|
||||
const ret_rooms = rooms.map(room => this.format_room(room));
|
||||
res.status(HttpStatus.OK).json({ rooms: ret_rooms });
|
||||
|
||||
console.log("- out getAllRooms controller");
|
||||
}
|
||||
@@ -86,6 +104,7 @@ export class ChatController {
|
||||
{
|
||||
console.log("- in createRoom controller");
|
||||
|
||||
// check chars in room name
|
||||
let chars = this.escape_chars(this.allowed_chars);
|
||||
let regex_base = `[a-zA-Z0-9\\s${chars}]`;
|
||||
let test_regex = new RegExp(`^${regex_base}+$`);
|
||||
@@ -95,10 +114,16 @@ export class ChatController {
|
||||
throw new HttpException(`Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
if (!room.password || room.password.length === 0)
|
||||
room.protection = false;
|
||||
else
|
||||
room.protection = true;
|
||||
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 });
|
||||
|
||||
const ret_room = this.format_room(room);
|
||||
res.status(HttpStatus.OK).json({ room: ret_room });
|
||||
|
||||
console.log("- out createRoom controller");
|
||||
}
|
||||
@@ -121,7 +146,7 @@ export class ChatController {
|
||||
}
|
||||
else
|
||||
{
|
||||
let fields = ["name", "type", "users", "messages", "owner"];
|
||||
let fields = ["name", "type", "users", "messages", "owner", "protection"];
|
||||
const room_db = await this.chatService.getRoomByName(room.name, fields);
|
||||
if (room_db.type === 'direct')
|
||||
{
|
||||
@@ -144,7 +169,8 @@ export class ChatController {
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
await this.chatService.socketJoinRoom(socket, room.name);
|
||||
|
||||
res.status(HttpStatus.OK).json({ room: room });
|
||||
const ret_room = this.format_room(room);
|
||||
res.status(HttpStatus.OK).json({ room: ret_room });
|
||||
|
||||
console.log("- out joinRoom controller");
|
||||
}
|
||||
@@ -159,7 +185,9 @@ export class ChatController {
|
||||
await this.chatService.setCurrentRoom(req.user.username, room.name);
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
await this.chatService.socketChangeRoom(socket, room.name);
|
||||
res.status(HttpStatus.OK).json({ room: room });
|
||||
|
||||
const ret_room = this.format_room(room);
|
||||
res.status(HttpStatus.OK).json({ room: ret_room });
|
||||
|
||||
console.log("- out changeRoom controller");
|
||||
}
|
||||
@@ -176,7 +204,8 @@ export class ChatController {
|
||||
let message = `${username} joined the room`;
|
||||
await this.chatService.addMessageToRoom(current_room_name, "SERVER", message);
|
||||
|
||||
res.status(HttpStatus.OK).json({ room: room });
|
||||
const ret_room = this.format_room(room);
|
||||
res.status(HttpStatus.OK).json({ room: ret_room });
|
||||
|
||||
console.log("- out inviteUser controller");
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { roomDto } from './dto/room.dto';
|
||||
import { messagesDto } from './dto/messages.dto';
|
||||
import { socketDto } from './dto/socket.dto';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -25,18 +26,6 @@ export class ChatService {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
private format_room(room: Chatroom)
|
||||
{
|
||||
let new_room: roomDto = {
|
||||
name: room.name,
|
||||
type: room.type,
|
||||
owner: room.owner,
|
||||
users: room.users,
|
||||
};
|
||||
|
||||
return new_room;
|
||||
}
|
||||
|
||||
|
||||
/* GETTERS ************************************************
|
||||
*/
|
||||
@@ -225,12 +214,22 @@ export class ChatService {
|
||||
throw new HttpException(`This room name already exist`, HttpStatus.CONFLICT);
|
||||
}
|
||||
|
||||
let hash;
|
||||
if (room.protection)
|
||||
{
|
||||
const saltOrRounds = 10;
|
||||
const password = room.password;
|
||||
hash = await bcrypt.hash(password, saltOrRounds);
|
||||
}
|
||||
|
||||
// create chatroom
|
||||
let newChatroom = new Chatroom();
|
||||
newChatroom.name = room.name;
|
||||
newChatroom.type = room.type;
|
||||
newChatroom.owner = room.owner;
|
||||
newChatroom.users = room.users;
|
||||
if (room.protection)
|
||||
newChatroom.hash = hash;
|
||||
newChatroom.messages = [
|
||||
{ name: "SERVER", message: `creation of room ${room.name}` },
|
||||
{ name: "SERVER", message: `${room.users[0]} joined the room` },
|
||||
|
||||
@@ -16,6 +16,14 @@ export class roomDto
|
||||
@IsIn(["public", "private", "direct", "user"])
|
||||
type: string;
|
||||
|
||||
@IsBoolean()
|
||||
@IsOptional()
|
||||
protection?: boolean = false;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
password?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
owner?: string;
|
||||
|
||||
@@ -21,6 +21,15 @@ export class Chatroom
|
||||
@IsIn(["public", "private", "direct", "user"])
|
||||
type: string;
|
||||
|
||||
@Column({ default: false })
|
||||
@IsBoolean()
|
||||
protection: boolean = false;
|
||||
|
||||
@Column()
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
hash?: string;
|
||||
|
||||
@Column()
|
||||
owner: string; // username
|
||||
|
||||
|
||||
Reference in New Issue
Block a user