wip load room messages, but i broke rooms list

This commit is contained in:
simplonco
2023-01-07 21:58:33 +01:00
parent e4a3504564
commit c41c7de745
11 changed files with 118 additions and 58 deletions

View File

@@ -44,11 +44,14 @@ export class ChatController {
@Post('leave')
async leaveRoom(@Body() body)
{
const { room_id } = body;
// get user
let user;
await this.chatService.removeUserFromRoom(user, room_id);
return { message: 'Successfully left room.' };
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('messages')
async getMessages(@Req() req, @Res() res)
{
return this.chatService.getMessagesFromCurrentRoom(req.user, res);
}
}

View File

@@ -1,12 +1,4 @@
import {
WebSocketGateway,
SubscribeMessage,
WebSocketServer,
MessageBody,
ConnectedSocket,
OnGatewayConnection,
OnGatewayDisconnect,
} from '@nestjs/websockets';
import { WebSocketGateway, SubscribeMessage, WebSocketServer, MessageBody, ConnectedSocket, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
import { UseGuards } from '@nestjs/common';
import { UsersService } from 'src/users/users.service';
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
@@ -51,6 +43,7 @@ export class ChatGateway
console.log('client.username :', client.username);
client.local.emit('message', client.username, message);
// this.chatService.add_message(this.server, message);
this.chatService.addMessageToCurrentRoom(client.username, message);
}
}

View File

@@ -5,6 +5,7 @@ import { Chatroom } from './entities/chatroom.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { joinRoomDto } from './dto/joinRoom.dto';
import { messagesDto } from './dto/messages.dto';
@Injectable()
export class ChatService {
@@ -33,14 +34,14 @@ export class ChatService {
return res.status(HttpStatus.OK).json({ rooms: rooms });
}
async findRoomByName(name: string, @Res() res)
async findRoomByName(name: string)
{
const room = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.name = :name', { name: name })
.getOne();
return res.status(HttpStatus.OK).json({ room: room });
return room;
}
async findRoomById(id: number, @Res() res)
@@ -53,6 +54,16 @@ export class ChatService {
return res.status(HttpStatus.OK).json({ room: room });
}
async findUserByName(name: string)
{
const user = await this.userRepository
.createQueryBuilder('user')
.where('user.username = :name', { name: name })
.getOne();
return user;
}
async setCurrentRoom(user: User, name: string, @Res() res)
{
const user_db = await this.usersService.findOneByFourtyTwoId(user.fortyTwoId);
@@ -77,6 +88,7 @@ export class ChatService {
newChatroom.type = joinRoomDto.room_type;
newChatroom.owner = user.fortyTwoId;
newChatroom.users = [user.fortyTwoId];
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${joinRoomDto.room_name}` }];
this.chatroomRepository.save(newChatroom);
this.setCurrentRoom(user, joinRoomDto.room_name, res)
@@ -89,5 +101,25 @@ export class ChatService {
// get room
// remove user
}
async addMessageToCurrentRoom(name: string, message: string)
{
const user_db = await this.findUserByName(name);
const currentRoom = await this.findRoomByName(user_db.currentRoom);
let chat_message = {
name: name,
message: message,
};
console.log(message);
currentRoom.messages.push(chat_message);
this.chatroomRepository.save(currentRoom);
}
async getMessagesFromCurrentRoom(user: User, @Res() res)
{
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 });
}
}

View File

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

View File

@@ -29,7 +29,10 @@ export class Chatroom {
@Column()
owner: string; // fortytwo id
@Column({ type: "simple-array" })
@Column("simple-array")
users: string[]; // fortytwo id
@Column("json")
messages: { name: string, message: string }[];
}

View File

@@ -111,4 +111,17 @@ export class UsersController {
return promise;
}
// TEMP FOR HUGO
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('ftId')
getFortyTwoId(@Query('username') username: string, @Req() req) {
if (username === undefined) {
return this.usersService.findOne(req.user.id);
} else {
const user : User = req.user;
return this.usersService.findOneByUsername(user.id.toString(),username);
}
}
}