From 6229398316b1cfbf110a501a67cbb537aa126549 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 25 Sep 2021 04:20:11 +0200 Subject: :bug: fix guild template create --- api/src/routes/guilds/index.ts | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) (limited to 'api/src') diff --git a/api/src/routes/guilds/index.ts b/api/src/routes/guilds/index.ts index abde147d..6a325ae2 100644 --- a/api/src/routes/guilds/index.ts +++ b/api/src/routes/guilds/index.ts @@ -87,19 +87,21 @@ router.post("/", route({ body: "GuildCreateSchema" }), async (req: Request, res: }); await Promise.all( - body.channels?.map((x) => { - var id = ids.get(x.id) || Snowflake.generate(); - - // TODO: should we abort if parent_id is a category? (to disallow sub category channels) - var parent_id = ids.get(x.parent_id); - - return Channel.createChannel({ ...x, guild_id, id, parent_id }, req.user_id, { - keepId: true, - skipExistsCheck: true, - skipPermissionCheck: true, - skipEventEmit: true - }); - }) + body.channels + ?.sort((a, b) => (a.parent_id ? -1 : 1)) + .map((x) => { + var id = ids.get(x.id) || Snowflake.generate(); + + // TODO: should we abort if parent_id is a category? (to disallow sub category channels) + var parent_id = ids.get(x.parent_id); + + return Channel.createChannel({ ...x, guild_id, id, parent_id }, req.user_id, { + keepId: true, + skipExistsCheck: true, + skipPermissionCheck: true, + skipEventEmit: true + }); + }) ); await Member.addToGuild(req.user_id, guild_id); -- cgit 1.5.1 From a83c9fe510d92fdccef264abc130e628b71e09db Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 25 Sep 2021 04:20:35 +0200 Subject: :bug: fix #392 --- api/src/util/route.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'api/src') diff --git a/api/src/util/route.ts b/api/src/util/route.ts index e7c7ed1c..45882d8a 100644 --- a/api/src/util/route.ts +++ b/api/src/util/route.ts @@ -81,10 +81,10 @@ export function route(opts: RouteOptions) { return async (req: Request, res: Response, next: NextFunction) => { if (opts.permission) { const required = new Permissions(opts.permission); - const permission = await getPermission(req.user_id, req.params.guild_id, req.params.channel_id); + req.permission = await getPermission(req.user_id, req.params.guild_id, req.params.channel_id); // bitfield comparison: check if user lacks certain permission - if (!permission.has(required)) { + if (!req.permission.has(required)) { throw DiscordApiErrors.MISSING_PERMISSIONS.withParams(opts.permission as string); } } -- cgit 1.5.1 From 202a8bd187c826889a0ee473794fdc92b867f0b8 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 25 Sep 2021 23:54:52 +0200 Subject: :bug: message attachment url --- api/src/routes/channels/#channel_id/messages/index.ts | 5 +++++ 1 file changed, 5 insertions(+) (limited to 'api/src') diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index fab20977..d3321fae 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -3,6 +3,7 @@ import { Attachment, Channel, ChannelType, + Config, DmChannelDTO, Embed, emitEvent, @@ -15,6 +16,7 @@ import { HTTPError } from "lambert-server"; import { handleMessage, postHandleMessage, route } from "@fosscord/api"; import multer from "multer"; import { FindManyOptions, LessThan, MoreThan } from "typeorm"; +import { URL } from "url"; const router: Router = Router(); @@ -111,6 +113,9 @@ router.get("/", async (req: Request, res: Response) => { }); // @ts-ignore if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null }; + x.attachments?.forEach((x) => { + x.proxy_url = `${Config.get().cdn.endpointPublic || "http://localhost:3003"}${new URL(x.proxy_url).pathname}`; + }); return x; }) -- cgit 1.5.1 From 286b4a69fd1a4a232afaea69aa29724cd7c53746 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 25 Sep 2021 23:55:19 +0200 Subject: :sparkles: add private and public endpoint --- api/src/middlewares/TestClient.ts | 4 +-- api/src/routes/gateway.ts | 8 +++--- api/src/util/cdn.ts | 53 --------------------------------------- bundle/src/Server.ts | 16 +++++++++--- bundle/src/stats.ts | 10 +++++--- cdn/src/routes/avatars.ts | 3 ++- util/src/util/cdn.ts | 4 +-- 7 files changed, 29 insertions(+), 69 deletions(-) delete mode 100644 api/src/util/cdn.ts (limited to 'api/src') diff --git a/api/src/middlewares/TestClient.ts b/api/src/middlewares/TestClient.ts index 79f8f442..7db35285 100644 --- a/api/src/middlewares/TestClient.ts +++ b/api/src/middlewares/TestClient.ts @@ -9,11 +9,11 @@ export default function TestClient(app: Application) { const indexHTML = fs.readFileSync(path.join(__dirname, "..", "..", "client_test", "index.html"), { encoding: "utf8" }); var html = indexHTML; - const CDN_ENDPOINT = (Config.get().cdn.endpointClient || Config.get()?.cdn.endpoint || process.env.CDN || "").replace( + const CDN_ENDPOINT = (Config.get().cdn.endpointClient || Config.get()?.cdn.endpointPublic || process.env.CDN || "").replace( /(https?)?(:\/\/?)/g, "" ); - const GATEWAY_ENDPOINT = Config.get().gateway.endpointClient || Config.get()?.gateway.endpoint || process.env.GATEWAY || ""; + const GATEWAY_ENDPOINT = Config.get().gateway.endpointClient || Config.get()?.gateway.endpointPublic || process.env.GATEWAY || ""; if (CDN_ENDPOINT) { html = html.replace(/CDN_HOST: .+/, `CDN_HOST: \`${CDN_ENDPOINT}\`,`); diff --git a/api/src/routes/gateway.ts b/api/src/routes/gateway.ts index 88d9dfda..4b3a0ea6 100644 --- a/api/src/routes/gateway.ts +++ b/api/src/routes/gateway.ts @@ -5,14 +5,14 @@ import { route } from "@fosscord/api"; const router = Router(); router.get("/", route({}), (req: Request, res: Response) => { - const { endpoint } = Config.get().gateway; - res.json({ url: endpoint || process.env.GATEWAY || "ws://localhost:3002" }); + const { endpointPublic } = Config.get().gateway; + res.json({ url: endpointPublic || process.env.GATEWAY || "ws://localhost:3002" }); }); router.get("/bot", route({}), (req: Request, res: Response) => { - const { endpoint } = Config.get().gateway; + const { endpointPublic } = Config.get().gateway; res.json({ - url: endpoint || process.env.GATEWAY || "ws://localhost:3002", + url: endpointPublic || process.env.GATEWAY || "ws://localhost:3002", shards: 1, session_start_limit: { total: 1000, diff --git a/api/src/util/cdn.ts b/api/src/util/cdn.ts deleted file mode 100644 index 8c6e9ac9..00000000 --- a/api/src/util/cdn.ts +++ /dev/null @@ -1,53 +0,0 @@ -import { Config } from "@fosscord/util"; -import FormData from "form-data"; -import { HTTPError } from "lambert-server"; -import fetch from "node-fetch"; - -export async function uploadFile(path: string, file: Express.Multer.File) { - const form = new FormData(); - form.append("file", file.buffer, { - contentType: file.mimetype, - filename: file.originalname - }); - - const response = await fetch(`${Config.get().cdn.endpoint || "http://localhost:3003"}${path}`, { - headers: { - signature: Config.get().security.requestSignature, - ...form.getHeaders() - }, - method: "POST", - body: form - }); - const result = await response.json(); - - if (response.status !== 200) throw result; - return result; -} - -export async function handleFile(path: string, body?: string): Promise { - if (!body || !body.startsWith("data:")) return body; - try { - const mimetype = body.split(":")[1].split(";")[0]; - const buffer = Buffer.from(body.split(",")[1], "base64"); - - // @ts-ignore - const { id } = await uploadFile(path, { buffer, mimetype, originalname: "banner" }); - return id; - } catch (error) { - console.error(error); - throw new HTTPError("Invalid " + path); - } -} - -export async function deleteFile(path: string) { - const response = await fetch(`${Config.get().cdn.endpoint || "http://localhost:3003"}${path}`, { - headers: { - signature: Config.get().security.requestSignature - }, - method: "DELETE" - }); - const result = await response.json(); - - if (response.status !== 200) throw result; - return result; -} diff --git a/bundle/src/Server.ts b/bundle/src/Server.ts index 56c82cd1..62764e8e 100644 --- a/bundle/src/Server.ts +++ b/bundle/src/Server.ts @@ -24,16 +24,24 @@ const gateway = new GatewayServer({ server, port, production }); async function main() { await initDatabase(); await Config.init(); + // only set endpointPublic, if not already set await Config.set({ cdn: { endpointClient: "${location.host}", - endpoint: `http://localhost:${port}`, + endpointPrivate: `http://localhost:${port}`, + ...(!Config.get().cdn.endpointPublic && { + endpointPublic: `http://localhost:${port}`, + }), }, gateway: { - endpointClient: '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}', - endpoint: `ws://localhost:${port}`, + endpointClient: + '${location.protocol === "https:" ? "wss://" : "ws://"}${location.host}', + endpointPrivate: `ws://localhost:${port}`, + ...(!Config.get().gateway.endpointPublic && { + endpointPublic: `http://localhost:${port}`, + }), }, - }); + } as any); await Promise.all([api.start(), cdn.start(), gateway.start()]); console.log(`[Server] listening on port ${port}`); diff --git a/bundle/src/stats.ts b/bundle/src/stats.ts index 37ddb4e2..e6941db2 100644 --- a/bundle/src/stats.ts +++ b/bundle/src/stats.ts @@ -9,16 +9,20 @@ export function initStats() { console.log(`[Process] running with pid: ${process.pid}`); setInterval(async () => { - const [cpuUsed, memory, network] = await Promise.all([osu.cpu.usage(), osu.mem.info(), osu.netstat.inOut()]); + const [cpuUsed, memory, network] = await Promise.all([ + osu.cpu.usage(), + osu.mem.info(), + osu.netstat.inOut(), + ]); var networkUsage = ""; if (typeof network === "object") { networkUsage = `| [Network]: in ${network.total.inputMb}mb | out ${network.total.outputMb}mb`; } console.log( - `[CPU] ${cpuUsed.toFixed(2)}% | [Memory] ${Math.round( + `[CPU] ${cpuUsed.toPrecision(3)}% | [Memory] ${Math.round( process.memoryUsage().rss / 1024 / 1024 )}mb/${memory.totalMemMb.toFixed(0)}mb ${networkUsage}` ); - }, 1000 * 30); + }, 1000 * 5); } diff --git a/cdn/src/routes/avatars.ts b/cdn/src/routes/avatars.ts index 3d745f90..93045925 100644 --- a/cdn/src/routes/avatars.ts +++ b/cdn/src/routes/avatars.ts @@ -44,7 +44,8 @@ router.post( if (ANIMATED_MIME_TYPES.includes(type.mime)) hash = `a_${hash}`; // animated icons have a_ infront of the hash const path = `avatars/${user_id}/${hash}`; - const endpoint = Config.get().cdn.endpoint || "http://localhost:3003"; + const endpoint = + Config.get().cdn.endpointPublic || "http://localhost:3003"; await storage.set(path, buffer); diff --git a/util/src/util/cdn.ts b/util/src/util/cdn.ts index 754d6244..2de23f5d 100644 --- a/util/src/util/cdn.ts +++ b/util/src/util/cdn.ts @@ -11,7 +11,7 @@ export async function uploadFile(path: string, file: Express.Multer.File) { filename: file.originalname, }); - const response = await fetch(`${Config.get().cdn.endpoint || "http://localhost:3003"}${path}`, { + const response = await fetch(`${Config.get().cdn.endpointPrivate || "http://localhost:3003"}${path}`, { headers: { signature: Config.get().security.requestSignature, ...form.getHeaders(), @@ -41,7 +41,7 @@ export async function handleFile(path: string, body?: string): Promise Date: Sun, 26 Sep 2021 11:34:06 +0200 Subject: :bug: fix channel permission overwrites --- api/assets/schemas.json | 176 ++++++++++----------- api/package.json | 4 +- api/scripts/generate_schema.js | 8 +- api/src/routes/channels/#channel_id/index.ts | 4 +- api/src/routes/channels/#channel_id/permissions.ts | 21 ++- util/src/entities/Channel.ts | 4 +- 6 files changed, 112 insertions(+), 105 deletions(-) (limited to 'api/src') diff --git a/api/assets/schemas.json b/api/assets/schemas.json index 76ad3b16..023f2092 100644 --- a/api/assets/schemas.json +++ b/api/assets/schemas.json @@ -1,73 +1,73 @@ { - "RegisterSchema": { + "LoginSchema": { "type": "object", "properties": { - "username": { - "minLength": 2, - "maxLength": 32, + "login": { "type": "string" }, "password": { - "minLength": 1, - "maxLength": 72, "type": "string" }, - "consent": { + "undelete": { "type": "boolean" }, - "email": { - "format": "email", - "type": "string" - }, - "fingerprint": { - "type": "string" - }, - "invite": { + "captcha_key": { "type": "string" }, - "date_of_birth": { + "login_source": { "type": "string" }, "gift_code_sku_id": { "type": "string" - }, - "captcha_key": { - "type": "string" } }, "additionalProperties": false, "required": [ - "consent", - "username" + "login", + "password" ], "$schema": "http://json-schema.org/draft-07/schema#" }, - "LoginSchema": { + "RegisterSchema": { "type": "object", "properties": { - "login": { + "username": { + "minLength": 2, + "maxLength": 32, "type": "string" }, "password": { + "minLength": 1, + "maxLength": 72, "type": "string" }, - "undelete": { + "consent": { "type": "boolean" }, - "captcha_key": { + "email": { + "format": "email", "type": "string" }, - "login_source": { + "fingerprint": { + "type": "string" + }, + "invite": { + "type": "string" + }, + "date_of_birth": { "type": "string" }, "gift_code_sku_id": { "type": "string" + }, + "captcha_key": { + "type": "string" } }, "additionalProperties": false, "required": [ - "login", - "password" + "consent", + "username" ], "$schema": "http://json-schema.org/draft-07/schema#" }, @@ -127,10 +127,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -353,10 +353,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -727,10 +727,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -1050,10 +1050,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -1352,10 +1352,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -1657,10 +1657,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -1758,10 +1758,10 @@ "type": "object", "properties": { "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" }, "id": { "type": "string" @@ -1971,10 +1971,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -2278,10 +2278,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -2580,10 +2580,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -2894,10 +2894,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -3221,10 +3221,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -3587,10 +3587,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -3889,10 +3889,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -4191,10 +4191,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -4505,10 +4505,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -4814,10 +4814,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -5119,10 +5119,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -5424,10 +5424,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -5725,10 +5725,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -6046,10 +6046,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -6374,10 +6374,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -6680,10 +6680,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -6988,10 +6988,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -7302,10 +7302,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -7610,10 +7610,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -7939,10 +7939,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -8244,10 +8244,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -8550,10 +8550,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, @@ -9039,10 +9039,10 @@ "$ref": "#/definitions/ChannelPermissionOverwriteType" }, "allow": { - "type": "bigint" + "type": "string" }, "deny": { - "type": "bigint" + "type": "string" } }, "additionalProperties": false, diff --git a/api/package.json b/api/package.json index cc47a45a..8fc55e94 100644 --- a/api/package.json +++ b/api/package.json @@ -14,8 +14,8 @@ "dev": "tsnd --respawn src/start.ts", "patch": "ts-patch install -s && npx patch-package", "postinstall": "npm run patch", - "generate:docs": "node scripts/generate_openapi.ts", - "generate:schema": "node scripts/generate_schema.ts" + "generate:docs": "node scripts/generate_openapi", + "generate:schema": "node scripts/generate_schema" }, "repository": { "type": "git", diff --git a/api/scripts/generate_schema.js b/api/scripts/generate_schema.js index 22d0b02e..fc787c22 100644 --- a/api/scripts/generate_schema.js +++ b/api/scripts/generate_schema.js @@ -1,9 +1,9 @@ // https://mermade.github.io/openapi-gui/# // https://editor.swagger.io/ -import path from "path"; -import fs from "fs"; -import * as TJS from "typescript-json-schema"; -import "missing-native-js-functions"; +const path = require("path"); +const fs = require("fs"); +const TJS = require("typescript-json-schema"); +require("missing-native-js-functions"); const schemaPath = path.join(__dirname, "..", "assets", "schemas.json"); const settings = { diff --git a/api/src/routes/channels/#channel_id/index.ts b/api/src/routes/channels/#channel_id/index.ts index 61c851e8..2fca4fdf 100644 --- a/api/src/routes/channels/#channel_id/index.ts +++ b/api/src/routes/channels/#channel_id/index.ts @@ -62,8 +62,8 @@ export interface ChannelModifySchema { permission_overwrites?: { id: string; type: ChannelPermissionOverwriteType; - allow: bigint; - deny: bigint; + allow: string; + deny: string; }[]; parent_id?: string; id?: string; // is not used (only for guild create) diff --git a/api/src/routes/channels/#channel_id/permissions.ts b/api/src/routes/channels/#channel_id/permissions.ts index bc7ad5b8..6ebf721a 100644 --- a/api/src/routes/channels/#channel_id/permissions.ts +++ b/api/src/routes/channels/#channel_id/permissions.ts @@ -1,4 +1,13 @@ -import { Channel, ChannelPermissionOverwrite, ChannelUpdateEvent, emitEvent, getPermission, Member, Role } from "@fosscord/util"; +import { + Channel, + ChannelPermissionOverwrite, + ChannelPermissionOverwriteType, + ChannelUpdateEvent, + emitEvent, + getPermission, + Member, + Role +} from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; @@ -14,7 +23,7 @@ router.put( route({ body: "ChannelPermissionOverwriteSchema", permission: "MANAGE_ROLES" }), async (req: Request, res: Response) => { const { channel_id, overwrite_id } = req.params; - const body = req.body as { allow: bigint; deny: bigint; type: number; id: string }; + const body = req.body as ChannelPermissionOverwriteSchema; var channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) throw new HTTPError("Channel not found", 404); @@ -31,14 +40,12 @@ router.put( // @ts-ignore overwrite = { id: overwrite_id, - type: body.type, - allow: body.allow, - deny: body.deny + type: body.type }; channel.permission_overwrites!.push(overwrite); } - overwrite.allow = body.allow; - overwrite.deny = body.deny; + overwrite.allow = String(req.permission!.bitfield & (BigInt(body.allow) || 0n)); + overwrite.deny = String(req.permission!.bitfield & (BigInt(body.deny) || 0n)); await Promise.all([ channel.save(), diff --git a/util/src/entities/Channel.ts b/util/src/entities/Channel.ts index ece82bd0..1644b265 100644 --- a/util/src/entities/Channel.ts +++ b/util/src/entities/Channel.ts @@ -320,8 +320,8 @@ export class Channel extends BaseClass { } export interface ChannelPermissionOverwrite { - allow: bigint; // for bitfields we use bigints - deny: bigint; // for bitfields we use bigints + allow: string; + deny: string; id: string; type: ChannelPermissionOverwriteType; } -- cgit 1.5.1