1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 13:14:06 +01:00
This commit is contained in:
Flam3rboy 2021-05-08 13:55:54 +02:00
commit f309ecb325
4 changed files with 201 additions and 12316 deletions

12354
package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

@ -31,7 +31,7 @@
}, },
"homepage": "https://github.com/fosscord/fosscord-api#readme", "homepage": "https://github.com/fosscord/fosscord-api#readme",
"dependencies": { "dependencies": {
"@fosscord/server-util": "^1.1.1", "@fosscord/server-util": "^1.1.2",
"@types/jest": "^26.0.22", "@types/jest": "^26.0.22",
"bcrypt": "^5.0.0", "bcrypt": "^5.0.0",
"body-parser": "^1.19.0", "body-parser": "^1.19.0",

View File

@ -1,8 +1,8 @@
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 } from "../../../util/Member"; import { PublicMemberProjection, addMember, removeMember, addRole, removeRole, changeNickname } from "../../../util/Member";
import { emitEvent } from "../../../util/Event"; import { emitEvent } from "../../../util/Event";
import { getPublicUser } from "../../../util/User"; import { getPublicUser } from "../../../util/User";
@ -37,45 +37,70 @@ 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(); router.delete("/:member_id/roles/:role_id", async (req: Request, res: Response) => {
if (!member) throw new HTTPError("Member not found", 404); const { guild_id, role_id, member_id } = req.params;
await removeMember(user_id, guild_id); const perms = await getPermission(member_id, guild_id);
perms.hasThrow("MANAGE_ROLES");
// https://discord.com/developers/docs/resources/guild#remove-guild-member 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(member_id, guild_id);
perms.hasThrow("MANAGE_ROLES");
await addRole(member_id, guild_id, role_id);
res.sendStatus(204);
});
router.patch("/:member_id/nick", async (req: Request, res: Response) => {
const { guild_id, member_id } = req.params;
if(!req.body.nickname) throw new HTTPError("No nickname defined", 404);
const perms = await getPermission(member_id, guild_id);
perms.hasThrow("MANAGE_NICKNAMES");
await changeNickname(member_id, guild_id, req.body.nickname);
res.status(204);
});
router.patch("/members/@me/nick", async (req: Request, res: Response) => {
const { guild_id, member_id } = req.params;
if(!req.body.nickname) throw new HTTPError("No nickname defined", 404);
const perms = await getPermission(member_id, guild_id);
perms.hasThrow("CHANGE_NICKNAME");
await changeNickname(member_id, guild_id, req.body.nickname);
res.status(204);
}); });

View File

@ -4,10 +4,13 @@ import {
GuildDeleteEvent, GuildDeleteEvent,
GuildMemberAddEvent, GuildMemberAddEvent,
GuildMemberRemoveEvent, GuildMemberRemoveEvent,
GuildMemberUpdateEvent,
GuildModel, GuildModel,
MemberModel, MemberModel,
RoleModel,
UserModel, UserModel,
} from "@fosscord/server-util"; } from "@fosscord/server-util";
import { response } from "express";
import { HTTPError } from "lambert-server"; import { HTTPError } from "lambert-server";
import Config from "./Config"; import Config from "./Config";
import { emitEvent } from "./Event"; import { emitEvent } from "./Event";
@ -120,3 +123,80 @@ export async function removeMember(user_id: string, guild_id: string) {
} as GuildMemberRemoveEvent), } as GuildMemberRemoveEvent),
]); ]);
} }
export async function addRole(user_id: string, guild_id: string, role_id: string) {
const user = await getPublicUser(user_id);
const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
if (!role) throw new HTTPError("role not found", 404);
var memberObj = await MemberModel.findOneAndUpdate({
id: user_id,
guild_id: guild_id,
}, { $push: { roles: role_id } }).exec();
if(!memberObj) throw new HTTPError("Member not found", 404);
await emitEvent({
event: "GUILD_MEMBER_UPDATE",
data: {
guild_id: guild_id,
user: user,
roles: memberObj.roles
},
guild_id: guild_id,
} as GuildMemberUpdateEvent);
}
export async function removeRole(user_id: string, guild_id: string, role_id: string) {
const user = await getPublicUser(user_id);
const role = await RoleModel.findOne({ id: role_id, guild_id: guild_id }).exec();
if (!role) throw new HTTPError("role not found", 404);
var memberObj = await MemberModel.findOneAndUpdate({
id: user_id,
guild_id: guild_id,
}, { $pull: { roles: role_id } }).exec();
if(!memberObj) throw new HTTPError("Member not found", 404);
await emitEvent({
event: "GUILD_MEMBER_UPDATE",
data: {
guild_id: guild_id,
user: user,
roles: memberObj.roles
},
guild_id: guild_id,
} as GuildMemberUpdateEvent);
}
export async function changeNickname(user_id: string, guild_id: string, nickname: string) {
const user = await getPublicUser(user_id);
var memberObj = await MemberModel.findOneAndUpdate({
id: user_id,
guild_id: guild_id,
}, { nick: nickname } ).exec();
if(!memberObj) throw new HTTPError("Member not found", 404);
await emitEvent({
event: "GUILD_MEMBER_UPDATE",
data: {
guild_id: guild_id,
user: user,
nick: nickname
},
guild_id: guild_id,
} as GuildMemberUpdateEvent);
}