From 3fa6755eed5eb33eb41b11005af09f50d301996b Mon Sep 17 00:00:00 2001 From: Pheuw1 Date: Sat, 18 Mar 2023 23:00:47 +0100 Subject: [PATCH] socket fix and chat routing --- back/volume/src/chat/chat.gateway.ts | 9 +++-- front/volume/src/components/Channels.svelte | 15 +++++++- front/volume/src/components/Chat.svelte | 40 ++++++++++----------- 3 files changed, 40 insertions(+), 24 deletions(-) diff --git a/back/volume/src/chat/chat.gateway.ts b/back/volume/src/chat/chat.gateway.ts index ebf4b70..31a025f 100644 --- a/back/volume/src/chat/chat.gateway.ts +++ b/back/volume/src/chat/chat.gateway.ts @@ -42,7 +42,9 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { async handleConnection (socket: Socket): Promise {} - async handleDisconnect (socket: Socket): Promise {} + async handleDisconnect (socket: Socket): Promise { + console.log('socket %s has disconnected', socket.id) + } @SubscribeMessage('joinChannel') async onJoinChannel (socket: Socket, connect: ConnectionDto): Promise { @@ -135,7 +137,10 @@ export class ChatGateway implements OnGatewayConnection, OnGatewayDisconnect { const connect = (await this.connectedUserRepository.findOneBy({ user: user.id })) as ConnectedUser - await this.onLeaveChannel(socket) + // await this.onLeaveChannel(socket) + await this.server.sockets.sockets + .get(connect.socket) + ?.leave(channel.id.toString()) this.server.sockets.sockets.get(connect.socket)?.disconnect() } } diff --git a/front/volume/src/components/Channels.svelte b/front/volume/src/components/Channels.svelte index 9a36ce0..85079d2 100644 --- a/front/volume/src/components/Channels.svelte +++ b/front/volume/src/components/Channels.svelte @@ -238,7 +238,7 @@
{#if channels.length > 0} -

Channels

+

Channels

{#each channels as channel}
  • {channel.name} : {channel.id} @@ -352,6 +352,19 @@ font-size: 100%; } + .refresh { + display: inline-block; + background-color: rgb(187, 187, 187); + color: #a0a0a0; + border: none; + border-radius: 5px; + font-size: 14px; + cursor: pointer; + outline: none; + position:relative; + top:-10px; + right:auto + } .button { background-color: #6b8e23; color: #ffffff; diff --git a/front/volume/src/components/Chat.svelte b/front/volume/src/components/Chat.svelte index 62fc6a1..34feef5 100644 --- a/front/volume/src/components/Chat.svelte +++ b/front/volume/src/components/Chat.svelte @@ -39,7 +39,7 @@ mode: "cors", }); if (res.ok) blockedUsers = await res.json(); - res = await fetch(`${API_URL}/channels/${channel.id}/users`, { + res = await fetch(`${API_URL}/channels/${channel.id}/users`, { credentials: "include", mode: "cors" }) @@ -56,7 +56,6 @@ socket.disconnect(); }); - //--------------------------------------------------------------------------------/ const sendMessage = () => { @@ -140,8 +139,8 @@ body: JSON.stringify({ id: target.ftId }), }); } - if (response.ok) show_popup("User blocked", false); - else show_popup("Failed to block user", false); + if (response.ok) await show_popup("User blocked", false); + else await show_popup("Failed to block user", false); }; //--------------------------------------------------------------------------------/ @@ -164,11 +163,9 @@ }, body: JSON.stringify({ data: [target.ftId, duration]}), }); - socket.emit("kickUser", channel.id, $store.ftId, target.ftId); + socket.emit("kickUser", channel.id, $store.ftId, target.ftId) + dispatch("return-home"); } - if (response.ok) { - show_popup("User banned", false); - } else show_popup("Failed to ban user", false); }; //--------------------------------------------------------------------------------/ @@ -190,8 +187,8 @@ body: JSON.stringify({ id: target.ftId }), }); } - if (response.ok) show_popup("User unbanned",false); - else show_popup("Failed to unban user",false); + if (response.ok) await show_popup("User unbanned",false); + else await show_popup("Failed to unban user",false); }; //--------------------------------------------------------------------------------/ @@ -204,7 +201,8 @@ if (response.ok) { const target = await response.json(); socket.emit("kickUser", {chan : channel.id, from : $store.ftId, to: target.ftId}); - } else {show_popup("merde",false)} + dispatch("return-home"); + } else {await show_popup("merde",false)} }; //--------------------------------------------------------------------------------/ @@ -217,7 +215,7 @@ }); const target = await response.json(); if (response.ok) { - response = await fetch(API_URL + "/channels/" + channel.id + "/mute", { + response = await fetch(API_URL + "/channels/" + channel.id + "/mute", { credentials: "include", method: "POST", mode: "cors", @@ -227,8 +225,8 @@ body: JSON.stringify({ data: [target.ftId, +prompt] }), }); } - if (response.ok) show_popup("User muted",false); - else show_popup("Failed to mute user",false); + if (response.ok) await show_popup("User muted",false); + else await show_popup("Failed to mute user",false); }; //--------------------------------------------------------------------------------/ @@ -251,9 +249,9 @@ }); } if (response.ok) { - show_popup("User admined",false); + await show_popup("User admined",false); } else { - show_popup("Failed to admin user",false); + await show_popup("Failed to admin user",false); } }; @@ -277,9 +275,9 @@ }); } if (response.ok) { - show_popup("User admined", false); + await show_popup("User admined", false); } else { - show_popup("Failed to admin user", false); + await show_popup("Failed to admin user", false); } }; @@ -288,10 +286,10 @@ const leaveChannel = async () => { await show_popup("Press \"Okay\" to leave this channel?", false); if ($content == 'ok') { - socket.emit("LeaveChanel", async (response) => { - console.log(response.status); + socket.emit("leaveChannel"); dispatch("return-home"); - }); + socket.disconnect() + } } function onSendMessage() {