room load old message but pbm to read them

This commit is contained in:
simplonco
2023-01-08 14:18:57 +01:00
parent c41c7de745
commit c5ed704a62
7 changed files with 143 additions and 116 deletions

View File

@@ -2,7 +2,7 @@ import { Controller, UseGuards, HttpException, HttpStatus, Get, Post, Body, Req,
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
import { ChatService } from './chat.service';
import { User } from 'src/users/entities/user.entity';
import { joinRoomDto } from './dto/joinRoom.dto';
import { createRoomDto } from './dto/createRoom.dto';
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
@Controller('chat')
@@ -15,28 +15,39 @@ export class ChatController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('rooms')
async getRooms(@Req() req, @Res() res)
async getRooms(@Req() req, @Res() res): Promise<object>
{
//console.log("in getRooms");
return await this.chatService.getRooms(req.user, res);
console.log("- in getRooms controller");
const rooms = await this.chatService.getRooms(req.user);
return res.status(HttpStatus.OK).json({ rooms: rooms });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('current')
async setCurrentRoom(@Body() setCurrentRoomDto: setCurrentRoomDto, @Req() req, @Res() res)
async setCurrentRoom(@Body() setCurrentRoomDto: setCurrentRoomDto, @Req() req, @Res() res): Promise<object>
{
//console.log("in setCurrentRoom");
return await this.chatService.setCurrentRoom(req.user, setCurrentRoomDto.name, res);
console.log("- in setCurrentRoom controller");
const response = await this.chatService.setCurrentRoom(req.user, setCurrentRoomDto.name);
return res.status(HttpStatus.OK).json({ message: response });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('create')
async createRoom(@Body() createRoomDto: createRoomDto, @Req() req, @Res() res): Promise<object>
{
console.log("- in createRoom controller");
const response = await this.chatService.addUserToRoom(req.user, createRoomDto);
return res.status(HttpStatus.OK).json({ room_name: createRoomDto.room_name, message: response });
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('join')
async joinRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res)
async joinRoom(@Body() body)
{
//console.log("in joinRoom");
return await this.chatService.addUserToRoom(req.user, joinRoomDto, res);
console.log("- in joinRoom controller");
}
@UseGuards(AuthenticateGuard)
@@ -44,14 +55,17 @@ export class ChatController {
@Post('leave')
async leaveRoom(@Body() body)
{
console.log("- in leaveRoom controller");
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('messages')
async getMessages(@Req() req, @Res() res)
async getMessages(@Req() req, @Res() res): Promise<object>
{
return this.chatService.getMessagesFromCurrentRoom(req.user, res);
console.log("- in getMessages controller");
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user);
return res.status(HttpStatus.OK).json({ messages: messages });
}
}

View File

@@ -4,7 +4,7 @@ import { UsersService } from 'src/users/users.service';
import { Chatroom } from './entities/chatroom.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { joinRoomDto } from './dto/joinRoom.dto';
import { createRoomDto } from './dto/createRoom.dto';
import { messagesDto } from './dto/messages.dto';
@Injectable()
@@ -24,38 +24,43 @@ export class ChatService {
return new Promise(resolve => setTimeout(resolve, ms));
}
async getRooms(user: User, @Res() res)
async getRooms(user: User): Promise<object>
{
console.log("-- in getRooms service");
const rooms = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where(':user_id IN (chatroom.users)', { user_id: user.fortyTwoId })
.getMany();
return res.status(HttpStatus.OK).json({ rooms: rooms });
return rooms;
}
async findRoomByName(name: string)
{
console.log("-- in findUserByName service");
const room = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.name = :name', { name: name })
.getOne();
console.log("room:", room, ", typeof room:", typeof room);
return room;
}
async findRoomById(id: number, @Res() res)
async findRoomById(id: number)
{
console.log("-- in findRoomById service");
const room = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.id = :id', { id: id })
.getOne();
return res.status(HttpStatus.OK).json({ room: room });
console.log("room:", room, ", typeof room:", typeof room);
return room;
}
async findUserByName(name: string)
async findUserByName(name: string): Promise<object>
{
console.log("-- in findUserByName service");
const user = await this.userRepository
.createQueryBuilder('user')
.where('user.username = :name', { name: name })
@@ -64,46 +69,50 @@ export class ChatService {
return user;
}
async setCurrentRoom(user: User, name: string, @Res() res)
async setCurrentRoom(user: User, name: string): Promise<string>
{
console.log("-- in setCurrentRoom service");
const user_db = await this.usersService.findOneByFourtyTwoId(user.fortyTwoId);
user_db.currentRoom = name;
this.userRepository.save(user_db);
return res.status(HttpStatus.OK).json({ message: `room "${name}" is now current room` });
return `room "${name}" is now current room`;
}
async addUserToRoom(user: User, joinRoomDto: joinRoomDto, @Res() res)
async addUserToRoom(user: User, createRoomDto: createRoomDto): Promise<string>
{
console.log("-- in addUserToRoom service");
const room = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.name = :name', { name: joinRoomDto.room_name })
.where('chatroom.name = :name', { name: createRoomDto.room_name })
.getOne();
if (room)
throw new HttpException(`This room already exist`, HttpStatus.CONFLICT);
// create chatroom
const newChatroom = new Chatroom();
newChatroom.name = joinRoomDto.room_name;
newChatroom.type = joinRoomDto.room_type;
newChatroom.name = createRoomDto.room_name;
newChatroom.type = createRoomDto.room_type;
newChatroom.owner = user.fortyTwoId;
newChatroom.users = [user.fortyTwoId];
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${joinRoomDto.room_name}` }];
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${createRoomDto.room_name}` }];
this.chatroomRepository.save(newChatroom);
this.setCurrentRoom(user, joinRoomDto.room_name, res)
this.setCurrentRoom(user, createRoomDto.room_name)
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: "successfull room creation" });
return "successfull room creation";
}
async removeUserFromRoom(user: User, room_name: string)
{
console.log("-- in removeUserFromRoom service");
// get room
// remove user
}
async addMessageToCurrentRoom(name: string, message: string)
{
console.log("-- in addMessageToCurrentRoom service");
const user_db = await this.findUserByName(name);
const currentRoom = await this.findRoomByName(user_db.currentRoom);
let chat_message = {
@@ -115,11 +124,13 @@ export class ChatService {
this.chatroomRepository.save(currentRoom);
}
async getMessagesFromCurrentRoom(user: User, @Res() res)
async getMessagesFromCurrentRoom(user: User): Promise<object>
{
console.log("-- in getMessagesFromCurrentRoom service");
const user_db = await this.usersService.findOneByFourtyTwoId(user.fortyTwoId);
const currentRoom = await this.findRoomByName(user_db.currentRoom);
return res.status(HttpStatus.OK).json({ messages: currentRoom.messages });
return currentRoom.messages;
}
}

View File

@@ -1,7 +1,7 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
import { IsNull } from "typeorm";
export class joinRoomDto
export class createRoomDto
{
@IsString()
@IsNotEmpty()