From 94192979a8a8f849612245d1640c2455e235361c Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Tue, 12 Apr 2022 00:19:29 +0300 Subject: [PATCH] avoid dispatching channel events to all clients --- src/database/init.ts | 2 +- src/gateway/index.ts | 9 +++++++++ src/routes/api/v1/channels.ts | 14 +++++++------- test.rest | 6 +++--- 4 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/database/init.ts b/src/database/init.ts index fb170a4..81ab7d3 100644 --- a/src/database/init.ts +++ b/src/database/init.ts @@ -10,7 +10,7 @@ export default async function databaseInit() { CREATE TABLE IF NOT EXISTS channels( id SERIAL PRIMARY KEY, - name VARCHAR(32) UNIQUE NOT NULL, + name VARCHAR(32) NOT NULL, owner_id SERIAL REFERENCES users ON DELETE CASCADE ); diff --git a/src/gateway/index.ts b/src/gateway/index.ts index 7deb6d6..4dbdb64 100644 --- a/src/gateway/index.ts +++ b/src/gateway/index.ts @@ -36,6 +36,15 @@ function clientUnsubscribe(ws: WebSocket, dispatchChannel: string) { } } +export function dispatchChannelSubscribe(target: string, dispatchChannel: string) { + const set = dispatchChannels.get(target); + if (!set) return; + + set.forEach(c => { + clientSubscribe(c, dispatchChannel); + }); +} + function clientUnsubscribeAll(ws: WebSocket) { if (!ws.state) return; diff --git a/src/routes/api/v1/channels.ts b/src/routes/api/v1/channels.ts index 85a9061..a4c336e 100644 --- a/src/routes/api/v1/channels.ts +++ b/src/routes/api/v1/channels.ts @@ -3,7 +3,7 @@ import { body, param, validationResult } from "express-validator"; import { authenticateRoute } from "../../../auth"; import { query } from "../../../database"; import { errors } from "../../../errors"; -import { dispatch } from "../../../gateway"; +import { dispatch, dispatchChannelSubscribe } from "../../../gateway"; import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype"; const router = express.Router(); @@ -31,6 +31,10 @@ router.post( d: result.rows[0] }); + // When a new channel is created, we will currently subscribe every client + // on the gateway (this will be changed when the concept of "communities" is added) + dispatchChannelSubscribe("*", `channel:${result.rows[0].id}`); + res.status(201).send(result.rows[0]); } ); @@ -74,9 +78,7 @@ router.put( owner_id: permissionCheckResult.rows[0].owner_id }; - // TODO: implement per-user channel joining and communities - //dispatch(`channel:${id}`, { - dispatch("*", { + dispatch(`channel:${id}`, { t: GatewayPayloadType.ChannelUpdate, d: updatePayload }); @@ -116,9 +118,7 @@ router.delete( }); } - // TODO: implement per-user channel joining and communities - //dispatch(`channel:${id}`, { - dispatch("*", { + dispatch(`channel:${id}`, { t: GatewayPayloadType.ChannelDelete, d: { id diff --git a/test.rest b/test.rest index 9db53bb..35f62d7 100644 --- a/test.rest +++ b/test.rest @@ -28,18 +28,18 @@ content-type: application/json Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NzAwMTE0LCJleHAiOjE2NDk4NzI5MTR9.EOn8MBHZLCxfU5fHc0ZY2x9p3y-_RdD7X915L1B6Ftc { - "name": "yet another channel" + "name": "yet another channelllll" } ### -PUT http://localhost:3000/api/v1/channels/5 HTTP/1.1 +PUT http://localhost:3000/api/v1/channels/8 HTTP/1.1 content-type: application/json Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NzAwMTE0LCJleHAiOjE2NDk4NzI5MTR9.EOn8MBHZLCxfU5fHc0ZY2x9p3y-_RdD7X915L1B6Ftc #Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5MjU5NDUwLCJleHAiOjE2NDk0MzIyNTB9.JmF9NujFZnln7A-ynNpeyayGBqmR5poAyACYV6RnSQY { - "name": "this is my channel" + "name": "this is my channelaaaaaa" } ###