From aae5ce66caac1689352e4cdacee737b1fa369d74 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 6 Feb 2022 22:58:38 +0200 Subject: [PATCH] add alive check for gateway and fix webhook message event bug --- GatewayServer.js | 21 ++++++++++++++++++++- WatchedGuild.js | 5 ----- frontend/src/components/App.svelte | 9 +++++++-- frontend/src/components/FuzzyView.svelte | 8 ++++++-- 4 files changed, 33 insertions(+), 10 deletions(-) diff --git a/GatewayServer.js b/GatewayServer.js index e756577..ee1e9ae 100644 --- a/GatewayServer.js +++ b/GatewayServer.js @@ -18,6 +18,8 @@ class GatewayServer { ...extraWebsocketServerConfig }); + setInterval(this.onAliveCheck.bind(this), 30000); + this.wss.on("connection", (ws) => { this.onConnection(ws); ws.on("message", (data, isBinary) => { @@ -25,7 +27,10 @@ class GatewayServer { }); ws.on("close", (code, reason) => { this.onDisconnect(code, reason.toString()); - }) + }); + ws.on("pong", () => { + this.onPong(ws); + }); }); } @@ -187,6 +192,20 @@ class GatewayServer { } } } + + onPong(ws) { + this._clientDispatch(ws, { type: "SET_ALIVE", alive: true }); + } + + onAliveCheck() { + this.wss.clients.forEach(ws => { + if (ws.isAlive === false) + return ws.terminate(); + + this._clientDispatch(ws, { type: "SET_ALIVE", alive: false }); + ws.ping(); + }); + } } export default GatewayServer; diff --git a/WatchedGuild.js b/WatchedGuild.js index a75a8d2..8cc549a 100644 --- a/WatchedGuild.js +++ b/WatchedGuild.js @@ -59,11 +59,6 @@ class WatchedGuild extends EventEmitter { if (message.guild_id !== this.upstreamGuildId) return; - // TODOOOOOO: bridge user's wont get message events from other bridge users in the same channel - const maybeKnownWebhook = this.knownWebhooks.get(message.channel_id); - if (maybeKnownWebhook && maybeKnownWebhook.id === message.webhook_id) - return; // ignore messages coming from our webhook - this.pushEvent({ eventType: "MESSAGE_CREATE", message: message diff --git a/frontend/src/components/App.svelte b/frontend/src/components/App.svelte index 347c309..eaa3fd9 100644 --- a/frontend/src/components/App.svelte +++ b/frontend/src/components/App.svelte @@ -15,6 +15,11 @@ function handleEvent(event) { if (event.eventType === "MESSAGE_CREATE") { + // ignore messages that are seemingly coming from ourselves + if (event.message.webhook_id && event.message.author.username === user.username) { + return; + } + const guildId = event.message.guild_id; const channelId = event.message.channel_id; @@ -69,13 +74,13 @@ selectedChannel = guilds[0].channels[0]; } - if (supportsWebsockets()) { + if (supportsWebsockets() && !localStorage.getItem("f_disableWebsockets")) { console.log("App: browser supports WebSocket, using gateway"); const gatewayConnection = new GatewayClient(`${ location.protocol === "https:" ? "wss://" : "ws://" }${location.host}/gateway`); gatewayConnection.onEvent = handleEvent; gatewayConnection.connect(apiClient.token); } else { - console.warn("App: browser does not WebSocket, using polling"); + console.warn("App: browser does not support WebSocket, using polling"); const { poll } = apiClient.createPollingListener(null, ({ event }) => { handleEvent(event); }); diff --git a/frontend/src/components/FuzzyView.svelte b/frontend/src/components/FuzzyView.svelte index 506d5a5..3db952c 100644 --- a/frontend/src/components/FuzzyView.svelte +++ b/frontend/src/components/FuzzyView.svelte @@ -43,8 +43,12 @@
{ title } - {#each displayedElements as element} - + {#each displayedElements as element, index} + {#if index === 0} + + {:else} + + {/if} {/each} {#if displayedElements.length < 1}