From 8a13a1f2ea990d7e1c07138847644653eb0aece6 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 22 May 2021 17:39:28 +0200 Subject: [PATCH] :art: Move member routes in separate files --- src/routes/guilds/#guild_id/members.ts | 114 ------------------ .../#guild_id/members/#member_id/index.ts | 70 +++++++++++ .../#guild_id/members/#member_id/nick.ts | 24 ++++ .../#member_id/roles/#role_id/index.ts | 27 +++++ src/routes/guilds/#guild_id/members/index.ts | 39 ++++++ src/routes/guilds/#guild_id/roles.ts | 68 ++++++++--- src/schema/Member.ts | 26 ++-- 7 files changed, 225 insertions(+), 143 deletions(-) delete mode 100644 src/routes/guilds/#guild_id/members.ts create mode 100644 src/routes/guilds/#guild_id/members/#member_id/index.ts create mode 100644 src/routes/guilds/#guild_id/members/#member_id/nick.ts create mode 100644 src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts create mode 100644 src/routes/guilds/#guild_id/members/index.ts diff --git a/src/routes/guilds/#guild_id/members.ts b/src/routes/guilds/#guild_id/members.ts deleted file mode 100644 index 4dd8f013..00000000 --- a/src/routes/guilds/#guild_id/members.ts +++ /dev/null @@ -1,114 +0,0 @@ -import { Request, Response, Router } from "express"; -import { - GuildModel, - MemberModel, - UserModel, - toObject, - GuildMemberAddEvent, - getPermission, - PermissionResolvable, -} from "@fosscord/server-util"; -import { HTTPError } from "lambert-server"; -import { instanceOf, Length, check } from "../../../util/instanceOf"; -import { PublicMemberProjection, addMember, removeMember, addRole, removeRole, changeNickname, isMember } from "../../../util/Member"; -import { emitEvent } from "../../../util/Event"; -import { MemberNickChangeSchema } from "../../../schema/Member"; -import { getPublicUser } from "../../../util/User"; - -const router = Router(); - -// TODO: not allowed for user -> only allowed for bots with privileged intents -// TODO: send over websocket -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 not found", 404); - await isMember(req.user_id, guild_id); - - try { - instanceOf({ $limit: new Length(Number, 1, 1000), $after: String }, req.query, { - path: "query", - req, - ref: { obj: null, key: "" }, - }); - } catch (error) { - return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error }); - } - - // @ts-ignore - if (!req.query.limit) req.query.limit = 1; - const { limit, after } = (req.query) as { limit: number; after: string }; - const query = after ? { id: { $gt: after } } : {}; - - var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) - .limit(limit) - .exec(); - - return res.json(toObject(members)); -}); - -router.get("/:member_id", async (req: Request, res: Response) => { - const { guild_id, member_id } = req.params; - await isMember(req.user_id, guild_id); - - const member = await MemberModel.findOne({ id: member_id, guild_id }).exec(); - if (!member) throw new HTTPError("Member not found", 404); - - return res.json(toObject(member)); -}); - -router.put("/:member_id", async (req: Request, res: Response) => { - const { guild_id, member_id } = req.params; - - throw new HTTPError("Maintenance: Currently you can't add a member", 403) - // TODO: only for oauth2 applications - await addMember(member_id, guild_id); - res.sendStatus(204) -}); - -router.delete("/:member_id", async (req: Request, res: Response) => { - const { guild_id, member_id } = req.params; - - const perms = await getPermission(req.user_id, guild_id); - perms.hasThrow("KICK_MEMBERS"); - - await removeMember(member_id, guild_id); - res.sendStatus(204) -}); - -router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => { - const { guild_id, role_id, member_id } = req.params; - - const perms = await getPermission(req.user_id, guild_id); - perms.hasThrow("MANAGE_ROLES"); - - await removeRole(member_id, guild_id, role_id); - res.sendStatus(204); -}); - -router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => { - const { guild_id, role_id, member_id } = req.params; - - const perms = await getPermission(req.user_id, guild_id); - perms.hasThrow("MANAGE_ROLES"); - - await addRole(member_id, guild_id, role_id); - res.sendStatus(204); -}); - -router.patch("/:member_id/nick", check(MemberNickChangeSchema), async (req: Request, res: Response) => { - var { guild_id, member_id } = req.params; - var permissionString: PermissionResolvable = "MANAGE_NICKNAMES"; - if (member_id === "@me") { - member_id = req.user_id; - permissionString = "CHANGE_NICKNAME"; - } - - const perms = await getPermission(req.user_id, guild_id); - perms.hasThrow(permissionString); - - await changeNickname(member_id, guild_id, req.body.nickname); - res.status(204); -}); - -export default router; diff --git a/src/routes/guilds/#guild_id/members/#member_id/index.ts b/src/routes/guilds/#guild_id/members/#member_id/index.ts new file mode 100644 index 00000000..e9356c0d --- /dev/null +++ b/src/routes/guilds/#guild_id/members/#member_id/index.ts @@ -0,0 +1,70 @@ +import { Request, Response, Router } from "express"; +import { + GuildModel, + MemberModel, + UserModel, + toObject, + GuildMemberAddEvent, + getPermission, + PermissionResolvable, + RoleModel, + GuildMemberUpdateEvent +} from "@fosscord/server-util"; +import { HTTPError } from "lambert-server"; +import { addMember, isMember, removeMember } from "../../../../../util/Member"; +import { check } from "../../../../../util/instanceOf"; +import { MemberChangeSchema } from "../../../../../schema/Member"; +import { emitEvent } from "../../../../../util/Event"; + +const router = Router(); + +router.get("/", async (req: Request, res: Response) => { + const { guild_id, member_id } = req.params; + await isMember(req.user_id, guild_id); + + const member = await MemberModel.findOne({ id: member_id, guild_id }).exec(); + if (!member) throw new HTTPError("Member not found", 404); + + return res.json(toObject(member)); +}); + +router.patch("/", check(MemberChangeSchema), async (req, res) => { + const { guild_id, member_id } = req.params; + const body = req.body as MemberChangeSchema; + if (body.roles) { + const roles = await RoleModel.find({ id: { $in: body.roles } }).exec(); + if (body.roles.length !== roles.length) throw new HTTPError("Roles not found", 404); + // TODO: check if user has permission to add role + } + + const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body).exec(); + + await emitEvent({ + event: "GUILD_MEMBER_UPDATE", + guild_id, + data: toObject(member) + } as GuildMemberUpdateEvent); + + res.json(toObject(member)); +}); + +router.put("/", async (req: Request, res: Response) => { + const { guild_id, member_id } = req.params; + + throw new HTTPError("Maintenance: Currently you can't add a member", 403); + // TODO: only for oauth2 applications + await addMember(member_id, guild_id); + res.sendStatus(204); +}); + +router.delete("/", async (req: Request, res: Response) => { + const { guild_id, member_id } = req.params; + + const perms = await getPermission(req.user_id, guild_id); + perms.hasThrow("KICK_MEMBERS"); + + await removeMember(member_id, guild_id); + res.sendStatus(204); +}); + +export default router; diff --git a/src/routes/guilds/#guild_id/members/#member_id/nick.ts b/src/routes/guilds/#guild_id/members/#member_id/nick.ts new file mode 100644 index 00000000..9078409d --- /dev/null +++ b/src/routes/guilds/#guild_id/members/#member_id/nick.ts @@ -0,0 +1,24 @@ +import { getPermission, PermissionResolvable } from "@fosscord/server-util"; +import { Request, Response, Router } from "express"; +import { check } from "lambert-server"; +import { MemberNickChangeSchema } from "../../../../../schema/Member"; +import { changeNickname } from "../../../../../util/Member"; + +const router = Router(); + +router.patch("/", check(MemberNickChangeSchema), async (req: Request, res: Response) => { + var { guild_id, member_id } = req.params; + var permissionString: PermissionResolvable = "MANAGE_NICKNAMES"; + if (member_id === "@me") { + member_id = req.user_id; + permissionString = "CHANGE_NICKNAME"; + } + + const perms = await getPermission(req.user_id, guild_id); + perms.hasThrow(permissionString); + + await changeNickname(member_id, guild_id, req.body.nickname); + res.status(204); +}); + +export default router; diff --git a/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts b/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts new file mode 100644 index 00000000..b7a43c74 --- /dev/null +++ b/src/routes/guilds/#guild_id/members/#member_id/roles/#role_id/index.ts @@ -0,0 +1,27 @@ +import { getPermission } from "@fosscord/server-util"; +import { Request, Response, Router } from "express"; +import { addRole, removeRole } from "../../../../../../../util/Member"; + +const router = Router(); + +router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => { + const { guild_id, role_id, member_id } = req.params; + + const perms = await getPermission(req.user_id, guild_id); + perms.hasThrow("MANAGE_ROLES"); + + await removeRole(member_id, guild_id, role_id); + res.sendStatus(204); +}); + +router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => { + const { guild_id, role_id, member_id } = req.params; + + const perms = await getPermission(req.user_id, guild_id); + perms.hasThrow("MANAGE_ROLES"); + + await addRole(member_id, guild_id, role_id); + res.sendStatus(204); +}); + +export default router; diff --git a/src/routes/guilds/#guild_id/members/index.ts b/src/routes/guilds/#guild_id/members/index.ts new file mode 100644 index 00000000..1ec21226 --- /dev/null +++ b/src/routes/guilds/#guild_id/members/index.ts @@ -0,0 +1,39 @@ +import { Request, Response, Router } from "express"; +import { GuildModel, MemberModel, toObject } from "@fosscord/server-util"; +import { HTTPError } from "lambert-server"; +import { instanceOf, Length } from "../../../../util/instanceOf"; +import { PublicMemberProjection, isMember } from "../../../../util/Member"; + +const router = Router(); + +// TODO: not allowed for user -> only allowed for bots with privileged intents +// TODO: send over websocket +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 not found", 404); + await isMember(req.user_id, guild_id); + + try { + instanceOf({ $limit: new Length(Number, 1, 1000), $after: String }, req.query, { + path: "query", + req, + ref: { obj: null, key: "" } + }); + } catch (error) { + return res.status(400).json({ code: 50035, message: "Invalid Query", success: false, errors: error }); + } + + // @ts-ignore + if (!req.query.limit) req.query.limit = 1; + const { limit, after } = (req.query) as { limit: number; after: string }; + const query = after ? { id: { $gt: after } } : {}; + + var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) + .limit(limit) + .exec(); + + return res.json(toObject(members)); +}); + +export default router; diff --git a/src/routes/guilds/#guild_id/roles.ts b/src/routes/guilds/#guild_id/roles.ts index c663fea1..940058b9 100644 --- a/src/routes/guilds/#guild_id/roles.ts +++ b/src/routes/guilds/#guild_id/roles.ts @@ -1,27 +1,37 @@ import { Request, Response, Router } from "express"; -import { RoleModel, GuildModel, getPermission, toObject, UserModel, Snowflake, MemberModel } from "@fosscord/server-util"; +import { + RoleModel, + GuildModel, + getPermission, + toObject, + UserModel, + Snowflake, + MemberModel, + GuildRoleCreateEvent, + GuildRoleUpdateEvent +} from "@fosscord/server-util"; import { HTTPError } from "lambert-server"; import { emitEvent } from "../../../util/Event"; import { check } from "../../../util/instanceOf"; -import { RoleCreateSchema, RoleModifySchema } from "../../../schema/Roles"; +import { RoleModifySchema } from "../../../schema/Roles"; import { getPublicUser } from "../../../util/User"; +import { isMember } from "../../../util/Member"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const guild_id = req.params.guild_id; - const guild = await GuildModel.exists({ id: guild_id }); - if (!guild) throw new HTTPError("Guild not found", 404); + await isMember(req.user_id, guild_id); + + const roles = await RoleModel.find({ guild_id: guild_id }).exec(); - var roles = await RoleModel.find({ guild_id: guild_id }).exec(); - if (!roles) res.send("No roles"); return res.json(toObject(roles)); }); -router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => { +router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; - const body = req.body as RoleCreateSchema; + const body = req.body as RoleModifySchema; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); @@ -30,22 +40,29 @@ router.post("/", check(RoleCreateSchema), async (req: Request, res: Response) => if (!user) throw new HTTPError("User not found", 404); const perms = await getPermission(req.user_id, guild_id); + perms.hasThrow("MANAGE_ROLES"); + if (!body.name) throw new HTTPError("You need to specify a name"); - if (!perms.has("MANAGE_ROLES")) throw new HTTPError("You missing the MANAGE_ROLES permission", 401); - - const role_id = Snowflake.generate(); - - var role = { + const role = await new RoleModel({ ...body, - id: role_id, + id: Snowflake.generate(), guild_id: guild_id, managed: false, position: 0, - tags: null - }; + tags: null, + permissions: body.permissions || 0n + }).save(); - const roleNew = await new RoleModel(role).save(); - res.json(toObject(roleNew)).send(); + await emitEvent({ + event: "GUILD_ROLE_CREATE", + guild_id, + data: { + guild_id, + role: toObject(role) + } + } as GuildRoleCreateEvent); + + res.json(toObject(role)); }); router.delete("/:role_id", async (req: Request, res: Response) => { @@ -68,7 +85,7 @@ router.delete("/:role_id", async (req: Request, res: Response) => { guild_id: guild_id }).exec(); - res.send("Deleted"); + res.sendStatus(204); }); // TODO: check role hierarchy @@ -76,6 +93,7 @@ router.delete("/:role_id", async (req: Request, res: Response) => { router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; const { role_id } = req.params; + const body = req.body as RoleModifySchema; const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); if (!guild) throw new HTTPError("Guild not found", 404); @@ -92,9 +110,19 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res id: role_id, guild_id: guild_id }, - ...req.body + // @ts-ignore + body ).exec(); + await emitEvent({ + event: "GUILD_ROLE_UPDATE", + guild_id, + data: { + guild_id, + role + } + } as GuildRoleUpdateEvent); + res.json(toObject(role)); }); diff --git a/src/schema/Member.ts b/src/schema/Member.ts index 49a4d7ce..607d0a06 100644 --- a/src/schema/Member.ts +++ b/src/schema/Member.ts @@ -1,21 +1,29 @@ export const MemberCreateSchema = { id: String, - nick: String, - guild_id: String, - joined_at: Date, + nick: String, + guild_id: String, + joined_at: Date }; export interface MemberCreateSchema { id: string; - nick: string; - guild_id: string; + nick: string; + guild_id: string; joined_at: Date; } export const MemberNickChangeSchema = { - nick: String, -} + nick: String +}; export interface MemberNickChangeSchema { - nick: string, -} \ No newline at end of file + nick: string; +} + +export const MemberChangeSchema = { + $roles: [String] +}; + +export interface MemberChangeSchema { + roles?: string[]; +}