mute user works, not forever so far
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
import { Controller, UseGuards, HttpException, HttpStatus, Get, Post, Delete, Body, Req, Res } from '@nestjs/common';
|
||||
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
||||
import { ConnectedSocket } from '@nestjs/websockets';
|
||||
import { ChatService } from './chat.service';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { PartialUsersDto } from 'src/users/dto/partial-users.dto';
|
||||
import { roomDto } from './dto/room.dto';
|
||||
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
|
||||
import { ChatGateway } from './chat.gateway';
|
||||
import { socketDto } from './dto/socket.dto';
|
||||
import { Chatroom } from './entities/chatroom.entity';
|
||||
import { socketDto } from './dto/socket.dto';
|
||||
import { roomDto } from './dto/room.dto';
|
||||
import { printCaller } from './dev/dev_utils';
|
||||
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
|
||||
import { muteDto } from './dto/mute.dto';
|
||||
|
||||
@Controller('chat')
|
||||
export class ChatController {
|
||||
@@ -451,9 +450,37 @@ export class ChatController {
|
||||
|
||||
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||
const users = await this.chatService.getAllUsersNotInRoom(room_name);
|
||||
|
||||
res.status(HttpStatus.OK).json({ users: users });
|
||||
printCaller("- out ");
|
||||
}
|
||||
|
||||
@UseGuards(AuthenticateGuard)
|
||||
@UseGuards(TwoFactorGuard)
|
||||
@Post('setmute')
|
||||
async setMuteUser(@Body('mute') mute: muteDto, @Body('time') time: string, @Req() req, @Res() res): Promise<void>
|
||||
{
|
||||
printCaller("- in ");
|
||||
|
||||
console.log("mute:", mute);
|
||||
|
||||
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||
await this.chatService.addMute(req.user.username, room_name, mute);
|
||||
|
||||
// inform other connected users
|
||||
let message = `${req.user.username} has muted ${mute.name} untill ${time}`;
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let server = this.chatGateway.server;
|
||||
await this.chatService.addMessageToRoom(room_name, "SERVER", message);
|
||||
await server.in(socket.room).emit('message', "SERVER", message);
|
||||
//await server.to(socket.id).emit('muted');
|
||||
|
||||
const room = await this.chatService.getRoomByName(room_name);
|
||||
console.log("room:", room);
|
||||
|
||||
res.status(HttpStatus.OK).json({ message: message });
|
||||
printCaller("- out ");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ import { messagesDto } from './dto/messages.dto';
|
||||
import { socketDto } from './dto/socket.dto';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { printCaller } from './dev/dev_utils';
|
||||
import { muteDto } from './dto/mute.dto';
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -353,9 +354,42 @@ export class ChatService {
|
||||
message: message,
|
||||
};
|
||||
my_room.messages.push(chat_message);
|
||||
await this.chatroomRepository.save(my_room);
|
||||
|
||||
printCaller("-- out ");
|
||||
}
|
||||
|
||||
async addMute(username: string, room_name: string, mute: muteDto): Promise<void>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
const room_db = await this.getRoomByName(room_name);
|
||||
|
||||
if (!room_db.admins.includes(username))
|
||||
{
|
||||
console.log("throw error: error: true, code: 'NO_ADMIN', message: 'only admins are allowed to set or modify mute time'");
|
||||
throw new HttpException({ error: true, code: 'NO_ADMIN', message: `only admins are allowed to set or modify mute time` }, HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
if (!room_db.mutes)
|
||||
room_db.mutes = [mute];
|
||||
else
|
||||
{
|
||||
let already_here = false;
|
||||
room_db.mutes.forEach(mute_elem =>
|
||||
{
|
||||
if (mute_elem.name === mute.name)
|
||||
{
|
||||
already_here = true;
|
||||
mute_elem.date = mute.date;
|
||||
}
|
||||
});
|
||||
if (!already_here)
|
||||
room_db.mutes.push(mute);
|
||||
}
|
||||
await this.chatroomRepository.save(room_db);
|
||||
|
||||
printCaller("-- out ");
|
||||
await this.chatroomRepository.save(my_room);
|
||||
}
|
||||
|
||||
|
||||
@@ -387,6 +421,21 @@ export class ChatService {
|
||||
return "successfully leaving room";
|
||||
}
|
||||
|
||||
async removeMute(username: string, room_name: string): Promise<void>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
const room_db = await this.getRoomByName(room_name);
|
||||
|
||||
let index = room_db.mutes.findIndex(mute => mute.name === username);
|
||||
if (index > -1)
|
||||
room_db.mutes.splice(index, 1);
|
||||
|
||||
await this.chatroomRepository.save(room_db);
|
||||
|
||||
printCaller("-- out ");
|
||||
}
|
||||
|
||||
|
||||
/* SEARCH IN USER *****************************************
|
||||
*/
|
||||
@@ -455,7 +504,6 @@ export class ChatService {
|
||||
|
||||
let room_name = await this.getCurrentRoomName(socket.username);
|
||||
|
||||
/*
|
||||
const current_room = await this.getRoomByName(room_name);
|
||||
|
||||
if (current_room.protection)
|
||||
@@ -466,7 +514,23 @@ export class ChatService {
|
||||
return;
|
||||
}
|
||||
}
|
||||
*/
|
||||
if (current_room.mutes)
|
||||
{
|
||||
let current_mute = current_room.mutes.find(mute => mute.name === socket.username);
|
||||
if (current_mute)
|
||||
{
|
||||
const date_now = new Date();
|
||||
const date_mute = new Date(current_mute.date);
|
||||
const date_diff = date_mute.getTime() - date_now.getTime();
|
||||
if (date_diff > 0)
|
||||
{
|
||||
socket.emit('message', "SERVER", "your message was not sent because you are muted");
|
||||
return;
|
||||
}
|
||||
else
|
||||
await this.removeMute(current_mute.name, room_name);
|
||||
}
|
||||
}
|
||||
|
||||
socket.to(socket.room).emit('message', socket.username, message);
|
||||
await this.addMessageToRoom(room_name, socket.username, message);
|
||||
|
||||
13
srcs/requirements/nestjs/api_back/src/chat/dto/mute.dto.ts
Normal file
13
srcs/requirements/nestjs/api_back/src/chat/dto/mute.dto.ts
Normal file
@@ -0,0 +1,13 @@
|
||||
import { IsNotEmpty, IsString, IsDate } from "class-validator";
|
||||
|
||||
export class muteDto
|
||||
{
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
name: string;
|
||||
|
||||
@IsDate()
|
||||
date: Date;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
import { Entity, Column, ManyToOne, ManyToMany, JoinTable, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { Entity, Column, CreateDateColumn, ManyToOne, ManyToMany, JoinTable, PrimaryGeneratedColumn } from "typeorm";
|
||||
import { IsBoolean, IsEmpty, IsInt, IsIn, IsNotEmpty, IsNumber, IsArray, IsString, IsOptional, IsEnum } from "class-validator";
|
||||
import { Exclude, Expose } from 'class-transformer';
|
||||
import { User } from 'src/users/entities/user.entity';
|
||||
import { messagesDto } from 'src/chat/dto/messages.dto';
|
||||
import { muteDto } from 'src/chat/dto/mute.dto';
|
||||
|
||||
@Entity('chatroom')
|
||||
export class Chatroom
|
||||
@@ -50,5 +51,9 @@ export class Chatroom
|
||||
|
||||
@Column("json")
|
||||
messages: messagesDto[];
|
||||
|
||||
@Column("json", { nullable: true })
|
||||
@IsOptional()
|
||||
mutes?: muteDto[];
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user