room update names in db

This commit is contained in:
simplonco
2023-01-17 17:11:23 +01:00
parent 7d3382aa09
commit d0dfb4a533
9 changed files with 111 additions and 21 deletions

View File

@@ -258,5 +258,8 @@
- on rooms with password
- on direct rooms
- after password is change / set / removed
- [ ]
- [ ] join empty room
- user join empty public room
- user join empty protected room
- is admin ?

View File

@@ -8,7 +8,6 @@ import { FriendshipsModule } from './friendship/friendships.module';
import { AuthenticationModule } from './auth/42/authentication.module';
import { PassportModule } from '@nestjs/passport';
import { GameModule } from './game/game.module';
import { ChatGateway } from './chat/chat.gateway';
import { ChatModule } from './chat/chat.module';
@Module({
@@ -32,7 +31,6 @@ import { ChatModule } from './chat/chat.module';
synchronize: true,
}),
ChatModule,
// GameModule,
],
controllers: [AppController],
providers: [AppService],

View File

@@ -9,11 +9,22 @@ import { AuthenticationController } from './authentication.controller';
import { AuthenticationService } from './authentication.service';
import { FortyTwoStrategy } from './strategy/42strategy';
import { SessionSerializer } from './utils/serializer';
import { ChatModule } from 'src/chat/chat.module';
import { ChatService } from 'src/chat/chat.service';
import { Chatroom } from 'src/chat/entities/chatroom.entity';
@Module({
imports: [TypeOrmModule.forFeature([User, Friendship]), UsersModule,
imports: [TypeOrmModule.forFeature([User, Friendship, Chatroom]),
UsersModule,
ChatModule,
],
providers: [AuthenticationService, FortyTwoStrategy, UsersService, SessionSerializer, FriendshipService
providers: [
AuthenticationService,
FortyTwoStrategy,
UsersService,
SessionSerializer,
FriendshipService,
ChatService,
],
exports: [AuthenticationService],
controllers: [AuthenticationController],

View File

@@ -52,6 +52,7 @@ export class ChatController {
let fields = ["name", "type", "users", "protection", "allowed_users"];
const rooms = await this.chatService.getMyRooms(req.user.username, fields);
console.log("rooms:", rooms);
const blocked = await this.chatService.getListBlockUser(req.user.username);
let filtered_rooms = rooms.filter(room =>

View File

@@ -1,10 +1,10 @@
import { Module } from '@nestjs/common';
import { Module, forwardRef } from '@nestjs/common';
import { FriendshipsModule } from 'src/friendship/friendships.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ChatController } from './chat.controller';
import { ChatService } from './chat.service';
import { ChatGateway } from './chat.gateway';
import { UsersModule } from 'src/users/users.module';
import { FriendshipsModule } from 'src/friendship/friendships.module';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Chatroom } from './entities/chatroom.entity';
import { User } from 'src/users/entities/user.entity';
import { Friendship } from 'src/friendship/entities/friendship.entity';
@@ -12,14 +12,13 @@ import { Friendship } from 'src/friendship/entities/friendship.entity';
@Module({
imports: [
TypeOrmModule.forFeature([Chatroom, User, Friendship]),
UsersModule,
FriendshipsModule,
forwardRef(() => UsersModule),
],
controllers: [
ChatController,
],
exports: [
],
exports: [],
providers: [
ChatService,
ChatGateway,

View File

@@ -1,4 +1,4 @@
import { HttpException, HttpStatus, Injectable, Res } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable, Res, NotFoundException, forwardRef, Inject } from '@nestjs/common';
import { User } from 'src/users/entities/user.entity';
import { Friendship, FriendshipStatus } from 'src/friendship/entities/friendship.entity';
import { Chatroom } from './entities/chatroom.entity';
@@ -19,6 +19,7 @@ import { printCaller } from './dev/dev_utils';
export class ChatService {
constructor(
@Inject(forwardRef(() => UsersService))
private usersService: UsersService,
private friendshipService: FriendshipService,
@InjectRepository(User)
@@ -144,6 +145,11 @@ export class ChatService {
printCaller("-- in ");
const user_db = await this.getUserByName(username);
if (!user_db)
{
printCaller(`throw error: error: true, code: 'USER_NOT_FOUND', message: 'the user was not found'`);
return;
}
printCaller("-- out ");
return user_db.currentRoom;
@@ -703,10 +709,61 @@ export class ChatService {
/* LAST MINUTE BAD SOLUTION *******************************
*/
async changeAllUsernames(socket: socketDto, room_name: string): Promise<void>
async changeAllUsernames(old_name: string, new_name: string): Promise<void>
{
printCaller("-- in ");
console.log("old_name:", old_name);
console.log("new_name:", new_name);
let rooms: Chatroom[] = await this.getAllRooms();
await rooms.forEach((room) =>
{
let room_has_change: boolean = false;
// users: string[]
room.users = room.users.map(item =>
{
if (item === old_name)
{
room_has_change = true;
return new_name
}
return item;
});
// admins: string[]
room.admins = room.admins.map(item =>
{
if (item === old_name)
{
room_has_change = true;
return new_name
}
return item;
});
// users: string[]
room.allowed_users = room.allowed_users.map(item =>
{
if (item === old_name)
{
room_has_change = true;
return new_name
}
return item;
});
// owner: string
if(room.owner === old_name)
room.owner = new_name;
if (room_has_change)
this.chatroomRepository.save(room);
this.addMessageToRoom(room.name, "SERVER", `${old_name} changes it's name for ${new_name}`);
});
rooms = await this.getAllRooms();
printCaller("-- out ");
}

View File

@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Module, forwardRef } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { Friendship } from 'src/friendship/entities/friendship.entity';
import { FriendshipService } from 'src/friendship/friendship.service';
@@ -8,10 +8,21 @@ import { Game } from './entity/game.entity';
import { TokenGame } from './entity/tokenGame.entity';
import { GameController } from './game.controller';
import { GameService } from './game.service';
import { ChatModule } from 'src/chat/chat.module';
import { ChatService } from 'src/chat/chat.service';
import { Chatroom } from 'src/chat/entities/chatroom.entity';
@Module({
imports: [TypeOrmModule.forFeature([TokenGame, User, Game, Friendship])],
imports: [
TypeOrmModule.forFeature([TokenGame, User, Game, Friendship, Chatroom]),
ChatModule,
],
controllers: [GameController],
providers: [GameService, UsersService, FriendshipService]
providers: [
GameService,
UsersService,
FriendshipService,
ChatService,
]
})
export class GameModule {}

View File

@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { Module, forwardRef } from '@nestjs/common';
import { UsersService } from './users.service';
import { UsersController } from './users.controller';
import { User } from './entities/user.entity';
@@ -6,10 +6,16 @@ import { TypeOrmModule } from '@nestjs/typeorm';
import { Friendship } from '../friendship/entities/friendship.entity';
import { UserStats } from './entities/userStat.entities';
import { FriendshipService } from 'src/friendship/friendship.service';
import { ChatModule } from 'src/chat/chat.module';
import { ChatService } from 'src/chat/chat.service';
import { Chatroom } from 'src/chat/entities/chatroom.entity';
@Module({
imports: [TypeOrmModule.forFeature([User, Friendship, UserStats])],
providers: [UsersService, FriendshipService],
imports: [
TypeOrmModule.forFeature([User, Friendship, UserStats, Chatroom]),
forwardRef(() => ChatModule),
],
providers: [UsersService, FriendshipService, ChatService],
exports: [UsersService],
controllers: [UsersController],
})

View File

@@ -1,4 +1,4 @@
import { HttpException, HttpStatus, Injectable, NotFoundException, } from '@nestjs/common';
import { HttpException, HttpStatus, Injectable, NotFoundException, forwardRef, Inject } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { User } from './entities/user.entity';
import { Repository, Not } from 'typeorm';
@@ -7,12 +7,15 @@ import { UpdateUsersDto } from './dto/update-users.dto';
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
import { UserStats } from './entities/userStat.entities';
import { FriendshipService } from 'src/friendship/friendship.service';
import { ChatService } from 'src/chat/chat.service';
// On va devoir sûrement trouver un moyen plus simple pour passer l'id, sûrement via des pipes
// ou des interceptors, mais pour l'instant on va faire comme ça.
@Injectable()
export class UsersService {
constructor(
@Inject(forwardRef(() => ChatService))
private chatService: ChatService,
private readonly friendshipService: FriendshipService,
@InjectRepository(User)
private readonly userRepository: Repository<User>,
@@ -110,7 +113,8 @@ export class UsersService {
if (!user)
throw new HttpException(`The user could not be updated.`,HttpStatus.NOT_FOUND);
this.userRepository.save(user);
// here goes the horrible bandage from hugo
this.chatService.changeAllUsernames(username, updateUserDto.username);
return user;
}