owner can leave room now

This commit is contained in:
simplonco
2023-01-17 13:45:30 +01:00
parent 7c437ef38c
commit 7d3382aa09
5 changed files with 83 additions and 22 deletions

View File

@@ -79,15 +79,17 @@
- [/] see all my rooms - [/] see all my rooms
- [/] invite someone in room - [/] invite someone in room
- [/] leave room - [/] leave room
- [ ] leave room if owner
- [ ] last leave room
- [/] leave direct impossible - [/] leave direct impossible
- [/] protect room with password - [/] protect room with password
- [/] add, change, and remove password in room - [/] add, change, and remove password in room
- [ ] make admin - [/] make admin
- [ ] ban - [/] ban
- [ ] mute - [/] mute
- [ ] block users - [/] block users
- [ ] send game invitation - [/] send game invitation
- [ ] view user profiles - [/] view user profiles
#### game : #### game :
@@ -244,3 +246,17 @@
- HTTP_VERSION_NOT_SUPPORTED: 505 - HTTP_VERSION_NOT_SUPPORTED: 505
``` ```
---
## tests chat :
- [ ] leave room
- user leave room
- admin user leave room
- owner user leave room
- on rooms with password
- on direct rooms
- after password is change / set / removed
- [ ]

View File

@@ -438,19 +438,21 @@ export class ChatController {
printCaller("- in "); printCaller("- in ");
const room_name = await this.chatService.getCurrentRoomName(req.user.username); const room_name = await this.chatService.getCurrentRoomName(req.user.username);
let response = await this.chatService.removeUserFromRoom(req.user.username, room_name); let messages: string[] = await this.chatService.removeUserFromRoom(req.user.username, room_name);
// set current room to nothing // set current room to nothing
await this.chatService.setCurrentRoom(req.user.username, ""); await this.chatService.setCurrentRoom(req.user.username, "");
// leaving message // leaving message
let message = `${req.user.username} left the room`;
await this.chatService.addMessageToRoom(room_name, "SERVER", message);
let socket: socketDto = this.chatGateway.sockets.get(req.user.username); let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
await socket.to(socket.room).emit('message', "SERVER", message); await messages.forEach(async (message) =>
{
await this.chatService.addMessageToRoom(room_name, "SERVER", message);
await socket.to(socket.room).emit('message', "SERVER", message);
});
await socket.leave(socket.room); await socket.leave(socket.room);
res.status(HttpStatus.OK).json({ message: response }); res.status(HttpStatus.OK).json({ message: messages[0] });
printCaller("- out "); printCaller("- out ");
} }

View File

@@ -10,9 +10,9 @@ import { InjectRepository } from '@nestjs/typeorm';
import { roomDto } from './dto/room.dto'; import { roomDto } from './dto/room.dto';
import { messagesDto } from './dto/messages.dto'; import { messagesDto } from './dto/messages.dto';
import { socketDto } from './dto/socket.dto'; import { socketDto } from './dto/socket.dto';
import { muteDto } from './dto/mute.dto';
import * as bcrypt from 'bcrypt'; import * as bcrypt from 'bcrypt';
import { printCaller } from './dev/dev_utils'; import { printCaller } from './dev/dev_utils';
import { muteDto } from './dto/mute.dto';
@Injectable() @Injectable()
@@ -436,10 +436,18 @@ export class ChatService {
{ {
printCaller("-- in "); printCaller("-- in ");
const room = await this.getRoomByName(room_name); const room: Chatroom = await this.getRoomByName(room_name);
// update room with new user // update room with new user
room.users.push(username); room.users.push(username);
if (room.owner === "")
{
room.owner = username;
if (!room.admins.includes(username))
room.admins.push(username);
if (!room.allowed_users.includes(username))
room.allowed_users.push(username);
}
await this.chatroomRepository.save(room); await this.chatroomRepository.save(room);
printCaller("-- out "); printCaller("-- out ");
@@ -498,10 +506,12 @@ export class ChatService {
/* REMOVERS *********************************************** /* REMOVERS ***********************************************
*/ */
async removeUserFromRoom(username: string, room_name: string): Promise<string> async removeUserFromRoom(username: string, room_name: string): Promise<string[]>
{ {
printCaller("-- in "); printCaller("-- in ");
let messages = [`${username} left the room`];
const room = await this.getRoomByName(room_name); const room = await this.getRoomByName(room_name);
if (!room.users.includes(username)) if (!room.users.includes(username))
{ {
@@ -517,10 +527,26 @@ export class ChatService {
// delete user from room // delete user from room
room.users.push(username); room.users.push(username);
room.users = room.users.filter(name => name !== username); room.users = room.users.filter(name => name !== username);
room.admins = room.admins.filter(name => name !== username);
if (room.owner === username)
{
if (room.admins.length > 0)
{
room.owner = room.admins[0];
messages.push(`${room.owner} is now owner of this room`);
}
else if (room.users)
{
room.owner = room.users[0];
messages.push(`${room.owner} is now owner of this room`);
}
else
room.owner = "";
}
await this.chatroomRepository.save(room); await this.chatroomRepository.save(room);
printCaller("-- out "); printCaller("-- out ");
return "successfully leaving room"; return messages;
} }
async removeMute(username: string, room_name: string): Promise<void> async removeMute(username: string, room_name: string): Promise<void>
@@ -672,5 +698,18 @@ export class ChatService {
printCaller("-- out "); printCaller("-- out ");
} }
/* LAST MINUTE BAD SOLUTION *******************************
*/
async changeAllUsernames(socket: socketDto, room_name: string): Promise<void>
{
printCaller("-- in ");
printCaller("-- out ");
}
} }

View File

@@ -31,28 +31,30 @@ export class Chatroom
@IsOptional() @IsOptional()
hash?: string; hash?: string;
@Column() @Column()
owner: string; // username owner: string; // username
@Column("simple-array") @Column("simple-array")
@IsArray() @IsArray()
@IsString({ each: true }) @IsString({ each: true })
admins: string[]; // username admins: string[]; // username
@Column("simple-array") @Column("simple-array")
@IsArray() @IsArray()
@IsString({ each: true }) @IsString({ each: true })
users: string[]; // usernames users: string[]; // usernames
@Column("simple-array") // users that can connect without (re)entering password
// is emptied each time password change
@Column("simple-array")
@IsArray() @IsArray()
@IsString({ each: true }) @IsString({ each: true })
allowed_users: string[]; // usernames allowed_users: string[]; // usernames
@Column("json") @Column("json")
messages: messagesDto[]; messages: messagesDto[];
@Column("json", { nullable: true }) @Column("json", { nullable: true })
@IsOptional() @IsOptional()
mutes?: muteDto[]; mutes?: muteDto[];
} }

View File

@@ -109,7 +109,9 @@ export class UsersService {
...updateUserDto}); ...updateUserDto});
if (!user) if (!user)
throw new HttpException(`The user could not be updated.`,HttpStatus.NOT_FOUND); throw new HttpException(`The user could not be updated.`,HttpStatus.NOT_FOUND);
return this.userRepository.save(user); this.userRepository.save(user);
return user;
} }
async remove(id: number) { async remove(id: number) {