Browse Source

some chat sockets modifications

master
nicolas-arnaud 2 years ago
parent
commit
b74cb7d47c
  1. 2
      back/volume/src/chat/chat.controller.ts
  2. 15
      back/volume/src/chat/chat.gateway.ts
  3. 3
      back/volume/src/chat/entity/connection.entity.ts
  4. 2
      front/volume/src/components/Channels.svelte
  5. 13
      front/volume/src/components/Chat.svelte

2
back/volume/src/chat/chat.controller.ts

@ -160,8 +160,8 @@ export class ChatController {
if (await this.channelService.isBanned(channel.id, target.data[0])) {
throw new BadRequestException('User is already banned from this channel')
}
channel.banned.push([target.data[0], Date.now() + target.data[1] * 1000])
console.log(channel.banned)
channel.banned.push([target.data[0], Date.now() + target.data[1] * 1000])
await this.channelService.save(channel)
}

15
back/volume/src/chat/chat.gateway.ts

@ -46,8 +46,9 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
const connect = await this.connectedUserRepository.findOneBy({
socket: socket.id,
});
if (connect)
if (connect) {
await this.connectedUserRepository.delete({ socket: socket.id });
}
socket.disconnect();
console.log("socket %s has disconnected", socket.id);
}
@ -124,7 +125,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
}
if (
channel.owner.ftId !== kick.from &&
channel.admins.findIndex((usr) => usr.ftId === kick.from) === -1
channel.admins.findIndex((usr) => +usr.ftId === kick.from) === -1
) {
throw new WsException("You do not have the required privileges");
}
@ -132,11 +133,9 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
const connect = (await this.connectedUserRepository.findOneBy({
user: user.ftId,
})) as ConnectedUser;
console.log(`kicking ${user.username} from ${channel.name}`);
// await this.onLeaveChannel(socket)
this.server.sockets.sockets.get(connect.socket)?.emit("kicked");
await this.server.sockets.sockets
.get(connect.socket)?.leave(channel.id.toString());
this.server.sockets.sockets.get(connect.socket)?.disconnect();
if (connect) {
console.log(`kicking ${user.username} from ${channel.name}`);
this.server.sockets.sockets.get(connect.socket)?.emit("kicked");
}
}
}

3
back/volume/src/chat/entity/connection.entity.ts

@ -1,8 +1,5 @@
import { Column, Entity, JoinColumn, OneToOne, PrimaryColumn } from 'typeorm'
import Channel from './channel.entity'
import User from 'src/users/entity/user.entity'
@Entity()
export default class ConnectedUser {
@Column()

2
front/volume/src/components/Channels.svelte

@ -7,6 +7,8 @@
isPrivate: boolean;
password: string;
owner: User;
banned: Array<User>;
muted: Array<User>;
}
export interface chatMessagesType {
id: number;

13
front/volume/src/components/Chat.svelte

@ -61,16 +61,23 @@
}
socket.on("newMessage", (msg: chatMessagesType) => {
console.log(msg);
messages = [...messages, msg];
if (blockedUsers.findIndex((user) => msg.author.ftId === user.ftId) === -1)
messages = [...messages, msg];
});
socket.on("messages", (msgs: Array<chatMessagesType>) => {
messages = msgs;
getMembers();
getMembers().then(() => {
console.log("You are joining channel: ", channel.name);
console.log(`Blocked users: ${blockedUsers.map((user) => user.username)}`);
console.log(`Chat members: ${chatMembers.map((user) => user.username)}`);
console.log(`Banned members: ${channel.banned.map((user) => user.username)}`);
console.log(`Muted users: ${channel.muted.map((user) => user.username)}`);
});
usersInterval = setInterval(async () => {
getMembers();
}, 1000);
console.log("You are joining channel: ", channel.name);
});
socket.on("failedJoin", (error: string) => {

Loading…
Cancel
Save