mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-06 19:02:33 +01:00
Merge pull request #476 from Thesourtimes/master
This commit is contained in:
commit
d02136e8df
@ -1,8 +1,11 @@
|
|||||||
import { Request, Response, Router } from "express";
|
import { Request, Response, Router } from "express";
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
import { Template, Guild, Role, Snowflake, Config, User, Member } from "@fosscord/util";
|
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 { route } from "@fosscord/api";
|
||||||
import { DiscordApiErrors } from "@fosscord/util";
|
import { DiscordApiErrors } from "@fosscord/util";
|
||||||
|
import fetch from "node-fetch";
|
||||||
|
|
||||||
|
|
||||||
export interface GuildTemplateCreateSchema {
|
export interface GuildTemplateCreateSchema {
|
||||||
name: string;
|
name: string;
|
||||||
@ -10,14 +13,35 @@ export interface GuildTemplateCreateSchema {
|
|||||||
}
|
}
|
||||||
|
|
||||||
router.get("/:code", route({}), async (req: Request, res: Response) => {
|
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 { 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);
|
res.json(template);
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/:code", route({ body: "GuildTemplateCreateSchema" }), async (req: Request, res: Response) => {
|
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 { code } = req.params;
|
||||||
const body = req.body as GuildTemplateCreateSchema;
|
const body = req.body as GuildTemplateCreateSchema;
|
||||||
|
|
||||||
|
@ -160,6 +160,12 @@ export interface ConfigValue {
|
|||||||
kafka: {
|
kafka: {
|
||||||
brokers: KafkaBroker[] | null;
|
brokers: KafkaBroker[] | null;
|
||||||
};
|
};
|
||||||
|
templates: {
|
||||||
|
enabled: Boolean;
|
||||||
|
allowTemplateCreation: Boolean;
|
||||||
|
allowDiscordTemplates: Boolean;
|
||||||
|
allowRaws: Boolean;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const DefaultConfigOptions: ConfigValue = {
|
export const DefaultConfigOptions: ConfigValue = {
|
||||||
@ -321,4 +327,10 @@ export const DefaultConfigOptions: ConfigValue = {
|
|||||||
kafka: {
|
kafka: {
|
||||||
brokers: null,
|
brokers: null,
|
||||||
},
|
},
|
||||||
|
templates: {
|
||||||
|
enabled: true,
|
||||||
|
allowTemplateCreation: true,
|
||||||
|
allowDiscordTemplates: true,
|
||||||
|
allowRaws: false
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user