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. 11
      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. 11
      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])) { if (await this.channelService.isBanned(channel.id, target.data[0])) {
throw new BadRequestException('User is already banned from this channel') 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) console.log(channel.banned)
channel.banned.push([target.data[0], Date.now() + target.data[1] * 1000])
await this.channelService.save(channel) await this.channelService.save(channel)
} }

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

@ -46,8 +46,9 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
const connect = await this.connectedUserRepository.findOneBy({ const connect = await this.connectedUserRepository.findOneBy({
socket: socket.id, socket: socket.id,
}); });
if (connect) if (connect) {
await this.connectedUserRepository.delete({ socket: socket.id }); await this.connectedUserRepository.delete({ socket: socket.id });
}
socket.disconnect(); socket.disconnect();
console.log("socket %s has disconnected", socket.id); console.log("socket %s has disconnected", socket.id);
} }
@ -124,7 +125,7 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect {
} }
if ( if (
channel.owner.ftId !== kick.from && 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"); 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({ const connect = (await this.connectedUserRepository.findOneBy({
user: user.ftId, user: user.ftId,
})) as ConnectedUser; })) as ConnectedUser;
if (connect) {
console.log(`kicking ${user.username} from ${channel.name}`); console.log(`kicking ${user.username} from ${channel.name}`);
// await this.onLeaveChannel(socket)
this.server.sockets.sockets.get(connect.socket)?.emit("kicked"); 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();
} }
} }

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

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

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

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

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

@ -61,16 +61,23 @@
} }
socket.on("newMessage", (msg: chatMessagesType) => { socket.on("newMessage", (msg: chatMessagesType) => {
console.log(msg); console.log(msg);
if (blockedUsers.findIndex((user) => msg.author.ftId === user.ftId) === -1)
messages = [...messages, msg]; messages = [...messages, msg];
}); });
socket.on("messages", (msgs: Array<chatMessagesType>) => { socket.on("messages", (msgs: Array<chatMessagesType>) => {
messages = msgs; 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 () => { usersInterval = setInterval(async () => {
getMembers(); getMembers();
}, 1000); }, 1000);
console.log("You are joining channel: ", channel.name);
}); });
socket.on("failedJoin", (error: string) => { socket.on("failedJoin", (error: string) => {

Loading…
Cancel
Save