Skip to content

Commit

Permalink
channelJoined emitted
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicolas Jaros committed Apr 6, 2023
1 parent 9c6fec1 commit 29def02
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 14 deletions.
13 changes: 12 additions & 1 deletion back/nest_project/src/chat/chat.classes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ export class UserRoomHandler {

public roomKill(channelId: string) {
let locGeneral: ChannelEntity = {
id: "general",
id: "00000000-0000-0000-0000-000000000000",
name: "general",
date: new Date(),
password: false,
Expand Down Expand Up @@ -282,4 +282,15 @@ export class UserRoomHandler {
return {room: found.room, isChannel: found.isChannel};
return undefined;
}

public emitToUserHavingThisSocket(socket: Socket, ev: string, ...args: any[]) {
let socketFound = this.socketMap.sockets.get(socket);
if (socketFound != undefined) {
let userFound = this.userMap.get(socketFound.userId);
if (userFound != undefined) {
userFound.emit(ev, ...args);
}
}
}

}
5 changes: 0 additions & 5 deletions back/nest_project/src/chat/chat.gateway.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import { MessageBody, SubscribeMessage, WebSocketGateway, ConnectedSocket, OnGatewayConnection, OnGatewayDisconnect, OnGatewayInit } from "@nestjs/websockets";
import { Server, Socket, Namespace } from 'socket.io';
import { Logger, UseGuards, UsePipes, ValidationPipe } from "@nestjs/common";
import { AuthGuard } from "@nestjs/passport";
import { IChannelToEmit, IMessageChat, IToken } from "./chat.interface";
import * as jsrsasign from 'jsrsasign';
import { ChatService } from "./chat.service";
import { UserRoomHandler } from "./chat.classes";
import { useContainer } from "class-validator";
import { UserService } from "src/user/user.service";
import { UserDto } from "src/user/user.dto";
import { JwtService } from '@nestjs/jwt';
import { ChannelEntity } from "./channel/channel.entity";
import { UserEntity } from "src/user/user.entity";
import { ChannelDto } from "./channel/channel.dto";
import { addMessageDto, changeLocDto, channelIdDto, createChannelDto, inviteUserDto, joinChannelDto, kickUserDto, makeHimNoOpDto, makeHimOpDto } from "./chat.gateway.dto";

@UsePipes(ValidationPipe)
Expand Down
17 changes: 10 additions & 7 deletions back/nest_project/src/chat/chat.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export class ChatService {

private goBackToGeneral(client: Socket) {
let locGeneral: ChannelEntity = {
id: "general",
id: "00000000-0000-0000-0000-000000000000",
name: "general",
date: new Date(),
password: false,
Expand Down Expand Up @@ -156,7 +156,7 @@ export class ChatService {
public changeLocEvent(client: Socket, user: UserEntity, loc: string, isChannel: boolean, roomHandler: UserRoomHandler) {
if (isChannel)
{
if (loc == 'general') {
if (loc == '00000000-0000-0000-0000-000000000000') {
roomHandler.joinRoom(user.id, client, loc, true, false, false, false);
this.goBackToGeneral(client);
return;
Expand Down Expand Up @@ -274,7 +274,7 @@ export class ChatService {
let room = roomHandler.roomMap.of(channel.id);
if (room != undefined)
room.emit("newUserInChannel", user.id, user.login);
this.listMyChannelEvent(client, user.id);
roomHandler.emitToUserHavingThisSocket(client, "channelJoined", channel.id, channel.name);
this.changeLocEvent(client, user, data.channelId, true, roomHandler);
})
}
Expand Down Expand Up @@ -310,14 +310,17 @@ export class ChatService {
room.emit("newUserInChannel", userEntity.id, userEntity.login);
let logged = roomHandler.userMap.get(userToInvite);
if (logged != undefined) {
logged.sockets.forEach(({}, socket) => {
this.listMyChannelEvent(socket, userToInvite);
this.channelService.getOneById(channelId)
.then((channelEntity) => {
logged.sockets.forEach(({}, socket) => {
socket.emit("channelJoined", channelEntity.id, channelEntity.name);
})
})
}
})
}
else
client.emit("notice", `The user ${userToInvite} already belong to this channel.`);
client.emit("notice", `The user ${userEntity.login} already belong to this channel.`);
}
)
}
Expand Down Expand Up @@ -345,7 +348,7 @@ export class ChatService {
let logged = roomHandler.userMap.get(userToInvite);
if (logged != undefined) {
logged.sockets.forEach(({}, socket) => {
this.listMyChannelEvent(socket, userToInvite);
socket.emit("channelJoined", chanOpts.id, chanOpts.name);
})
}
})
Expand Down
2 changes: 1 addition & 1 deletion back/nest_project/src/user/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ export class UserService {
const user = await this.findById(userId);
let arrayOfChannels: {channel: ChannelEntity, status: string}[] = [{
channel: {
id: "general",
id: "00000000-0000-0000-0000-000000000000",
date: new Date(),
name: "general",
password: false,
Expand Down

0 comments on commit 29def02

Please sign in to comment.