From ce9d331bc68da12b8f7f9ae53206cd3951190619 Mon Sep 17 00:00:00 2001 From: hippoz <10706925-hippoz@users.noreply.gitlab.com> Date: Sun, 10 Apr 2022 22:14:10 +0300 Subject: [PATCH] dispatch a gateway event when a channel is created, updated or removed --- src/gateway/gatewaypayloadtype.ts | 6 +++++- src/routes/api/v1/channels.ts | 32 +++++++++++++++++++++++++++---- test.rest | 4 ++-- 3 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/gateway/gatewaypayloadtype.ts b/src/gateway/gatewaypayloadtype.ts index 6c848c3..65af6b4 100644 --- a/src/gateway/gatewaypayloadtype.ts +++ b/src/gateway/gatewaypayloadtype.ts @@ -2,5 +2,9 @@ export enum GatewayPayloadType { Hello = 0, Authenticate, Ready, - Ping + Ping, + + ChannelCreate = 1000, + ChannelUpdate, + ChannelDelete } diff --git a/src/routes/api/v1/channels.ts b/src/routes/api/v1/channels.ts index c83b9db..0ac0c7b 100644 --- a/src/routes/api/v1/channels.ts +++ b/src/routes/api/v1/channels.ts @@ -4,6 +4,7 @@ import { authenticateRoute } from "../../../auth"; import { query } from "../../../database"; import { errors } from "../../../errors"; import { dispatch } from "../../../gateway"; +import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype"; const router = express.Router(); @@ -25,6 +26,11 @@ router.post( }); } + dispatch("*", { + t: GatewayPayloadType.ChannelCreate, + d: result.rows[0] + }); + res.status(201).send(result.rows[0]); } ); @@ -41,7 +47,7 @@ router.put( } const { name } = req.body; - const { id } = req.params; + const id = parseInt(req.params.id); // TODO: ?? const permissionCheckResult = await query("SELECT owner_id FROM channels WHERE id = $1", [id]); if (permissionCheckResult.rowCount < 1) { @@ -62,11 +68,20 @@ router.put( }); } - return res.status(200).send({ - id: parseInt(id), // TODO: ?? + const updatePayload = { + id, name, owner_id: permissionCheckResult.rows[0].owner_id + }; + + // TODO: implement per-user channel joining and communities + //dispatch(`channel:${id}`, { + dispatch("*", { + t: GatewayPayloadType.ChannelUpdate, + d: updatePayload }); + + return res.status(200).send(updatePayload); } ); @@ -80,7 +95,7 @@ router.delete( return res.status(400).json({ ...errors.INVALID_DATA, errors: validationErrors.array() }); } - const { id } = req.params; + const id = parseInt(req.params.id); // TODO: ?? const permissionCheckResult = await query("SELECT owner_id FROM channels WHERE id = $1", [id]); if (permissionCheckResult.rowCount < 1) { @@ -101,6 +116,15 @@ router.delete( }); } + // TODO: implement per-user channel joining and communities + //dispatch(`channel:${id}`, { + dispatch("*", { + t: GatewayPayloadType.ChannelDelete, + d: { + id + } + }); + return res.status(204).send(""); } ); diff --git a/test.rest b/test.rest index ff7ea59..0091900 100644 --- a/test.rest +++ b/test.rest @@ -28,12 +28,12 @@ content-type: application/json Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NTI0NDA1LCJleHAiOjE2NDk2OTcyMDV9.4nIDs0K8MCT18GsdlKdicT_GK2KbEqi_P7cND3_aZvE { - "name": "another channel" + "name": "yet another channel" } ### -PUT http://localhost:3000/api/v1/channels/2 HTTP/1.1 +PUT http://localhost:3000/api/v1/channels/5 HTTP/1.1 content-type: application/json Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NTI0NDA1LCJleHAiOjE2NDk2OTcyMDV9.4nIDs0K8MCT18GsdlKdicT_GK2KbEqi_P7cND3_aZvE #Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5MjU5NDUwLCJleHAiOjE2NDk0MzIyNTB9.JmF9NujFZnln7A-ynNpeyayGBqmR5poAyACYV6RnSQY