Merge branch 'master' into luke
This commit is contained in:
10
Makefile
10
Makefile
@@ -24,5 +24,15 @@ destroy:
|
|||||||
- docker images -aq | xargs --no-run-if-empty docker rmi -f
|
- docker images -aq | xargs --no-run-if-empty docker rmi -f
|
||||||
- docker volume ls -q | xargs --no-run-if-empty docker volume rm
|
- docker volume ls -q | xargs --no-run-if-empty docker volume rm
|
||||||
|
|
||||||
|
# temp for hugo, only reinit database
|
||||||
|
db:
|
||||||
|
- docker rm -f postgresql
|
||||||
|
- docker rm -f nestjs
|
||||||
|
- docker volume rm -f srcs_data_nest_postgresql
|
||||||
|
docker compose -f ${DOCKERCOMPOSEPATH} up -d --build
|
||||||
|
@make start
|
||||||
|
@docker ps
|
||||||
|
|
||||||
|
|
||||||
stop:
|
stop:
|
||||||
docker compose -f ${DOCKERCOMPOSEPATH} stop
|
docker compose -f ${DOCKERCOMPOSEPATH} stop
|
||||||
|
|||||||
29
README.md
29
README.md
@@ -1,4 +1,3 @@
|
|||||||
CONFLICT srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts
|
|
||||||
|
|
||||||
|
|
||||||
### Pour lancer le docker :
|
### Pour lancer le docker :
|
||||||
@@ -69,15 +68,25 @@ CONFLICT srcs/requirements/nestjs/api_back/src/friendship/friendship.service.ts
|
|||||||
|
|
||||||
#### chat :
|
#### chat :
|
||||||
|
|
||||||
- [ ] can create chat-rooms (public/private, password protected)
|
- [/] create public room
|
||||||
- [ ] send direct messages
|
- [ ] create private room
|
||||||
- [ ] block other users
|
- [/] create direct room
|
||||||
- [ ] creators of chat-room are owners, untill they leave
|
- [/] chat in room
|
||||||
- [ ] chat-room owner can set, change, remove password
|
- [/] join public rooms
|
||||||
- [ ] chat-room owner is administrator and can set other administrators
|
- [ ] join private rooms
|
||||||
- [ ] administrators can ban or mute for a time other users
|
- [ ] join direct rooms
|
||||||
- [ ] send game invitation in chat
|
- [/] see all joignable rooms
|
||||||
- [ ] view user profiles from chat
|
- [/] see all my rooms
|
||||||
|
- [/] leave room
|
||||||
|
- [ ] leave direct
|
||||||
|
- [ ] invite someone in room
|
||||||
|
- [ ] make admin
|
||||||
|
- [ ] ban
|
||||||
|
- [ ] mute
|
||||||
|
- [ ] protect room with password
|
||||||
|
- [ ] bock users
|
||||||
|
- [ ] send game invitation
|
||||||
|
- [ ] view user profiles
|
||||||
|
|
||||||
#### game :
|
#### game :
|
||||||
|
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ RESET="\033[0m"
|
|||||||
function make_env_for_docker_and_svelte
|
function make_env_for_docker_and_svelte
|
||||||
{
|
{
|
||||||
docker rm -f postgresql
|
docker rm -f postgresql
|
||||||
|
docker rm -f nestjs
|
||||||
docker volume rm -f srcs_data_nest_postgresql
|
docker volume rm -f srcs_data_nest_postgresql
|
||||||
echo -e "${BOLD_BLUE}Creating a new environment for docker${RESET}"
|
echo -e "${BOLD_BLUE}Creating a new environment for docker${RESET}"
|
||||||
NODE_ENV=""
|
NODE_ENV=""
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ import { Response } from 'express';
|
|||||||
import { TwoFaDto } from './dto/2fa.dto';
|
import { TwoFaDto } from './dto/2fa.dto';
|
||||||
import { UsersService } from 'src/users/users.service';
|
import { UsersService } from 'src/users/users.service';
|
||||||
import { User } from 'src/users/entities/user.entity';
|
import { User } from 'src/users/entities/user.entity';
|
||||||
|
import { STATUS } from 'src/common/constants/constants';
|
||||||
|
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
export class AuthenticationController {
|
export class AuthenticationController {
|
||||||
@@ -36,6 +37,7 @@ export class AuthenticationController {
|
|||||||
console.log('On redirige');
|
console.log('On redirige');
|
||||||
const user : User = request.user
|
const user : User = request.user
|
||||||
if (user.isEnabledTwoFactorAuth === false || user.isTwoFactorAuthenticated === true){
|
if (user.isEnabledTwoFactorAuth === false || user.isTwoFactorAuthenticated === true){
|
||||||
|
this.userService.updateStatus(user.id, STATUS.CONNECTED)
|
||||||
console.log('ON VA VERS PROFILE');
|
console.log('ON VA VERS PROFILE');
|
||||||
return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/profile');
|
return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/profile');
|
||||||
}
|
}
|
||||||
@@ -51,7 +53,7 @@ export class AuthenticationController {
|
|||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
logout(@Req() request, @Res() response, @Next() next) {
|
logout(@Req() request, @Res() response, @Next() next) {
|
||||||
this.userService.setIsTwoFactorAuthenticatedWhenLogout(request.user.id);
|
this.userService.setIsTwoFactorAuthenticatedWhenLogout(request.user.id);
|
||||||
this.userService.updateStatus(request.user.id, 'disconnected');
|
this.userService.updateStatus(request.user.id, STATUS.DISCONNECTED);
|
||||||
request.logout(function(err) {
|
request.logout(function(err) {
|
||||||
if (err) { return next(err); }
|
if (err) { return next(err); }
|
||||||
response.redirect('/');
|
response.redirect('/');
|
||||||
@@ -83,6 +85,7 @@ export class AuthenticationController {
|
|||||||
throw new UnauthorizedException('Wrong Code.');
|
throw new UnauthorizedException('Wrong Code.');
|
||||||
await this.userService.authenticateUserWith2FA(request.user.id);
|
await this.userService.authenticateUserWith2FA(request.user.id);
|
||||||
console.log('ON REDIRIGE');
|
console.log('ON REDIRIGE');
|
||||||
|
this.userService.updateStatus(user.id, STATUS.CONNECTED)
|
||||||
return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/profile');
|
return response.status(200).redirect('http://' + process.env.WEBSITE_HOST + ':' + process.env.WEBSITE_PORT + '/#/profile');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,84 +1,147 @@
|
|||||||
import { Controller, UseGuards, HttpException, HttpStatus, Get, Post, Body, Req, Res } from '@nestjs/common';
|
import { Controller, UseGuards, HttpException, HttpStatus, Get, Post, Delete, Body, Req, Res } from '@nestjs/common';
|
||||||
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
import { AuthenticateGuard, TwoFactorGuard } from 'src/auth/42/guards/42guards';
|
||||||
import { ConnectedSocket } from '@nestjs/websockets';
|
import { ConnectedSocket } from '@nestjs/websockets';
|
||||||
import { ChatService } from './chat.service';
|
import { ChatService } from './chat.service';
|
||||||
import { User } from 'src/users/entities/user.entity';
|
import { User } from 'src/users/entities/user.entity';
|
||||||
import { PartialUsersDto } from 'src/users/dto/partial-users.dto';
|
import { PartialUsersDto } from 'src/users/dto/partial-users.dto';
|
||||||
import { createRoomDto } from './dto/createRoom.dto';
|
import { roomDto } from './dto/room.dto';
|
||||||
import { joinRoomDto } from './dto/joinRoom.dto';
|
|
||||||
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
|
import { setCurrentRoomDto } from './dto/setCurrentRoom.dto';
|
||||||
|
import { ChatGateway } from './chat.gateway';
|
||||||
|
import { socketDto } from './dto/socket.dto';
|
||||||
|
import { Chatroom } from './entities/chatroom.entity';
|
||||||
|
|
||||||
@Controller('chat')
|
@Controller('chat')
|
||||||
export class ChatController {
|
export class ChatController {
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private chatService: ChatService,
|
private chatService: ChatService,
|
||||||
|
private chatGateway: ChatGateway,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
// don't allow '+' because it's used in direct rooms name
|
||||||
|
private allowed_chars = '-#!?_';
|
||||||
|
private escape_chars(str)
|
||||||
|
{
|
||||||
|
return str.split("").join("\\");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Get('myrooms')
|
@Get('myrooms')
|
||||||
async getMyRooms(@Req() req, @Res() res): Promise<object>
|
async getMyRooms(@Req() req, @Res() res): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("- in getMyRooms controller");
|
console.log("- in getMyRooms controller");
|
||||||
const rooms = await this.chatService.getMyRooms(req.user);
|
|
||||||
|
let fields = ["name", "type", "users"];
|
||||||
|
const rooms = await this.chatService.getMyRooms(req.user.username, fields);
|
||||||
|
|
||||||
|
res.status(HttpStatus.OK).json({ rooms: rooms });
|
||||||
|
|
||||||
console.log("- out getMyRooms controller");
|
console.log("- out getMyRooms controller");
|
||||||
return res.status(HttpStatus.OK).json({ rooms: rooms });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Get('allrooms')
|
@Get('allrooms')
|
||||||
async getAllRooms(@Req() req, @Res() res): Promise<object>
|
async getAllRooms(@Req() req, @Res() res): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("- in getAllRooms controller");
|
console.log("- in getAllRooms controller");
|
||||||
const rooms = await this.chatService.getAllNotMyRooms(req.user);
|
|
||||||
|
const rooms: roomDto[] = await this.chatService.getAllOtherRoomsAndUsers(req.user.username)
|
||||||
|
console.log("--- rooms:", rooms);
|
||||||
|
res.status(HttpStatus.OK).json({ rooms: rooms });
|
||||||
|
|
||||||
console.log("- out getAllRooms controller");
|
console.log("- out getAllRooms controller");
|
||||||
return res.status(HttpStatus.OK).json({ rooms: rooms });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Get('current')
|
@Get('current')
|
||||||
async setCurrentRoom(@Body() setCurrentRoomDto: setCurrentRoomDto, @Req() req, @Res() res): Promise<object>
|
async setCurrentRoom(@Body() setCurrentRoomDto: setCurrentRoomDto, @Req() req, @Res() res): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("- in setCurrentRoom controller");
|
console.log("- in setCurrentRoom controller");
|
||||||
const response = await this.chatService.setCurrentRoom(req.user, setCurrentRoomDto.name);
|
|
||||||
|
const response = await this.chatService.setCurrentRoom(req.user.username, setCurrentRoomDto.name);
|
||||||
|
res.status(HttpStatus.OK).json({ message: response });
|
||||||
|
|
||||||
console.log("- out setCurrentRoom controller");
|
console.log("- out setCurrentRoom controller");
|
||||||
return res.status(HttpStatus.OK).json({ message: response });
|
}
|
||||||
|
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
@Get('allowedchars')
|
||||||
|
async allowedChars(@Res() res): Promise<void>
|
||||||
|
{
|
||||||
|
console.log("- in allowedChars controller");
|
||||||
|
|
||||||
|
res.status(HttpStatus.OK).json({ chars: this.allowed_chars });
|
||||||
|
|
||||||
|
console.log("- out allowedChars controller");
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Post('create')
|
@Post('create')
|
||||||
async createRoom(@Body() createRoomDto: createRoomDto, @Req() req, @Res() res): Promise<object>
|
async createRoom(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("- in createRoom controller");
|
console.log("- in createRoom controller");
|
||||||
const response = await this.chatService.addUserToNewRoom(req.user, createRoomDto);
|
|
||||||
|
let chars = this.escape_chars(this.allowed_chars);
|
||||||
|
let regex_base = `[a-zA-Z0-9\\s${chars}]`;
|
||||||
|
let test_regex = new RegExp(`^${regex_base}+$`);
|
||||||
|
if (test_regex.test(room.name) === false)
|
||||||
|
{
|
||||||
|
let forbidden_chars = room.name.replace(new RegExp(regex_base, "g"), "");
|
||||||
|
throw new HttpException(`Your room name can not contains these characters : ${forbidden_chars}`, HttpStatus.UNPROCESSABLE_ENTITY);
|
||||||
|
}
|
||||||
|
|
||||||
|
await this.chatService.addUserToNewRoom(req.user.username, room);
|
||||||
|
res.status(HttpStatus.OK).json({ room: room });
|
||||||
|
|
||||||
console.log("- out createRoom controller");
|
console.log("- out createRoom controller");
|
||||||
return res.status(HttpStatus.OK).json({ room_name: createRoomDto.room_name, message: response });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Post('join')
|
@Post('join')
|
||||||
async joinRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res): Promise<object>
|
async joinRoom(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("- in joinRoom controller");
|
console.log("- in joinRoom controller");
|
||||||
const response = await this.chatService.addUserToRoom(req.user, joinRoomDto);
|
|
||||||
|
let response = "";
|
||||||
|
if (room.type === 'direct')
|
||||||
|
throw new HttpException(`cannot join a direct messages room`, HttpStatus.CONFLICT);
|
||||||
|
else if (room.type === 'user')
|
||||||
|
{
|
||||||
|
room.type = 'direct';
|
||||||
|
room.users = [room.name, req.user.username];
|
||||||
|
room.name += ` + ${req.user.username}`;
|
||||||
|
await this.chatService.addUserToNewRoom(req.user.username, room);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
await this.chatService.addUserToRoom(req.user.username, room.name);
|
||||||
|
|
||||||
|
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||||
|
await this.chatService.socketJoinRoom(socket, room.name);
|
||||||
|
res.status(HttpStatus.OK).json({ room: room });
|
||||||
|
|
||||||
console.log("- out joinRoom controller");
|
console.log("- out joinRoom controller");
|
||||||
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Post('change')
|
@Post('change')
|
||||||
async changeRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res): Promise<object>
|
async changeRoom(@Body() room: roomDto, @Req() req, @Res() res): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("- in changeRoom controller");
|
console.log("- in changeRoom controller");
|
||||||
const response = await this.chatService.setCurrentRoom(req.user, joinRoomDto.room_name);
|
|
||||||
|
const response = await this.chatService.setCurrentRoom(req.user.username, room.name);
|
||||||
|
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||||
|
await this.chatService.socketChangeRoom(socket, room.name);
|
||||||
|
res.status(HttpStatus.OK).json({ room: room });
|
||||||
|
|
||||||
console.log("- out changeRoom controller");
|
console.log("- out changeRoom controller");
|
||||||
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@@ -93,12 +156,43 @@ export class ChatController {
|
|||||||
@UseGuards(AuthenticateGuard)
|
@UseGuards(AuthenticateGuard)
|
||||||
@UseGuards(TwoFactorGuard)
|
@UseGuards(TwoFactorGuard)
|
||||||
@Get('messages')
|
@Get('messages')
|
||||||
async getMessages(@Req() req, @Res() res): Promise<object>
|
async getMessages(@Req() req, @Res() res): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("- in getMessages controller");
|
console.log("- in getMessages controller");
|
||||||
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user);
|
|
||||||
|
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username);
|
||||||
|
res.status(HttpStatus.OK).json({ messages: messages });
|
||||||
|
|
||||||
console.log("- out getMessages controller");
|
console.log("- out getMessages controller");
|
||||||
return res.status(HttpStatus.OK).json({ messages: messages });
|
}
|
||||||
|
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
@Get('roomusers')
|
||||||
|
async getRoomUsers(@Req() req, @Res() res): Promise<void>
|
||||||
|
{
|
||||||
|
console.log("- in getRoomUsers controller");
|
||||||
|
|
||||||
|
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||||
|
const room = await this.chatService.getRoomByName(room_name);
|
||||||
|
const users = room.users;
|
||||||
|
res.status(HttpStatus.OK).json({ users: users });
|
||||||
|
|
||||||
|
console.log("- out getRoomUsers controller");
|
||||||
|
}
|
||||||
|
|
||||||
|
@UseGuards(AuthenticateGuard)
|
||||||
|
@UseGuards(TwoFactorGuard)
|
||||||
|
@Delete('removeuser')
|
||||||
|
async removeUser(@Req() req, @Res() res): Promise<void>
|
||||||
|
{
|
||||||
|
console.log("- in removeUser controller");
|
||||||
|
|
||||||
|
const room_name = await this.chatService.getCurrentRoomName(req.user.username);
|
||||||
|
let response = await this.chatService.removeUserFromRoom(req.user.username, room_name);
|
||||||
|
res.status(HttpStatus.OK).json({ message: response });
|
||||||
|
|
||||||
|
console.log("- out removeUser controller");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
import { WebSocketGateway, SubscribeMessage, WebSocketServer, MessageBody, ConnectedSocket, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
|
import { WebSocketGateway, SubscribeMessage, WebSocketServer, MessageBody, ConnectedSocket, OnGatewayConnection, OnGatewayDisconnect } from '@nestjs/websockets';
|
||||||
import { UsersService } from 'src/users/users.service';
|
import { UsersService } from 'src/users/users.service';
|
||||||
import { PaginationQueryDto } from 'src/common/dto/pagination-query.dto';
|
|
||||||
import { ChatService } from './chat.service';
|
import { ChatService } from './chat.service';
|
||||||
|
import { socketDto } from './dto/socket.dto';
|
||||||
|
|
||||||
@WebSocketGateway(5000, {
|
@WebSocketGateway(5000, {
|
||||||
path: '/chat',
|
path: '/chat',
|
||||||
})
|
})
|
||||||
|
|
||||||
export class ChatGateway
|
export class ChatGateway
|
||||||
implements OnGatewayConnection, OnGatewayDisconnect
|
implements OnGatewayConnection, OnGatewayDisconnect
|
||||||
{
|
{
|
||||||
constructor
|
constructor
|
||||||
(
|
(
|
||||||
@@ -16,40 +16,44 @@ export class ChatGateway
|
|||||||
private chatService: ChatService,
|
private chatService: ChatService,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
|
sockets = new Map<string, socketDto>();
|
||||||
|
|
||||||
@WebSocketServer()
|
@WebSocketServer()
|
||||||
server;
|
server;
|
||||||
|
|
||||||
|
async handleConnection(socket: socketDto) {
|
||||||
// how to guard the handleConnection ?
|
console.log('- socket connected :', socket.id, socket.handshake.query.username);
|
||||||
// https://github.com/nestjs/nest/issues/882
|
socket.username = socket.handshake.query.username.toString();
|
||||||
async handleConnection(client) {
|
this.sockets.set(socket.username, socket);
|
||||||
console.log('- Client connected :', client.id, client.handshake.query.username);
|
|
||||||
client.username = client.handshake.query.username;
|
|
||||||
}
|
}
|
||||||
async handleDisconnect(client) {
|
async handleDisconnect(socket: socketDto) {
|
||||||
console.log('- Client disconnected :', client.id, client.username);
|
this.sockets.delete(socket.username);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('join')
|
@SubscribeMessage('join')
|
||||||
async joinRoom(@ConnectedSocket() socket, @MessageBody() room_name: string): Promise<void>
|
async joinRoom(@ConnectedSocket() socket: socketDto, @MessageBody() room_name: string): Promise<void>
|
||||||
{
|
{
|
||||||
console.log('- in joinRoom gateway');
|
console.log('- in joinRoom gateway');
|
||||||
socket.leave(socket.room);
|
socket.leave(socket.room);
|
||||||
socket.join(room_name);
|
socket.join(room_name);
|
||||||
socket.room = room_name;
|
socket.room = room_name;
|
||||||
|
let message = `${socket.username} has join the room`;
|
||||||
|
await socket.to(socket.room).emit('message', "SERVER", message);
|
||||||
|
await this.chatService.addMessageToRoom(room_name, "SERVER", message);
|
||||||
|
}
|
||||||
|
|
||||||
console.log('- out joinRoom gateway');
|
@SubscribeMessage('change')
|
||||||
|
async changeRoom(@ConnectedSocket() socket: socketDto, @MessageBody() room_name: string): Promise<void>
|
||||||
|
{
|
||||||
|
console.log('- in changeRoom gateway');
|
||||||
|
await this.chatService.socketChangeRoom(socket, room_name);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SubscribeMessage('message')
|
@SubscribeMessage('message')
|
||||||
async handleMessage(@ConnectedSocket() socket, @MessageBody() message: string): Promise<void>
|
async handleMessage(@ConnectedSocket() socket: socketDto, @MessageBody() message: string): Promise<void>
|
||||||
{
|
{
|
||||||
console.log('- in handleMessage gateway');
|
console.log('- in handleMessage gateway');
|
||||||
//let room_name = await this.chatService.getCurrentRoom(socket.username);
|
await this.chatService.socketIncommingMessage(socket, message);
|
||||||
socket.to(socket.room).emit('message', socket.username, message);
|
|
||||||
this.chatService.addMessageToCurrentRoom(socket.username, message);
|
|
||||||
|
|
||||||
console.log('- out handleMessage gateway');
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,6 @@ import { ChatController } from './chat.controller';
|
|||||||
import { ChatService } from './chat.service';
|
import { ChatService } from './chat.service';
|
||||||
import { ChatGateway } from './chat.gateway';
|
import { ChatGateway } from './chat.gateway';
|
||||||
import { UsersModule } from 'src/users/users.module';
|
import { UsersModule } from 'src/users/users.module';
|
||||||
|
|
||||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
import { Chatroom } from './entities/chatroom.entity';
|
import { Chatroom } from './entities/chatroom.entity';
|
||||||
import { User } from 'src/users/entities/user.entity';
|
import { User } from 'src/users/entities/user.entity';
|
||||||
|
|||||||
@@ -4,9 +4,10 @@ import { UsersService } from 'src/users/users.service';
|
|||||||
import { Chatroom } from './entities/chatroom.entity';
|
import { Chatroom } from './entities/chatroom.entity';
|
||||||
import { Repository } from 'typeorm';
|
import { Repository } from 'typeorm';
|
||||||
import { InjectRepository } from '@nestjs/typeorm';
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
import { createRoomDto } from './dto/createRoom.dto';
|
import { roomDto } from './dto/room.dto';
|
||||||
import { joinRoomDto } from './dto/joinRoom.dto';
|
|
||||||
import { messagesDto } from './dto/messages.dto';
|
import { messagesDto } from './dto/messages.dto';
|
||||||
|
import { socketDto } from './dto/socket.dto';
|
||||||
|
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ChatService {
|
export class ChatService {
|
||||||
@@ -17,8 +18,7 @@ export class ChatService {
|
|||||||
private readonly userRepository: Repository<User>,
|
private readonly userRepository: Repository<User>,
|
||||||
@InjectRepository(Chatroom)
|
@InjectRepository(Chatroom)
|
||||||
private readonly chatroomRepository: Repository<Chatroom>,
|
private readonly chatroomRepository: Repository<Chatroom>,
|
||||||
) { }
|
) {}
|
||||||
|
|
||||||
|
|
||||||
// temp for test
|
// temp for test
|
||||||
sleep(ms) {
|
sleep(ms) {
|
||||||
@@ -29,86 +29,140 @@ export class ChatService {
|
|||||||
/* GETTERS ************************************************
|
/* GETTERS ************************************************
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async getMyRooms(user: User)
|
async getMyRooms(username: string, fieldsToReturn: string[] = null): Promise<Chatroom[]>
|
||||||
{
|
{
|
||||||
console.log("-- in getMyRooms service");
|
console.log("-- in getMyRooms service");
|
||||||
const rooms = await this.chatroomRepository
|
|
||||||
|
const queryBuilder = this.chatroomRepository
|
||||||
.createQueryBuilder('chatroom')
|
.createQueryBuilder('chatroom')
|
||||||
.where('chatroom.users LIKE :user_name', { user_name: `%${user.username}%` })
|
.where('chatroom.users LIKE :user_name', { user_name: `%${username}%` });
|
||||||
.getMany();
|
|
||||||
|
if (fieldsToReturn)
|
||||||
|
{
|
||||||
|
let fields = fieldsToReturn.map(field => `chatroom.${field}`);
|
||||||
|
queryBuilder.select(fields);
|
||||||
|
}
|
||||||
|
|
||||||
|
const rooms = await queryBuilder.getMany();
|
||||||
|
console.log("--- rooms:", rooms);
|
||||||
|
|
||||||
console.log("-- out getMyRooms service");
|
console.log("-- out getMyRooms service");
|
||||||
return rooms;
|
return rooms;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllRooms()
|
async getMyDirects(username: string): Promise<Chatroom[]>
|
||||||
|
{
|
||||||
|
console.log("-- in getAllNotMyRooms service");
|
||||||
|
|
||||||
|
const my_rooms = await this.getMyRooms(username);
|
||||||
|
const directs = my_rooms.filter(room => room.type === 'direct');
|
||||||
|
|
||||||
|
console.log("-- out getAllNotMyRooms service");
|
||||||
|
return directs;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getAllRooms(): Promise<Chatroom[]>
|
||||||
{
|
{
|
||||||
console.log("-- in getAllRooms service");
|
console.log("-- in getAllRooms service");
|
||||||
|
|
||||||
const rooms = await this.chatroomRepository
|
const rooms = await this.chatroomRepository
|
||||||
.createQueryBuilder('chatroom')
|
.createQueryBuilder('chatroom')
|
||||||
.getMany();
|
.getMany();
|
||||||
|
console.log("--- rooms:", rooms);
|
||||||
|
|
||||||
console.log("-- out getAllRooms service");
|
console.log("-- out getAllRooms service");
|
||||||
return rooms;
|
return rooms;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getAllNotMyRooms(user: User)
|
async getAllNotMyRooms(username: string): Promise<Chatroom[]>
|
||||||
{
|
{
|
||||||
console.log("-- in getAllNotMyRooms service");
|
console.log("-- in getAllNotMyRooms service");
|
||||||
const user_db = await this.getUserByName(user.username);
|
|
||||||
//const user_db = await this.usersService.findOne(user.username);
|
const user_db = await this.getUserByName(username);
|
||||||
const rooms = await this.chatroomRepository
|
const rooms = await this.chatroomRepository
|
||||||
.createQueryBuilder('chatroom')
|
.createQueryBuilder('chatroom')
|
||||||
.where('chatroom.type != :type', { type: 'private' })
|
.where('chatroom.type != :type', { type: 'private' })
|
||||||
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${user.username}%` })
|
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
|
||||||
.getMany();
|
.getMany();
|
||||||
//const users = await this.getAllUsers();
|
console.log("--- rooms:", rooms);
|
||||||
//let allRooms = [...rooms, ...users];
|
|
||||||
|
|
||||||
console.log("-- out getAllNotMyRooms service");
|
console.log("-- out getAllNotMyRooms service");
|
||||||
return rooms;
|
return rooms;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getMessagesFromCurrentRoom(user: User)
|
async getAllOtherRoomsAndUsers(username: string): Promise<roomDto[]>
|
||||||
{
|
{
|
||||||
console.log("-- in getMessagesFromCurrentRoom service");
|
console.log("-- in getAllOtherRoomsAndUsers service");
|
||||||
const user_db = await this.getUserByName(user.username);
|
|
||||||
//const user_db = await this.usersService.findOne(user.username);
|
|
||||||
const currentRoom = await this.getRoomByName(user_db.currentRoom);
|
|
||||||
|
|
||||||
console.log("-- out getMessagesFromCurrentRoom service");
|
const all_rooms = await this.getAllNotMyRooms(username);
|
||||||
return currentRoom.messages;
|
const all_users = await this.getAllUsersNotMyRooms(username);
|
||||||
|
|
||||||
|
let row_rooms = all_rooms.map(room => {
|
||||||
|
return {
|
||||||
|
name: room.name,
|
||||||
|
type: room.type,
|
||||||
|
};
|
||||||
|
});
|
||||||
|
let users = all_users.map(user => {
|
||||||
|
return {
|
||||||
|
name: user.username,
|
||||||
|
type: "user",
|
||||||
|
};
|
||||||
|
});
|
||||||
|
let rooms = row_rooms.concat(users);
|
||||||
|
console.log("--- rooms:", rooms);
|
||||||
|
|
||||||
|
console.log("-- in getAllOtherRoomsAndUsers service");
|
||||||
|
return rooms;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getCurrentRoom(username: string)
|
async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]>
|
||||||
{
|
{
|
||||||
console.log("-- in getCurrentRoom service");
|
console.log("-- in getMessagesFromCurrentRoom service");
|
||||||
const user_db = await this.getUserByName(username);
|
|
||||||
//const user_db = await this.usersService.findOne(username);
|
|
||||||
|
|
||||||
console.log("-- out getCurrentRoom service");
|
const user_db = await this.getUserByName(username);
|
||||||
|
const currentRoom = await this.getRoomByName(user_db.currentRoom);
|
||||||
|
let messages = null;
|
||||||
|
if (currentRoom)
|
||||||
|
messages = currentRoom.messages;
|
||||||
|
|
||||||
|
console.log("-- out getMessagesFromCurrentRoom service");
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getCurrentRoomName(username: string): Promise<string>
|
||||||
|
{
|
||||||
|
console.log("-- in getCurrentRoomName service");
|
||||||
|
|
||||||
|
const user_db = await this.getUserByName(username);
|
||||||
|
|
||||||
|
console.log("-- out getCurrentRoomName service");
|
||||||
return user_db.currentRoom;
|
return user_db.currentRoom;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRoomByName(name: string)
|
async getRoomByName(room_name: string): Promise<Chatroom>
|
||||||
{
|
{
|
||||||
console.log("-- in getRoomByName service");
|
console.log("-- in getRoomByName service");
|
||||||
|
|
||||||
const room = await this.chatroomRepository
|
const room = await this.chatroomRepository
|
||||||
.createQueryBuilder('chatroom')
|
.createQueryBuilder('chatroom')
|
||||||
.where('chatroom.name = :name', { name: name })
|
.where('chatroom.name = :name', { name: room_name })
|
||||||
.getOne();
|
.getOne();
|
||||||
|
console.log("--- room:", room);
|
||||||
|
|
||||||
console.log("-- out getRoomByName service");
|
console.log("-- out getRoomByName service");
|
||||||
return room;
|
return room;
|
||||||
}
|
}
|
||||||
|
|
||||||
async getRoomById(id: number)
|
async getRoomById(id: number): Promise<Chatroom>
|
||||||
{
|
{
|
||||||
console.log("-- in getRoomById service");
|
console.log("-- in getRoomById service");
|
||||||
|
|
||||||
const room = await this.chatroomRepository
|
const room = await this.chatroomRepository
|
||||||
.createQueryBuilder('chatroom')
|
.createQueryBuilder('chatroom')
|
||||||
.where('chatroom.id = :id', { id: id })
|
.where('chatroom.id = :id', { id: id })
|
||||||
.getOne();
|
.getOne();
|
||||||
|
console.log("--- room:", room);
|
||||||
|
|
||||||
console.log("-- out getRoomById service");
|
console.log("-- out getRoomById service");
|
||||||
return room;
|
return room;
|
||||||
@@ -118,102 +172,180 @@ export class ChatService {
|
|||||||
/* SETTERS ************************************************
|
/* SETTERS ************************************************
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async setCurrentRoom(user: User, name: string)
|
async setCurrentRoom(username: string, room_name: string): Promise<string>
|
||||||
{
|
{
|
||||||
console.log("-- in setCurrentRoom service");
|
console.log("-- in setCurrentRoom service");
|
||||||
const user_db = await this.getUserByName(user.username);
|
|
||||||
//const user_db = await this.usersService.findOne(user.username);
|
const user_db = await this.getUserByName(username);
|
||||||
user_db.currentRoom = name;
|
user_db.currentRoom = room_name;
|
||||||
this.userRepository.save(user_db);
|
this.userRepository.save(user_db);
|
||||||
|
|
||||||
console.log("-- out setCurrentRoom service");
|
console.log("-- out setCurrentRoom service");
|
||||||
return `room "${name}" is now current room`;
|
return `room "${room_name}" is now current room`;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* ADDERS *************************************************
|
/* ADDERS *************************************************
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async addUserToNewRoom(user: User, createRoomDto: createRoomDto)
|
async addUserToNewRoom(username: string, room: roomDto): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("-- in addUserToRoom service");
|
console.log("-- in addUserToNewRoom service");
|
||||||
const room = await this.getRoomByName(createRoomDto.room_name);
|
|
||||||
if (room)
|
const find_room = await this.getRoomByName(room.name);
|
||||||
throw new HttpException(`This room already exist`, HttpStatus.CONFLICT);
|
if (find_room)
|
||||||
|
throw new HttpException(`This room name already exist`, HttpStatus.CONFLICT);
|
||||||
|
|
||||||
// create chatroom
|
// create chatroom
|
||||||
const newChatroom = new Chatroom();
|
const newChatroom = new Chatroom();
|
||||||
newChatroom.name = createRoomDto.room_name;
|
newChatroom.name = room.name;
|
||||||
newChatroom.type = createRoomDto.room_type;
|
newChatroom.type = room.type;
|
||||||
newChatroom.owner = user.username;
|
newChatroom.owner = username;
|
||||||
newChatroom.users = [user.username];
|
newChatroom.users = [username];
|
||||||
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${createRoomDto.room_name}` }];
|
if (room.type === 'direct')
|
||||||
this.chatroomRepository.save(newChatroom);
|
newChatroom.users = room.users;
|
||||||
|
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${room.name}` }];
|
||||||
|
await this.chatroomRepository.save(newChatroom);
|
||||||
|
|
||||||
console.log("-- out addUserToRoom service");
|
console.log("-- out addUserToNewRoom service");
|
||||||
return "successfull room creation";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addUserToRoom(user: User, joinRoomDto: joinRoomDto)
|
async addUserToRoom(username: string, room_name: string): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("-- in addUserToRoom service");
|
console.log("-- in addUserToRoom service");
|
||||||
const room = await this.getRoomByName(joinRoomDto.room_name);
|
|
||||||
if (room.users.includes(user.username))
|
const room = await this.getRoomByName(room_name);
|
||||||
throw new HttpException(`your have already join this room`, HttpStatus.CONFLICT);
|
if (room.users.includes(username))
|
||||||
|
throw new HttpException(`your have already joined this room`, HttpStatus.CONFLICT);
|
||||||
|
|
||||||
// update room with new user
|
// update room with new user
|
||||||
room.users.push(user.username);
|
room.users.push(username);
|
||||||
this.chatroomRepository.save(room);
|
this.chatroomRepository.save(room);
|
||||||
|
|
||||||
const rooms = await this.getMyRooms(user);
|
|
||||||
const allRooms = await this.getAllRooms();
|
|
||||||
|
|
||||||
console.log("-- out addUserToRoom service");
|
console.log("-- out addUserToRoom service");
|
||||||
return "successfull joining room";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async addMessageToCurrentRoom(username: string, message: string)
|
async addMessageToRoom(room_name: string, username: string, message: string): Promise<void>
|
||||||
{
|
{
|
||||||
console.log("-- in addMessageToCurrentRoom service");
|
console.log("-- in addMessageToRoom service");
|
||||||
const user_db = await this.getUserByName(username);
|
|
||||||
//const user_db = await this.usersService.findOne(username);
|
const my_room = await this.getRoomByName(room_name);
|
||||||
const currentRoom = await this.getRoomByName(user_db.currentRoom);
|
|
||||||
let chat_message = {
|
let chat_message = {
|
||||||
name: username,
|
name: username,
|
||||||
message: message,
|
message: message,
|
||||||
};
|
};
|
||||||
currentRoom.messages.push(chat_message);
|
my_room.messages.push(chat_message);
|
||||||
this.chatroomRepository.save(currentRoom);
|
this.chatroomRepository.save(my_room);
|
||||||
|
|
||||||
console.log("-- out addMessageToCurrentRoom service");
|
console.log("-- out addMessageToRoom service");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* REMOVERS ***********************************************
|
/* REMOVERS ***********************************************
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async removeUserFromRoom(user: User, room_name: string)
|
async removeUserFromRoom(username: string, room_name: string): Promise<string>
|
||||||
{
|
{
|
||||||
console.log("-- in removeUserFromRoom service");
|
console.log("-- in removeUserFromRoom service");
|
||||||
// get room
|
|
||||||
// remove user
|
const room = await this.getRoomByName(room_name);
|
||||||
|
if (!room.users.includes(username))
|
||||||
|
throw new HttpException(`your are not in this room`, HttpStatus.CONFLICT);
|
||||||
|
|
||||||
|
// delete user from room
|
||||||
|
room.users.push(username);
|
||||||
|
room.users = room.users.filter(name => name !== username);
|
||||||
|
this.chatroomRepository.save(room);
|
||||||
|
|
||||||
|
// set current room to nothing
|
||||||
|
await this.setCurrentRoom(username, "");
|
||||||
|
|
||||||
|
console.log("-- out removeUserFromRoom service");
|
||||||
|
return "successfully leaving room";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* SEARCH IN USER *****************************************
|
/* SEARCH IN USER *****************************************
|
||||||
*/
|
*/
|
||||||
|
|
||||||
async getUserByName(name: string)
|
async getUserByName(username: string): Promise<User>
|
||||||
{
|
{
|
||||||
console.log("-- in getUserByName service");
|
console.log("-- in getUserByName service");
|
||||||
|
|
||||||
const user = await this.userRepository
|
const user = await this.userRepository
|
||||||
.createQueryBuilder('user')
|
.createQueryBuilder('user')
|
||||||
.where('user.username = :name', { name: name })
|
.where('user.username = :name', { name: username })
|
||||||
.getOne();
|
.getOne();
|
||||||
|
console.log("--- user:", user);
|
||||||
|
|
||||||
console.log("-- out getUserByName service");
|
console.log("-- out getUserByName service");
|
||||||
return user;
|
return user;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getAllUsersNotMyRooms(username: string): Promise<User[]>
|
||||||
|
{
|
||||||
|
console.log("-- in getAllUsersNotMyRooms service");
|
||||||
|
|
||||||
|
const directs = await this.getMyDirects(username);
|
||||||
|
|
||||||
|
// get all users from directs
|
||||||
|
let usernames = directs.map(room => {
|
||||||
|
let user = room.users[0];
|
||||||
|
if (user === username)
|
||||||
|
user = room.users[1];
|
||||||
|
return user;
|
||||||
|
});
|
||||||
|
usernames.push(username);
|
||||||
|
|
||||||
|
const users = await this.userRepository
|
||||||
|
.createQueryBuilder('user')
|
||||||
|
.where('user.username NOT IN (:...usernames)', { usernames: usernames })
|
||||||
|
.getMany();
|
||||||
|
console.log("--- users:", users);
|
||||||
|
|
||||||
|
console.log("-- out getAllUsersNotMyRooms service");
|
||||||
|
return users;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/* GATEWAY EVENTS *****************************************
|
||||||
|
*/
|
||||||
|
|
||||||
|
async socketIncommingMessage(socket: socketDto, message: string): Promise<void>
|
||||||
|
{
|
||||||
|
console.log("-- in handleSocketIncommingMessage service");
|
||||||
|
|
||||||
|
socket.to(socket.room).emit('message', socket.username, message);
|
||||||
|
let room_name = await this.getCurrentRoomName(socket.username);
|
||||||
|
await this.addMessageToRoom(room_name, socket.username, message);
|
||||||
|
|
||||||
|
console.log("-- out handleSocketIncommingMessage service");
|
||||||
|
}
|
||||||
|
|
||||||
|
async socketChangeRoom(socket: socketDto, room_name: string): Promise<void>
|
||||||
|
{
|
||||||
|
console.log('-- in socketChangeRoom service');
|
||||||
|
|
||||||
|
socket.leave(socket.room);
|
||||||
|
socket.join(room_name);
|
||||||
|
socket.room = room_name;
|
||||||
|
|
||||||
|
console.log('-- out socketChangeRoom service');
|
||||||
|
}
|
||||||
|
|
||||||
|
async socketJoinRoom(socket: socketDto, room_name: string): Promise<void>
|
||||||
|
{
|
||||||
|
console.log('- in socketJoinRoom service');
|
||||||
|
|
||||||
|
socket.leave(socket.room);
|
||||||
|
socket.join(room_name);
|
||||||
|
socket.room = room_name;
|
||||||
|
let message = `${socket.username} has join the room`;
|
||||||
|
await socket.to(socket.room).emit('message', "SERVER", message);
|
||||||
|
await this.addMessageToRoom(room_name, "SERVER", message);
|
||||||
|
|
||||||
|
console.log('- out socketJoinRoom service');
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,18 +0,0 @@
|
|||||||
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
|
|
||||||
import { IsNull } from "typeorm";
|
|
||||||
|
|
||||||
export class createRoomDto
|
|
||||||
{
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
room_name: string;
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsNotEmpty()
|
|
||||||
room_type: 'public' | 'private' | 'direct';
|
|
||||||
|
|
||||||
@IsString()
|
|
||||||
@IsOptional()
|
|
||||||
room_password: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,9 +1,13 @@
|
|||||||
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional, IsArray } from "class-validator";
|
import { IsString, IsOptional } from "class-validator";
|
||||||
import { IsNull } from "typeorm";
|
|
||||||
|
|
||||||
export class messagesDto
|
export class messagesDto
|
||||||
{
|
{
|
||||||
@IsArray()
|
@IsString()
|
||||||
messages: { name: string; message: string }[];
|
@IsOptional()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsOptional()
|
||||||
|
message: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
srcs/requirements/nestjs/api_back/src/chat/dto/room.dto.ts
Normal file
19
srcs/requirements/nestjs/api_back/src/chat/dto/room.dto.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
import { IsBoolean, IsEmpty, IsInt, IsIn, IsNotEmpty, IsNumber, IsArray, IsString, IsOptional, IsEnum } from "class-validator";
|
||||||
|
|
||||||
|
export class roomDto
|
||||||
|
{
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
name: string;
|
||||||
|
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsIn(["public", "protected", "private", "direct", "user"])
|
||||||
|
type: string;
|
||||||
|
|
||||||
|
@IsArray()
|
||||||
|
@IsString({ each: true })
|
||||||
|
@IsOptional()
|
||||||
|
users?: string[]; // usernames
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
|
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
|
||||||
import { IsNull } from "typeorm";
|
|
||||||
|
|
||||||
export class setCurrentRoomDto
|
export class setCurrentRoomDto
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -1,10 +1,13 @@
|
|||||||
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
|
import { IsBoolean, IsEmpty, IsInt, IsNotEmpty, IsNumber, IsString, IsOptional } from "class-validator";
|
||||||
import { IsNull } from "typeorm";
|
import { Socket } from 'socket.io';
|
||||||
|
|
||||||
export class joinRoomDto
|
export class socketDto extends Socket
|
||||||
{
|
{
|
||||||
@IsString()
|
@IsString()
|
||||||
@IsNotEmpty()
|
username: string;
|
||||||
room_name: string;
|
|
||||||
|
@IsString()
|
||||||
|
room: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -1,38 +1,36 @@
|
|||||||
import {
|
import { Entity, Column, ManyToOne, ManyToMany, JoinTable, PrimaryGeneratedColumn } from "typeorm";
|
||||||
Entity,
|
import { IsBoolean, IsEmpty, IsInt, IsIn, IsNotEmpty, IsNumber, IsArray, IsString, IsOptional, IsEnum } from "class-validator";
|
||||||
Column,
|
import { Exclude, Expose } from 'class-transformer';
|
||||||
ManyToOne,
|
|
||||||
ManyToMany,
|
|
||||||
JoinTable,
|
|
||||||
PrimaryGeneratedColumn
|
|
||||||
} from "typeorm";
|
|
||||||
import { User } from 'src/users/entities/user.entity';
|
import { User } from 'src/users/entities/user.entity';
|
||||||
|
import { messagesDto } from 'src/chat/dto/messages.dto';
|
||||||
|
|
||||||
@Entity('chatroom')
|
@Entity('chatroom')
|
||||||
export class Chatroom {
|
export class Chatroom
|
||||||
|
{
|
||||||
@PrimaryGeneratedColumn()
|
@PrimaryGeneratedColumn()
|
||||||
id: number;
|
id: number;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
name: string;
|
name: string;
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
|
@IsString()
|
||||||
|
@IsNotEmpty()
|
||||||
|
@IsIn(["public", "protected", "private", "direct", "user"])
|
||||||
type: string;
|
type: string;
|
||||||
|
|
||||||
// @ManyToOne(type => User, user => user.ownedRooms)
|
|
||||||
// owner: User;
|
|
||||||
//
|
|
||||||
// @ManyToMany(type => User)
|
|
||||||
// @JoinTable()
|
|
||||||
// users: User[];
|
|
||||||
|
|
||||||
@Column()
|
@Column()
|
||||||
owner: string; // name
|
owner: string; // username
|
||||||
|
|
||||||
@Column("simple-array")
|
@Column("simple-array")
|
||||||
users: string[]; // names
|
@IsArray()
|
||||||
|
@IsString({ each: true })
|
||||||
|
@IsOptional()
|
||||||
|
users?: string[]; // usernames
|
||||||
|
|
||||||
@Column("json")
|
@Column("json")
|
||||||
messages: { name: string, message: string }[];
|
messages: messagesDto[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -92,7 +92,6 @@ export class GameService {
|
|||||||
}
|
}
|
||||||
this.userRepository.save(user);
|
this.userRepository.save(user);
|
||||||
}
|
}
|
||||||
// if (grantTicketDto.isGameIsWithInvitation === true && user.status !== STATUS.IN_GAME) // WIP: need to fix STATUS.IN_GAME
|
|
||||||
if (grantTicketDto.isGameIsWithInvitation === true)
|
if (grantTicketDto.isGameIsWithInvitation === true)
|
||||||
{
|
{
|
||||||
const secondUser : Partial<User> = await this.userService.findOne(grantTicketDto.playerTwoUsername)
|
const secondUser : Partial<User> = await this.userService.findOne(grantTicketDto.playerTwoUsername)
|
||||||
@@ -106,6 +105,7 @@ export class GameService {
|
|||||||
tok.token = encryptedTextToReturn;
|
tok.token = encryptedTextToReturn;
|
||||||
this.tokenGameRepository.save(tok);
|
this.tokenGameRepository.save(tok);
|
||||||
this.userService.updateStatus(user.id, STATUS.IN_POOL)
|
this.userService.updateStatus(user.id, STATUS.IN_POOL)
|
||||||
|
this.userService.updateStatus(secondUser.id, STATUS.IN_POOL)
|
||||||
return res.status(HttpStatus.OK).json({ token : encryptedTextToReturn });
|
return res.status(HttpStatus.OK).json({ token : encryptedTextToReturn });
|
||||||
}
|
}
|
||||||
// else if (grantTicketDto.isGameIsWithInvitation === false && user.status !== STATUS.IN_GAME) { // WIP: need to fix STATUS.IN_GAME
|
// else if (grantTicketDto.isGameIsWithInvitation === false && user.status !== STATUS.IN_GAME) { // WIP: need to fix STATUS.IN_GAME
|
||||||
@@ -142,13 +142,11 @@ export class GameService {
|
|||||||
const userOne : User = await this.userRepository.createQueryBuilder('user')
|
const userOne : User = await this.userRepository.createQueryBuilder('user')
|
||||||
.where("user.username = :username", {username : tokenGame.playerOneUsername})
|
.where("user.username = :username", {username : tokenGame.playerOneUsername})
|
||||||
.getOne();
|
.getOne();
|
||||||
this.userService.updateStatus(userOne.id, STATUS.IN_GAME)
|
|
||||||
const userTwo : User = await this.userRepository.createQueryBuilder('user')
|
const userTwo : User = await this.userRepository.createQueryBuilder('user')
|
||||||
.where("user.username = :username", {username : tokenGame.playerTwoUsername})
|
.where("user.username = :username", {username : tokenGame.playerTwoUsername})
|
||||||
.getOne();
|
.getOne();
|
||||||
this.deleteToken(userOne)
|
this.deleteToken(userOne)
|
||||||
this.deleteToken(userTwo)
|
this.deleteToken(userTwo)
|
||||||
this.userService.updateStatus(userTwo.id, STATUS.IN_GAME)
|
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -168,7 +166,6 @@ export class GameService {
|
|||||||
const user : User = await this.userRepository.createQueryBuilder('user')
|
const user : User = await this.userRepository.createQueryBuilder('user')
|
||||||
.where("user.username = :username", {username : tokenGame.playerOneUsername})
|
.where("user.username = :username", {username : tokenGame.playerOneUsername})
|
||||||
.getOne();
|
.getOne();
|
||||||
this.userService.updateStatus(user.id, STATUS.IN_GAME)
|
|
||||||
this.deleteToken(user)
|
this.deleteToken(user)
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -259,6 +256,14 @@ export class GameService {
|
|||||||
this.gameRepository.save(game);
|
this.gameRepository.save(game);
|
||||||
if (!game)
|
if (!game)
|
||||||
return HttpStatus.INTERNAL_SERVER_ERROR
|
return HttpStatus.INTERNAL_SERVER_ERROR
|
||||||
|
const playerOne : User = await this.userRepository.createQueryBuilder('user')
|
||||||
|
.where("user.username = :username", {username : creategameDto.playerOneUsername})
|
||||||
|
.getOne();
|
||||||
|
const playerTwo : User = await this.userRepository.createQueryBuilder('user')
|
||||||
|
.where("user.username = :username", {username : creategameDto.playerTwoUsername})
|
||||||
|
.getOne();
|
||||||
|
this.userService.updateStatus(playerOne.id, STATUS.IN_GAME)
|
||||||
|
this.userService.updateStatus(playerTwo.id, STATUS.IN_GAME)
|
||||||
console.log("200 retourné pour la création de partie")
|
console.log("200 retourné pour la création de partie")
|
||||||
return HttpStatus.OK
|
return HttpStatus.OK
|
||||||
}
|
}
|
||||||
@@ -280,6 +285,8 @@ export class GameService {
|
|||||||
const playerTwo = await this.userRepository.findOneBy({username : game.playerTwoUsername})
|
const playerTwo = await this.userRepository.findOneBy({username : game.playerTwoUsername})
|
||||||
if (!playerOne || !playerTwo)
|
if (!playerOne || !playerTwo)
|
||||||
return new HttpException("Internal Server Error. Impossible to update the database", HttpStatus.INTERNAL_SERVER_ERROR);
|
return new HttpException("Internal Server Error. Impossible to update the database", HttpStatus.INTERNAL_SERVER_ERROR);
|
||||||
|
this.userService.updateStatus(playerOne.id, STATUS.CONNECTED)
|
||||||
|
this.userService.updateStatus(playerTwo.id, STATUS.CONNECTED)
|
||||||
if (game.playerOneUsernameResult === game.playerTwoUsernameResult)
|
if (game.playerOneUsernameResult === game.playerTwoUsernameResult)
|
||||||
{
|
{
|
||||||
this.userService.incrementDraws(playerOne.id)
|
this.userService.incrementDraws(playerOne.id)
|
||||||
@@ -296,8 +303,6 @@ export class GameService {
|
|||||||
this.userService.incrementVictories(playerOne.id)
|
this.userService.incrementVictories(playerOne.id)
|
||||||
this.userService.incrementDefeats(playerTwo.id)
|
this.userService.incrementDefeats(playerTwo.id)
|
||||||
}
|
}
|
||||||
this.userService.updateStatus(playerOne.id, STATUS.CONNECTED)
|
|
||||||
this.userService.updateStatus(playerTwo.id, STATUS.CONNECTED)
|
|
||||||
return HttpStatus.OK
|
return HttpStatus.OK
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -45,7 +45,6 @@ export class UsersService {
|
|||||||
isEnabledTwoFactorAuth: user.isEnabledTwoFactorAuth,
|
isEnabledTwoFactorAuth: user.isEnabledTwoFactorAuth,
|
||||||
status: user.status,
|
status: user.status,
|
||||||
stats: user.stats,
|
stats: user.stats,
|
||||||
currentRoom: user.currentRoom,
|
|
||||||
};
|
};
|
||||||
console.log(`Returned Partial User.` + partialUser.username + user.username);
|
console.log(`Returned Partial User.` + partialUser.username + user.username);
|
||||||
return partialUser;
|
return partialUser;
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
server {
|
|
||||||
listen 8080;
|
|
||||||
listen [::]:8080;
|
|
||||||
server_name localhost;
|
|
||||||
|
|
||||||
location /api/v2 {
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
|
||||||
proxy_pass http://backend_dev:3000;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /chat {
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
||||||
proxy_http_version 1.1;
|
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
|
||||||
proxy_set_header Connection "Upgrade";
|
|
||||||
proxy_pass http://backend_dev:5000/chat;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /api/v2/game/gameserver {
|
|
||||||
deny all;
|
|
||||||
}
|
|
||||||
|
|
||||||
location /pong {
|
|
||||||
proxy_pass http://game_server:8042/pong;
|
|
||||||
proxy_http_version 1.1;
|
|
||||||
proxy_set_header Upgrade $http_upgrade;
|
|
||||||
proxy_set_header Connection $connection_upgrade;
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
}
|
|
||||||
|
|
||||||
location / {
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
|
||||||
proxy_pass http://frontend_dev:8080;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
server {
|
|
||||||
listen 35729 default_server;
|
|
||||||
listen [::]:35729 default_server;
|
|
||||||
server_name localhost;
|
|
||||||
location / {
|
|
||||||
proxy_set_header Host $host;
|
|
||||||
proxy_set_header X-Real-IP $remote_addr;
|
|
||||||
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
|
|
||||||
proxy_set_header X-Forwarded-Proto $scheme;
|
|
||||||
proxy_pass http://frontend_dev:35729;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,6 @@
|
|||||||
|
export interface Room
|
||||||
|
{
|
||||||
|
name: string;
|
||||||
|
type: "public" | "protected" | "private" | "direct" | "user";
|
||||||
|
users?: string[];
|
||||||
|
}
|
||||||
@@ -1,12 +1,23 @@
|
|||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
|
||||||
import { msgs, layout } from './Store_chat';
|
import { msgs, layout, allowed_chars } from './Store_chat';
|
||||||
import { change_room, create_room } from './Request_rooms';
|
import { change_room, create_room } from './Request_rooms';
|
||||||
|
import { onMount } from 'svelte';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
import Warning from './Element_warning.svelte';
|
import Warning from './Element_warning.svelte';
|
||||||
|
|
||||||
export let back = "";
|
export let back = "";
|
||||||
|
|
||||||
|
let allowed_chars = 'loading...';
|
||||||
|
//let regex;
|
||||||
|
onMount(async() => {
|
||||||
|
let response = await fetch('/api/v2/chat/allowedchars');
|
||||||
|
let data = await response.json();
|
||||||
|
console.log("data:", data);
|
||||||
|
allowed_chars = data.chars;
|
||||||
|
//regex = new RegExp(`^[a-zA-Z0-9\\s${allowed_chars}]+$`);
|
||||||
|
});
|
||||||
|
|
||||||
let room_name: string;
|
let room_name: string;
|
||||||
let room_type: string;
|
let room_type: string;
|
||||||
let room_password: string;
|
let room_password: string;
|
||||||
@@ -22,12 +33,16 @@
|
|||||||
if (!formIsValid)
|
if (!formIsValid)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
let room = {
|
||||||
|
name: room_name,
|
||||||
|
type: room_type,
|
||||||
|
};
|
||||||
// send the new room
|
// send the new room
|
||||||
response = await create_room(room_name, room_type);
|
response = await create_room(room);
|
||||||
|
|
||||||
// go to room
|
// go to room
|
||||||
if (response.status === 200)
|
if (response.status === 200)
|
||||||
await change_room(room_name);
|
await change_room(response.room);
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
@@ -57,7 +72,10 @@
|
|||||||
{/if}
|
{/if}
|
||||||
<!-- name: -->
|
<!-- name: -->
|
||||||
<label for="chat_name"><p>new room name :</p></label>
|
<label for="chat_name"><p>new room name :</p></label>
|
||||||
<input id="chat_name" bind:value={room_name} name="room_name" required>
|
<!--
|
||||||
|
<input id="chat_name" bind:value={room_name} name="room_name" placeholder="allowed special characters: {allowed_chars}" pattern={regex} required>
|
||||||
|
-->
|
||||||
|
<input id="chat_name" bind:value={room_name} name="room_name" placeholder="allowed special characters: {allowed_chars}" required>
|
||||||
<!-- [ ] pubic -->
|
<!-- [ ] pubic -->
|
||||||
<label for="chat_public" class="_radio">
|
<label for="chat_public" class="_radio">
|
||||||
<p>public</p>
|
<p>public</p>
|
||||||
|
|||||||
@@ -1,17 +1,19 @@
|
|||||||
<script>
|
<script>
|
||||||
|
|
||||||
import { layout, msgs, user } from './Store_chat';
|
import { layout, msgs, user } from './Store_chat';
|
||||||
import { change_room, get_room_messages, get_all_rooms } from './Request_rooms';
|
import { change_room, get_room_messages, get_my_rooms } from './Request_rooms';
|
||||||
import { onMount } from 'svelte';
|
import { onMount } from 'svelte';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
|
|
||||||
let rooms = get_all_rooms();
|
let rooms = get_my_rooms();
|
||||||
|
|
||||||
// go to clicked room
|
// go to clicked room
|
||||||
async function go_to_room(evt)
|
async function go_to_room(room)
|
||||||
{
|
{
|
||||||
console.log("inside go_to_room");
|
console.log("inside go_to_room");
|
||||||
await change_room(evt.target.innerText);
|
|
||||||
|
console.log("room:", room);
|
||||||
|
await change_room(room);
|
||||||
await get_room_messages();
|
await get_room_messages();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -42,11 +44,10 @@
|
|||||||
<p class="__center">/ you have no chat room yet /</p>
|
<p class="__center">/ you have no chat room yet /</p>
|
||||||
</div>
|
</div>
|
||||||
{#await rooms}
|
{#await rooms}
|
||||||
<!-- promise is pending -->
|
<p>rooms are loading...</p>
|
||||||
<p>rooms are loaded...</p>
|
|
||||||
{:then rooms}
|
{:then rooms}
|
||||||
{#each rooms as room}
|
{#each rooms as room}
|
||||||
<Button my_class="list" on_click={go_to_room}>
|
<Button my_class="list" on_click={function() {go_to_room(room)}}>
|
||||||
{room.name}
|
{room.name}
|
||||||
</Button>
|
</Button>
|
||||||
{/each}
|
{/each}
|
||||||
|
|||||||
@@ -1,32 +1,22 @@
|
|||||||
<script>
|
<script lang="ts">
|
||||||
|
|
||||||
import { layout, msgs, user, socket } from './Store_chat';
|
import { layout, msgs, user, socket } from './Store_chat';
|
||||||
import { join_room, change_room, get_room_messages } from './Request_rooms';
|
import { join_room, change_room, get_room_messages, get_all_rooms } from './Request_rooms';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
|
|
||||||
export let back = "";
|
export let back = "";
|
||||||
|
|
||||||
let rooms = [];
|
let rooms = get_all_rooms();
|
||||||
|
|
||||||
// ask api for the rooms
|
|
||||||
const get_rooms = fetch('/api/v2/chat/allrooms')
|
|
||||||
.then(resp => resp.json())
|
|
||||||
.then(data =>
|
|
||||||
{
|
|
||||||
console.log("data.rooms:", data.rooms);
|
|
||||||
for (let room of data.rooms)
|
|
||||||
console.log(room.name);
|
|
||||||
rooms = data.rooms;
|
|
||||||
});
|
|
||||||
|
|
||||||
// join the room
|
// join the room
|
||||||
async function join_rooms(evt)
|
async function join_rooms(room)
|
||||||
{
|
{
|
||||||
console.log("inside join_room");
|
console.log("inside join_room");
|
||||||
let room_name = evt.target.innerText;
|
|
||||||
|
|
||||||
await join_room(room_name);
|
console.log("room:", room);
|
||||||
await change_room(room_name);
|
const updated_room = await join_room(room);
|
||||||
|
console.log("updated room:", updated_room);
|
||||||
|
await change_room(updated_room);
|
||||||
}
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
@@ -58,12 +48,11 @@
|
|||||||
<div class="__show_if_only_child">
|
<div class="__show_if_only_child">
|
||||||
<p class="__center">/ there are no public rooms yet /</p>
|
<p class="__center">/ there are no public rooms yet /</p>
|
||||||
</div>
|
</div>
|
||||||
{#await get_rooms}
|
{#await rooms}
|
||||||
<!-- promise is pending -->
|
<p>rooms are loading...</p>
|
||||||
<p>rooms are loaded...</p>
|
{:then rooms}
|
||||||
{:then}
|
|
||||||
{#each rooms as room}
|
{#each rooms as room}
|
||||||
<Button my_class="list" on_click={join_rooms}>
|
<Button my_class="list" on_click={function() {join_rooms(room)}}>
|
||||||
{room.name}
|
{room.name}
|
||||||
</Button>
|
</Button>
|
||||||
{/each}
|
{/each}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
<script>
|
<script>
|
||||||
|
|
||||||
import { layout, socket, msgs, add_msg, room_name } from './Store_chat';
|
import { layout, socket, msgs, add_msg, current_room_name } from './Store_chat';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
import Msg from './Element_msg.svelte';
|
import Msg from './Element_msg.svelte';
|
||||||
|
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
|
|
||||||
<!-- room_name -->
|
<!-- room_name -->
|
||||||
<Button new_layout="room_set" my_class="room_name transparent">
|
<Button new_layout="room_set" my_class="room_name transparent">
|
||||||
{$room_name}
|
{$current_room_name}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<!-- close -->
|
<!-- close -->
|
||||||
|
|||||||
@@ -1,10 +1,25 @@
|
|||||||
<script>
|
<script>
|
||||||
|
|
||||||
import { layout } from './Store_chat';
|
import { layout, current_room_name } from './Store_chat';
|
||||||
|
import { get_room_users, user_leave_room } from './Request_rooms';
|
||||||
import Button from './Element_button.svelte';
|
import Button from './Element_button.svelte';
|
||||||
|
|
||||||
export let back = "";
|
export let back = "";
|
||||||
|
|
||||||
|
let users = get_room_users();
|
||||||
|
|
||||||
|
function user_profile()
|
||||||
|
{
|
||||||
|
console/log("in user_profile");
|
||||||
|
}
|
||||||
|
|
||||||
|
function leave_room()
|
||||||
|
{
|
||||||
|
console.log("in leave_room");
|
||||||
|
user_leave_room();
|
||||||
|
layout.set("home");
|
||||||
|
}
|
||||||
|
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<div class="grid_box">
|
<div class="grid_box">
|
||||||
@@ -16,7 +31,7 @@
|
|||||||
|
|
||||||
<!-- room_name -->
|
<!-- room_name -->
|
||||||
<Button my_class="room_name deactivate">
|
<Button my_class="room_name deactivate">
|
||||||
<room_name>
|
{$current_room_name}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
||||||
<!-- close -->
|
<!-- close -->
|
||||||
@@ -26,7 +41,7 @@
|
|||||||
|
|
||||||
<!-- panel_room_set -->
|
<!-- panel_room_set -->
|
||||||
<div class="panel panel_room_set __border_top">
|
<div class="panel panel_room_set __border_top">
|
||||||
<Button new_layout="create" my_class="create">
|
<Button on_click={leave_room}>
|
||||||
leave
|
leave
|
||||||
</Button>
|
</Button>
|
||||||
<p>room users :</p>
|
<p>room users :</p>
|
||||||
@@ -34,21 +49,15 @@
|
|||||||
<div class="__show_if_only_child">
|
<div class="__show_if_only_child">
|
||||||
<p class="__center">/ there are no public rooms yet /</p>
|
<p class="__center">/ there are no public rooms yet /</p>
|
||||||
</div>
|
</div>
|
||||||
<!-- placeholders
|
{#await users}
|
||||||
------------- -->
|
<p>list of users is loading...</p>
|
||||||
<Button new_layout="user" my_class="list">
|
{:then users}
|
||||||
user 1
|
{#each users as user}
|
||||||
|
<Button new_layout="user" my_class="list" on_click={user_profile}>
|
||||||
|
{user}
|
||||||
</Button>
|
</Button>
|
||||||
<Button new_layout="user" my_class="list blocked">
|
{/each}
|
||||||
user 2
|
{/await}
|
||||||
</Button>
|
|
||||||
<Button new_layout="user" my_class="list">
|
|
||||||
user 3
|
|
||||||
</Button>
|
|
||||||
<Button new_layout="user" my_class="list">
|
|
||||||
user 4
|
|
||||||
</Button>
|
|
||||||
<!-- END placeholders -->
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import { msgs, user, layout, socket, room_name } from './Store_chat';
|
import { msgs, user, layout, socket, current_room_name } from './Store_chat';
|
||||||
|
import type { Room } from './Interface_chat';
|
||||||
|
|
||||||
export async function get_room_messages()
|
export async function get_room_messages()
|
||||||
{
|
{
|
||||||
@@ -7,6 +8,9 @@ export async function get_room_messages()
|
|||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
const messages = data.messages;
|
const messages = data.messages;
|
||||||
|
|
||||||
|
if (messages === null)
|
||||||
|
return;
|
||||||
|
|
||||||
messages.forEach(function(item) {
|
messages.forEach(function(item) {
|
||||||
if (item.name === user.username) {
|
if (item.name === user.username) {
|
||||||
item.name = "me";
|
item.name = "me";
|
||||||
@@ -16,88 +20,106 @@ export async function get_room_messages()
|
|||||||
msgs.set(messages);
|
msgs.set(messages);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function create_room(room_name, room_type)
|
export async function create_room(room: Room)
|
||||||
{
|
{
|
||||||
console.log("in create_room");
|
console.log("in create_room");
|
||||||
|
|
||||||
let form_data = {
|
|
||||||
room_name: room_name,
|
|
||||||
room_type: room_type,
|
|
||||||
};
|
|
||||||
|
|
||||||
// send the new room
|
// send the new room
|
||||||
const response = await fetch('/api/v2/chat/create', {
|
const response = await fetch('/api/v2/chat/create', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(form_data),
|
body: JSON.stringify(room),
|
||||||
});
|
});
|
||||||
|
|
||||||
// get response status and message
|
// get response status and message
|
||||||
let response_status = response.status;
|
let response_status = response.status;
|
||||||
let data = await response.json();
|
let data = await response.json();
|
||||||
let response_message = "";
|
let response_message = "";
|
||||||
if (data.message)
|
|
||||||
response_message = data.message;
|
|
||||||
|
|
||||||
return {
|
return {
|
||||||
status: response_status,
|
status: response_status,
|
||||||
message: response_message
|
message: data.message,
|
||||||
|
room: data.room,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function join_room(room_name)
|
export async function join_room(room: Room)
|
||||||
{
|
{
|
||||||
console.log("in join_room");
|
console.log("in join_room");
|
||||||
|
|
||||||
let name = {
|
|
||||||
room_name: room_name,
|
|
||||||
}
|
|
||||||
const response = await fetch('/api/v2/chat/join', {
|
const response = await fetch('/api/v2/chat/join', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(name),
|
body: JSON.stringify(room),
|
||||||
});
|
});
|
||||||
let data = await response.json();
|
let data = await response.json();
|
||||||
console.log(data.message);
|
|
||||||
|
|
||||||
socket.emit('join', room_name);
|
return data.room;
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function change_room(name)
|
export async function change_room(room: Room)
|
||||||
{
|
{
|
||||||
console.log("in change_room");
|
console.log("in change_room");
|
||||||
|
|
||||||
let r_name = {
|
console.log("room:", room);
|
||||||
room_name: name,
|
|
||||||
}
|
|
||||||
const response = await fetch('/api/v2/chat/change', {
|
const response = await fetch('/api/v2/chat/change', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: { 'Content-Type': 'application/json' },
|
headers: { 'Content-Type': 'application/json' },
|
||||||
body: JSON.stringify(r_name),
|
body: JSON.stringify(room),
|
||||||
});
|
});
|
||||||
let data = await response.json();
|
let data = await response.json();
|
||||||
console.log(data.message);
|
|
||||||
|
|
||||||
await get_room_messages();
|
await get_room_messages();
|
||||||
socket.emit('join', name);
|
|
||||||
|
|
||||||
room_name.set(name);
|
let room_name = data.room.name;
|
||||||
|
if (room.type === 'direct')
|
||||||
|
{
|
||||||
|
room_name === room.users[0];
|
||||||
|
if (room_name === user.username)
|
||||||
|
room_name === room.users[1];
|
||||||
|
}
|
||||||
|
current_room_name.set(room_name);
|
||||||
layout.set("room");
|
layout.set("room");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function get_my_rooms()
|
||||||
|
{
|
||||||
|
console.log("in get_my_rooms");
|
||||||
|
|
||||||
|
const response = await fetch('/api/v2/chat/myrooms');
|
||||||
|
const data = await response.json();
|
||||||
|
console.log("data.rooms:", data.rooms);
|
||||||
|
|
||||||
|
return data.rooms;
|
||||||
|
}
|
||||||
|
|
||||||
export async function get_all_rooms()
|
export async function get_all_rooms()
|
||||||
{
|
{
|
||||||
console.log("in get_all_rooms");
|
console.log("in get_all_rooms");
|
||||||
|
|
||||||
// ask api for the rooms
|
const response = await fetch('/api/v2/chat/allrooms');
|
||||||
const response = await fetch('/api/v2/chat/myrooms');
|
|
||||||
const data = await response.json();
|
const data = await response.json();
|
||||||
|
|
||||||
console.log("data.rooms:", data.rooms);
|
return data.rooms;
|
||||||
for (let room of data.rooms)
|
|
||||||
console.log(room.name);
|
|
||||||
let rooms = data.rooms;
|
|
||||||
|
|
||||||
return rooms;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export async function get_room_users()
|
||||||
|
{
|
||||||
|
console.log("in get_room_users");
|
||||||
|
|
||||||
|
const response = await fetch('/api/v2/chat/roomusers');
|
||||||
|
const data = await response.json();
|
||||||
|
|
||||||
|
return data.users;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function user_leave_room()
|
||||||
|
{
|
||||||
|
console.log("in leave_room");
|
||||||
|
|
||||||
|
const response = await fetch('/api/v2/chat/removeuser', {
|
||||||
|
method: 'DELETE',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,7 @@ import { writable } from 'svelte/store';
|
|||||||
|
|
||||||
export let msgs = writable([]);
|
export let msgs = writable([]);
|
||||||
export let layout = writable("close");
|
export let layout = writable("close");
|
||||||
export let room_name = writable("");
|
export let current_room_name = writable("");
|
||||||
|
|
||||||
export let user;
|
export let user;
|
||||||
export let socket;
|
export let socket;
|
||||||
@@ -14,3 +14,4 @@ export function add_msg(name: string, message: string)
|
|||||||
{
|
{
|
||||||
msgs.update(msgs => [...msgs, { name: "me", message: message }]);
|
msgs.update(msgs => [...msgs, { name: "me", message: message }]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user