fixed pbm if messages return null

This commit is contained in:
simplonco
2023-01-11 12:42:38 +01:00
parent 9db3c053ea
commit dcf129b4f1
11 changed files with 112 additions and 60 deletions

View File

@@ -24,6 +24,46 @@ export class ChatController {
return str.split("").join("\\"); 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(AuthenticateGuard)
@UseGuards(TwoFactorGuard) @UseGuards(TwoFactorGuard)
@@ -33,6 +73,7 @@ export class ChatController {
console.log("- in getMyRooms controller"); console.log("- in getMyRooms controller");
const rooms = await this.chatService.getMyRooms(req.user.username); const rooms = await this.chatService.getMyRooms(req.user.username);
res.status(HttpStatus.OK).json({ rooms: rooms }); res.status(HttpStatus.OK).json({ rooms: rooms });
console.log("- out getMyRooms controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -41,9 +82,25 @@ export class ChatController {
async getAllRooms(@Req() req, @Res() res): Promise<void> async getAllRooms(@Req() req, @Res() res): Promise<void>
{ {
console.log("- in getAllRooms controller"); console.log("- in getAllRooms controller");
const rooms = await this.chatService.getAllNotMyRooms(req.user.username); const all_rooms = await this.chatService.getAllNotMyRooms(req.user.username);
const users = await this.chatService.getAllUsersNotMyRooms(req.user.username); const all_users = await this.chatService.getAllUsersNotMyRooms(req.user.username);
res.status(HttpStatus.OK).json({ rooms: rooms, users: users });
let row_rooms = all_rooms.map(room => {
return {
room_name: room.name,
room_type: "direct",
}
});
let users = all_users.map(user => {
return {
room_name: user.username,
room_type: "direct",
}
});
let rooms: roomDto[] = row_rooms.concat(users);
res.status(HttpStatus.OK).json({ rooms: rooms });
console.log("- out getAllRooms controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -54,6 +111,7 @@ export class ChatController {
console.log("- in setCurrentRoom controller"); console.log("- in setCurrentRoom controller");
const response = await this.chatService.setCurrentRoom(req.user.username, setCurrentRoomDto.name); const response = await this.chatService.setCurrentRoom(req.user.username, setCurrentRoomDto.name);
res.status(HttpStatus.OK).json({ message: response }); res.status(HttpStatus.OK).json({ message: response });
console.log("- out setCurrentRoom controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -63,6 +121,7 @@ export class ChatController {
{ {
console.log("- in allowedChars controller"); console.log("- in allowedChars controller");
res.status(HttpStatus.OK).json({ chars: this.allowed_chars }); res.status(HttpStatus.OK).json({ chars: this.allowed_chars });
console.log("- out allowedChars controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -83,6 +142,7 @@ export class ChatController {
const response = await this.chatService.addUserToNewRoom(req.user.username, createRoomDto); 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.room_name, message: response });
console.log("- out createRoom controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -97,6 +157,7 @@ export class ChatController {
await this.chatService.socketJoinRoom(socket, room.room_name); await this.chatService.socketJoinRoom(socket, room.room_name);
res.status(HttpStatus.OK).json({ room_name: room.room_name, message: response }); res.status(HttpStatus.OK).json({ room_name: room.room_name, message: response });
console.log("- out joinRoom controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -111,6 +172,7 @@ export class ChatController {
await this.chatService.socketChangeRoom(socket, room.room_name); await this.chatService.socketChangeRoom(socket, room.room_name);
res.status(HttpStatus.OK).json({ room_name: room.room_name, message: response }); res.status(HttpStatus.OK).json({ room_name: room.room_name, message: response });
console.log("- out changeRoom controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -130,6 +192,7 @@ export class ChatController {
console.log("- in getMessages controller"); console.log("- in getMessages controller");
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username); const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username);
res.status(HttpStatus.OK).json({ messages: messages }); res.status(HttpStatus.OK).json({ messages: messages });
console.log("- out getMessages controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -142,6 +205,7 @@ export class ChatController {
const room = await this.chatService.getRoomByName(room_name); const room = await this.chatService.getRoomByName(room_name);
const users = room.users; const users = room.users;
res.status(HttpStatus.OK).json({ users: users }); res.status(HttpStatus.OK).json({ users: users });
console.log("- out getRoomUsers controller");
} }
@UseGuards(AuthenticateGuard) @UseGuards(AuthenticateGuard)
@@ -153,6 +217,7 @@ export class ChatController {
const room_name = await this.chatService.getCurrentRoomName(req.user.username); const room_name = await this.chatService.getCurrentRoomName(req.user.username);
let response = await this.chatService.removeUserFromRoom(req.user.username, room_name); let response = await this.chatService.removeUserFromRoom(req.user.username, room_name);
res.status(HttpStatus.OK).json({ message: response }); res.status(HttpStatus.OK).json({ message: response });
console.log("- out removeUser controller");
} }
} }

View File

@@ -39,6 +39,7 @@ export class ChatService {
.where('chatroom.users LIKE :user_name', { user_name: `%${username}%` }) .where('chatroom.users LIKE :user_name', { user_name: `%${username}%` })
.getMany(); .getMany();
console.log("-- out getMyRooms service");
return rooms; return rooms;
} }
@@ -48,6 +49,7 @@ export class ChatService {
const my_rooms = await this.getMyRooms(username); const my_rooms = await this.getMyRooms(username);
const directs = my_rooms.filter(room => room.type === 'direct'); const directs = my_rooms.filter(room => room.type === 'direct');
console.log("-- out getAllNotMyRooms service");
return directs; return directs;
} }
@@ -58,6 +60,7 @@ export class ChatService {
.createQueryBuilder('chatroom') .createQueryBuilder('chatroom')
.getMany(); .getMany();
console.log("-- out getAllRooms service");
return rooms; return rooms;
} }
@@ -71,6 +74,7 @@ export class ChatService {
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` }) .andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
.getMany(); .getMany();
console.log("-- out getAllNotMyRooms service");
return rooms; return rooms;
} }
@@ -78,9 +82,17 @@ export class ChatService {
{ {
console.log("-- in getMessagesFromCurrentRoom service"); console.log("-- in getMessagesFromCurrentRoom service");
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
console.log("--- currentUser:");
console.log(user_db);
const currentRoom = await this.getRoomByName(user_db.currentRoom); const currentRoom = await this.getRoomByName(user_db.currentRoom);
console.log("--- currentRoom:");
console.log(currentRoom);
let messages = null;
if (currentRoom)
messages = currentRoom.messages;
return currentRoom.messages; console.log("-- out getMessagesFromCurrentRoom service");
return messages;
} }
async getCurrentRoomName(username: string): Promise<string> async getCurrentRoomName(username: string): Promise<string>
@@ -89,6 +101,7 @@ export class ChatService {
console.log('username:', username); console.log('username:', username);
const user_db = await this.getUserByName(username); const user_db = await this.getUserByName(username);
console.log("-- out getCurrentRoomName service");
return user_db.currentRoom; return user_db.currentRoom;
} }
@@ -100,6 +113,7 @@ export class ChatService {
.where('chatroom.name = :name', { name: room_name }) .where('chatroom.name = :name', { name: room_name })
.getOne(); .getOne();
console.log("-- out getRoomByName service");
return room; return room;
} }
@@ -111,6 +125,7 @@ export class ChatService {
.where('chatroom.id = :id', { id: id }) .where('chatroom.id = :id', { id: id })
.getOne(); .getOne();
console.log("-- out getRoomById service");
return room; return room;
} }
@@ -125,6 +140,7 @@ export class ChatService {
user_db.currentRoom = room_name; user_db.currentRoom = room_name;
this.userRepository.save(user_db); this.userRepository.save(user_db);
console.log("-- out setCurrentRoom service");
return `room "${room_name}" is now current room`; return `room "${room_name}" is now current room`;
} }
@@ -148,6 +164,7 @@ export class ChatService {
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${createRoomDto.room_name}` }]; newChatroom.messages = [{ name: "SERVER", message: `creation of room ${createRoomDto.room_name}` }];
this.chatroomRepository.save(newChatroom); this.chatroomRepository.save(newChatroom);
console.log("-- out addUserToNewRoom service");
return "successfull room creation"; return "successfull room creation";
} }
@@ -164,6 +181,7 @@ export class ChatService {
await this.setCurrentRoom(username, room_name); await this.setCurrentRoom(username, room_name);
console.log("-- out addUserToRoom service");
return "successfully joining room"; return "successfully joining room";
} }
@@ -177,6 +195,7 @@ export class ChatService {
}; };
currentRoom.messages.push(chat_message); currentRoom.messages.push(chat_message);
this.chatroomRepository.save(currentRoom); this.chatroomRepository.save(currentRoom);
console.log("-- out addMessageToRoom service");
} }
@@ -198,6 +217,7 @@ export class ChatService {
// set current room to nothing // set current room to nothing
await this.setCurrentRoom(username, ""); await this.setCurrentRoom(username, "");
console.log("-- out removeUserFromRoom service");
return "successfully leaving room"; return "successfully leaving room";
} }
@@ -213,6 +233,7 @@ export class ChatService {
.where('user.username = :name', { name: username }) .where('user.username = :name', { name: username })
.getOne(); .getOne();
console.log("-- out getUserByName service");
return user; return user;
} }
@@ -232,6 +253,7 @@ export class ChatService {
console.log("users:", users); console.log("users:", users);
console.log("-- out getAllUsersNotMyRooms service");
return users; return users;
} }
@@ -246,6 +268,7 @@ export class ChatService {
socket.to(socket.room).emit('message', socket.username, message); socket.to(socket.room).emit('message', socket.username, message);
let room_name = await this.getCurrentRoomName(socket.username); let room_name = await this.getCurrentRoomName(socket.username);
await this.addMessageToRoom(room_name, socket.username, message); await this.addMessageToRoom(room_name, socket.username, message);
console.log("-- out handleSocketIncommingMessage service");
} }
async socketChangeRoom(socket: socketDto, room_name: string): Promise<void> async socketChangeRoom(socket: socketDto, room_name: string): Promise<void>
@@ -255,6 +278,7 @@ export class ChatService {
socket.leave(socket.room); socket.leave(socket.room);
socket.join(room_name); socket.join(room_name);
socket.room = room_name; socket.room = room_name;
console.log('-- out socketChangeRoom service');
} }
async socketJoinRoom(socket: socketDto, room_name: string): Promise<void> async socketJoinRoom(socket: socketDto, room_name: string): Promise<void>
@@ -267,6 +291,7 @@ export class ChatService {
let message = `${socket.username} has join the room`; let message = `${socket.username} has join the room`;
await socket.to(socket.room).emit('message', "SERVER", message); await socket.to(socket.room).emit('message', "SERVER", message);
await this.addMessageToRoom(room_name, "SERVER", message); await this.addMessageToRoom(room_name, "SERVER", message);
console.log('- out socketJoinRoom service');
} }
} }

