summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--api/src/routes/guilds/templates/index.ts30
-rw-r--r--util/src/entities/Config.ts12
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
 	}
 };