wip try to make chatrooms db remember users

This commit is contained in:
simplonco
2023-01-07 17:27:15 +01:00
parent 41dbee1cc0
commit d21c1d1e4e
11 changed files with 425 additions and 175 deletions

View File

@@ -14,24 +14,19 @@ export class ChatController {
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Get('rooms')
async getRooms()
async getRooms(@Req() req, @Res() res)
{
const rooms = await this.chatService.getRooms();
return { rooms };
console.log("in getRooms");
return await this.chatService.getRooms(req.user, res);
}
@UseGuards(AuthenticateGuard)
@UseGuards(TwoFactorGuard)
@Post('join')
async joinRoom(@Body() joinRoomDto: joinRoomDto, @Req() req)
async joinRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res)
{
const user: User = req.user;
let room = await this.chatService.addUserToRoom(user, joinRoomDto);
//return { message: 'Successfully joined room.' };
//return res.status(HttpStatus.BAD_REQUEST).json({message : 'You can\'t grant a ticket to another user'});
return { room };
return await this.chatService.addUserToRoom(user, joinRoomDto, res);
}
@UseGuards(AuthenticateGuard)

View File

@@ -1,5 +1,6 @@
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable, Res } from '@nestjs/common';
import { User } from 'src/users/entities/user.entity';
import { UsersService } from 'src/users/users.service';
import { Chatroom } from './entities/chatroom.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
@@ -9,6 +10,7 @@ import { joinRoomDto } from './dto/joinRoom.dto';
export class ChatService {
constructor(
private usersService: UsersService,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
@InjectRepository(Chatroom)
@@ -19,17 +21,22 @@ export class ChatService {
// return server.emit('message', message);
//}
async getRooms()
async getRooms(user: User, @Res() res)
{
/*
const room = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where(':user_id IN chatroom.users', { user_id: user.fortyTwoId })
.getMany();
console.log(room);
*/
//return chatrooms;
}
async getUserRooms()
async addUserToRoom(user: User, joinRoomDto: joinRoomDto, @Res() res)
{
}
async addUserToRoom(user: User, joinRoomDto: joinRoomDto)
{
//const room = await this.chatroomRepository.findOneBy({ name : joinRoomDto.room_name });
const room = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.name = :name', { name: joinRoomDto.room_name })
@@ -38,15 +45,15 @@ export class ChatService {
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.owner = user;
newChatroom.users = [user];
const savedChatroom = await this.chatroomRepository.save(newChatroom);
console.log(savedChatroom);
newChatroom.owner = user.fortyTwoId;
newChatroom.users = [user.fortyTwoId];
this.chatroomRepository.save(newChatroom);
return "good room";
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: "successfull room creation" });
}
async removeUserFromRoom(user: User, room_name: string)

View File

@@ -19,11 +19,17 @@ export class Chatroom {
@Column()
type: string;
@ManyToOne(type => User, user => user.ownedRooms)
owner: User;
// @ManyToOne(type => User, user => user.ownedRooms)
// owner: User;
//
// @ManyToMany(type => User)
// @JoinTable()
// users: User[];
@ManyToMany(type => User)
@JoinTable()
users: User[];
@Column()
owner: string; // fortytwo id
@Column({ type: "simple-array" })
users: string[]; // fortytwo id
}

View File

@@ -54,10 +54,10 @@ export class User {
@OneToOne(() => UserStats, { cascade: true })
stats: UserStats;
@OneToMany(type => Chatroom, chatroom => chatroom.owner)
ownedRooms: Chatroom[];
@ManyToMany(type => Chatroom)
@JoinTable()
userRooms: Chatroom[];
// @OneToMany(type => Chatroom, chatroom => chatroom.owner)
// ownedRooms: Chatroom[];
//
// @ManyToMany(type => Chatroom)
// @JoinTable()
// userRooms: Chatroom[];
}