View File

@@ -1,5 +1,4 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional, IsEnum } from "class-validator"; import { IsString, IsOptional } from "class-validator";
import { RoomType } from 'src/chat/enums/roomType.enum';
import { roomDto } from './room.dto'; import { roomDto } from './room.dto';
export class createRoomDto extends roomDto export class createRoomDto extends roomDto

View File

@@ -1,12 +1,13 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional, IsArray } from "class-validator"; import { IsString, IsOptional } from "class-validator";
import { IsNull } from "typeorm";
export class messagesDto export class messagesDto
{ {
@IsString() @IsString()
@IsOptional()
name: string; name: string;
@IsString() @IsString()
@IsOptional()
message: string; message: string;
} }

View File

@@ -1,6 +1,5 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional, IsEnum } from "class-validator"; import { IsBoolean, IsEmpty, IsInt, IsIn, IsNotEmpty, IsNumber, IsString, IsOptional, IsEnum } from "class-validator";
import { Expose } from 'class-transformer'; import { Expose } from 'class-transformer';
import { RoomType } from 'src/chat/enums/roomType.enum';
export class roomDto export class roomDto
{ {
@@ -12,7 +11,7 @@ export class roomDto
@Expose() @Expose()
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
@IsEnum(RoomType) @IsIn(["public", "protected", "private", "direct", "user"])
room_type: RoomType; room_type: string;
} }

