⚡ use mongoose autopopulate
This commit is contained in:
@@ -13,7 +13,7 @@ const router: Router = Router();
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const guild_id = req.params.id;
|
||||
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
const guild = await GuildModel.exists({ id: guild_id });
|
||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||
|
||||
var bans = await BanModel.find({ guild_id: guild_id }).exec();
|
||||
@@ -61,30 +61,30 @@ router.post("/:user_id", check(BanCreateSchema), async (req: Request, res: Respo
|
||||
});
|
||||
|
||||
router.delete("/:user_id", async (req: Request, res: Response) => {
|
||||
var guild_id = req.params.id;
|
||||
var { guild_id } = req.params;
|
||||
var banned_user_id = req.params.user_id;
|
||||
|
||||
const banned_user = await getPublicUser(banned_user_id);
|
||||
const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec();
|
||||
const guild = await GuildModel.exists({ id: guild_id });
|
||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||
|
||||
const perms = await getPermission(req.user_id, guild.id);
|
||||
const perms = await getPermission(req.user_id, guild_id);
|
||||
if (!perms.has("BAN_MEMBERS")) {
|
||||
throw new HTTPError("No permissions", 403);
|
||||
}
|
||||
|
||||
await BanModel.deleteOne({
|
||||
user_id: banned_user_id,
|
||||
guild_id: guild.id,
|
||||
guild_id,
|
||||
}).exec();
|
||||
|
||||
await emitEvent({
|
||||
event: "GUILD_BAN_REMOVE",
|
||||
data: {
|
||||
guild_id: guild.id,
|
||||
guild_id,
|
||||
user: banned_user,
|
||||
},
|
||||
guild_id: guild.id,
|
||||
guild_id,
|
||||
} as GuildBanRemoveEvent);
|
||||
|
||||
return res.status(204).send();
|
||||
@@ -7,14 +7,14 @@ import { check } from "../../../util/instanceOf";
|
||||
const router = Router();
|
||||
|
||||
router.get("/", async (req, res) => {
|
||||
const guild_id = req.params.id;
|
||||
const { guild_id } = req.params;
|
||||
const channels = await ChannelModel.find({ guild_id }).exec();
|
||||
|
||||
res.json(toObject(channels));
|
||||
});
|
||||
|
||||
router.post("/", check(ChannelModifySchema), async (req, res) => {
|
||||
const guild_id = req.params.id;
|
||||
const { guild_id } = req.params;
|
||||
const body = req.body as ChannelModifySchema;
|
||||
if (!body.permission_overwrites) body.permission_overwrites = [];
|
||||
if (!body.topic) body.topic = "";
|
||||
@@ -5,26 +5,31 @@ import {
|
||||
getPermission,
|
||||
GuildDeleteEvent,
|
||||
GuildModel,
|
||||
GuildUpdateEvent,
|
||||
InviteModel,
|
||||
MemberModel,
|
||||
MessageModel,
|
||||
RoleModel,
|
||||
toObject,
|
||||
UserModel,
|
||||
} from "fosscord-server-util";
|
||||
import { HTTPError } from "lambert-server";
|
||||
import { GuildUpdateSchema } from "../../../schema/Guild";
|
||||
import { emitEvent } from "../../../util/Event";
|
||||
import { check } from "../../../util/instanceOf";
|
||||
import "missing-native-js-functions";
|
||||
|
||||
const router = Router();
|
||||
|
||||
router.get("/", async (req: Request, res: Response) => {
|
||||
const guild_id = req.params.id;
|
||||
const { guild_id } = req.params;
|
||||
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
const guild = await GuildModel.findOne({ id: guild_id })
|
||||
.populate({ path: "joined_at", match: { id: req.user_id } })
|
||||
.exec();
|
||||
if (!guild) throw new HTTPError("Guild does not exist", 404);
|
||||
|
||||
const member = await MemberModel.findOne({ guild_id: guild_id, id: req.user_id }, "id").exec();
|
||||
const member = await MemberModel.exists({ guild_id: guild_id, id: req.user_id });
|
||||
if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401);
|
||||
|
||||
return res.json(guild);
|
||||
@@ -32,20 +37,27 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
|
||||
router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) => {
|
||||
const body = req.body as GuildUpdateSchema;
|
||||
const guild_id = req.params.id;
|
||||
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
if (!guild) throw new HTTPError("This guild does not exist", 404);
|
||||
const { guild_id } = req.params;
|
||||
// TODO: guild update check image
|
||||
|
||||
const perms = await getPermission(req.user_id, guild_id);
|
||||
if (!perms.has("MANAGE_GUILD")) throw new HTTPError("You do not have the MANAGE_GUILD permission", 401);
|
||||
|
||||
await GuildModel.updateOne({ id: guild_id }, body).exec();
|
||||
return res.status(204);
|
||||
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
|
||||
.populate({ path: "joined_at", match: { id: req.user_id } })
|
||||
.exec();
|
||||
|
||||
const data = toObject(guild);
|
||||
|
||||
emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent);
|
||||
|
||||
return res.send(data);
|
||||
});
|
||||
|
||||
router.delete("/", async (req: Request, res: Response) => {
|
||||
var guild_id = req.params.id;
|
||||
// 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);
|
||||
@@ -67,7 +79,7 @@ router.delete("/", async (req: Request, res: Response) => {
|
||||
await InviteModel.deleteMany({ guild_id }).exec();
|
||||
await MessageModel.deleteMany({ guild_id }).exec();
|
||||
|
||||
return res.status(204).send();
|
||||
return res.sendStatus(204);
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -10,7 +10,7 @@ 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.id;
|
||||
const { guild_id } = req.params;
|
||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||
|
||||
@@ -31,17 +31,16 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
|
||||
var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection)
|
||||
.limit(limit)
|
||||
.populate({ path: "user", select: PublicUserProjection })
|
||||
.exec();
|
||||
|
||||
return res.json(toObject(members));
|
||||
});
|
||||
|
||||
router.get("/:member", async (req: Request, res: Response) => {
|
||||
const guild_id = req.params.id;
|
||||
const { guild_id } = req.params;
|
||||
const user_id = req.params.member;
|
||||
|
||||
const member = await MemberModel.findOne({ id: user_id, guild_id }).populate({ path: "user", select: PublicUserProjection }).exec();
|
||||
const member = await MemberModel.findOne({ id: user_id, guild_id }).exec();
|
||||
if (!member) throw new HTTPError("Member not found", 404);
|
||||
|
||||
return res.json(member);
|
||||
Reference in New Issue
Block a user