1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-14 06:32:36 +01:00

Update members.ts

This commit is contained in:
Intevel ツ 2021-05-08 13:34:36 +02:00
parent 73b1674439
commit 614a0bf057

View File

@ -1,5 +1,5 @@
import { Request, Response, Router } from "express"; import { Request, Response, Router } from "express";
import { GuildModel, MemberModel, UserModel, toObject, GuildMemberAddEvent } from "@fosscord/server-util"; import { GuildModel, MemberModel, UserModel, toObject, GuildMemberAddEvent, getPermission } from "@fosscord/server-util";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import { instanceOf, Length } from "../../../util/instanceOf"; import { instanceOf, Length } from "../../../util/instanceOf";
import { PublicMemberProjection, addMember, removeMember, addRole, removeRole } from "../../../util/Member"; import { PublicMemberProjection, addMember, removeMember, addRole, removeRole } from "../../../util/Member";
@ -37,83 +37,58 @@ router.get("/", async (req: Request, res: Response) => {
return res.json(toObject(members)); return res.json(toObject(members));
}); });
router.get("/:member", async (req: Request, res: Response) => { router.get("/:member_id", async (req: Request, res: Response) => {
const { guild_id } = req.params; const { guild_id, member_id } = req.params;
const user_id = req.params.member;
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec(); const member = await MemberModel.findOne({ id: member_id, guild_id }).exec();
if (!member) throw new HTTPError("Member not found", 404); if (!member) throw new HTTPError("Member not found", 404);
return res.json(member); return res.json(toObject(member));
}); });
router.put("/:member", async (req: Request, res: Response) => { router.put("/:member_id", async (req: Request, res: Response) => {
const { guild_id } = req.params; const { guild_id, member_id } = req.params;
const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
const user_id = req.params.member; await addMember(member_id, guild_id);
res.sendStatus(204)
const user = await UserModel.findOne({ id: user_id }).exec();
if (!user) throw new HTTPError("User not found", 404);
await addMember(user_id, guild_id);
// https://discord.com/developers/docs/resources/guild#add-guild-member
}); });
router.delete("/:member", async (req: Request, res: Response) => { router.delete("/:member_id", async (req: Request, res: Response) => {
const { guild_id } = req.params; const { guild_id, member_id } = req.params;
const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
const user_id = req.params.member; await removeMember(member_id, guild_id);
res.sendStatus(204)
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec();
if (!member) throw new HTTPError("Member not found", 404);
await removeMember(user_id, guild_id);
// https://discord.com/developers/docs/resources/guild#remove-guild-member
}); });
router.delete("/:member/roles/:role_id", async (req: Request, res: Response) => { router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
const { guild_id, role_id, member_id } = req.params;
const { guild_id, role_id } = req.params; const perms = await getPermission(member_id, guild_id);
if(!role_id) throw new HTTPError("role_id not defined", 404); perms.hasThrow("MANAGE_ROLES");
const guild = await GuildModel.findOne({ id: guild_id }).exec(); await removeRole(member_id, guild_id, role_id);
if (!guild) throw new HTTPError("Guild not found", 404); res.sendStatus(204);
});
const user_id = req.params.member; router.put("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
const { guild_id, role_id, member_id } = req.params;
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec(); const perms = await getPermission(member_id, guild_id);
if (!member) throw new HTTPError("Member not found", 404); perms.hasThrow("MANAGE_ROLES");
await removeRole(user_id, guild_id, role_id); await addRole(member_id, guild_id, role_id);
res.sendStatus(204);
});
router.patch("/:member_id/@me/nick", async (req: Request, res: Response) => {
const { guild_id, member_id } = req.params;
const perms = await getPermission(member_id, guild_id);
perms.hasThrow("CHANGE_NICKNAME");
//await addRole(member_id, guild_id, role_id);
res.status(204); res.status(204);
// https://discord.com/developers/docs/resources/guild#remove-guild-member-role
});
router.put("/:member/roles/:role_id", async (req: Request, res: Response) => {
const { guild_id, role_id } = req.params;
if(!role_id) throw new HTTPError("role_id not defined", 404);
const guild = await GuildModel.findOne({ id: guild_id }).exec();
if (!guild) throw new HTTPError("Guild not found", 404);
const user_id = req.params.member;
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec();
if (!member) throw new HTTPError("Member not found", 404);
await addRole(user_id, guild_id, role_id);
res.status(204);
// https://discord.com/developers/docs/resources/guild#remove-guild-member-role
}); });