wip multiple rooms

This commit is contained in:
hugogogo
2023-01-09 12:23:41 +01:00
parent 258e7596fe
commit 45edd2f25f
8 changed files with 200 additions and 156 deletions

View File

@@ -1,9 +1,7 @@
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';
import { ChatService } from './chat.service';
import { AuthenticateGuard } from 'src/auth/42/guards/42guards';
@WebSocketGateway(5000, {
path: '/chat',
@@ -31,19 +29,22 @@ export class ChatGateway
console.log('- Client disconnected :', client.id, client.username);
}
// @UseGuards(AuthenticateGuard)
@SubscribeMessage('join')
joinRoom(@ConnectedSocket() socket, @MessageBody() room_name: string): void
{
console.log('- in joinRoom gateway');
console.log(room_name);
socket.join(room_name);
}
@SubscribeMessage('message')
// handleMessage(@MessageBody() message: string): void {
// handleMessage(client: any, message: string): void {
handleMessage(@ConnectedSocket() client, @MessageBody() message: string): void{
// const paginationQuery = new PaginationQueryDto();
// const users = this.usersService.findAll(paginationQuery);
// console.log('users :', users);
// this.server.emit('message', message);
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);
async handleMessage(@ConnectedSocket() socket, @MessageBody() message: string): Promise<void>
{
console.log('- in handleMessage gateway');
let room_name = await this.chatService.getCurrentRoom(socket.username);
console.log('room_name:', room_name);
socket.to(room_name).emit('message', socket.username, message);
this.chatService.addMessageToCurrentRoom(socket.username, message);
}
}

View File

@@ -25,6 +25,10 @@ export class ChatService {
return new Promise(resolve => setTimeout(resolve, ms));
}
/* GETTERS ************************************************
*/
async getMyRooms(user: User)
{
console.log("-- in getMyRooms service");
@@ -49,81 +53,79 @@ export class ChatService {
async getAllNotMyRooms(user: User)
{
console.log("-- in getAllNotMyRooms service");
const user_db = await this.findUserByName(user.username);
const user_db = await this.getUserByName(user.username);
const rooms = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.type != :type', { type: 'private' })
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${user.username}%` })
.getMany();
//const users = await this.findAllUsers();
//const users = await this.getAllUsers();
//let allRooms = [...rooms, ...users];
return rooms;
}
async findRoomByName(name: string)
async getMessagesFromCurrentRoom(user: User)
{
console.log("-- in findUserByName service");
console.log("-- in getMessagesFromCurrentRoom service");
const user_db = await this.usersService.findOneByFourtyTwoId(user.fortyTwoId);
const currentRoom = await this.getRoomByName(user_db.currentRoom);
return currentRoom.messages;
}
async getCurrentRoom(username: string)
{
console.log("-- in getCurrentRoom service");
const user_db = await this.getUserByName(username);
return user_db.currentRoom;
}
async getRoomByName(name: string)
{
console.log("-- in getRoomByName 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)
async getRoomById(id: number)
{
console.log("-- in findRoomById service");
console.log("-- in getRoomById service");
const room = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.id = :id', { id: id })
.getOne();
console.log("room:", room, ", typeof room:", typeof room);
return room;
}
/* temp *****************************************
/* SETTERS ************************************************
*/
async findUserByName(name: string)
{
console.log("-- in findUserByName service");
const user = await this.userRepository
.createQueryBuilder('user')
.where('user.username = :name', { name: name })
.getOne();
return user;
}
/* temp *****************************************
*/
async findAllUsers()
{
console.log("-- in findAllUsers service");
const users = await this.userRepository
.createQueryBuilder('user')
.getMany();
return users;
}
async setCurrentRoom(user: User, name: string)
{
console.log("-- in setCurrentRoom service");
const user_db = await this.findUserByName(user.username);
const user_db = await this.getUserByName(user.username);
user_db.currentRoom = name;
this.userRepository.save(user_db);
return `room "${name}" is now current room`;
}
/* ADDERS *************************************************
*/
async addUserToNewRoom(user: User, createRoomDto: createRoomDto)
{
console.log("-- in addUserToRoom service");
const room = await this.findRoomByName(createRoomDto.room_name);
const room = await this.getRoomByName(createRoomDto.room_name);
if (room)
throw new HttpException(`This room already exist`, HttpStatus.CONFLICT);
@@ -144,7 +146,7 @@ export class ChatService {
async addUserToRoom(user: User, joinRoomDto: joinRoomDto)
{
console.log("-- in addUserToRoom service");
const room = await this.findRoomByName(joinRoomDto.room_name);
const room = await this.getRoomByName(joinRoomDto.room_name);
if (room.users.includes(user.username))
throw new HttpException(`your have already join this room`, HttpStatus.CONFLICT);
@@ -153,15 +155,30 @@ export class ChatService {
this.chatroomRepository.save(room);
const rooms = await this.getMyRooms(user);
console.log("rooms:", rooms);
const allRooms = await this.getAllRooms();
console.log("allRooms:", allRooms);
this.setCurrentRoom(user, joinRoomDto.room_name)
return "successfull joining room";
}
async addMessageToCurrentRoom(name: string, message: string)
{
console.log("-- in addMessageToCurrentRoom service");
const user_db = await this.getUserByName(name);
const currentRoom = await this.getRoomByName(user_db.currentRoom);
let chat_message = {
name: name,
message: message,
};
currentRoom.messages.push(chat_message);
this.chatroomRepository.save(currentRoom);
}
/* REMOVERS ***********************************************
*/
async removeUserFromRoom(user: User, room_name: string)
{
console.log("-- in removeUserFromRoom service");
@@ -169,27 +186,34 @@ export class ChatService {
// remove user
}
async addMessageToCurrentRoom(name: string, message: string)
/* SEARCH IN USER *****************************************
*/
async getUserByName(name: string)
{
console.log("-- in addMessageToCurrentRoom service");
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);
console.log("-- in getUserByName service");
const user = await this.userRepository
.createQueryBuilder('user')
.where('user.username = :name', { name: name })
.getOne();
return user;
}
async getMessagesFromCurrentRoom(user: User)
async getAllUsers()
{
console.log("-- in getMessagesFromCurrentRoom service");
const user_db = await this.usersService.findOneByFourtyTwoId(user.fortyTwoId);
const currentRoom = await this.findRoomByName(user_db.currentRoom);
console.log("-- in getAllUsers service");
const users = await this.userRepository
.createQueryBuilder('user')
.getMany();
return currentRoom.messages;
return users;
}
/* SOCKET EVENTS ******************************************
*/
}

View File

@@ -56,13 +56,6 @@ export class User {
// ROOMS :
//@OneToMany(type => Chatroom, chatroom => chatroom.owner)
//ownedRooms: Chatroom[];
//@ManyToMany(type => Chatroom)
//@JoinTable()
//userRooms: Chatroom[];
@Column({ nullable: true })
currentRoom: string;
currentRoom: string; // room name
}