wip direct messages
This commit is contained in:
@@ -18,52 +18,13 @@ export class ChatController {
|
||||
private chatGateway: ChatGateway,
|
||||
) {}
|
||||
|
||||
// don't allow '+' because it's used in direct rooms name
|
||||
private allowed_chars = '-#!?_';
|
||||
private escape_chars(str)
|
||||
{
|
||||
return str.split("").join("\\");
|
||||
}
|
||||
|
||||
/*
|
||||
|
||||
The requested user found.
|
||||
AuthenticateGuard : Is User authenticated : true
|
||||
- in createRoom controller
|
||||
-- in addUserToNewRoom service
|
||||
-- in getRoomByName service
|
||||
-- out getRoomByName service
|
||||
-- out addUserToNewRoom service
|
||||
- out createRoom controller
|
||||
The requested user found.
|
||||
AuthenticateGuard : Is User authenticated : true
|
||||
The requested user found.
|
||||
AuthenticateGuard : Is User authenticated : true
|
||||
- in getMessages controller
|
||||
-- in getMessagesFromCurrentRoom service
|
||||
-- in getUserByName service
|
||||
-- out getUserByName service
|
||||
--- currentUser:
|
||||
User {
|
||||
id: 1,
|
||||
fortyTwoId: '42522',
|
||||
username: 'hulamy',
|
||||
email: 'hulamy@student.42.fr',
|
||||
image_url: 'default.png',
|
||||
phone: null,
|
||||
status: 'Connected',
|
||||
isEnabledTwoFactorAuth: false,
|
||||
isTwoFactorAuthenticated: false,
|
||||
secretTwoFactorAuth: null,
|
||||
currentRoom: null
|
||||
}
|
||||
-- in getRoomByName service
|
||||
-- out getRoomByName service
|
||||
--- currentRoom:
|
||||
null
|
||||
-- out getMessagesFromCurrentRoom service
|
||||
- out getMessages controller
|
||||
|
||||
*/
|
||||
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
@@ -83,6 +44,7 @@ null
|
||||
{
|
||||
console.log("- in getAllRooms controller");
|
||||
const rooms = await this.chatService.getAllOtherRoomsAndUsers(req.user.username)
|
||||
console.log("--- rooms:", rooms);
|
||||
|
||||
res.status(HttpStatus.OK).json({ rooms: rooms });
|
||||
console.log("- out getAllRooms controller");
|
||||
@@ -119,14 +81,14 @@ null
|
||||
let chars = this.escape_chars(this.allowed_chars);
|
||||
let regex_base = `[a-zA-Z0-9\\s${chars}]`;
|
||||
let test_regex = new RegExp(`^${regex_base}+$`);
|
||||
if (test_regex.test(createRoomDto.room_name) === false)
|
||||
if (test_regex.test(createRoomDto.name) === false)
|
||||
{
|
||||
let forbidden_chars = createRoomDto.room_name.replace(new RegExp(regex_base, "g"), "");
|
||||
let forbidden_chars = createRoomDto.name.replace(new RegExp(regex_base, "g"), "");
|
||||
throw new HttpException(`Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||
}
|
||||
|
||||
const response = await this.chatService.addUserToNewRoom(req.user.username, createRoomDto);
|
||||
res.status(HttpStatus.OK).json({ room_name: createRoomDto.room_name, message: response });
|
||||
res.status(HttpStatus.OK).json({ room_name: createRoomDto.name, message: response });
|
||||
console.log("- out createRoom controller");
|
||||
}
|
||||
|
||||
@@ -136,12 +98,23 @@ null
|
||||
async joinRoom(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
|
||||
{
|
||||
console.log("- in joinRoom controller");
|
||||
const response = await this.chatService.addUserToRoom(req.user.username, room.room_name);
|
||||
let response = "";
|
||||
if (room.type === 'direct')
|
||||
throw new HttpException(`cannot join a direct messages room`, HttpStatus.CONFLICT);
|
||||
else if (room.type === 'user')
|
||||
{
|
||||
room.type = 'direct';
|
||||
room.users = [room.name, req.user.username];
|
||||
room.name += ` + ${req.user.username}`;
|
||||
await this.chatService.addUserToNewRoom(req.user.username, room);
|
||||
}
|
||||
else
|
||||
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.room_name);
|
||||
await this.chatService.socketJoinRoom(socket, room.name);
|
||||
|
||||
res.status(HttpStatus.OK).json({ room_name: room.room_name, message: response });
|
||||
res.status(HttpStatus.OK).json({ room: room });
|
||||
console.log("- out joinRoom controller");
|
||||
}
|
||||
|
||||
@@ -151,12 +124,12 @@ null
|
||||
async changeRoom(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
|
||||
{
|
||||
console.log("- in changeRoom controller");
|
||||
const response = await this.chatService.setCurrentRoom(req.user.username, room.room_name);
|
||||
const response = 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.room_name);
|
||||
await this.chatService.socketChangeRoom(socket, room.name);
|
||||
|
||||
res.status(HttpStatus.OK).json({ room_name: room.room_name, message: response });
|
||||
res.status(HttpStatus.OK).json({ room: room });
|
||||
console.log("- out changeRoom controller");
|
||||
}
|
||||
|
||||
|
||||
@@ -78,31 +78,28 @@ export class ChatService {
|
||||
return rooms;
|
||||
}
|
||||
|
||||
//async getAllOtherRoomsAndUsers(username: string): Promise<roomDto[]>
|
||||
async getAllOtherRoomsAndUsers(username: string): Promise<roomDto>
|
||||
async getAllOtherRoomsAndUsers(username: string): Promise<roomDto[]>
|
||||
{
|
||||
console.log("-- in getAllOtherRoomsAndUsers service");
|
||||
const all_rooms = await this.getAllNotMyRooms(username);
|
||||
const all_users = await this.getAllUsersNotMyRooms(username);
|
||||
|
||||
// let row_rooms = all_rooms.map(room => {
|
||||
// return {
|
||||
// room_name: room.name,
|
||||
// room_type: "",
|
||||
// };
|
||||
// });
|
||||
// let users = all_users.map(user => {
|
||||
// return {
|
||||
// room_name: user.username,
|
||||
// room_type: "",
|
||||
// };
|
||||
// });
|
||||
// let rooms = row_rooms.concat(users);
|
||||
|
||||
let rooms: roomDto = {
|
||||
room_name: "room.name",
|
||||
room_type: "",
|
||||
};
|
||||
console.log("--- all_rooms:", all_rooms)
|
||||
console.log("--- all_users:", all_users)
|
||||
let row_rooms = all_rooms.map(room => {
|
||||
return {
|
||||
name: room.name,
|
||||
type: room.type,
|
||||
};
|
||||
});
|
||||
let users = all_users.map(user => {
|
||||
return {
|
||||
name: user.username,
|
||||
type: "user",
|
||||
};
|
||||
});
|
||||
let rooms = row_rooms.concat(users);
|
||||
console.log("--- rooms:", rooms)
|
||||
|
||||
console.log("-- in getAllOtherRoomsAndUsers service");
|
||||
return rooms;
|
||||
@@ -112,11 +109,7 @@ export class ChatService {
|
||||
{
|
||||
console.log("-- in getMessagesFromCurrentRoom service");
|
||||
const user_db = await this.getUserByName(username);
|
||||
console.log("--- currentUser:");
|
||||
console.log(user_db);
|
||||
const currentRoom = await this.getRoomByName(user_db.currentRoom);
|
||||
console.log("--- currentRoom:");
|
||||
console.log(currentRoom);
|
||||
let messages = null;
|
||||
if (currentRoom)
|
||||
messages = currentRoom.messages;
|
||||
@@ -140,7 +133,7 @@ export class ChatService {
|
||||
console.log("-- in getRoomByName service");
|
||||
const room = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.where('chatroom.name = :name', { name: room_name })
|
||||
.where('chatroom.name = :name', { name: room_name.replace("+", "\\+") })
|
||||
.getOne();
|
||||
|
||||
console.log("-- out getRoomByName service");
|
||||
@@ -178,27 +171,28 @@ export class ChatService {
|
||||
/* ADDERS *************************************************
|
||||
*/
|
||||
|
||||
async addUserToNewRoom(username: string, createRoomDto: createRoomDto): Promise<string>
|
||||
async addUserToNewRoom(username: string, createRoomDto: createRoomDto): Promise<void>
|
||||
{
|
||||
console.log("-- in addUserToNewRoom service");
|
||||
const room = await this.getRoomByName(createRoomDto.room_name);
|
||||
const room = await this.getRoomByName(createRoomDto.name);
|
||||
if (room)
|
||||
throw new HttpException(`This room name already exist`, HttpStatus.CONFLICT);
|
||||
|
||||
// create chatroom
|
||||
const newChatroom = new Chatroom();
|
||||
newChatroom.name = createRoomDto.room_name;
|
||||
newChatroom.type = createRoomDto.room_type;
|
||||
newChatroom.name = createRoomDto.name;
|
||||
newChatroom.type = createRoomDto.type;
|
||||
newChatroom.owner = username;
|
||||
newChatroom.users = [username];
|
||||
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${createRoomDto.room_name}` }];
|
||||
if (createRoomDto.type === 'direct')
|
||||
newChatroom.users = createRoomDto.users;
|
||||
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${createRoomDto.name}` }];
|
||||
this.chatroomRepository.save(newChatroom);
|
||||
|
||||
console.log("-- out addUserToNewRoom service");
|
||||
return "successfull room creation";
|
||||
}
|
||||
|
||||
async addUserToRoom(username: string, room_name: string): Promise<string>
|
||||
async addUserToRoom(username: string, room_name: string): Promise<void>
|
||||
{
|
||||
console.log("-- in addUserToRoom service");
|
||||
const room = await this.getRoomByName(room_name);
|
||||
@@ -212,19 +206,20 @@ export class ChatService {
|
||||
await this.setCurrentRoom(username, room_name);
|
||||
|
||||
console.log("-- out addUserToRoom service");
|
||||
return "successfully joining room";
|
||||
}
|
||||
|
||||
async addMessageToRoom(room_name: string, username: string, message: string): Promise<void>
|
||||
{
|
||||
console.log("-- in addMessageToRoom service");
|
||||
const currentRoom = await this.getRoomByName(room_name);
|
||||
console.log("--- room_name:", room_name);
|
||||
const my_room = await this.getRoomByName(room_name);
|
||||
console.log("--- my_room:", my_room);
|
||||
let chat_message = {
|
||||
name: username,
|
||||
message: message,
|
||||
};
|
||||
currentRoom.messages.push(chat_message);
|
||||
this.chatroomRepository.save(currentRoom);
|
||||
my_room.messages.push(chat_message);
|
||||
this.chatroomRepository.save(my_room);
|
||||
console.log("-- out addMessageToRoom service");
|
||||
}
|
||||
|
||||
@@ -272,7 +267,15 @@ export class ChatService {
|
||||
console.log("-- in getAllUsersNotMyRooms service");
|
||||
|
||||
const directs = await this.getMyDirects(username);
|
||||
let usernames = directs.map(room => room.users[0]);
|
||||
console.log("--- directs:", directs);
|
||||
|
||||
// get all users from directs
|
||||
let usernames = directs.map(room => {
|
||||
let user = room.users[0];
|
||||
if (user === username)
|
||||
user = room.users[1];
|
||||
return user;
|
||||
});
|
||||
usernames.push(username);
|
||||
console.log("usernames:", usernames);
|
||||
|
||||
|
||||
@@ -5,6 +5,6 @@ export class createRoomDto extends roomDto
|
||||
{
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
room_password: string;
|
||||
password?: string;
|
||||
}
|
||||
|
||||
|
||||
@@ -6,12 +6,17 @@ export class roomDto
|
||||
@Expose()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
room_name: string;
|
||||
name: string;
|
||||
|
||||
@Expose()
|
||||
//@IsString()
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
//@IsIn(["public", "protected", "private", "direct", "user"])
|
||||
room_type: string;
|
||||
@IsIn(["public", "protected", "private", "direct", "user"])
|
||||
type: string;
|
||||
|
||||
@Expose()
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
users?: string[];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user