mirror of
https://github.com/spacebarchat/server.git
synced 2024-11-11 05:02:37 +01:00
⚡ use mongoose autopopulate
This commit is contained in:
parent
482088b91b
commit
2a95275502
@ -17,6 +17,7 @@ import { PublicUserProjection } from "../../../../util/User";
|
||||
import multer from "multer";
|
||||
import { emitEvent } from "../../../../util/Event";
|
||||
import { Query } from "mongoose";
|
||||
import { PublicMemberProjection } from "../../../../util/Member";
|
||||
const router: Router = Router();
|
||||
|
||||
export default router;
|
||||
@ -80,14 +81,7 @@ router.get("/", async (req, res) => {
|
||||
query = MessageModel.find({ channel_id }).sort({ id: -1 });
|
||||
}
|
||||
|
||||
const messages = await query
|
||||
.limit(limit)
|
||||
.populate({ path: "author", select: PublicUserProjection })
|
||||
.populate({ path: "mentions", select: PublicUserProjection })
|
||||
.populate({ path: "mention_channels", select: { id: true, guild_id: true, type: true, name: true } })
|
||||
.populate("mention_roles")
|
||||
// .populate({ path: "member", select: PublicMemberProjection })
|
||||
.exec();
|
||||
const messages = await query.limit(limit).exec();
|
||||
|
||||
return res.json(toObject(messages));
|
||||
});
|
||||
@ -147,19 +141,11 @@ router.post("/", check(MessageCreateSchema), async (req, res) => {
|
||||
type: 0,
|
||||
tts: body.tts,
|
||||
nonce: body.nonce,
|
||||
pinned: false,
|
||||
};
|
||||
|
||||
const doc = new MessageModel(message);
|
||||
await doc.save();
|
||||
|
||||
const data = toObject(
|
||||
await MessageModel.populate(doc, [
|
||||
{ path: "author", select: PublicUserProjection },
|
||||
{ path: "mentions", select: PublicUserProjection },
|
||||
{ path: "mention_roles" },
|
||||
{ path: "mention_channels", select: { id: true, guild_id: true, type: true, name: true } },
|
||||
])
|
||||
);
|
||||
const doc = await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save();
|
||||
const data = toObject(doc);
|
||||
|
||||
await emitEvent({ event: "MESSAGE_CREATE", channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent);
|
||||
|
||||
|
@ -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);
|
@ -11,37 +11,42 @@ router.get("/", async (req: Request, res: Response) => {
|
||||
if (!user) throw new HTTPError("User not found", 404);
|
||||
|
||||
var guildIDs = user.guilds || [];
|
||||
var guild = await GuildModel.find({ id: { $in: guildIDs } }).exec();
|
||||
var guild = await GuildModel.find({ id: { $in: guildIDs } })
|
||||
.populate({ path: "joined_at", match: { id: req.user_id } })
|
||||
.exec();
|
||||
|
||||
res.json(toObject(guild));
|
||||
});
|
||||
|
||||
// user send to leave a certain guild
|
||||
router.delete("/:id", async (req: Request, res: Response) => {
|
||||
const guildID = req.params.id;
|
||||
const guild = await GuildModel.findOne({ id: guildID }).exec();
|
||||
const guild_id = req.params.id;
|
||||
const guild = await GuildModel.findOne({ id: guild_id }, { guild_id: true }).exec();
|
||||
|
||||
if (!guild) throw new HTTPError("Guild doesn't exist", 404);
|
||||
if (guild.owner_id === req.user_id) throw new HTTPError("You can't leave your own guild", 400);
|
||||
|
||||
await MemberModel.deleteOne({ id: req.user_id, guild_id: guildID }).exec();
|
||||
await UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guildID } }).exec();
|
||||
const user = await getPublicUser(req.user_id);
|
||||
|
||||
await emitEvent({
|
||||
await Promise.all([
|
||||
MemberModel.deleteOne({ id: req.user_id, guild_id: guild_id }).exec(),
|
||||
UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guild_id } }).exec(),
|
||||
emitEvent({
|
||||
event: "GUILD_DELETE",
|
||||
data: {
|
||||
id: guildID,
|
||||
id: guild_id,
|
||||
},
|
||||
user_id: req.user_id,
|
||||
} as GuildDeleteEvent);
|
||||
} as GuildDeleteEvent),
|
||||
]);
|
||||
|
||||
const user = await getPublicUser(req.user_id);
|
||||
|
||||
await emitEvent({
|
||||
event: "GUILD_MEMBER_REMOVE",
|
||||
data: {
|
||||
guild_id: guildID,
|
||||
guild_id: guild_id,
|
||||
user: user,
|
||||
},
|
||||
guild_id: guildID,
|
||||
guild_id: guild_id,
|
||||
} as GuildMemberRemoveEvent);
|
||||
|
||||
return res.status(204).send();
|
||||
|
@ -5,6 +5,7 @@ export async function emitEvent(payload: Omit<Event, "created_at">) {
|
||||
created_at: new Date(), // in seconds
|
||||
...payload,
|
||||
};
|
||||
// TODO: bigint isn't working
|
||||
|
||||
return await new EventModel(obj).save();
|
||||
}
|
||||
|
@ -75,6 +75,7 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui
|
||||
},
|
||||
guild_id: guild_id,
|
||||
} as GuildMemberAddEvent),
|
||||
|
||||
emitEvent({
|
||||
event: "GUILD_CREATE",
|
||||
data: guild,
|
||||
|
Loading…
Reference in New Issue
Block a user