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

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

View File

@@ -1,12 +1,13 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional, IsArray } from "class-validator";
import { IsNull } from "typeorm";
import { IsString, IsOptional } from "class-validator";
export class messagesDto
{
@IsString()
@IsOptional()
name: string;
@IsString()
@IsOptional()
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 { RoomType } from 'src/chat/enums/roomType.enum';
export class roomDto
{
@@ -12,7 +11,7 @@ export class roomDto
@Expose()
@IsString()
@IsNotEmpty()
@IsEnum(RoomType)
room_type: RoomType;
@IsIn(["public", "protected", "private", "direct", "user"])
room_type: string;
}

View File

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

View File

@@ -1,14 +1,7 @@
import {
Entity,
Column,
ManyToOne,
ManyToMany,
JoinTable,
PrimaryGeneratedColumn
} from "typeorm";
import { Entity, Column, ManyToOne, ManyToMany, JoinTable, PrimaryGeneratedColumn } from "typeorm";
import { IsIn } from "class-validator";
import { User } from 'src/users/entities/user.entity';
import { messagesDto } from 'src/chat/dto/messages.dto';
import { RoomType } from 'src/chat/enums/roomType.enum';
@Entity('chatroom')
export class Chatroom {
@@ -18,12 +11,9 @@ export class Chatroom {
@Column()
name: string;
@Column({
type: "enum",
enum: RoomType,
default: RoomType.public
})
type: RoomType;
@Column()
@IsIn(["public", "protected", "private", "direct", "user"])
type: string;
@Column()
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 data = await response.json();
const messages = data.messages;
if (messages === null)
return;
messages.forEach(function (item) {
if (item.name === user.username) {
item.name = "me";
@@ -3935,17 +3937,6 @@ var app = (function () {
console.log("data.rooms:", data.rooms);
for (let room of data.rooms)
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;
}
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 messages = data.messages;
if (messages === null)
return;
messages.forEach(function(item) {
if (item.name === user.username) {
item.name = "me";
@@ -108,17 +111,6 @@ export async function get_all_rooms()
console.log("data.rooms:", data.rooms);
for (let room of data.rooms)
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;
}