dispatch a gateway event when a channel is created, updated or removed

This commit is contained in:
hippoz 2022-04-10 22:14:10 +03:00
parent 701f6ae1ac
commit ce9d331bc6
Signed by: hippoz
GPG key ID: 7C52899193467641
3 changed files with 35 additions and 7 deletions

View file

@ -2,5 +2,9 @@ export enum GatewayPayloadType {
Hello = 0, Hello = 0,
Authenticate, Authenticate,
Ready, Ready,
Ping Ping,
ChannelCreate = 1000,
ChannelUpdate,
ChannelDelete
} }

View file

@ -4,6 +4,7 @@ import { authenticateRoute } from "../../../auth";
import { query } from "../../../database"; import { query } from "../../../database";
import { errors } from "../../../errors"; import { errors } from "../../../errors";
import { dispatch } from "../../../gateway"; import { dispatch } from "../../../gateway";
import { GatewayPayloadType } from "../../../gateway/gatewaypayloadtype";
const router = express.Router(); 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]); res.status(201).send(result.rows[0]);
} }
); );
@ -41,7 +47,7 @@ router.put(
} }
const { name } = req.body; 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]); const permissionCheckResult = await query("SELECT owner_id FROM channels WHERE id = $1", [id]);
if (permissionCheckResult.rowCount < 1) { if (permissionCheckResult.rowCount < 1) {
@ -62,11 +68,20 @@ router.put(
}); });
} }
return res.status(200).send({ const updatePayload = {
id: parseInt(id), // TODO: ?? id,
name, name,
owner_id: permissionCheckResult.rows[0].owner_id 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() }); 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]); const permissionCheckResult = await query("SELECT owner_id FROM channels WHERE id = $1", [id]);
if (permissionCheckResult.rowCount < 1) { 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(""); return res.status(204).send("");
} }
); );

View file

@ -28,12 +28,12 @@ content-type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NTI0NDA1LCJleHAiOjE2NDk2OTcyMDV9.4nIDs0K8MCT18GsdlKdicT_GK2KbEqi_P7cND3_aZvE 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 content-type: application/json
Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NTI0NDA1LCJleHAiOjE2NDk2OTcyMDV9.4nIDs0K8MCT18GsdlKdicT_GK2KbEqi_P7cND3_aZvE Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5NTI0NDA1LCJleHAiOjE2NDk2OTcyMDV9.4nIDs0K8MCT18GsdlKdicT_GK2KbEqi_P7cND3_aZvE
#Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5MjU5NDUwLCJleHAiOjE2NDk0MzIyNTB9.JmF9NujFZnln7A-ynNpeyayGBqmR5poAyACYV6RnSQY #Authorization: Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpZCI6MiwidHlwZSI6MSwiaWF0IjoxNjQ5MjU5NDUwLCJleHAiOjE2NDk0MzIyNTB9.JmF9NujFZnln7A-ynNpeyayGBqmR5poAyACYV6RnSQY