avoid dispatching channel events to all clients

This commit is contained in:
hippoz 2022-04-12 00:19:29 +03:00
parent 6a6cc1aafd
commit 94192979a8
Signed by: hippoz
GPG key ID: 7C52899193467641
4 changed files with 20 additions and 11 deletions

View file

@ -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
);

View file

@ -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;

View file

@ -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

View file

@ -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"
}
###