summary refs log tree commit diff
diff options
context:
space:
mode:
authorSamuel <34555296+Flam3rboy@users.noreply.github.com>2021-10-31 06:44:24 -0400
committerGitHub <noreply@github.com>2021-10-31 06:44:24 -0400
commitd02136e8df658683175ee3e5b4a4d95fd8edc874 (patch)
tree27ee0b95cd6920504f21415744d11b3b0366127d
parentMerge pull request #492 from fosscord/translation (diff)
parentUpdate Authentication.ts (diff)
downloadserver-d02136e8df658683175ee3e5b4a4d95fd8edc874.tar.xz
Merge pull request #476 from Thesourtimes/master
-rw-r--r--api/src/routes/guilds/templates/index.ts26
-rw-r--r--util/src/entities/Config.ts12
2 files changed, 37 insertions, 1 deletions
diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts
index 86316d23..dd906198 100644
--- a/api/src/routes/guilds/templates/index.ts
+++ b/api/src/routes/guilds/templates/index.ts
@@ -1,8 +1,11 @@
 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";
+
 
 export interface GuildTemplateCreateSchema {
 	name: string;
@@ -10,14 +13,35 @@ export interface GuildTemplateCreateSchema {
 }
 
 router.get("/:code", route({}), async (req: Request, res: Response) => {
+	if (!enabled) res.json({ code: 403, message: "Template creation & usage is disabled on this instance." }).sendStatus(403);
+
 	const { code } = req.params;
 
-	const template = await Template.findOneOrFail({ code: code });
+	if (code.startsWith("discord:")) {
+		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());
+	}
 
+	if (code.startsWith("external:")) {
+		if (!allowRaws)	return res.json({ code: 403, message: "Importing raws is disabled on this instance." }).sendStatus(403);
+
+		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) 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 b3167ac7..cac5d4da 100644
--- a/util/src/entities/Config.ts
+++ b/util/src/entities/Config.ts
@@ -160,6 +160,12 @@ export interface ConfigValue {
 	kafka: {
 		brokers: KafkaBroker[] | null;
 	};
+	templates: {
+		enabled: Boolean;
+		allowTemplateCreation: Boolean;
+		allowDiscordTemplates: Boolean;
+		allowRaws: Boolean;
+	}
 }
 
 export const DefaultConfigOptions: ConfigValue = {
@@ -321,4 +327,10 @@ export const DefaultConfigOptions: ConfigValue = {
 	kafka: {
 		brokers: null,
 	},
+	templates: {
+		enabled: true,
+		allowTemplateCreation: true,
+		allowDiscordTemplates: true,
+		allowRaws: false
+	}
 };