From 3b6fc607838cc3abc0214813c96245a12ec7affd Mon Sep 17 00:00:00 2001 From: uurgothat Date: Sun, 17 Oct 2021 23:20:46 +0300 Subject: [PATCH 1/6] Add discord template support --- api/src/routes/guilds/templates/index.ts | 15 ++++++++++++++- util/src/entities/Config.ts | 6 ++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index 86316d23..f303d690 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -3,7 +3,8 @@ const router: Router = Router(); import { Template, Guild, Role, Snowflake, Config, User, Member } from "@fosscord/util"; import { route } from "@fosscord/api"; import { DiscordApiErrors } from "@fosscord/util"; - +import fetch from "node-fetch"; +import { HTTPError } from "lambert-server"; export interface GuildTemplateCreateSchema { name: string; avatar?: string | null; @@ -12,6 +13,18 @@ export interface GuildTemplateCreateSchema { router.get("/:code", route({}), async (req: Request, res: Response) => { const { code } = req.params; + if (code.startsWith("discord:")) { + const discordTemplateID = code.split("discord:", 2)[1]; + if (Config.get().templates.allowDiscordTemplates == false) return res.json({ code: 403, message: "Discord templates are disabled on this instance."}).sendStatus(403) + + const discordTemplateData = await fetch(`https://discord.com/api/v9/guilds/templates/${discordTemplateID}`, { + method: "get", + headers: { "Content-Type": "application/json" } + }); + + return res.json(await discordTemplateData.json()); + } + const template = await Template.findOneOrFail({ code: code }); res.json(template); diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index b3167ac7..48a8b34e 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -160,6 +160,9 @@ export interface ConfigValue { kafka: { brokers: KafkaBroker[] | null; }; + templates: { + allowDiscordTemplates: Boolean + } } export const DefaultConfigOptions: ConfigValue = { @@ -321,4 +324,7 @@ export const DefaultConfigOptions: ConfigValue = { kafka: { brokers: null, }, + templates: { + allowDiscordTemplates: true + } }; From 6268064241f79ebe80a83d849eb392ce7528d8ec Mon Sep 17 00:00:00 2001 From: uurgothat Date: Sun, 17 Oct 2021 23:23:57 +0300 Subject: [PATCH 2/6] remove unneeded import --- api/src/routes/guilds/templates/index.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index f303d690..81724a76 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -4,7 +4,6 @@ import { Template, Guild, Role, Snowflake, Config, User, Member } from "@fosscor import { route } from "@fosscord/api"; import { DiscordApiErrors } from "@fosscord/util"; import fetch from "node-fetch"; -import { HTTPError } from "lambert-server"; export interface GuildTemplateCreateSchema { name: string; avatar?: string | null; From 89c3ec1b4aff4d6b057ec0897c4f066563301a72 Mon Sep 17 00:00:00 2001 From: uurgothat Date: Mon, 18 Oct 2021 04:36:49 +0300 Subject: [PATCH 3/6] external templates + fosscord draft and more configs --- api/src/routes/guilds/templates/index.ts | 30 ++++++++++++++++++++++-- util/src/entities/Config.ts | 12 ++++++++-- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index 81724a76..c275d5da 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -4,32 +4,58 @@ import { Template, Guild, Role, Snowflake, Config, User, Member } from "@fosscor import { route } from "@fosscord/api"; import { DiscordApiErrors } from "@fosscord/util"; import fetch from "node-fetch"; + +const { enabled, allowTemplateCreation, allowDiscordTemplates, allowOtherInstancesTemplates, allowExternalRaws } = Config.get().templates; export interface GuildTemplateCreateSchema { name: string; avatar?: string | null; } router.get("/:code", route({}), async (req: Request, res: Response) => { + if(enabled == false) return res.json({ code: 403, message: "Templates are disabled on this instance."}).sendStatus(403); const { code } = req.params; if (code.startsWith("discord:")) { + if (allowDiscordTemplates == false) return res.json({ code: 403, message: "Discord templates are disabled on this instance."}).sendStatus(403); const discordTemplateID = code.split("discord:", 2)[1]; - if (Config.get().templates.allowDiscordTemplates == false) return res.json({ code: 403, message: "Discord templates are disabled on this instance."}).sendStatus(403) const discordTemplateData = await fetch(`https://discord.com/api/v9/guilds/templates/${discordTemplateID}`, { method: "get", headers: { "Content-Type": "application/json" } }); - return res.json(await discordTemplateData.json()); + res.json(await discordTemplateData.json()); } + + if (code.startsWith("fosscord:")) { + if (allowOtherInstancesTemplates == false) return res.json({ code: 403, message: "Other instance templates are disabled on this instance."}).sendStatus(403); + //TODO: TBD when federation came out + res.json({}).sendStatus(200) + }; + + //TODO: Validation + if (code.startsWith("external:")) { + if (allowExternalRaws == false) return res.json({ code: 403, message: "Importing templates from raws is disabled on this instance."}).sendStatus(403); + const url = code.split("external:", 2)[1] + + const rawTemplateData = await fetch(`${url}`, { + method: "get", + headers: { "Content-Type": "application/json" } + }) || null; + + res.json(rawTemplateData !== null ? await rawTemplateData.json(): { code: 500, message: "An error occurred while trying to fetch the raw."}); + } + const template = await Template.findOneOrFail({ code: code }); res.json(template); }); router.post("/:code", route({ body: "GuildTemplateCreateSchema" }), async (req: Request, res: Response) => { + if(enabled == false) return res.json({ code: 403, message: "Templates are disabled on this instance."}).sendStatus(403); + if(allowTemplateCreation == false) return res.json({ code: 403, message: "Template creation is disabled on this instance."}).sendStatus(403); + const { code } = req.params; const body = req.body as GuildTemplateCreateSchema; diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index 48a8b34e..1d2d496a 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -161,7 +161,11 @@ export interface ConfigValue { brokers: KafkaBroker[] | null; }; templates: { - allowDiscordTemplates: Boolean + enabled: Boolean; + allowTemplateCreation: Boolean; + allowDiscordTemplates: Boolean; + allowOtherInstancesTemplates: Boolean; + allowExternalRaws: Boolean } } @@ -325,6 +329,10 @@ export const DefaultConfigOptions: ConfigValue = { brokers: null, }, templates: { - allowDiscordTemplates: true + enabled: true, + allowTemplateCreation: true, + allowDiscordTemplates: true, + allowOtherInstancesTemplates: false, //Incomple + allowExternalRaws: false } }; From 1d535a6b674b711b9326afdec9f8ab4b14ec9be4 Mon Sep 17 00:00:00 2001 From: uurgothat Date: Mon, 18 Oct 2021 04:38:26 +0300 Subject: [PATCH 4/6] Format the file --- api/src/routes/guilds/templates/index.ts | 50 ++++++++++++++---------- 1 file changed, 29 insertions(+), 21 deletions(-) diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index c275d5da..b82fb102 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -12,11 +12,12 @@ export interface GuildTemplateCreateSchema { } router.get("/:code", route({}), async (req: Request, res: Response) => { - if(enabled == false) return res.json({ code: 403, message: "Templates are disabled on this instance."}).sendStatus(403); + if (enabled == false) return res.json({ code: 403, message: "Templates are disabled on this instance." }).sendStatus(403); const { code } = req.params; if (code.startsWith("discord:")) { - if (allowDiscordTemplates == false) return res.json({ code: 403, message: "Discord templates are disabled on this instance."}).sendStatus(403); + if (allowDiscordTemplates == false) + return res.json({ code: 403, message: "Discord templates are disabled on this instance." }).sendStatus(403); const discordTemplateID = code.split("discord:", 2)[1]; const discordTemplateData = await fetch(`https://discord.com/api/v9/guilds/templates/${discordTemplateID}`, { @@ -25,27 +26,33 @@ router.get("/:code", route({}), async (req: Request, res: Response) => { }); res.json(await discordTemplateData.json()); - } - - - if (code.startsWith("fosscord:")) { - if (allowOtherInstancesTemplates == false) return res.json({ code: 403, message: "Other instance templates are disabled on this instance."}).sendStatus(403); - //TODO: TBD when federation came out - res.json({}).sendStatus(200) }; - //TODO: Validation - if (code.startsWith("external:")) { - if (allowExternalRaws == false) return res.json({ code: 403, message: "Importing templates from raws is disabled on this instance."}).sendStatus(403); - const url = code.split("external:", 2)[1] - - const rawTemplateData = await fetch(`${url}`, { + if (code.startsWith("fosscord:")) { + if (allowOtherInstancesTemplates == false) + return res.json({ code: 403, message: "Other instance templates are disabled on this instance." }).sendStatus(403); + //TODO: TBD when federation came out + res.json({}).sendStatus(200); + }; + + //TODO: Validation + if (code.startsWith("external:")) { + if (allowExternalRaws == false) + return res.json({ code: 403, message: "Importing templates from raws is disabled on this instance." }).sendStatus(403); + const url = code.split("external:", 2)[1]; + + const rawTemplateData = + (await fetch(`${url}`, { method: "get", headers: { "Content-Type": "application/json" } - }) || null; - - res.json(rawTemplateData !== null ? await rawTemplateData.json(): { code: 500, message: "An error occurred while trying to fetch the raw."}); - } + })) || null; + + res.json( + rawTemplateData !== null + ? await rawTemplateData.json() + : { code: 500, message: "An error occurred while trying to fetch the raw." } + ); + }; const template = await Template.findOneOrFail({ code: code }); @@ -53,8 +60,9 @@ router.get("/:code", route({}), async (req: Request, res: Response) => { }); router.post("/:code", route({ body: "GuildTemplateCreateSchema" }), async (req: Request, res: Response) => { - if(enabled == false) return res.json({ code: 403, message: "Templates are disabled on this instance."}).sendStatus(403); - if(allowTemplateCreation == false) return res.json({ code: 403, message: "Template creation is disabled on this instance."}).sendStatus(403); + if (enabled == false) return res.json({ code: 403, message: "Templates are disabled on this instance." }).sendStatus(403); + if (allowTemplateCreation == false) + return res.json({ code: 403, message: "Template creation is disabled on this instance." }).sendStatus(403); const { code } = req.params; const body = req.body as GuildTemplateCreateSchema; From f45d1d579d7c6e845f296f7db1df983fb3170c8b Mon Sep 17 00:00:00 2001 From: uurgothat Date: Sun, 24 Oct 2021 19:49:32 +0300 Subject: [PATCH 5/6] make templates configurable + authorization --- api/src/middlewares/Authentication.ts | 1 + api/src/routes/guilds/templates/index.ts | 46 +++++++----------------- util/src/entities/Config.ts | 6 ++-- 3 files changed, 15 insertions(+), 38 deletions(-) diff --git a/api/src/middlewares/Authentication.ts b/api/src/middlewares/Authentication.ts index 59a181e6..a69b29a6 100644 --- a/api/src/middlewares/Authentication.ts +++ b/api/src/middlewares/Authentication.ts @@ -11,6 +11,7 @@ export const NO_AUTHORIZATION_ROUTES = [ "/experiments", "/-/readyz", "/-/healthz", + "/guilds/templates", /\/guilds\/\d+\/widget\.(json|png)/ ]; diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index b82fb102..dd906198 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -1,68 +1,46 @@ import { Request, Response, Router } from "express"; const router: Router = Router(); import { Template, Guild, Role, Snowflake, Config, User, Member } from "@fosscord/util"; +const { enabled, allowTemplateCreation, allowDiscordTemplates, allowRaws } = Config.get().templates; import { route } from "@fosscord/api"; import { DiscordApiErrors } from "@fosscord/util"; import fetch from "node-fetch"; -const { enabled, allowTemplateCreation, allowDiscordTemplates, allowOtherInstancesTemplates, allowExternalRaws } = Config.get().templates; + export interface GuildTemplateCreateSchema { name: string; avatar?: string | null; } router.get("/:code", route({}), async (req: Request, res: Response) => { - if (enabled == false) return res.json({ code: 403, message: "Templates are disabled on this instance." }).sendStatus(403); + if (!enabled) res.json({ code: 403, message: "Template creation & usage is disabled on this instance." }).sendStatus(403); + const { code } = req.params; if (code.startsWith("discord:")) { - if (allowDiscordTemplates == false) - return res.json({ code: 403, message: "Discord templates are disabled on this instance." }).sendStatus(403); + if (!allowDiscordTemplates) return res.json({ code: 403, message: "Discord templates cannot be used on this instance." }).sendStatus(403); const discordTemplateID = code.split("discord:", 2)[1]; const discordTemplateData = await fetch(`https://discord.com/api/v9/guilds/templates/${discordTemplateID}`, { method: "get", headers: { "Content-Type": "application/json" } }); + return res.json(await discordTemplateData.json()); + } - res.json(await discordTemplateData.json()); - }; - - if (code.startsWith("fosscord:")) { - if (allowOtherInstancesTemplates == false) - return res.json({ code: 403, message: "Other instance templates are disabled on this instance." }).sendStatus(403); - //TODO: TBD when federation came out - res.json({}).sendStatus(200); - }; - - //TODO: Validation if (code.startsWith("external:")) { - if (allowExternalRaws == false) - return res.json({ code: 403, message: "Importing templates from raws is disabled on this instance." }).sendStatus(403); - const url = code.split("external:", 2)[1]; + if (!allowRaws) return res.json({ code: 403, message: "Importing raws is disabled on this instance." }).sendStatus(403); - const rawTemplateData = - (await fetch(`${url}`, { - method: "get", - headers: { "Content-Type": "application/json" } - })) || null; - - res.json( - rawTemplateData !== null - ? await rawTemplateData.json() - : { code: 500, message: "An error occurred while trying to fetch the raw." } - ); - }; + return res.json(code.split("external:", 2)[1]); + } const template = await Template.findOneOrFail({ code: code }); - res.json(template); }); router.post("/:code", route({ body: "GuildTemplateCreateSchema" }), async (req: Request, res: Response) => { - if (enabled == false) return res.json({ code: 403, message: "Templates are disabled on this instance." }).sendStatus(403); - if (allowTemplateCreation == false) - return res.json({ code: 403, message: "Template creation is disabled on this instance." }).sendStatus(403); + if (!enabled) return res.json({ code: 403, message: "Template creation & usage is disabled on this instance." }).sendStatus(403); + if (!allowTemplateCreation) return res.json({ code: 403, message: "Template creation is disabled on this instance." }).sendStatus(403); const { code } = req.params; const body = req.body as GuildTemplateCreateSchema; diff --git a/util/src/entities/Config.ts b/util/src/entities/Config.ts index 1d2d496a..cac5d4da 100644 --- a/util/src/entities/Config.ts +++ b/util/src/entities/Config.ts @@ -164,8 +164,7 @@ export interface ConfigValue { enabled: Boolean; allowTemplateCreation: Boolean; allowDiscordTemplates: Boolean; - allowOtherInstancesTemplates: Boolean; - allowExternalRaws: Boolean + allowRaws: Boolean; } } @@ -332,7 +331,6 @@ export const DefaultConfigOptions: ConfigValue = { enabled: true, allowTemplateCreation: true, allowDiscordTemplates: true, - allowOtherInstancesTemplates: false, //Incomple - allowExternalRaws: false + allowRaws: false } }; From 6e9ab52d82aab3aee9a03bc1d52c518967f72f9b Mon Sep 17 00:00:00 2001 From: Kuna <65683493+Thesourtimes@users.noreply.github.com> Date: Sun, 24 Oct 2021 20:30:10 +0300 Subject: [PATCH 6/6] Update Authentication.ts --- api/src/middlewares/Authentication.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/api/src/middlewares/Authentication.ts b/api/src/middlewares/Authentication.ts index a69b29a6..59a181e6 100644 --- a/api/src/middlewares/Authentication.ts +++ b/api/src/middlewares/Authentication.ts @@ -11,7 +11,6 @@ export const NO_AUTHORIZATION_ROUTES = [ "/experiments", "/-/readyz", "/-/healthz", - "/guilds/templates", /\/guilds\/\d+\/widget\.(json|png)/ ];