created querybuilder for direct users

This commit is contained in:
simplonco
2023-01-10 19:49:53 +01:00
parent 4af3654e58
commit 7f01ccdb78
5 changed files with 62 additions and 36 deletions

View File

@@ -43,6 +43,7 @@ export class ChatController {
{ {
console.log("- in getAllRooms controller"); console.log("- in getAllRooms controller");
const rooms = await this.chatService.getAllNotMyRooms(req.user.username); const rooms = await this.chatService.getAllNotMyRooms(req.user.username);
await this.chatService.getAllUsersNotMyRooms(req.user.username);
console.log("- out getAllRooms controller"); console.log("- out getAllRooms controller");
return res.status(HttpStatus.OK).json({ rooms: rooms }); return res.status(HttpStatus.OK).json({ rooms: rooms });
} }

View File

@@ -9,6 +9,7 @@ import { joinRoomDto } from './dto/joinRoom.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';
@Injectable() @Injectable()
export class ChatService { export class ChatService {
@@ -46,7 +47,7 @@ export class ChatService {
{ {
console.log("-- in getAllNotMyRooms service"); console.log("-- in getAllNotMyRooms service");
const my_rooms = await this.getMyRooms(username); const my_rooms = await this.getMyRooms(username);
const directs = my_rooms; const directs = my_rooms.filter(room => room.type === 'direct');
console.log("-- out getAllNotMyRooms service"); console.log("-- out getAllNotMyRooms service");
return directs; return directs;
@@ -77,25 +78,6 @@ export class ChatService {
return rooms; return rooms;
} }
/*
async getAllUsersNotRooms(username: string)
{
console.log("-- in getAllNotMyRooms service");
const user_db = await this.getUserByName(username);
//const user_db = await this.usersService.findOne(username);
const rooms = await this.chatroomRepository
.createQueryBuilder('chatroom')
.where('chatroom.type != :type', { type: 'private' })
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
.getMany();
//const users = await this.getAllUsers();
//let allRooms = [...rooms, ...users];
console.log("-- out getAllNotMyRooms service");
return rooms;
}
*/
async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]> async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]>
{ {
console.log("-- in getMessagesFromCurrentRoom service"); console.log("-- in getMessagesFromCurrentRoom service");
@@ -202,9 +184,6 @@ export class ChatService {
async addMessageToRoom(room_name: string, username: string, message: string): Promise<void> async addMessageToRoom(room_name: string, username: string, message: string): Promise<void>
{ {
console.log("-- in addMessageToRoom service"); console.log("-- in addMessageToRoom service");
//const user_db = await this.getUserByName(username);
//const user_db = await this.usersService.findOne(username);
//console.log("user_db:", user_db);
const currentRoom = await this.getRoomByName(room_name); const currentRoom = await this.getRoomByName(room_name);
let chat_message = { let chat_message = {
name: username, name: username,
@@ -255,6 +234,45 @@ export class ChatService {
return user; return user;
} }
async getAllUsersNotMyRooms(username: string)
{
console.log("-- in getAllUsersNotMyRooms service");
const directs = await this.getMyDirects(username);
let usernames = directs.map(room => room.users[0]);
if (usernames.length === 0)
usernames = [""];
console.log("usernames:", usernames);
const my_users = await this.userRepository
.createQueryBuilder('user')
.where('user.username NOT IN (:...usernames)', { usernames: usernames })
.getMany();
console.log("my_users:", my_users);
/*
User {
id: 1,
fortyTwoId: '42522',
username: 'hulamy',
email: 'hulamy@student.42.fr',
image_url: 'default.png',
phone: null,
status: 'Connected',
isEnabledTwoFactorAuth: false,
isTwoFactorAuthenticated: false,
secretTwoFactorAuth: null,
currentRoom: 'hugo2'
}
*/
console.log("-- out getAllUsersNotMyRooms service");
}
/*
*/
/* GATEWAY EVENTS ***************************************** /* GATEWAY EVENTS *****************************************
*/ */

View File

@@ -1,5 +1,5 @@
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator"; import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional, IsEnum } from "class-validator";
import { IsNull } from "typeorm"; import { RoomType } from 'src/chat/enums/roomType.enum';
export class createRoomDto export class createRoomDto
{ {
@@ -9,7 +9,8 @@ export class createRoomDto
@IsString() @IsString()
@IsNotEmpty() @IsNotEmpty()
room_type: 'public' | 'private' | 'direct'; @IsEnum(RoomType)
room_type: RoomType;
@IsString() @IsString()
@IsOptional() @IsOptional()

View File

@@ -7,7 +7,8 @@ import {
PrimaryGeneratedColumn PrimaryGeneratedColumn
} from "typeorm"; } from "typeorm";
import { User } from 'src/users/entities/user.entity'; import { User } from 'src/users/entities/user.entity';
import { messagesDto } from '../dto/messages.dto'; import { messagesDto } from 'src/chat/dto/messages.dto';
import { RoomType } from 'src/chat/enums/roomType.enum';
@Entity('chatroom') @Entity('chatroom')
export class Chatroom { export class Chatroom {
@@ -17,15 +18,12 @@ export class Chatroom {
@Column() @Column()
name: string; name: string;
@Column() @Column({
type: string; type: "enum",
enum: RoomType,
// @ManyToOne(type => User, user => user.ownedRooms) default: RoomType.public
// owner: User; })
// type: RoomType;
// @ManyToMany(type => User)
// @JoinTable()
// users: User[];
@Column() @Column()
owner: string; // username owner: string; // username

View File

@@ -0,0 +1,8 @@
export enum RoomType {
public = 'public',
protected = 'protected',
private = 'private',
direct = 'direct',
}