Templates, maybe?
This commit is contained in:
parent
021a387679
commit
95b08ac51c
@ -17,16 +17,10 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { route } from "@spacebar/api";
|
import { route } from "@spacebar/api";
|
||||||
import {
|
import { Config, DiscordApiErrors, Guild, GuildTemplateCreateSchema, Member, Template } from "@spacebar/util";
|
||||||
Config,
|
|
||||||
DiscordApiErrors,
|
|
||||||
Guild,
|
|
||||||
GuildTemplateCreateSchema,
|
|
||||||
Member,
|
|
||||||
Template,
|
|
||||||
} from "@spacebar/util";
|
|
||||||
import { Request, Response, Router } from "express";
|
import { Request, Response, Router } from "express";
|
||||||
import fetch from "node-fetch-commonjs";
|
import fetch from "node-fetch-commonjs";
|
||||||
|
import { HTTPError } from "lambert-server*";
|
||||||
const router: Router = Router({ mergeParams: true });
|
const router: Router = Router({ mergeParams: true });
|
||||||
|
|
||||||
router.get(
|
router.get(
|
||||||
@ -45,112 +39,65 @@ router.get(
|
|||||||
},
|
},
|
||||||
}),
|
}),
|
||||||
async (req: Request, res: Response) => {
|
async (req: Request, res: Response) => {
|
||||||
const { allowDiscordTemplates, allowRaws, enabled } =
|
|
||||||
Config.get().templates;
|
|
||||||
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;
|
||||||
|
|
||||||
if (code.startsWith("discord:")) {
|
const template = await getTemplate(code);
|
||||||
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({
|
|
||||||
where: { code: code },
|
|
||||||
});
|
|
||||||
res.json(template);
|
res.json(template);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|
||||||
router.post(
|
router.post("/:code", route({ requestBody: "GuildTemplateCreateSchema" }), async (req: Request, res: Response) => {
|
||||||
"/:code",
|
const { code } = req.params;
|
||||||
route({ requestBody: "GuildTemplateCreateSchema" }),
|
const body = req.body as GuildTemplateCreateSchema;
|
||||||
async (req: Request, res: Response) => {
|
|
||||||
const {
|
|
||||||
enabled,
|
|
||||||
allowTemplateCreation,
|
|
||||||
// allowDiscordTemplates,
|
|
||||||
// allowRaws,
|
|
||||||
} = Config.get().templates;
|
|
||||||
if (!enabled) {
|
|
||||||
return res
|
|
||||||
.json({
|
|
||||||
code: 403,
|
|
||||||
message:
|
|
||||||
"Template creation & usage is disabled on this instance.",
|
|
||||||
})
|
|
||||||
.sendStatus(403);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!allowTemplateCreation) {
|
const { maxGuilds } = Config.get().limits.user;
|
||||||
return res
|
|
||||||
.json({
|
|
||||||
code: 403,
|
|
||||||
message: "Template creation is disabled on this instance.",
|
|
||||||
})
|
|
||||||
.sendStatus(403);
|
|
||||||
}
|
|
||||||
|
|
||||||
const { code } = req.params;
|
const guild_count = await Member.count({ where: { id: req.user_id } });
|
||||||
const body = req.body as GuildTemplateCreateSchema;
|
if (guild_count >= maxGuilds) throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds);
|
||||||
|
|
||||||
const { maxGuilds } = Config.get().limits.user;
|
const template = await getTemplate(code) as Template;
|
||||||
|
|
||||||
const guild_count = await Member.count({ where: { id: req.user_id } });
|
const guild = await Guild.createGuild({
|
||||||
if (guild_count >= maxGuilds) {
|
...template.serialized_source_guild,
|
||||||
throw DiscordApiErrors.MAXIMUM_GUILDS.withParams(maxGuilds);
|
// body comes after the template
|
||||||
}
|
...body,
|
||||||
|
owner_id: req.user_id,
|
||||||
|
template_guild_id: template.source_guild_id,
|
||||||
|
});
|
||||||
|
|
||||||
const template = await Template.findOneOrFail({
|
await Member.addToGuild(req.user_id, guild.id);
|
||||||
where: { code: code },
|
|
||||||
|
res.status(201).json({ id: guild.id });
|
||||||
|
});
|
||||||
|
|
||||||
|
async function getTemplate(code: string) {
|
||||||
|
const { allowDiscordTemplates, allowRaws, enabled } = Config.get().templates;
|
||||||
|
|
||||||
|
if (!enabled) throw new HTTPError("Template creation & usage is disabled on this instance.", 403);
|
||||||
|
|
||||||
|
if (code.startsWith("discord:")) {
|
||||||
|
if (!allowDiscordTemplates) throw new HTTPError("Discord templates cannot be used on this instance.", 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" },
|
||||||
});
|
});
|
||||||
|
|
||||||
const guild = await Guild.createGuild({
|
return await discordTemplateData.json();
|
||||||
...template.serialized_source_guild,
|
}
|
||||||
// body comes after the template
|
|
||||||
...body,
|
|
||||||
owner_id: req.user_id,
|
|
||||||
template_guild_id: template.source_guild_id,
|
|
||||||
});
|
|
||||||
|
|
||||||
await Member.addToGuild(req.user_id, guild.id);
|
if (code.startsWith("external:")) {
|
||||||
|
if (!allowRaws) throw new HTTPError("Importing raws is disabled on this instance.", 403);
|
||||||
|
|
||||||
res.status(201).json({ id: guild.id });
|
return code.split("external:", 2)[1];
|
||||||
},
|
}
|
||||||
);
|
|
||||||
|
const template = await Template.findOneOrFail({
|
||||||
|
where: { code: code },
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
|
|||||||
Reference in New Issue
Block a user