View File

@@ -1,5 +1,4 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator"; import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
import { IsNull } from "typeorm";
export class setCurrentRoomDto export class setCurrentRoomDto
{ {

View File

@@ -1,14 +1,7 @@
import { import { Entity, Column, ManyToOne, ManyToMany, JoinTable, PrimaryGeneratedColumn } from "typeorm";
Entity, import { IsIn } from "class-validator";
Column,
ManyToOne,
ManyToMany,
JoinTable,
PrimaryGeneratedColumn
} from "typeorm";
import { User } from 'src/users/entities/user.entity'; import { User } from 'src/users/entities/user.entity';
import { messagesDto } from 'src/chat/dto/messages.dto'; import { messagesDto } from 'src/chat/dto/messages.dto';
import { RoomType } from 'src/chat/enums/roomType.enum';
@Entity('chatroom') @Entity('chatroom')
export class Chatroom { export class Chatroom {
@@ -18,12 +11,9 @@ export class Chatroom {
@Column() @Column()
name: string; name: string;
@Column({ @Column()
type: "enum", @IsIn(["public", "protected", "private", "direct", "user"])
enum: RoomType, type: string;
default: RoomType.public
})
type: RoomType;
@Column() @Column()
owner: string; // username owner: string; // username

View File

@@ -1,9 +0,0 @@
export enum RoomType {
public = 'public',
protected = 'protected',
private = 'private',
direct = 'direct',
user = 'user',
}

View File

@@ -3858,6 +3858,8 @@ var app = (function () {
const response = await fetch('/api/v2/chat/messages'); const response = await fetch('/api/v2/chat/messages');
const data = await response.json(); const data = await response.json();
const messages = data.messages; const messages = data.messages;
if (messages === null)
return;
messages.forEach(function (item) { messages.forEach(function (item) {
if (item.name === user.username) { if (item.name === user.username) {
item.name = "me"; item.name = "me";
@@ -3935,17 +3937,6 @@ var app = (function () {
console.log("data.rooms:", data.rooms); console.log("data.rooms:", data.rooms);
for (let room of data.rooms) for (let room of data.rooms)
console.log(room.name); console.log(room.name);
let row_rooms = data.rooms.map(room => {
return {
name: room.name,
};
});
let users = data.users.map(user => {
return {
name: user.username,
};
});
let rooms = row_rooms.concat(users);
return rooms; return rooms;
} }
async function get_room_users() { async function get_room_users() {

File diff suppressed because one or more lines are too long

View File

@@ -7,6 +7,9 @@ export async function get_room_messages()
const data = await response.json(); const data = await response.json();
const messages = data.messages; const messages = data.messages;
if (messages === null)
return;
messages.forEach(function(item) { messages.forEach(function(item) {
if (item.name === user.username) { if (item.name === user.username) {
item.name = "me"; item.name = "me";
@@ -108,17 +111,6 @@ export async function get_all_rooms()
console.log("data.rooms:", data.rooms); console.log("data.rooms:", data.rooms);
for (let room of data.rooms) for (let room of data.rooms)
console.log(room.name); console.log(room.name);
let row_rooms = data.rooms.map(room => {
return {
name: room.name,
}
});
let users = data.users.map(user => {
return {
name: user.username,
}
});
let rooms = row_rooms.concat(users);
return rooms; return rooms;
} }