change socket username
This commit is contained in:
@@ -122,7 +122,7 @@ export class ChatController {
|
||||
|
||||
let message = `${username} is now admin`;
|
||||
await this.chatService.addMessageToRoom(current_room_name, "SERVER", message);
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
let server = this.chatGateway.server;
|
||||
await server.in(socket.room).emit('message', "SERVER", message);
|
||||
|
||||
@@ -207,7 +207,7 @@ export class ChatController {
|
||||
let message = `${req.user.username} changed the password`;
|
||||
room.allowed_users = [req.user.username];
|
||||
await this.chatService.setPassword(req.user.username, message, room);
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
await socket.to(socket.room).emit('message', "SERVER", message);
|
||||
}
|
||||
|
||||
@@ -267,7 +267,7 @@ export class ChatController {
|
||||
room = await this.chatService.addUserToRoom(req.user.username, room.name);
|
||||
}
|
||||
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
await this.chatService.socketJoinRoom(socket, room.name);
|
||||
|
||||
const ret_room = this.format_room(room);
|
||||
@@ -308,7 +308,7 @@ export class ChatController {
|
||||
}
|
||||
|
||||
await this.chatService.setCurrentRoom(req.user.username, room.name);
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
await this.chatService.socketChangeRoom(socket, room.name);
|
||||
|
||||
const ret_room = this.format_room(room);
|
||||
@@ -354,7 +354,7 @@ export class ChatController {
|
||||
await this.chatService.setPassword(req.user.username, message, room, old_password);
|
||||
|
||||
// inform other connected users
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
await socket.to(socket.room).emit('message', "SERVER", message);
|
||||
await socket.to(socket.room).emit('new_password');
|
||||
|
||||
@@ -376,7 +376,7 @@ export class ChatController {
|
||||
await this.chatService.setPassword(req.user.username, message, room);
|
||||
|
||||
// inform other connected users
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
await socket.to(socket.room).emit('message', "SERVER", message);
|
||||
await socket.to(socket.room).emit('new_password');
|
||||
|
||||
@@ -398,7 +398,7 @@ export class ChatController {
|
||||
await this.chatService.setPassword(req.user.username, message, room);
|
||||
|
||||
// inform other connected users
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
await socket.to(socket.room).emit('message', "SERVER", message);
|
||||
|
||||
const ret_room = this.format_room(room);
|
||||
@@ -445,7 +445,7 @@ export class ChatController {
|
||||
await this.chatService.setCurrentRoom(req.user.username, "");
|
||||
|
||||
// leaving message
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
await messages.forEach(async (message) =>
|
||||
{
|
||||
await this.chatService.addMessageToRoom(room_name, "SERVER", message);
|
||||
@@ -552,7 +552,7 @@ export class ChatController {
|
||||
|
||||
// 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 socket: socketDto = this.chatService.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);
|
||||
@@ -601,7 +601,7 @@ export class ChatController {
|
||||
await this.chatService.removeMute(username, room_name);
|
||||
|
||||
let message = `${req.user.username} has un-muted ${username}`;
|
||||
let socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let socket: socketDto = this.chatService.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);
|
||||
@@ -619,7 +619,7 @@ export class ChatController {
|
||||
|
||||
await this.chatService.addBlockUser(req.user.username, username);
|
||||
|
||||
let user_socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let user_socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
user_socket.join(`${username}_not_emit`);
|
||||
|
||||
res.status(HttpStatus.OK).json({ message: "successfull block" });
|
||||
@@ -634,7 +634,7 @@ export class ChatController {
|
||||
printCaller("- in ");
|
||||
|
||||
await this.chatService.removeBlockUser(req.user.username, username);
|
||||
let user_socket: socketDto = this.chatGateway.sockets.get(req.user.username);
|
||||
let user_socket: socketDto = this.chatService.sockets.get(req.user.username);
|
||||
user_socket.leave(`${username}_not_emit`);
|
||||
|
||||
res.status(HttpStatus.OK).json({ message: "successfull unblock" });
|
||||
|
||||
@@ -19,8 +19,6 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
||||
private chatService: ChatService,
|
||||
) {}
|
||||
|
||||
sockets = new Map<string, socketDto>();
|
||||
|
||||
@WebSocketServer()
|
||||
server;
|
||||
|
||||
@@ -33,7 +31,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
||||
if (!socket.username)
|
||||
return;
|
||||
|
||||
this.sockets.set(socket.username, socket);
|
||||
this.chatService.sockets.set(socket.username, socket);
|
||||
printCaller("--- socket.username:", socket.username);
|
||||
|
||||
let not_emit: string = `${socket.username}_not_emit`;
|
||||
@@ -49,7 +47,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
||||
socket.join(current_room);
|
||||
}
|
||||
async handleDisconnect(socket: socketDto) {
|
||||
this.sockets.delete(socket.username);
|
||||
this.chatService.sockets.delete(socket.username);
|
||||
}
|
||||
|
||||
@SubscribeMessage('join')
|
||||
@@ -74,6 +72,7 @@ implements OnGatewayConnection, OnGatewayDisconnect
|
||||
async handleMessage(@ConnectedSocket() socket: socketDto, @MessageBody() message: string): Promise<void>
|
||||
{
|
||||
console.log('- in handleMessage gateway');
|
||||
|
||||
await this.chatService.socketIncommingMessage(socket, message);
|
||||
console.log('- out handleMessage gateway');
|
||||
}
|
||||
|
||||
@@ -30,6 +30,8 @@ export class ChatService {
|
||||
private readonly friendshipRepository: Repository<Friendship>,
|
||||
) {}
|
||||
|
||||
sockets = new Map<string, socketDto>();
|
||||
|
||||
// temp for test
|
||||
sleep(ms) {
|
||||
return new Promise(resolve => setTimeout(resolve, ms));
|
||||
@@ -44,6 +46,7 @@ export class ChatService {
|
||||
printCaller("-- in ");
|
||||
|
||||
//await this.sleep(1000);
|
||||
let rooms: Chatroom[];
|
||||
|
||||
const queryBuilder = this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
@@ -55,48 +58,67 @@ export class ChatService {
|
||||
queryBuilder.select(fields);
|
||||
}
|
||||
|
||||
const rooms = await queryBuilder.getMany();
|
||||
rooms = await queryBuilder.getMany();
|
||||
|
||||
printCaller("-- out ");
|
||||
return rooms;
|
||||
if (rooms)
|
||||
return rooms;
|
||||
else
|
||||
return [];
|
||||
}
|
||||
|
||||
async getMyDirects(username: string): Promise<Chatroom[]>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
let directs: Chatroom[];
|
||||
|
||||
const my_rooms = await this.getMyRooms(username);
|
||||
const directs = my_rooms.filter(room => room.type === 'direct');
|
||||
if (my_rooms)
|
||||
directs = my_rooms.filter(room => room.type === 'direct');
|
||||
|
||||
printCaller("-- out ");
|
||||
return directs;
|
||||
if (directs)
|
||||
return directs;
|
||||
else
|
||||
return [];
|
||||
}
|
||||
|
||||
async getAllRooms(): Promise<Chatroom[]>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
const rooms = await this.chatroomRepository
|
||||
let rooms: Chatroom[] = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.getMany();
|
||||
|
||||
printCaller("-- out ");
|
||||
return rooms;
|
||||
if (rooms)
|
||||
return rooms;
|
||||
else
|
||||
return [];
|
||||
}
|
||||
|
||||
async getAllNotMyRooms(username: string): Promise<Chatroom[]>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
let rooms: Chatroom[];
|
||||
const user_db = await this.getUserByName(username);
|
||||
const rooms = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.where('chatroom.type NOT IN (:...type)', { type: ['private', 'direct'] })
|
||||
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
|
||||
.getMany();
|
||||
if (user_db)
|
||||
{
|
||||
rooms = await this.chatroomRepository
|
||||
.createQueryBuilder('chatroom')
|
||||
.where('chatroom.type NOT IN (:...type)', { type: ['private', 'direct'] })
|
||||
.andWhere('chatroom.users NOT LIKE :user_name', { user_name: `%${username}%` })
|
||||
.getMany();
|
||||
}
|
||||
|
||||
printCaller("-- out ");
|
||||
return rooms;
|
||||
if (rooms)
|
||||
return rooms;
|
||||
else
|
||||
return [];
|
||||
}
|
||||
|
||||
async getAllOtherRoomsAndUsers(username: string): Promise<roomDto[]>
|
||||
@@ -105,6 +127,8 @@ export class ChatService {
|
||||
|
||||
const all_rooms = await this.getAllNotMyRooms(username);
|
||||
const all_users = await this.getAllUsersNotMyRooms(username);
|
||||
if (!all_users || !all_users)
|
||||
return [];
|
||||
|
||||
let row_rooms = all_rooms.map(room => {
|
||||
return {
|
||||
@@ -123,7 +147,10 @@ export class ChatService {
|
||||
let rooms: roomDto[] = row_rooms.concat(users);
|
||||
|
||||
printCaller("-- out ");
|
||||
return rooms;
|
||||
if (rooms)
|
||||
return rooms;
|
||||
else
|
||||
return [];
|
||||
}
|
||||
|
||||
async getMessagesFromCurrentRoom(username: string): Promise<messagesDto[]>
|
||||
@@ -131,13 +158,20 @@ export class ChatService {
|
||||
printCaller("-- in ");
|
||||
|
||||
const user_db = await this.getUserByName(username);
|
||||
if (!user_db)
|
||||
return [];
|
||||
const currentRoom = await this.getRoomByName(user_db.currentRoom);
|
||||
if (!currentRoom)
|
||||
return [];
|
||||
let messages = null;
|
||||
if (currentRoom)
|
||||
messages = currentRoom.messages;
|
||||
|
||||
printCaller("-- out ");
|
||||
return messages;
|
||||
if (messages)
|
||||
return messages;
|
||||
else
|
||||
return [];
|
||||
}
|
||||
|
||||
async getCurrentRoomName(username: string): Promise<string>
|
||||
@@ -147,12 +181,15 @@ export class ChatService {
|
||||
const user_db = await this.getUserByName(username);
|
||||
if (!user_db)
|
||||
{
|
||||
printCaller(`throw error: error: true, code: 'USER_NOT_FOUND', message: 'the user was not found'`);
|
||||
printCaller(`error: 'the user was not found'`);
|
||||
return;
|
||||
}
|
||||
|
||||
printCaller("-- out ");
|
||||
return user_db.currentRoom;
|
||||
if (user_db && user_db.currentRoom)
|
||||
return user_db.currentRoom;
|
||||
else
|
||||
return "";
|
||||
}
|
||||
|
||||
async getRoomByName(room_name: string, fieldsToReturn: string[] = null): Promise<Chatroom>
|
||||
@@ -172,7 +209,10 @@ export class ChatService {
|
||||
const room = await queryBuilder.getOne();
|
||||
|
||||
printCaller("-- out ");
|
||||
return room;
|
||||
if (room)
|
||||
return room;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
async getRoomById(id: number): Promise<Chatroom>
|
||||
@@ -185,7 +225,10 @@ export class ChatService {
|
||||
.getOne();
|
||||
|
||||
printCaller("-- out ");
|
||||
return room;
|
||||
if (room)
|
||||
return room;
|
||||
else
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@@ -197,6 +240,8 @@ export class ChatService {
|
||||
printCaller("-- in ");
|
||||
|
||||
const user_db = await this.getUserByName(username);
|
||||
if (!user_db)
|
||||
return "";
|
||||
user_db.currentRoom = room_name;
|
||||
await this.userRepository.save(user_db);
|
||||
|
||||
@@ -209,6 +254,8 @@ export class ChatService {
|
||||
printCaller("-- in ");
|
||||
|
||||
const room_db = await this.getRoomByName(room.name);
|
||||
if (!room_db)
|
||||
return;
|
||||
const is_match = await bcrypt.compare(room.password, room_db.hash);
|
||||
if (!is_match)
|
||||
{
|
||||
@@ -233,6 +280,8 @@ export class ChatService {
|
||||
}
|
||||
|
||||
const room_db = await this.getRoomByName(room.name);
|
||||
if (!room_db)
|
||||
return;
|
||||
|
||||
if (!room_db.admins.includes(username))
|
||||
{
|
||||
@@ -293,6 +342,8 @@ export class ChatService {
|
||||
printCaller("-- in ");
|
||||
|
||||
const room_db = await this.getRoomByName(room_name);
|
||||
if (!room_db)
|
||||
return;
|
||||
if (room_db.type === "direct")
|
||||
{
|
||||
printCaller(`throw error: error: true, code: 'NO_DIRECT_ADMIN', message: 'there are no admins in direct messages'`);
|
||||
@@ -310,7 +361,7 @@ export class ChatService {
|
||||
printCaller("-- out ");
|
||||
}
|
||||
|
||||
async findOneRelationshipByUsername(friendUsername : string, username : string)
|
||||
async findOneRelationshipByUsername(friendUsername : string, username : string): Promise<SendableFriendship>
|
||||
{
|
||||
printCaller("-- in ");
|
||||
|
||||
@@ -350,6 +401,8 @@ export class ChatService {
|
||||
printCaller("-- in ");
|
||||
|
||||
let user = await this.getUserByName(username);
|
||||
if (!user)
|
||||
return;
|
||||
let relation = await this.findOneRelationshipByUsername(to_block_username, username);
|
||||
if (relation)
|
||||
{
|
||||
@@ -760,10 +813,18 @@ export class ChatService {
|
||||
|
||||
if (room_has_change)
|
||||
this.chatroomRepository.save(room);
|
||||
|
||||
this.addMessageToRoom(room.name, "SERVER", `${old_name} changes it's name for ${new_name}`);
|
||||
|
||||
let socket: socketDto = this.sockets.get(old_name);
|
||||
if (socket)
|
||||
{
|
||||
this.sockets.delete(old_name);
|
||||
socket.username = new_name;
|
||||
this.sockets.set(new_name, socket);
|
||||
}
|
||||
});
|
||||
|
||||
rooms = await this.getAllRooms();
|
||||
|
||||
printCaller("-- out ");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user