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 multer from "multer";
|
||||||
import { emitEvent } from "../../../../util/Event";
|
import { emitEvent } from "../../../../util/Event";
|
||||||
import { Query } from "mongoose";
|
import { Query } from "mongoose";
|
||||||
|
import { PublicMemberProjection } from "../../../../util/Member";
|
||||||
const router: Router = Router();
|
const router: Router = Router();
|
||||||
|
|
||||||
export default router;
|
export default router;
|
||||||
@ -80,14 +81,7 @@ router.get("/", async (req, res) => {
|
|||||||
query = MessageModel.find({ channel_id }).sort({ id: -1 });
|
query = MessageModel.find({ channel_id }).sort({ id: -1 });
|
||||||
}
|
}
|
||||||
|
|
||||||
const messages = await query
|
const messages = await query.limit(limit).exec();
|
||||||
.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();
|
|
||||||
|
|
||||||
return res.json(toObject(messages));
|
return res.json(toObject(messages));
|
||||||
});
|
});
|
||||||
@ -147,19 +141,11 @@ router.post("/", check(MessageCreateSchema), async (req, res) => {
|
|||||||
type: 0,
|
type: 0,
|
||||||
tts: body.tts,
|
tts: body.tts,
|
||||||
nonce: body.nonce,
|
nonce: body.nonce,
|
||||||
|
pinned: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
const doc = new MessageModel(message);
|
const doc = await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save();
|
||||||
await doc.save();
|
const data = toObject(doc);
|
||||||
|
|
||||||
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 } },
|
|
||||||
])
|
|
||||||
);
|
|
||||||
|
|
||||||
await emitEvent({ event: "MESSAGE_CREATE", channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent);
|
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) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
const guild_id = req.params.id;
|
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);
|
if (!guild) throw new HTTPError("Guild not found", 404);
|
||||||
|
|
||||||
var bans = await BanModel.find({ guild_id: guild_id }).exec();
|
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) => {
|
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;
|
var banned_user_id = req.params.user_id;
|
||||||
|
|
||||||
const banned_user = await getPublicUser(banned_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);
|
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")) {
|
if (!perms.has("BAN_MEMBERS")) {
|
||||||
throw new HTTPError("No permissions", 403);
|
throw new HTTPError("No permissions", 403);
|
||||||
}
|
}
|
||||||
|
|
||||||
await BanModel.deleteOne({
|
await BanModel.deleteOne({
|
||||||
user_id: banned_user_id,
|
user_id: banned_user_id,
|
||||||
guild_id: guild.id,
|
guild_id,
|
||||||
}).exec();
|
}).exec();
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "GUILD_BAN_REMOVE",
|
event: "GUILD_BAN_REMOVE",
|
||||||
data: {
|
data: {
|
||||||
guild_id: guild.id,
|
guild_id,
|
||||||
user: banned_user,
|
user: banned_user,
|
||||||
},
|
},
|
||||||
guild_id: guild.id,
|
guild_id,
|
||||||
} as GuildBanRemoveEvent);
|
} as GuildBanRemoveEvent);
|
||||||
|
|
||||||
return res.status(204).send();
|
return res.status(204).send();
|
@ -7,14 +7,14 @@ import { check } from "../../../util/instanceOf";
|
|||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get("/", async (req, res) => {
|
router.get("/", async (req, res) => {
|
||||||
const guild_id = req.params.id;
|
const { guild_id } = req.params;
|
||||||
const channels = await ChannelModel.find({ guild_id }).exec();
|
const channels = await ChannelModel.find({ guild_id }).exec();
|
||||||
|
|
||||||
res.json(toObject(channels));
|
res.json(toObject(channels));
|
||||||
});
|
});
|
||||||
|
|
||||||
router.post("/", check(ChannelModifySchema), async (req, res) => {
|
router.post("/", check(ChannelModifySchema), async (req, res) => {
|
||||||
const guild_id = req.params.id;
|
const { guild_id } = req.params;
|
||||||
const body = req.body as ChannelModifySchema;
|
const body = req.body as ChannelModifySchema;
|
||||||
if (!body.permission_overwrites) body.permission_overwrites = [];
|
if (!body.permission_overwrites) body.permission_overwrites = [];
|
||||||
if (!body.topic) body.topic = "";
|
if (!body.topic) body.topic = "";
|
@ -5,26 +5,31 @@ import {
|
|||||||
getPermission,
|
getPermission,
|
||||||
GuildDeleteEvent,
|
GuildDeleteEvent,
|
||||||
GuildModel,
|
GuildModel,
|
||||||
|
GuildUpdateEvent,
|
||||||
InviteModel,
|
InviteModel,
|
||||||
MemberModel,
|
MemberModel,
|
||||||
MessageModel,
|
MessageModel,
|
||||||
RoleModel,
|
RoleModel,
|
||||||
|
toObject,
|
||||||
UserModel,
|
UserModel,
|
||||||
} from "fosscord-server-util";
|
} from "fosscord-server-util";
|
||||||
import { HTTPError } from "lambert-server";
|
import { HTTPError } from "lambert-server";
|
||||||
import { GuildUpdateSchema } from "../../../schema/Guild";
|
import { GuildUpdateSchema } from "../../../schema/Guild";
|
||||||
import { emitEvent } from "../../../util/Event";
|
import { emitEvent } from "../../../util/Event";
|
||||||
import { check } from "../../../util/instanceOf";
|
import { check } from "../../../util/instanceOf";
|
||||||
|
import "missing-native-js-functions";
|
||||||
|
|
||||||
const router = Router();
|
const router = Router();
|
||||||
|
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
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);
|
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);
|
if (!member) throw new HTTPError("You are not a member of the guild you are trying to access", 401);
|
||||||
|
|
||||||
return res.json(guild);
|
return res.json(guild);
|
||||||
@ -32,20 +37,27 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
|
|
||||||
router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) => {
|
router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) => {
|
||||||
const body = req.body as GuildUpdateSchema;
|
const body = req.body as GuildUpdateSchema;
|
||||||
const guild_id = req.params.id;
|
const { guild_id } = req.params;
|
||||||
|
// TODO: guild update check image
|
||||||
const guild = await GuildModel.findOne({ id: guild_id }).exec();
|
|
||||||
if (!guild) throw new HTTPError("This guild does not exist", 404);
|
|
||||||
|
|
||||||
const perms = await getPermission(req.user_id, guild_id);
|
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);
|
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();
|
const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body)
|
||||||
return res.status(204);
|
.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) => {
|
// discord prefixes this route with /delete instead of using the delete method
|
||||||
var guild_id = req.params.id;
|
// 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();
|
const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec();
|
||||||
if (!guild) throw new HTTPError("This guild does not exist", 404);
|
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 InviteModel.deleteMany({ guild_id }).exec();
|
||||||
await MessageModel.deleteMany({ guild_id }).exec();
|
await MessageModel.deleteMany({ guild_id }).exec();
|
||||||
|
|
||||||
return res.status(204).send();
|
return res.sendStatus(204);
|
||||||
});
|
});
|
||||||
|
|
||||||
export default router;
|
export default router;
|
@ -10,7 +10,7 @@ const router = Router();
|
|||||||
// TODO: not allowed for user -> only allowed for bots with privileged intents
|
// TODO: not allowed for user -> only allowed for bots with privileged intents
|
||||||
// TODO: send over websocket
|
// TODO: send over websocket
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
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 }).exec();
|
||||||
if (!guild) throw new HTTPError("Guild not found", 404);
|
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)
|
var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection)
|
||||||
.limit(limit)
|
.limit(limit)
|
||||||
.populate({ path: "user", select: PublicUserProjection })
|
|
||||||
.exec();
|
.exec();
|
||||||
|
|
||||||
return res.json(toObject(members));
|
return res.json(toObject(members));
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/:member", async (req: Request, res: Response) => {
|
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 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);
|
if (!member) throw new HTTPError("Member not found", 404);
|
||||||
|
|
||||||
return res.json(member);
|
return res.json(member);
|
@ -11,37 +11,42 @@ router.get("/", async (req: Request, res: Response) => {
|
|||||||
if (!user) throw new HTTPError("User not found", 404);
|
if (!user) throw new HTTPError("User not found", 404);
|
||||||
|
|
||||||
var guildIDs = user.guilds || [];
|
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));
|
res.json(toObject(guild));
|
||||||
});
|
});
|
||||||
|
|
||||||
// user send to leave a certain guild
|
// user send to leave a certain guild
|
||||||
router.delete("/:id", async (req: Request, res: Response) => {
|
router.delete("/:id", async (req: Request, res: Response) => {
|
||||||
const guildID = req.params.id;
|
const guild_id = req.params.id;
|
||||||
const guild = await GuildModel.findOne({ id: guildID }).exec();
|
const guild = await GuildModel.findOne({ id: guild_id }, { guild_id: true }).exec();
|
||||||
|
|
||||||
if (!guild) throw new HTTPError("Guild doesn't exist", 404);
|
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);
|
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 Promise.all([
|
||||||
await UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guildID } }).exec();
|
MemberModel.deleteOne({ id: req.user_id, guild_id: guild_id }).exec(),
|
||||||
const user = await getPublicUser(req.user_id);
|
UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guild_id } }).exec(),
|
||||||
|
emitEvent({
|
||||||
|
event: "GUILD_DELETE",
|
||||||
|
data: {
|
||||||
|
id: guild_id,
|
||||||
|
},
|
||||||
|
user_id: req.user_id,
|
||||||
|
} as GuildDeleteEvent),
|
||||||
|
]);
|
||||||
|
|
||||||
await emitEvent({
|
const user = await getPublicUser(req.user_id);
|
||||||
event: "GUILD_DELETE",
|
|
||||||
data: {
|
|
||||||
id: guildID,
|
|
||||||
},
|
|
||||||
user_id: req.user_id,
|
|
||||||
} as GuildDeleteEvent);
|
|
||||||
|
|
||||||
await emitEvent({
|
await emitEvent({
|
||||||
event: "GUILD_MEMBER_REMOVE",
|
event: "GUILD_MEMBER_REMOVE",
|
||||||
data: {
|
data: {
|
||||||
guild_id: guildID,
|
guild_id: guild_id,
|
||||||
user: user,
|
user: user,
|
||||||
},
|
},
|
||||||
guild_id: guildID,
|
guild_id: guild_id,
|
||||||
} as GuildMemberRemoveEvent);
|
} as GuildMemberRemoveEvent);
|
||||||
|
|
||||||
return res.status(204).send();
|
return res.status(204).send();
|
||||||
|
@ -5,6 +5,7 @@ export async function emitEvent(payload: Omit<Event, "created_at">) {
|
|||||||
created_at: new Date(), // in seconds
|
created_at: new Date(), // in seconds
|
||||||
...payload,
|
...payload,
|
||||||
};
|
};
|
||||||
|
// TODO: bigint isn't working
|
||||||
|
|
||||||
return await new EventModel(obj).save();
|
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,
|
guild_id: guild_id,
|
||||||
} as GuildMemberAddEvent),
|
} as GuildMemberAddEvent),
|
||||||
|
|
||||||
emitEvent({
|
emitEvent({
|
||||||
event: "GUILD_CREATE",
|
event: "GUILD_CREATE",
|
||||||
data: guild,
|
data: guild,
|
||||||
|
Loading…
Reference in New Issue
Block a user