summary refs log tree commit diff
path: root/api/src/routes/guilds/templates/index.ts
blob: b82fb10246cb157122373c6b95494af1bb7ee255 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { Request, Response, Router } from "express";
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";

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];

		const discordTemplateData = await fetch(`https://discord.com/api/v9/guilds/templates/${discordTemplateID}`, {
			method: "get",
			headers: { "Content-Type": "application/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;

	const { maxGuilds } = Config.get().limits.user;

	const guild_count = await Member.count({ id: req.user_id });
	if (guild_count >= maxGuilds) {
		throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds);
	}

	const template = await Template.findOneOrFail({ code: code });

	const guild_id = Snowflake.generate();

	const [guild, role] = await Promise.all([
		new Guild({
			...body,
			...template.serialized_source_guild,
			id: guild_id,
			owner_id: req.user_id
		}).save(),
		new Role({
			id: guild_id,
			guild_id: guild_id,
			color: 0,
			hoist: false,
			managed: true,
			mentionable: true,
			name: "@everyone",
			permissions: BigInt("2251804225"),
			position: 0,
			tags: null
		}).save()
	]);

	await Member.addToGuild(req.user_id, guild_id);

	res.status(201).json({ id: guild.id });
});

export default router;