no more duplicate messages join

This commit is contained in:
hugogogo
2023-01-13 15:06:00 +01:00
parent 126d7cefd0
commit 5bcd52b573
5 changed files with 27 additions and 14 deletions

View File

@@ -68,13 +68,14 @@
#### chat :
- [/] create public room
- [ ] create private room
- [/] create private room
- [/] create direct room
- [/] chat in room
- [/] join public rooms
- [ ] join private rooms
- [/] join private rooms only by invitation
- [/] join direct rooms
- [/] see all joignable rooms
- [/] cannot see private rooms
- [/] see all my rooms
- [/] leave room
- [/] leave direct impossible

View File

@@ -121,7 +121,7 @@ export class ChatController {
}
else
{
let fields = ["name", "type", "users", "messages"];
let fields = ["name", "type", "users", "messages", "owner"];
const room_db = await this.chatService.getRoomByName(room.name, fields);
if (room_db.type === 'direct')
{
@@ -138,7 +138,7 @@ export class ChatController {
console.log("throw error: your have already joined this room");
throw new HttpException(`your have already joined this room`, HttpStatus.CONFLICT);
}
room = await this.chatService.addUserToRoom(req.user.username, room_db);
room = await this.chatService.addUserToRoom(req.user.username, room.name);
}
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
@@ -172,8 +172,9 @@ export class ChatController {
console.log("- in inviteUser controller");
let current_room_name = await this.chatService.getCurrentRoomName(req.user.username);
const room_db = await this.chatService.getRoomByName(current_room_name);
let room = await this.chatService.addUserToRoom(username, room_db);
let room = await this.chatService.addUserToRoom(username, current_room_name);
let message = `${username} joined the room`;
await this.chatService.addMessageToRoom(current_room_name, "SERVER", message);
res.status(HttpStatus.OK).json({ room: room });

View File

@@ -36,8 +36,6 @@ implements OnGatewayConnection, OnGatewayDisconnect
console.log('- in joinRoom gateway');
await this.chatService.socketJoinRoom(socket, room_name)
let message = `${socket.username} joined the room`;
await this.chatService.addMessageToRoom(room_name, "SERVER", message);
console.log('- out joinRoom gateway');
}

View File

@@ -106,6 +106,11 @@ export class ChatService {
{
console.log("-- in getAllOtherRoomsAndUsers service");
const temp = await this.chatroomRepository
.createQueryBuilder('chatroom')
.getMany();
console.log("all rooms:", temp);
const all_rooms = await this.getAllNotMyRooms(username);
const all_users = await this.getAllUsersNotMyRooms(username);
@@ -121,7 +126,7 @@ export class ChatService {
type: "user",
};
});
let rooms = row_rooms.concat(users);
let rooms: roomDto[] = row_rooms.concat(users);
console.log("--- rooms:", rooms);
console.log("-- in getAllOtherRoomsAndUsers service");
@@ -235,13 +240,14 @@ export class ChatService {
console.log("-- out addUserToNewRoom service");
}
async addUserToRoom(username: string, room: roomDto): Promise<roomDto>
async addUserToRoom(username: string, room_name: string): Promise<roomDto>
{
console.log("-- in addUserToRoom service");
const room = await this.getRoomByName(room_name);
// update room with new user
room.users.push(username);
room.messages.push({ name: "SERVER", message: `${username} joined the room`});
await this.chatroomRepository.save(room);
console.log("-- out addUserToRoom service");
@@ -253,7 +259,7 @@ export class ChatService {
console.log("-- in addMessageToRoom service");
const my_room = await this.getRoomByName(room_name);
let chat_message = {
let chat_message: messagesDto = {
name: username,
message: message,
};
@@ -390,6 +396,7 @@ export class ChatService {
socket.room = room_name;
let message = `${socket.username} joined 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,8 +1,12 @@
import { IsBoolean, IsEmpty, IsInt, IsIn, IsNotEmpty, IsNumber, IsArray, IsString, IsInstance, IsOptional, IsEnum } from "class-validator";
import { IsBoolean, IsEmpty, IsInt, IsIn, IsNotEmpty, IsNumber, IsArray, IsString, IsInstance, ValidateNested, IsObject, IsOptional, IsEnum } from "class-validator";
import { messagesDto } from 'src/chat/dto/messages.dto';
export class roomDto
{
@IsNumber()
@IsOptional()
id?: number;
@IsString()
@IsNotEmpty()
name: string;
@@ -26,7 +30,9 @@ export class roomDto
users?: string[]; // usernames
@IsArray()
@IsInstance(messagesDto, { each: true })
//@IsInstance(messagesDto, { each: true })
//@IsObject({ each: true })
@ValidateNested({ each: true })
@IsOptional()
messages?: messagesDto[];
}