changed services to add join message
This commit is contained in:
@@ -21,7 +21,7 @@ export class ChatController {
|
||||
async getMyRooms(@Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
console.log("- in getMyRooms controller");
|
||||
const rooms = await this.chatService.getMyRooms(req.user);
|
||||
const rooms = await this.chatService.getMyRooms(req.user.username);
|
||||
console.log("- out getMyRooms controller");
|
||||
return res.status(HttpStatus.OK).json({ rooms: rooms });
|
||||
}
|
||||
@@ -32,7 +32,7 @@ export class ChatController {
|
||||
async getAllRooms(@Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
console.log("- in getAllRooms controller");
|
||||
const rooms = await this.chatService.getAllNotMyRooms(req.user);
|
||||
const rooms = await this.chatService.getAllNotMyRooms(req.user.username);
|
||||
console.log("- out getAllRooms controller");
|
||||
return res.status(HttpStatus.OK).json({ rooms: rooms });
|
||||
}
|
||||
@@ -43,7 +43,7 @@ export class ChatController {
|
||||
async setCurrentRoom(@Body() setCurrentRoomDto: setCurrentRoomDto, @Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
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);
|
||||
console.log("- out setCurrentRoom controller");
|
||||
return res.status(HttpStatus.OK).json({ message: response });
|
||||
}
|
||||
@@ -54,7 +54,7 @@ export class ChatController {
|
||||
async createRoom(@Body() createRoomDto: createRoomDto, @Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
console.log("- in createRoom controller");
|
||||
const response = await this.chatService.addUserToNewRoom(req.user, createRoomDto);
|
||||
const response = await this.chatService.addUserToNewRoom(req.user.username, createRoomDto);
|
||||
console.log("- out createRoom controller");
|
||||
return res.status(HttpStatus.OK).json({ room_name: createRoomDto.room_name, message: response });
|
||||
}
|
||||
@@ -65,7 +65,8 @@ export class ChatController {
|
||||
async joinRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
console.log("- in joinRoom controller");
|
||||
const response = await this.chatService.addUserToRoom(req.user, joinRoomDto);
|
||||
console.log("-- room_name", joinRoomDto.room_name);
|
||||
const response = await this.chatService.addUserToRoom(req.user.username, joinRoomDto.room_name);
|
||||
console.log("- out joinRoom controller");
|
||||
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
|
||||
}
|
||||
@@ -76,7 +77,7 @@ export class ChatController {
|
||||
async changeRoom(@Body() joinRoomDto: joinRoomDto, @Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
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, joinRoomDto.room_name);
|
||||
console.log("- out changeRoom controller");
|
||||
return res.status(HttpStatus.OK).json({ room_name: joinRoomDto.room_name, message: response });
|
||||
}
|
||||
@@ -96,7 +97,7 @@ export class ChatController {
|
||||
async getMessages(@Req() req, @Res() res): Promise<object>
|
||||
{
|
||||
console.log("- in getMessages controller");
|
||||
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user);
|
||||
const messages = await this.chatService.getMessagesFromCurrentRoom(req.user.username);
|
||||
console.log("- out getMessages controller");
|
||||
return res.status(HttpStatus.OK).json({ messages: messages });
|
||||
}
|
||||
|
||||
@@ -37,17 +37,32 @@ export class ChatGateway
|
||||
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.chatService.addMessageToRoom(room_name, "SERVER", message);
|
||||
|
||||
console.log('- out joinRoom gateway');
|
||||
}
|
||||
|
||||
@SubscribeMessage('change')
|
||||
async changeRoom(@ConnectedSocket() socket, @MessageBody() room_name: string): Promise<void>
|
||||
{
|
||||
console.log('- in changeRoom gateway');
|
||||
socket.leave(socket.room);
|
||||
socket.join(room_name);
|
||||
socket.room = room_name;
|
||||
|
||||
console.log('- out changeRoom gateway');
|
||||
}
|
||||
|
||||
@SubscribeMessage('message')
|
||||
async handleMessage(@ConnectedSocket() socket, @MessageBody() message: string): Promise<void>
|
||||
{
|
||||
console.log('- in handleMessage gateway');
|
||||
//let room_name = await this.chatService.getCurrentRoom(socket.username);
|
||||
socket.to(socket.room).emit('message', socket.username, message);
|
||||
this.chatService.addMessageToCurrentRoom(socket.username, message);
|
||||
let room_name = await this.chatService.getCurrentRoom(socket.username);
|
||||
await this.chatService.addMessageToRoom(room_name, socket.username, message);
|
||||
|
||||
console.log('- out handleMessage gateway');
|
||||
}
|
||||
|
||||
@@ -29,12 +29,12 @@ export class ChatService {
|
||||
/* GETTERS ************************************************
|
||||
*/
|
||||
|
||||
async getMyRooms(user: User)
|
||||
async getMyRooms(username: string)
|
||||
{
|
||||
console.log("-- in getMyRooms service");
|
||||
const rooms = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.where('chatroom.users LIKE :user_name', { user_name: `%${user.username}%` })
|
||||
.where('chatroom.users LIKE :user_name', { user_name: `%${username}%` })
|
||||
.getMany();
|
||||
|
||||
console.log("-- out getMyRooms service");
|
||||
@@ -52,15 +52,15 @@ export class ChatService {
|
||||
return rooms;
|
||||
}
|
||||
|
||||
async getAllNotMyRooms(user: User)
|
||||
async getAllNotMyRooms(username: string)
|
||||
{
|
||||
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 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: `%${user.username}%` })
|
||||
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
|
||||
.getMany();
|
||||
//const users = await this.getAllUsers();
|
||||
//let allRooms = [...rooms, ...users];
|
||||
@@ -69,11 +69,11 @@ export class ChatService {
|
||||
return rooms;
|
||||
}
|
||||
|
||||
async getMessagesFromCurrentRoom(user: User)
|
||||
async getMessagesFromCurrentRoom(username: string)
|
||||
{
|
||||
console.log("-- in getMessagesFromCurrentRoom 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 user_db = await this.usersService.findOne(username);
|
||||
const currentRoom = await this.getRoomByName(user_db.currentRoom);
|
||||
|
||||
console.log("-- out getMessagesFromCurrentRoom service");
|
||||
@@ -90,12 +90,12 @@ export class ChatService {
|
||||
return user_db.currentRoom;
|
||||
}
|
||||
|
||||
async getRoomByName(name: string)
|
||||
async getRoomByName(room_name: string)
|
||||
{
|
||||
console.log("-- in getRoomByName service");
|
||||
const room = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.where('chatroom.name = :name', { name: name })
|
||||
.where('chatroom.name = :name', { name: room_name })
|
||||
.getOne();
|
||||
|
||||
console.log("-- out getRoomByName service");
|
||||
@@ -118,25 +118,25 @@ export class ChatService {
|
||||
/* SETTERS ************************************************
|
||||
*/
|
||||
|
||||
async setCurrentRoom(user: User, name: string)
|
||||
async setCurrentRoom(username: string, room_name: string)
|
||||
{
|
||||
console.log("-- in setCurrentRoom service");
|
||||
const user_db = await this.getUserByName(user.username);
|
||||
//const user_db = await this.usersService.findOne(user.username);
|
||||
user_db.currentRoom = name;
|
||||
const user_db = await this.getUserByName(username);
|
||||
//const user_db = await this.usersService.findOne(username);
|
||||
user_db.currentRoom = room_name;
|
||||
this.userRepository.save(user_db);
|
||||
|
||||
console.log("-- out setCurrentRoom service");
|
||||
return `room "${name}" is now current room`;
|
||||
return `room "${room_name}" is now current room`;
|
||||
}
|
||||
|
||||
|
||||
/* ADDERS *************************************************
|
||||
*/
|
||||
|
||||
async addUserToNewRoom(user: User, createRoomDto: createRoomDto)
|
||||
async addUserToNewRoom(username: string, createRoomDto: createRoomDto)
|
||||
{
|
||||
console.log("-- in addUserToRoom service");
|
||||
console.log("-- in addUserToNewRoom service");
|
||||
const room = await this.getRoomByName(createRoomDto.room_name);
|
||||
if (room)
|
||||
throw new HttpException(`This room already exist`, HttpStatus.CONFLICT);
|
||||
@@ -145,39 +145,39 @@ export class ChatService {
|
||||
const newChatroom = new Chatroom();
|
||||
newChatroom.name = createRoomDto.room_name;
|
||||
newChatroom.type = createRoomDto.room_type;
|
||||
newChatroom.owner = user.username;
|
||||
newChatroom.users = [user.username];
|
||||
newChatroom.owner = username;
|
||||
newChatroom.users = [username];
|
||||
newChatroom.messages = [{ name: "SERVER", message: `creation of room ${createRoomDto.room_name}` }];
|
||||
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)
|
||||
{
|
||||
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);
|
||||
if (room.users.includes(username))
|
||||
throw new HttpException(`your have already join this room`, HttpStatus.CONFLICT);
|
||||
|
||||
// update room with new user
|
||||
room.users.push(user.username);
|
||||
room.users.push(username);
|
||||
this.chatroomRepository.save(room);
|
||||
|
||||
const rooms = await this.getMyRooms(user);
|
||||
const allRooms = await this.getAllRooms();
|
||||
await this.setCurrentRoom(username, room_name);
|
||||
|
||||
console.log("-- out addUserToRoom service");
|
||||
return "successfull joining room";
|
||||
}
|
||||
|
||||
async addMessageToCurrentRoom(username: string, message: string)
|
||||
async addMessageToRoom(room_name: string, username: string, message: string)
|
||||
{
|
||||
console.log("-- in addMessageToCurrentRoom service");
|
||||
const user_db = await this.getUserByName(username);
|
||||
console.log("-- in addMessageToRoom service");
|
||||
//const user_db = await this.getUserByName(username);
|
||||
//const user_db = await this.usersService.findOne(username);
|
||||
const currentRoom = await this.getRoomByName(user_db.currentRoom);
|
||||
//console.log("user_db:", user_db);
|
||||
const currentRoom = await this.getRoomByName(room_name);
|
||||
let chat_message = {
|
||||
name: username,
|
||||
message: message,
|
||||
@@ -185,14 +185,14 @@ export class ChatService {
|
||||
currentRoom.messages.push(chat_message);
|
||||
this.chatroomRepository.save(currentRoom);
|
||||
|
||||
console.log("-- out addMessageToCurrentRoom service");
|
||||
console.log("-- out addMessageToRoom service");
|
||||
}
|
||||
|
||||
|
||||
/* REMOVERS ***********************************************
|
||||
*/
|
||||
|
||||
async removeUserFromRoom(user: User, room_name: string)
|
||||
async removeUserFromRoom(username: string, room_name: string)
|
||||
{
|
||||
console.log("-- in removeUserFromRoom service");
|
||||
// get room
|
||||
@@ -203,12 +203,12 @@ export class ChatService {
|
||||
/* SEARCH IN USER *****************************************
|
||||
*/
|
||||
|
||||
async getUserByName(name: string)
|
||||
async getUserByName(username: string)
|
||||
{
|
||||
console.log("-- in getUserByName service");
|
||||
const user = await this.userRepository
|
||||
.createQueryBuilder('user')
|
||||
.where('user.username = :name', { name: name })
|
||||
.where('user.username = :name', { name: username })
|
||||
.getOne();
|
||||
|
||||
console.log("-- out getUserByName service");
|
||||
|
||||
Reference in New Issue
Block a user