summary refs log tree commit diff
path: root/src/routes/guilds
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-21 22:12:21 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-21 22:12:21 +0200
commitd48b286e4bfc27d872c42a05cc0d3fb7fd71772b (patch)
tree2efd7676f8250da833afc0d7716cd91fb18c03a4 /src/routes/guilds
parent:sparkles: Message Pins (diff)
downloadserver-d48b286e4bfc27d872c42a05cc0d3fb7fd71772b.tar.xz
:art: refactor/format
Diffstat (limited to 'src/routes/guilds')
-rw-r--r--src/routes/guilds/#guild_id/channels.ts14
-rw-r--r--src/routes/guilds/#guild_id/delete.ts45
-rw-r--r--src/routes/guilds/#guild_id/index.ts32
-rw-r--r--src/routes/guilds/#guild_id/roles.ts47
-rw-r--r--src/routes/guilds/#guild_id/vanity-url.ts18
5 files changed, 98 insertions, 58 deletions
diff --git a/src/routes/guilds/#guild_id/channels.ts b/src/routes/guilds/#guild_id/channels.ts

index 9e8d3506..5c90b5b9 100644 --- a/src/routes/guilds/#guild_id/channels.ts +++ b/src/routes/guilds/#guild_id/channels.ts
@@ -1,5 +1,14 @@ import { Router } from "express"; -import { ChannelCreateEvent, ChannelModel, ChannelType, GuildModel, Snowflake, toObject, ChannelUpdateEvent } from "@fosscord/server-util"; +import { + ChannelCreateEvent, + ChannelModel, + ChannelType, + GuildModel, + Snowflake, + toObject, + ChannelUpdateEvent, + AnyChannel +} from "@fosscord/server-util"; import { HTTPError } from "lambert-server"; import { ChannelModifySchema } from "../../../schema/Channel"; import { emitEvent } from "../../../util/Event"; @@ -45,7 +54,8 @@ router.post("/", check(ChannelModifySchema), async (req, res) => { ...body, id: Snowflake.generate(), created_at: new Date(), - guild_id + guild_id, + recipients: null }; await new ChannelModel(channel).save(); diff --git a/src/routes/guilds/#guild_id/delete.ts b/src/routes/guilds/#guild_id/delete.ts new file mode 100644
index 00000000..5d4db816 --- /dev/null +++ b/src/routes/guilds/#guild_id/delete.ts
@@ -0,0 +1,45 @@ +import { + ChannelModel, + EmojiModel, + GuildDeleteEvent, + GuildModel, + InviteModel, + MessageModel, + RoleModel, + UserModel +} from "@fosscord/server-util"; +import { Router, Request, Response } from "express"; +import { HTTPError } from "lambert-server"; +import { emitEvent } from "../../../util/Event"; + +const router = Router(); + +// discord prefixes this route with /delete instead of using the delete method +// docs are wrong https://discord.com/developers/docs/resources/guild#delete-guild +router.post("/", async (req: Request, res: Response) => { + var { guild_id } = req.params; + + const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); + if (!guild) throw new HTTPError("This guild does not exist", 404); + if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401); + + await emitEvent({ + event: "GUILD_DELETE", + data: { + id: guild_id + }, + guild_id: guild_id + } as GuildDeleteEvent); + + await GuildModel.deleteOne({ id: guild_id }).exec(); + await UserModel.updateMany({ guilds: guild_id }, { $pull: { guilds: guild_id } }).exec(); + await RoleModel.deleteMany({ guild_id }).exec(); + await ChannelModel.deleteMany({ guild_id }).exec(); + await EmojiModel.deleteMany({ guild_id }).exec(); + await InviteModel.deleteMany({ guild_id }).exec(); + await MessageModel.deleteMany({ guild_id }).exec(); + + return res.sendStatus(204); +}); + +export default router; diff --git a/src/routes/guilds/#guild_id/index.ts b/src/routes/guilds/#guild_id/index.ts
index 18e6372e..47675609 100644 --- a/src/routes/guilds/#guild_id/index.ts +++ b/src/routes/guilds/#guild_id/index.ts
@@ -11,7 +11,7 @@ import { MessageModel, RoleModel, toObject, - UserModel, + UserModel } from "@fosscord/server-util"; import { HTTPError } from "lambert-server"; import { GuildUpdateSchema } from "../../../schema/Guild"; @@ -54,41 +54,13 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) return res.send(data); }); -// discord prefixes this route with /delete instead of using the delete method -// docs are wrong https://discord.com/developers/docs/resources/guild#delete-guild -router.post("/delete", async (req: Request, res: Response) => { - var { guild_id } = req.params; - - const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); - if (!guild) throw new HTTPError("This guild does not exist", 404); - if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401); - - await emitEvent({ - event: "GUILD_DELETE", - data: { - id: guild_id, - }, - guild_id: guild_id, - } as GuildDeleteEvent); - - await GuildModel.deleteOne({ id: guild_id }).exec(); - await UserModel.updateMany({ guilds: guild_id }, { $pull: { guilds: guild_id } }).exec(); - await RoleModel.deleteMany({ guild_id }).exec(); - await ChannelModel.deleteMany({ guild_id }).exec(); - await EmojiModel.deleteMany({ guild_id }).exec(); - await InviteModel.deleteMany({ guild_id }).exec(); - await MessageModel.deleteMany({ guild_id }).exec(); - - return res.sendStatus(204); -}); - router.get("/vanity-url", async (req: Request, res: Response) => { const { guild_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }).exec(); if (!guild) throw new HTTPError("Guild does not exist", 404); - if(!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204) + if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204); return res.json(guild.vanity_url); }); diff --git a/src/routes/guilds/#guild_id/roles.ts b/src/routes/guilds/#guild_id/roles.ts
index 9e42f8d9..c663fea1 100644 --- a/src/routes/guilds/#guild_id/roles.ts +++ b/src/routes/guilds/#guild_id/roles.ts
@@ -15,14 +15,13 @@ router.get("/", async (req: Request, res: Response) => { if (!guild) throw new HTTPError("Guild not found", 404); var roles = await RoleModel.find({ guild_id: guild_id }).exec(); - if(!roles) res.send("No roles"); + if (!roles) res.send("No roles"); return res.json(toObject(roles)); }); router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => { - - const guild_id = req.params.guild_id; - const body = req.body as RoleCreateSchema; + const guild_id = req.params.guild_id; + const body = req.body as RoleCreateSchema; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); @@ -32,8 +31,7 @@ router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => const perms = await getPermission(req.user_id, guild_id); - if (!perms.has("MANAGE_ROLES")) - throw new HTTPError("You missing the MANAGE_ROLES permission", 401); + if (!perms.has("MANAGE_ROLES")) throw new HTTPError("You missing the MANAGE_ROLES permission", 401); const role_id = Snowflake.generate(); @@ -43,17 +41,16 @@ router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => guild_id: guild_id, managed: false, position: 0, - tags: null, - } + tags: null + }; const roleNew = await new RoleModel(role).save(); res.json(toObject(roleNew)).send(); }); router.delete("/:role_id", async (req: Request, res: Response) => { - - const guild_id = req.params.guild_id; - const { role_id } = req.params; + const guild_id = req.params.guild_id; + const { role_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); @@ -64,8 +61,7 @@ router.delete("/:role_id", async (req: Request, res: Response) => { const perms = await getPermission(req.user_id, guild_id); - if (!perms.has("MANAGE_ROLES")) - throw new HTTPError("You missing the MANAGE_ROLES permission", 401); + if (!perms.has("MANAGE_ROLES")) throw new HTTPError("You missing the MANAGE_ROLES permission", 401); await RoleModel.findOneAndDelete({ id: role_id, @@ -75,10 +71,11 @@ router.delete("/:role_id", async (req: Request, res: Response) => { res.send("Deleted"); }); +// TODO: check role hierarchy router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Response) => { - const guild_id = req.params.guild_id; - const { role_id } = req.params; + const guild_id = req.params.guild_id; + const { role_id } = req.params; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); @@ -87,20 +84,18 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res const user = await UserModel.findOne({ id: req.user_id }).exec(); if (!user) throw new HTTPError("User not found", 404); - const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec(); - if (!role) throw new HTTPError("role not found", 404); - const perms = await getPermission(req.user_id, guild_id); + perms.hasThrow("MANAGE_ROLES"); - if (!perms.has("MANAGE_ROLES")) - throw new HTTPError("You missing the MANAGE_ROLES permission", 401); + const role = await RoleModel.findOneAndUpdate( + { + id: role_id, + guild_id: guild_id + }, + ...req.body + ).exec(); - var roleObj = await RoleModel.findOneAndUpdate({ - id: role_id, guild_id: guild_id - }, ...req.body).exec(); - - res.json(toObject(roleObj)).send(); + res.json(toObject(role)); }); - export default router; diff --git a/src/routes/guilds/#guild_id/vanity-url.ts b/src/routes/guilds/#guild_id/vanity-url.ts new file mode 100644
index 00000000..052a638f --- /dev/null +++ b/src/routes/guilds/#guild_id/vanity-url.ts
@@ -0,0 +1,18 @@ +import { GuildModel } from "@fosscord/server-util"; +import { Router, Request, Response } from "express"; +import { HTTPError } from "lambert-server"; + +const router = Router(); + +router.get("/", async (req: Request, res: Response) => { + const { guild_id } = req.params; + + const guild = await GuildModel.findOne({ id: guild_id }).exec(); + if (!guild) throw new HTTPError("Guild does not exist", 404); + + if (!guild.vanity_url) throw new HTTPError("This guild has no vanity url", 204); + + return res.json({ vanity_ur: guild.vanity_url }); +}); + +export default router;