diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-08-24 16:35:04 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-08-24 16:35:04 +0200 |
commit | ef4d4a318176c3e572adc17427a8b8c728a618ab (patch) | |
tree | d95fedbf18b7b7438613b08b72eb30fa5fca8921 /api | |
parent | :sparkles: typeorm entities (diff) | |
download | server-ef4d4a318176c3e572adc17427a8b8c728a618ab.tar.xz |
:construction: api
Diffstat (limited to 'api')
45 files changed, 381 insertions, 454 deletions
diff --git a/api/src/middlewares/RateLimit.ts b/api/src/middlewares/RateLimit.ts index acf92606..9601dad3 100644 --- a/api/src/middlewares/RateLimit.ts +++ b/api/src/middlewares/RateLimit.ts @@ -72,7 +72,7 @@ export default function RateLimit(opts: { offender.expires_at = new Date(Date.now() + opts.window * 1000); offender.blocked = false; // mongodb ttl didn't update yet -> manually update/delete - db.collection("ratelimits").updateOne({ id: bucket_id, user_id }, { $set: offender }); + db.collection("ratelimits").update({ id: bucket_id, user_id }, { $set: offender }); Cache.delete(user_id + bucket_id); } } @@ -132,7 +132,7 @@ export async function initRateLimits(app: Router) { async function hitRoute(opts: { user_id: string; bucket_id: string; max_hits: number; window: number }) { const filter = { id: opts.bucket_id, user_id: opts.user_id }; - const { value } = await db.collection("ratelimits").findOneAndUpdate( + const { value } = await db.collection("ratelimits").findOneOrFailAndUpdate( filter, { $setOnInsert: { @@ -158,7 +158,7 @@ async function hitRoute(opts: { user_id: string; bucket_id: string; max_hits: nu event: EventRateLimit, data: value }); - await db.collection("ratelimits").updateOne(filter, { $set: { blocked: true } }); + await db.collection("ratelimits").update(filter, { $set: { blocked: true } }); } else { Cache.delete(opts.user_id); } diff --git a/api/src/routes/auth/login.ts b/api/src/routes/auth/login.ts index dc970e4c..579a097e 100644 --- a/api/src/routes/auth/login.ts +++ b/api/src/routes/auth/login.ts @@ -2,7 +2,7 @@ import { Request, Response, Router } from "express"; import { check, FieldErrors, Length } from "../../util/instanceOf"; import bcrypt from "bcrypt"; import jwt from "jsonwebtoken"; -import { Config, UserModel } from "@fosscord/util"; +import { Config, User } from "@fosscord/util"; import { adjustEmail } from "./register"; const router: Router = Router(); @@ -41,27 +41,25 @@ router.post( // TODO: check captcha } - const user = await UserModel.findOne( + const user = await User.findOneOrFail( { $or: query }, - { "user_data.hash": true, id: true, disabled: true, deleted: true, "user_settings.locale": true, "user_settings.theme": true } - ) - .exec() - .catch((e) => { - console.log(e, query); - throw FieldErrors({ login: { message: req.t("auth:login.INVALID_LOGIN"), code: "INVALID_LOGIN" } }); - }); + { "data.hash": true, id: true, disabled: true, deleted: true, "settings.locale": true, "settings.theme": true } + ).catch((e) => { + console.log(e, query); + throw FieldErrors({ login: { message: req.t("auth:login.INVALID_LOGIN"), code: "INVALID_LOGIN" } }); + }); if (undelete) { // undelete refers to un'disable' here - if (user.disabled) await UserModel.updateOne({ id: user.id }, { disabled: false }).exec(); - if (user.deleted) await UserModel.updateOne({ id: user.id }, { deleted: false }).exec(); + if (user.disabled) await User.update({ id: user.id }, { disabled: false }); + if (user.deleted) await User.update({ id: user.id }, { deleted: false }); } else { if (user.deleted) return res.status(400).json({ message: "This account is scheduled for deletion.", code: 20011 }); if (user.disabled) return res.status(400).json({ message: req.t("auth:login.ACCOUNT_DISABLED"), code: 20013 }); } // the salt is saved in the password refer to bcrypt docs - const same_password = await bcrypt.compare(password, user.user_data.hash || ""); + const same_password = await bcrypt.compare(password, user.data.hash || ""); if (!same_password) { throw FieldErrors({ password: { message: req.t("auth:login.INVALID_PASSWORD"), code: "INVALID_PASSWORD" } }); } @@ -72,7 +70,7 @@ router.post( // Discord header is just the user id as string, which is not possible with npm-jsonwebtoken package // https://user-images.githubusercontent.com/6506416/81051916-dd8c9900-8ec2-11ea-8794-daf12d6f31f0.png - res.json({ token, user_settings: user.user_settings }); + res.json({ token, settings: user.settings }); } ); @@ -106,6 +104,6 @@ export async function generateToken(id: string) { * @returns {"captcha_key": ["captcha-required"], "captcha_sitekey": null, "captcha_service": "recaptcha"} * Sucess: - * @returns {"token": "USERTOKEN", "user_settings": {"locale": "en", "theme": "dark"}} + * @returns {"token": "USERTOKEN", "settings": {"locale": "en", "theme": "dark"}} */ diff --git a/api/src/routes/auth/register.ts b/api/src/routes/auth/register.ts index fecde874..1405e219 100644 --- a/api/src/routes/auth/register.ts +++ b/api/src/routes/auth/register.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { trimSpecial, User, Snowflake, UserModel, Config } from "@fosscord/util"; +import { trimSpecial, User, Snowflake, User, Config } from "@fosscord/util"; import bcrypt from "bcrypt"; import { check, Email, EMAIL_REGEX, FieldErrors, Length } from "../../util/instanceOf"; import "missing-native-js-functions"; @@ -92,9 +92,7 @@ router.post( if (!adjusted_email) throw FieldErrors({ email: { code: "INVALID_EMAIL", message: req.t("auth:register.INVALID_EMAIL") } }); // check if there is already an account with this email - const exists = await UserModel.findOne({ email: adjusted_email }) - .exec() - .catch((e) => {}); + const exists = await User.findOneOrFail({ email: adjusted_email }).catch((e) => {}); if (exists) { throw FieldErrors({ @@ -131,9 +129,7 @@ router.post( if (!register.allowMultipleAccounts) { // TODO: check if fingerprint was eligible generated - const exists = await UserModel.findOne({ fingerprints: fingerprint }) - .exec() - .catch((e) => {}); + const exists = await User.findOneOrFail({ fingerprints: fingerprint }).catch((e) => {}); if (exists) { throw FieldErrors({ @@ -169,7 +165,7 @@ router.post( for (let tries = 0; tries < 5; tries++) { discriminator = Math.randomIntBetween(1, 9999).toString().padStart(4, "0"); try { - exists = await UserModel.findOne({ discriminator, username: adjusted_username }, "id").exec(); + exists = await User.findOneOrFail({ discriminator, username: adjusted_username }, "id"); } catch (error) { // doesn't exist -> break break; @@ -223,14 +219,14 @@ router.post( public_flags: 0n, flags: 0n, // TODO: generate default flags guilds: [], - user_data: { + data: { hash: adjusted_password, valid_tokens_since: new Date(), relationships: [], connected_accounts: [], fingerprints: [] }, - user_settings: { + settings: { afk_timeout: 300, allow_accessibility_detection: true, animate_emoji: true, @@ -272,7 +268,7 @@ router.post( }; // insert user into database - await new UserModel(user).save(); + await new User(user).save(); return res.json({ token: await generateToken(user.id) }); } diff --git a/api/src/routes/channels/#channel_id/index.ts b/api/src/routes/channels/#channel_id/index.ts index fb6bcb1a..91534602 100644 --- a/api/src/routes/channels/#channel_id/index.ts +++ b/api/src/routes/channels/#channel_id/index.ts @@ -1,4 +1,4 @@ -import { ChannelDeleteEvent, ChannelModel, ChannelUpdateEvent, emitEvent, getPermission, GuildUpdateEvent, toObject } from "@fosscord/util"; +import { ChannelDeleteEvent, Channel, ChannelUpdateEvent, emitEvent, getPermission, GuildUpdateEvent, toObject } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; import { ChannelModifySchema } from "../../../schema/Channel"; @@ -10,28 +10,28 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); - return res.send(toObject(channel)); + return res.send(channel); }); router.delete("/", async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); permission.hasThrow("MANAGE_CHANNELS"); // TODO: Dm channel "close" not delete - const data = toObject(channel); + const data = channel; await emitEvent({ event: "CHANNEL_DELETE", data, channel_id } as ChannelDeleteEvent); - await ChannelModel.deleteOne({ id: channel_id }); + await Channel.deleteOne({ id: channel_id }); res.send(data); }); @@ -43,9 +43,9 @@ router.patch("/", check(ChannelModifySchema), async (req: Request, res: Response const permission = await getPermission(req.user_id, undefined, channel_id); permission.hasThrow("MANAGE_CHANNELS"); - const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload, { new: true }).exec(); + const channel = await Channel.findOneOrFailAndUpdate({ id: channel_id }, payload, { new: true }); - const data = toObject(channel); + const data = channel; await emitEvent({ event: "CHANNEL_UPDATE", diff --git a/api/src/routes/channels/#channel_id/invites.ts b/api/src/routes/channels/#channel_id/invites.ts index 438f8c51..cc77df26 100644 --- a/api/src/routes/channels/#channel_id/invites.ts +++ b/api/src/routes/channels/#channel_id/invites.ts @@ -6,14 +6,14 @@ import { random } from "../../../util/RandomInviteID"; import { InviteCreateSchema } from "../../../schema/Invite"; -import { getPermission, ChannelModel, InviteModel, InviteCreateEvent, toObject, emitEvent } from "@fosscord/util"; +import { getPermission, Channel, InviteModel, InviteCreateEvent, toObject, emitEvent } from "@fosscord/util"; const router: Router = Router(); router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => { const { user_id } = req; const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); @@ -47,7 +47,7 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) router.get("/", async (req: Request, res: Response) => { const { user_id } = req; const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); if (!channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); @@ -56,9 +56,9 @@ router.get("/", async (req: Request, res: Response) => { const permission = await getPermission(user_id, guild_id); permission.hasThrow("MANAGE_CHANNELS"); - const invites = await InviteModel.find({ guild_id }).exec(); + const invites = await Invite.find({ guild_id }); - res.status(200).send(toObject(invites)); + res.status(200).send(invites); }); export default router; diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts b/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts index bbc779dd..ff576d90 100644 --- a/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts +++ b/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts @@ -1,4 +1,4 @@ -import { emitEvent, getPermission, MessageAckEvent, ReadStateModel } from "@fosscord/util"; +import { emitEvent, getPermission, MessageAckEvent, ReadState } from "@fosscord/util"; import { Request, Response, Router } from "express"; import { check } from "../../../../../util/instanceOf"; @@ -14,10 +14,7 @@ router.post("/", check({ $manual: Boolean, $mention_count: Number }), async (req const permission = await getPermission(req.user_id, undefined, channel_id); permission.hasThrow("VIEW_CHANNEL"); - await ReadStateModel.updateOne( - { user_id: req.user_id, channel_id, message_id }, - { user_id: req.user_id, channel_id, message_id } - ).exec(); + await ReadState.update({ user_id: req.user_id, channel_id, message_id }, { user_id: req.user_id, channel_id, message_id }); await emitEvent({ event: "MESSAGE_ACK", diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/index.ts b/api/src/routes/channels/#channel_id/messages/#message_id/index.ts index 35952d26..058e2390 100644 --- a/api/src/routes/channels/#channel_id/messages/#message_id/index.ts +++ b/api/src/routes/channels/#channel_id/messages/#message_id/index.ts @@ -1,4 +1,4 @@ -import { ChannelModel, emitEvent, getPermission, MessageDeleteEvent, MessageModel, MessageUpdateEvent, toObject } from "@fosscord/util"; +import { Channel, emitEvent, getPermission, MessageDeleteEvent, Message, MessageUpdateEvent, toObject } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; import { MessageCreateSchema } from "../../../../../schema/Message"; @@ -12,7 +12,7 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response const { message_id, channel_id } = req.params; var body = req.body as MessageCreateSchema; - var message = await MessageModel.findOne({ id: message_id, channel_id }, { author_id: true, message_reference: true }).lean().exec(); + var message = await Message.findOneOrFail({ id: message_id, channel_id }, { author_id: true, message_reference: true }); const permissions = await getPermission(req.user_id, undefined, channel_id); @@ -31,17 +31,17 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response }); // @ts-ignore - message = await MessageModel.findOneAndUpdate({ id: message_id }, opts, { new: true }).populate("author").exec(); + message = await Message.findOneOrFailAndUpdate({ id: message_id }, opts, { new: true }).populate("author"); await emitEvent({ event: "MESSAGE_UPDATE", channel_id, - data: { ...toObject(message), nonce: undefined } + data: { ...message, nonce: undefined } } as MessageUpdateEvent); postHandleMessage(message); - return res.json(toObject(message)); + return res.json(message); }); // TODO: delete attachments in message @@ -49,13 +49,13 @@ router.patch("/", check(MessageCreateSchema), async (req: Request, res: Response router.delete("/", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }); - const message = await MessageModel.findOne({ id: message_id }, { author_id: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }, { guild_id: true }); + const message = await Message.findOneOrFail({ id: message_id }, { author_id: true }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); if (message.author_id !== req.user_id) permission.hasThrow("MANAGE_MESSAGES"); - await MessageModel.deleteOne({ id: message_id }).exec(); + await Message.deleteOne({ id: message_id }); await emitEvent({ event: "MESSAGE_DELETE", diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/reactions.ts b/api/src/routes/channels/#channel_id/messages/#message_id/reactions.ts index 7da63644..9a6e4436 100644 --- a/api/src/routes/channels/#channel_id/messages/#message_id/reactions.ts +++ b/api/src/routes/channels/#channel_id/messages/#message_id/reactions.ts @@ -1,10 +1,10 @@ import { - ChannelModel, + Channel, emitEvent, EmojiModel, getPermission, - MemberModel, - MessageModel, + Member, + Message, MessageReactionAddEvent, MessageReactionRemoveAllEvent, MessageReactionRemoveEmojiEvent, @@ -12,7 +12,7 @@ import { PartialEmoji, PublicUserProjection, toObject, - UserModel + User } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; @@ -38,12 +38,12 @@ function getEmoji(emoji: string): PartialEmoji { router.delete("/", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }, { guild_id: true }); const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_MESSAGES"); - await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }, { new: true }).exec(); + await Message.findOneOrFailAndUpdate({ id: message_id, channel_id }, { reactions: [] }, { new: true }); await emitEvent({ event: "MESSAGE_REACTION_REMOVE_ALL", @@ -62,18 +62,18 @@ router.delete("/:emoji", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; const emoji = getEmoji(req.params.emoji); - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }, { guild_id: true }); const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_MESSAGES"); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); if (!already_added) throw new HTTPError("Reaction not found", 404); message.reactions.remove(already_added); - await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); + await Message.update({ id: message_id, channel_id }, message); await emitEvent({ event: "MESSAGE_REACTION_REMOVE_EMOJI", @@ -93,7 +93,7 @@ router.get("/:emoji", async (req: Request, res: Response) => { const { message_id, channel_id } = req.params; const emoji = getEmoji(req.params.emoji); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); if (!message) throw new HTTPError("Message not found", 404); const reaction = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); if (!reaction) throw new HTTPError("Reaction not found", 404); @@ -101,9 +101,9 @@ router.get("/:emoji", async (req: Request, res: Response) => { const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("VIEW_CHANNEL"); - const users = await UserModel.find({ id: { $in: reaction.user_ids } }, PublicUserProjection).exec(); + const users = await User.find({ id: { $in: reaction.user_ids } }, PublicUserProjection); - res.json(toObject(users)); + res.json(users); }); router.put("/:emoji/:user_id", async (req: Request, res: Response) => { @@ -111,8 +111,8 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => { if (user_id !== "@me") throw new HTTPError("Invalid user"); const emoji = getEmoji(req.params.emoji); - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }, { guild_id: true }); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); const already_added = message.reactions.find((x) => (x.emoji.id === emoji.id && emoji.id) || x.emoji.name === emoji.name); const permissions = await getPermission(req.user_id, undefined, channel_id); @@ -120,7 +120,7 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => { if (!already_added) permissions.hasThrow("ADD_REACTIONS"); if (emoji.id) { - const external_emoji = await EmojiModel.findOne({ id: emoji.id }).exec(); + const external_emoji = await Emoji.findOneOrFail({ id: emoji.id }); if (!already_added) permissions.hasThrow("USE_EXTERNAL_EMOJIS"); emoji.animated = external_emoji.animated; emoji.name = external_emoji.name; @@ -131,9 +131,9 @@ router.put("/:emoji/:user_id", async (req: Request, res: Response) => { already_added.count++; } else message.reactions.push({ count: 1, emoji, user_ids: [req.user_id] }); - await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); + await Message.update({ id: message_id, channel_id }, message); - const member = channel.guild_id && (await MemberModel.findOne({ id: req.user_id }).exec()); + const member = channel.guild_id && (await Member.findOneOrFail({ id: req.user_id })); await emitEvent({ event: "MESSAGE_REACTION_ADD", @@ -156,8 +156,8 @@ router.delete("/:emoji/:user_id", async (req: Request, res: Response) => { const emoji = getEmoji(req.params.emoji); - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true }).exec(); - const message = await MessageModel.findOne({ id: message_id, channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }, { guild_id: true }); + const message = await Message.findOneOrFail({ id: message_id, channel_id }); const permissions = await getPermission(req.user_id, undefined, channel_id); @@ -171,7 +171,7 @@ router.delete("/:emoji/:user_id", async (req: Request, res: Response) => { if (already_added.count <= 0) message.reactions.remove(already_added); - await MessageModel.updateOne({ id: message_id, channel_id }, message).exec(); + await Message.update({ id: message_id, channel_id }, message); await emitEvent({ event: "MESSAGE_REACTION_REMOVE", diff --git a/api/src/routes/channels/#channel_id/messages/bulk-delete.ts b/api/src/routes/channels/#channel_id/messages/bulk-delete.ts index 8132462f..028a5d73 100644 --- a/api/src/routes/channels/#channel_id/messages/bulk-delete.ts +++ b/api/src/routes/channels/#channel_id/messages/bulk-delete.ts @@ -1,5 +1,5 @@ import { Router, Response, Request } from "express"; -import { ChannelModel, Config, emitEvent, getPermission, MessageDeleteBulkEvent, MessageModel } from "@fosscord/util"; +import { Channel, Config, emitEvent, getPermission, MessageDeleteBulkEvent, Message } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "../../../../util/instanceOf"; @@ -13,7 +13,7 @@ export default router; // https://discord.com/developers/docs/resources/channel#bulk-delete-messages router.post("/", check({ messages: [String] }), async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }, { permission_overwrites: true, guild_id: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }, { permission_overwrites: true, guild_id: true }); if (!channel.guild_id) throw new HTTPError("Can't bulk delete dm channel messages", 400); const permission = await getPermission(req.user_id, channel?.guild_id, channel_id, { channel }); @@ -25,7 +25,7 @@ router.post("/", check({ messages: [String] }), async (req: Request, res: Respon if (messages.length < 2) throw new HTTPError("You must at least specify 2 messages to bulk delete"); if (messages.length > maxBulkDelete) throw new HTTPError(`You cannot delete more than ${maxBulkDelete} messages`); - await MessageModel.deleteMany({ id: { $in: messages } }).exec(); + await Message.deleteMany({ id: { $in: messages } }); await emitEvent({ event: "MESSAGE_DELETE_BULK", diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts index 6ae6491f..b5c650bb 100644 --- a/api/src/routes/channels/#channel_id/messages/index.ts +++ b/api/src/routes/channels/#channel_id/messages/index.ts @@ -1,5 +1,5 @@ import { Router, Response, Request } from "express"; -import { Attachment, ChannelModel, ChannelType, getPermission, MessageDocument, MessageModel, toObject } from "@fosscord/util"; +import { Attachment, Channel, ChannelType, getPermission, MessageDocument, Message, toObject } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { MessageCreateSchema } from "../../../../schema/Message"; import { check, instanceOf, Length } from "../../../../util/instanceOf"; @@ -30,12 +30,10 @@ export function isTextChannel(type: ChannelType): boolean { // get messages router.get("/", async (req: Request, res: Response) => { const channel_id = req.params.channel_id; - const channel = await ChannelModel.findOne( + const channel = await Channel.findOneOrFail( { id: channel_id }, - { guild_id: true, type: true, permission_overwrites: true, recipient_ids: true, owner_id: true } - ) - .lean() // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids - .exec(); + { select: ["guild_id", "type", "permission_overwrites", "recipient_ids", "owner_id"] } + ); // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids if (!channel) throw new HTTPError("Channel not found", 404); isTextChannel(channel.type); @@ -58,35 +56,33 @@ router.get("/", async (req: Request, res: Response) => { if (!permissions.has("READ_MESSAGE_HISTORY")) return res.json([]); var query: Query<MessageDocument[], MessageDocument>; - if (after) query = MessageModel.find({ channel_id, id: { $gt: after } }); - else if (before) query = MessageModel.find({ channel_id, id: { $lt: before } }); + if (after) query = Message.find({ channel_id, id: { $gt: after } }); + else if (before) query = Message.find({ channel_id, id: { $lt: before } }); else if (around) - query = MessageModel.find({ + query = Message.find({ channel_id, id: { $gt: (BigInt(around) - BigInt(halfLimit)).toString(), $lt: (BigInt(around) + BigInt(halfLimit)).toString() } }); else { - query = MessageModel.find({ channel_id }); + query = Message.find({ channel_id }); } query = query.sort({ id: -1 }); - const messages = await query.limit(limit).exec(); + const messages = await query.limit(limit); - return res.json( - toObject(messages).map((x) => { - (x.reactions || []).forEach((x) => { - // @ts-ignore - if ((x.user_ids || []).includes(req.user_id)) x.me = true; - // @ts-ignore - delete x.user_ids; - }); + return res.json(messages).map((x) => { + (x.reactions || []).forEach((x) => { // @ts-ignore - if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: 0n, avatar: null }; + if ((x.user_ids || []).includes(req.user_id)) x.me = true; + // @ts-ignore + delete x.user_ids; + }); + // @ts-ignore + if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: 0n, avatar: null }; - return x; - }) - ); + return x; + }); }); // TODO: config max upload size diff --git a/api/src/routes/channels/#channel_id/permissions.ts b/api/src/routes/channels/#channel_id/permissions.ts index f93075b1..2990e35b 100644 --- a/api/src/routes/channels/#channel_id/permissions.ts +++ b/api/src/routes/channels/#channel_id/permissions.ts @@ -1,13 +1,4 @@ -import { - ChannelModel, - ChannelPermissionOverwrite, - ChannelUpdateEvent, - emitEvent, - getPermission, - MemberModel, - RoleModel, - toObject -} from "@fosscord/util"; +import { Channel, ChannelPermissionOverwrite, ChannelUpdateEvent, emitEvent, getPermission, Member, Role, toObject } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; @@ -20,16 +11,16 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number, const { channel_id, overwrite_id } = req.params; const body = req.body as { allow: bigint; deny: bigint; type: number; id: string }; - var channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, permission_overwrites: true }).exec(); + var channel = await Channel.findOneOrFail({ id: channel_id }, { guild_id: true, permission_overwrites: true }); if (!channel.guild_id) throw new HTTPError("Channel not found", 404); const permissions = await getPermission(req.user_id, channel.guild_id, channel_id); permissions.hasThrow("MANAGE_ROLES"); if (body.type === 0) { - if (!(await RoleModel.exists({ id: overwrite_id }))) throw new HTTPError("role not found", 404); + if (!(await Role.exists({ id: overwrite_id }))) throw new HTTPError("role not found", 404); } else if (body.type === 1) { - if (!(await MemberModel.exists({ id: overwrite_id }))) throw new HTTPError("user not found", 404); + if (!(await Member.exists({ id: overwrite_id }))) throw new HTTPError("user not found", 404); } else throw new HTTPError("type not supported", 501); // @ts-ignore @@ -48,12 +39,12 @@ router.put("/:overwrite_id", check({ allow: String, deny: String, type: Number, overwrite.deny = body.deny; // @ts-ignore - channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, channel, { new: true }).exec(); + channel = await Channel.findOneOrFailAndUpdate({ id: channel_id }, channel, { new: true }); await emitEvent({ event: "CHANNEL_UPDATE", channel_id, - data: toObject(channel) + data: channel } as ChannelUpdateEvent); return res.sendStatus(204); @@ -66,7 +57,7 @@ router.delete("/:overwrite_id", async (req: Request, res: Response) => { const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_ROLES"); - const channel = await ChannelModel.findOneAndUpdate( + const channel = await Channel.findOneOrFailAndUpdate( { id: channel_id }, { $pull: { permission_overwrites: { id: overwrite_id } } }, { new: true } @@ -76,7 +67,7 @@ router.delete("/:overwrite_id", async (req: Request, res: Response) => { await emitEvent({ event: "CHANNEL_UPDATE", channel_id, - data: toObject(channel) + data: channel } as ChannelUpdateEvent); return res.sendStatus(204); diff --git a/api/src/routes/channels/#channel_id/pins.ts b/api/src/routes/channels/#channel_id/pins.ts index 0dd81bd3..3ed42ab4 100644 --- a/api/src/routes/channels/#channel_id/pins.ts +++ b/api/src/routes/channels/#channel_id/pins.ts @@ -1,13 +1,4 @@ -import { - ChannelModel, - ChannelPinsUpdateEvent, - Config, - emitEvent, - getPermission, - MessageModel, - MessageUpdateEvent, - toObject -} from "@fosscord/util"; +import { Channel, ChannelPinsUpdateEvent, Config, emitEvent, getPermission, Message, MessageUpdateEvent, toObject } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; @@ -15,19 +6,19 @@ const router: Router = Router(); router.put("/:message_id", async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); // * in dm channels anyone can pin messages -> only check for guilds if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES"); - const pinned_count = await MessageModel.count({ channel_id, pinned: true }).exec(); + const pinned_count = await Messagecount({ channel_id, pinned: true }); const { maxPins } = Config.get().limits.channel; if (pinned_count >= maxPins) throw new HTTPError("Max pin count reached: " + maxPins); - await MessageModel.updateOne({ id: message_id }, { pinned: true }).exec(); - const message = toObject(await MessageModel.findOne({ id: message_id }).exec()); + await Message.update({ id: message_id }, { pinned: true }); + const message = await Message.findOneOrFail({ id: message_id }); await emitEvent({ event: "MESSAGE_UPDATE", @@ -51,13 +42,13 @@ router.put("/:message_id", async (req: Request, res: Response) => { router.delete("/:message_id", async (req: Request, res: Response) => { const { channel_id, message_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES"); - const message = toObject(await MessageModel.findOneAndUpdate({ id: message_id }, { pinned: false }, { new: true }).exec()); + const message = await Message.findOneOrFailAndUpdate({ id: message_id }, { pinned: false }, { new: true }); await emitEvent({ event: "MESSAGE_UPDATE", @@ -81,13 +72,13 @@ router.delete("/:message_id", async (req: Request, res: Response) => { router.get("/", async (req: Request, res: Response) => { const { channel_id } = req.params; - const channel = await ChannelModel.findOne({ id: channel_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); const permission = await getPermission(req.user_id, channel.guild_id, channel_id); permission.hasThrow("VIEW_CHANNEL"); - let pins = await MessageModel.find({ channel_id: channel_id, pinned: true }).exec(); + let pins = await Message.find({ channel_id: channel_id, pinned: true }); - res.send(toObject(pins)); + res.send(pins); }); export default router; diff --git a/api/src/routes/channels/#channel_id/typing.ts b/api/src/routes/channels/#channel_id/typing.ts index 21d453d8..298d2f50 100644 --- a/api/src/routes/channels/#channel_id/typing.ts +++ b/api/src/routes/channels/#channel_id/typing.ts @@ -1,4 +1,4 @@ -import { ChannelModel, emitEvent, MemberModel, toObject, TypingStartEvent } from "@fosscord/util"; +import { Channel, emitEvent, Member, toObject, TypingStartEvent } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; @@ -9,15 +9,15 @@ router.post("/", async (req: Request, res: Response) => { const { channel_id } = req.params; const user_id = req.user_id; const timestamp = Date.now(); - const channel = await ChannelModel.findOne({ id: channel_id }); - const member = await MemberModel.findOne({ id: user_id }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }); + const member = await Member.findOneOrFail({ id: user_id }); await emitEvent({ event: "TYPING_START", channel_id: channel_id, data: { // this is the paylod - member: toObject(member), + member: member, channel_id, timestamp, user_id, diff --git a/api/src/routes/channels/#channel_id/webhooks.ts b/api/src/routes/channels/#channel_id/webhooks.ts index 7852f8f3..38a8226b 100644 --- a/api/src/routes/channels/#channel_id/webhooks.ts +++ b/api/src/routes/channels/#channel_id/webhooks.ts @@ -1,6 +1,6 @@ import { Router, Response, Request } from "express"; import { check, Length } from "../../../util/instanceOf"; -import { ChannelModel, getPermission, trimSpecial } from "@fosscord/util"; +import { Channel, getPermission, trimSpecial } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { isTextChannel } from "./messages/index"; @@ -10,7 +10,7 @@ const router: Router = Router(); // TODO: use Image Data Type for avatar instead of String router.post("/", check({ name: new Length(String, 1, 80), $avatar: String }), async (req: Request, res: Response) => { const channel_id = req.params.channel_id; - const channel = await ChannelModel.findOne({ id: channel_id }, { guild_id: true, type: true }).exec(); + const channel = await Channel.findOneOrFail({ id: channel_id }, { guild_id: true, type: true }); isTextChannel(channel.type); if (!channel.guild_id) throw new HTTPError("Not a guild channel", 400); diff --git a/api/src/routes/guilds/#guild_id/bans.ts b/api/src/routes/guilds/#guild_id/bans.ts index bb3eac03..cbc0b0fa 100644 --- a/api/src/routes/guilds/#guild_id/bans.ts +++ b/api/src/routes/guilds/#guild_id/bans.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { BanModel, emitEvent, getPermission, GuildBanAddEvent, GuildBanRemoveEvent, GuildModel, toObject } from "@fosscord/util"; +import { BanModel, emitEvent, getPermission, GuildBanAddEvent, GuildBanRemoveEvent, Guild, toObject } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { getIpAdress } from "../../../util/ipAddress"; import { BanCreateSchema } from "../../../schema/Ban"; @@ -13,18 +13,18 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.exists({ id: guild_id }); + const guild = await Guild.exists({ id: guild_id }); if (!guild) throw new HTTPError("Guild not found", 404); - var bans = await BanModel.find({ guild_id: guild_id }, { user_id: true, reason: true }).exec(); - return res.json(toObject(bans)); + var bans = await Ban.find({ guild_id: guild_id }, { user_id: true, reason: true }); + return res.json(bans); }); router.get("/:user", async (req: Request, res: Response) => { const { guild_id } = req.params; const user_id = req.params.ban; - var ban = await BanModel.findOne({ guild_id: guild_id, user_id: user_id }).exec(); + var ban = await Ban.findOneOrFail({ guild_id: guild_id, user_id: user_id }); return res.json(ban); }); @@ -56,7 +56,7 @@ router.put("/:user_id", check(BanCreateSchema), async (req: Request, res: Respon guild_id: guild_id } as GuildBanAddEvent); - return res.json(toObject(ban)); + return res.json(ban); }); router.delete("/:user_id", async (req: Request, res: Response) => { @@ -64,16 +64,16 @@ router.delete("/:user_id", async (req: Request, res: Response) => { var banned_user_id = req.params.user_id; const banned_user = await getPublicUser(banned_user_id); - const guild = await GuildModel.exists({ id: guild_id }); + const guild = await Guild.exists({ id: guild_id }); if (!guild) throw new HTTPError("Guild not found", 404); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("BAN_MEMBERS"); - await BanModel.deleteOne({ + await Ban.deleteOne({ user_id: banned_user_id, guild_id - }).exec(); + }); await emitEvent({ event: "GUILD_BAN_REMOVE", diff --git a/api/src/routes/guilds/#guild_id/channels.ts b/api/src/routes/guilds/#guild_id/channels.ts index 1c55ef24..b53c9a5a 100644 --- a/api/src/routes/guilds/#guild_id/channels.ts +++ b/api/src/routes/guilds/#guild_id/channels.ts @@ -1,5 +1,5 @@ import { Router, Response, Request } from "express"; -import { ChannelModel, toObject, ChannelUpdateEvent, getPermission, emitEvent } from "@fosscord/util"; +import { Channel, toObject, ChannelUpdateEvent, getPermission, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { ChannelModifySchema } from "../../../schema/Channel"; @@ -9,9 +9,9 @@ const router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const channels = await ChannelModel.find({ guild_id }).exec(); + const channels = await Channel.find({ guild_id }); - res.json(toObject(channels)); + res.json(channels); }); // TODO: check if channel type is permitted @@ -24,7 +24,7 @@ router.post("/", check(ChannelModifySchema), async (req: Request, res: Response) const channel = await createChannel({ ...body, guild_id }, req.user_id); - res.status(201).json(toObject(channel)); + res.status(201).json(channel); }); // TODO: check if parent_id exists @@ -48,18 +48,15 @@ router.patch( if (x.parent_id) { opts.parent_id = x.parent_id; - const parent_channel = await ChannelModel.findOne( - { id: x.parent_id, guild_id }, - { permission_overwrites: true } - ).exec(); + const parent_channel = await Channel.findOneOrFail({ id: x.parent_id, guild_id }, { permission_overwrites: true }); if (x.lock_permissions) { opts.permission_overwrites = parent_channel.permission_overwrites; } } - const channel = await ChannelModel.findOneAndUpdate({ id: x.id, guild_id }, opts, { new: true }).exec(); + const channel = await Channel.findOneOrFailAndUpdate({ id: x.id, guild_id }, opts, { new: true }); - await emitEvent({ event: "CHANNEL_UPDATE", data: toObject(channel), channel_id: x.id, guild_id } as ChannelUpdateEvent); + await emitEvent({ event: "CHANNEL_UPDATE", data: channel), channel_id: x.id, guild_id } as ChannelUpdateEvent; }) ]); diff --git a/api/src/routes/guilds/#guild_id/delete.ts b/api/src/routes/guilds/#guild_id/delete.ts index ba1c2fde..a53271ce 100644 --- a/api/src/routes/guilds/#guild_id/delete.ts +++ b/api/src/routes/guilds/#guild_id/delete.ts @@ -1,15 +1,4 @@ -import { - ChannelModel, - emitEvent, - EmojiModel, - GuildDeleteEvent, - GuildModel, - InviteModel, - MemberModel, - MessageModel, - RoleModel, - UserModel -} from "@fosscord/util"; +import { Channel, emitEvent, EmojiModel, GuildDeleteEvent, Guild, InviteModel, Member, Message, Role, User } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; @@ -20,7 +9,7 @@ const router = Router(); router.post("/", async (req: Request, res: Response) => { var { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }, "owner_id").exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }, "owner_id"); if (guild.owner_id !== req.user_id) throw new HTTPError("You are not the owner of this guild", 401); await emitEvent({ @@ -32,14 +21,14 @@ router.post("/", async (req: Request, res: Response) => { } as GuildDeleteEvent); await Promise.all([ - GuildModel.deleteOne({ id: guild_id }).exec(), - UserModel.updateMany({ guilds: guild_id }, { $pull: { guilds: guild_id } }).exec(), - RoleModel.deleteMany({ guild_id }).exec(), - ChannelModel.deleteMany({ guild_id }).exec(), - EmojiModel.deleteMany({ guild_id }).exec(), - InviteModel.deleteMany({ guild_id }).exec(), - MessageModel.deleteMany({ guild_id }).exec(), - MemberModel.deleteMany({ guild_id }).exec() + Guild.deleteOne({ id: guild_id }), + User.updateMany({ guilds: guild_id }, { $pull: { guilds: guild_id } }), + Role.deleteMany({ guild_id }), + Channel.deleteMany({ guild_id }), + Emoji.deleteMany({ guild_id }), + Invite.deleteMany({ guild_id }), + Message.deleteMany({ guild_id }), + Member.deleteMany({ guild_id }) ]); return res.sendStatus(204); diff --git a/api/src/routes/guilds/#guild_id/index.ts b/api/src/routes/guilds/#guild_id/index.ts index 87103caa..af9ea9d6 100644 --- a/api/src/routes/guilds/#guild_id/index.ts +++ b/api/src/routes/guilds/#guild_id/index.ts @@ -1,18 +1,18 @@ import { Request, Response, Router } from "express"; import { - ChannelModel, + Channel, emitEvent, EmojiModel, getPermission, GuildDeleteEvent, - GuildModel, + Guild, GuildUpdateEvent, InviteModel, - MemberModel, - MessageModel, - RoleModel, + Member, + Message, + Role, toObject, - UserModel + User } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { GuildUpdateSchema } from "../../../schema/Guild"; @@ -26,11 +26,8 @@ const router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }) - .populate({ path: "joined_at", match: { id: req.user_id } }) - .exec(); - - const member = await MemberModel.exists({ guild_id: guild_id, id: req.user_id }); + const guild = await Guild.findOneOrFail({ id: guild_id }).populate({ path: "joined_at", match: { id: req.user_id } }); + const member = await Member.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); @@ -48,11 +45,11 @@ router.patch("/", check(GuildUpdateSchema), async (req: Request, res: Response) if (body.banner) body.banner = await handleFile(`/banners/${guild_id}`, body.banner); if (body.splash) body.splash = await handleFile(`/splashes/${guild_id}`, body.splash); - const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body, { new: true }) - .populate({ path: "joined_at", match: { id: req.user_id } }) - .exec(); - - const data = toObject(guild); + const guild = await Guild.findOneOrFailAndUpdate({ id: guild_id }, body, { new: true }).populate({ + path: "joined_at", + match: { id: req.user_id } + }); + const data = guild; emitEvent({ event: "GUILD_UPDATE", data: data, guild_id } as GuildUpdateEvent); diff --git a/api/src/routes/guilds/#guild_id/invites.ts b/api/src/routes/guilds/#guild_id/invites.ts index 08048d61..ca72cce8 100644 --- a/api/src/routes/guilds/#guild_id/invites.ts +++ b/api/src/routes/guilds/#guild_id/invites.ts @@ -9,9 +9,9 @@ router.get("/", async (req: Request, res: Response) => { const permissions = await getPermission(req.user_id, guild_id); permissions.hasThrow("MANAGE_GUILD"); - const invites = await InviteModel.find({ guild_id }).exec(); + const invites = await Invite.find({ guild_id }); - return res.json(toObject(invites)); + return res.json(invites); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/members/#member_id/index.ts b/api/src/routes/guilds/#guild_id/members/#member_id/index.ts index 515434d6..1dacbdad 100644 --- a/api/src/routes/guilds/#guild_id/members/#member_id/index.ts +++ b/api/src/routes/guilds/#guild_id/members/#member_id/index.ts @@ -1,13 +1,13 @@ import { Request, Response, Router } from "express"; import { - GuildModel, - MemberModel, - UserModel, + Guild, + Member, + User, toObject, GuildMemberAddEvent, getPermission, PermissionResolvable, - RoleModel, + Role, GuildMemberUpdateEvent, emitEvent } from "@fosscord/util"; @@ -22,29 +22,29 @@ router.get("/", async (req: Request, res: Response) => { const { guild_id, member_id } = req.params; await isMember(req.user_id, guild_id); - const member = await MemberModel.findOne({ id: member_id, guild_id }).exec(); + const member = await Member.findOneOrFail({ id: member_id, guild_id }); - return res.json(toObject(member)); + return res.json(member); }); router.patch("/", check(MemberChangeSchema), async (req: Request, res: Response) => { const { guild_id, member_id } = req.params; const body = req.body as MemberChangeSchema; if (body.roles) { - const roles = await RoleModel.find({ id: { $in: body.roles } }).exec(); + const roles = await Role.find({ id: { $in: body.roles } }); if (body.roles.length !== roles.length) throw new HTTPError("Roles not found", 404); // TODO: check if user has permission to add role } - const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body, { new: true }).exec(); + const member = await Member.findOneOrFailAndUpdate({ id: member_id, guild_id }, body, { new: true }); await emitEvent({ event: "GUILD_MEMBER_UPDATE", guild_id, - data: toObject(member) + data: member } as GuildMemberUpdateEvent); - res.json(toObject(member)); + res.json(member); }); router.put("/", async (req: Request, res: Response) => { diff --git a/api/src/routes/guilds/#guild_id/members/index.ts b/api/src/routes/guilds/#guild_id/members/index.ts index 70303436..656d3acd 100644 --- a/api/src/routes/guilds/#guild_id/members/index.ts +++ b/api/src/routes/guilds/#guild_id/members/index.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { GuildModel, MemberModel, toObject } from "@fosscord/util"; +import { Guild, Member, toObject } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { instanceOf, Length } from "../../../../util/instanceOf"; import { PublicMemberProjection, isMember } from "../../../../util/Member"; @@ -10,7 +10,7 @@ const router = Router(); // TODO: send over websocket router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); await isMember(req.user_id, guild_id); try { @@ -28,11 +28,8 @@ router.get("/", async (req: Request, res: Response) => { const { limit, after } = (<unknown>req.query) as { limit: number; after: string }; const query = after ? { id: { $gt: after } } : {}; - var members = await MemberModel.find({ guild_id, ...query }, PublicMemberProjection) - .limit(limit) - .exec(); - - return res.json(toObject(members)); + var members = await Member.find({ guild_id, ...query }, PublicMemberProjection).limit(limit); + return res.json(members); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts index f095c885..5ebc0580 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts @@ -1,12 +1,12 @@ import { Request, Response, Router } from "express"; import { - RoleModel, - GuildModel, + Role, + Guild, getPermission, toObject, - UserModel, + User, Snowflake, - MemberModel, + Member, GuildRoleCreateEvent, GuildRoleUpdateEvent, GuildRoleDeleteEvent, @@ -26,23 +26,23 @@ router.get("/", async (req: Request, res: Response) => { await isMember(req.user_id, guild_id); - const roles = await RoleModel.find({ guild_id: guild_id }).exec(); + const roles = await Role.find({ guild_id: guild_id }); - return res.json(toObject(roles)); + return res.json(roles); }); router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; const body = req.body as RoleModifySchema; - const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - const user = await UserModel.findOne({ id: req.user_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }, { id: true }); + const user = await User.findOneOrFail({ id: req.user_id }); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); if (!body.name) throw new HTTPError("You need to specify a name"); - const role = await new RoleModel({ + const role = await new Role({ ...body, id: Snowflake.generate(), guild_id: guild_id, @@ -57,11 +57,11 @@ router.post("/", check(RoleModifySchema), async (req: Request, res: Response) => guild_id, data: { guild_id, - role: toObject(role) + role: role } } as GuildRoleCreateEvent); - res.json(toObject(role)); + res.json(role); }); router.delete("/:role_id", async (req: Request, res: Response) => { @@ -72,10 +72,10 @@ router.delete("/:role_id", async (req: Request, res: Response) => { const permissions = await getPermission(req.user_id, guild_id); permissions.hasThrow("MANAGE_ROLES"); - await RoleModel.deleteOne({ + await Role.deleteOne({ id: role_id, guild_id: guild_id - }).exec(); + }); await emitEvent({ event: "GUILD_ROLE_DELETE", @@ -96,13 +96,13 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res const { role_id } = req.params; const body = req.body as RoleModifySchema; - const guild = await GuildModel.findOne({ id: guild_id }, { id: true }).exec(); - const user = await UserModel.findOne({ id: req.user_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }, { id: true }); + const user = await User.findOneOrFail({ id: req.user_id }); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_ROLES"); - const role = await RoleModel.findOneAndUpdate( + const role = await Role.findOneOrFailAndUpdate( { id: role_id, guild_id: guild_id @@ -110,7 +110,7 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res // @ts-ignore body, { new: true } - ).exec(); + ); await emitEvent({ event: "GUILD_ROLE_UPDATE", @@ -121,7 +121,7 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res } } as GuildRoleUpdateEvent); - res.json(toObject(role)); + res.json(role); }); export default router; diff --git a/api/src/routes/guilds/#guild_id/templates.ts b/api/src/routes/guilds/#guild_id/templates.ts index e441ee12..13917dbd 100644 --- a/api/src/routes/guilds/#guild_id/templates.ts +++ b/api/src/routes/guilds/#guild_id/templates.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { TemplateModel, GuildModel, getPermission, toObject, UserModel, Snowflake } from "@fosscord/util"; +import { TemplateModel, Guild, getPermission, toObject, User, Snowflake } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { TemplateCreateSchema, TemplateModifySchema } from "../../../schema/Template"; import { check } from "../../../util/instanceOf"; @@ -27,20 +27,18 @@ const TemplateGuildProjection = { router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - var templates = await TemplateModel.find({ source_guild_id: guild_id }).exec(); + var templates = await Template.find({ source_guild_id: guild_id }); - return res.json(toObject(templates)); + return res.json(templates); }); router.post("/", check(TemplateCreateSchema), async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }, TemplateGuildProjection); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const exists = await TemplateModel.findOne({ id: guild_id }) - .exec() - .catch((e) => {}); + const exists = await Template.findOneOrFail({ id: guild_id }).catch((e) => {}); if (exists) throw new HTTPError("Template already exists", 400); const template = await new TemplateModel({ @@ -53,7 +51,7 @@ router.post("/", check(TemplateCreateSchema), async (req: Request, res: Response serialized_source_guild: guild }).save(); - res.json(toObject(template)).send(); + res.json(template)).send(; }); router.delete("/:code", async (req: Request, res: Response) => { @@ -63,25 +61,25 @@ router.delete("/:code", async (req: Request, res: Response) => { const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const template = await TemplateModel.findOneAndDelete({ + const template = await Template.findOneOrFailAndDelete({ code - }).exec(); + }); - res.send(toObject(template)); + res.send(template); }); router.put("/:code", async (req: Request, res: Response) => { const guild_id = req.params.guild_id; const { code } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }, TemplateGuildProjection).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }, TemplateGuildProjection); const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const template = await TemplateModel.findOneAndUpdate({ code }, { serialized_source_guild: guild }, { new: true }).exec(); + const template = await Template.findOneOrFailAndUpdate({ code }, { serialized_source_guild: guild }, { new: true }); - res.json(toObject(template)).send(); + res.json(template)).send(; }); router.patch("/:code", check(TemplateModifySchema), async (req: Request, res: Response) => { @@ -91,13 +89,9 @@ router.patch("/:code", check(TemplateModifySchema), async (req: Request, res: Re const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const template = await TemplateModel.findOneAndUpdate( - { code }, - { name: req.body.name, description: req.body.description }, - { new: true } - ).exec(); + const template = await Template.findOneOrFailAndUpdate({ code }, { name: req.body.name, description: req.body.description }, { new: true }); - res.json(toObject(template)).send(); + res.json(template)).send(; }); export default router; diff --git a/api/src/routes/guilds/#guild_id/vanity-url.ts b/api/src/routes/guilds/#guild_id/vanity-url.ts index 1e659d8d..335cea27 100644 --- a/api/src/routes/guilds/#guild_id/vanity-url.ts +++ b/api/src/routes/guilds/#guild_id/vanity-url.ts @@ -1,4 +1,4 @@ -import { ChannelModel, ChannelType, getPermission, GuildModel, InviteModel, trimSpecial } from "@fosscord/util"; +import { Channel, ChannelType, getPermission, Guild, InviteModel, trimSpecial } from "@fosscord/util"; import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; import { check, Length } from "../../../util/instanceOf"; @@ -14,9 +14,9 @@ router.get("/", async (req: Request, res: Response) => { const permission = await getPermission(req.user_id, guild_id); permission.hasThrow("MANAGE_GUILD"); - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); if (!guild.vanity_url_code) return res.json({ code: null }); - const { uses } = await InviteModel.findOne({ code: guild.vanity_url_code }).exec(); + const { uses } = await Invite.findOneOrFail({ code: guild.vanity_url_code }); return res.json({ code: guild.vanity_url_code, uses }); }); @@ -27,23 +27,19 @@ router.patch("/", check({ code: new Length(String, 0, 20) }), async (req: Reques var code = req.body.code.replace(InviteRegex); if (!code) code = null; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); const permission = await getPermission(req.user_id, guild_id, undefined, { guild }); permission.hasThrow("MANAGE_GUILD"); const alreadyExists = await Promise.all([ - GuildModel.findOne({ vanity_url_code: code }) - .exec() - .catch(() => null), - InviteModel.findOne({ code: code }) - .exec() - .catch(() => null) + Guild.findOneOrFail({ vanity_url_code: code }).catch(() => null), + Invite.findOneOrFail({ code: code }).catch(() => null) ]); if (alreadyExists.some((x) => x)) throw new HTTPError("Vanity url already exists", 400); - await GuildModel.updateOne({ id: guild_id }, { vanity_url_code: code }).exec(); - const { id } = await ChannelModel.findOne({ guild_id, type: ChannelType.GUILD_TEXT }).exec(); - await InviteModel.updateOne( + await Guild.update({ id: guild_id }, { vanity_url_code: code }); + const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT }); + await Invite.update( { code: guild.vanity_url_code }, { code: code, @@ -53,7 +49,7 @@ router.patch("/", check({ code: new Length(String, 0, 20) }), async (req: Reques channel_id: id }, { upsert: true } - ).exec(); + ); return res.json({ code: code }); }); diff --git a/api/src/routes/guilds/#guild_id/welcome_screen.ts b/api/src/routes/guilds/#guild_id/welcome_screen.ts index c717042e..b457efb6 100644 --- a/api/src/routes/guilds/#guild_id/welcome_screen.ts +++ b/api/src/routes/guilds/#guild_id/welcome_screen.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { GuildModel, getPermission, toObject, Snowflake } from "@fosscord/util"; +import { Guild, getPermission, toObject, Snowflake } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "../../../util/instanceOf"; @@ -12,18 +12,18 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const guild_id = req.params.guild_id; - const guild = await GuildModel.findOne({ id: guild_id }); + const guild = await Guild.findOneOrFail({ id: guild_id }); await isMember(req.user_id, guild_id); - res.json(toObject(guild.welcome_screen)); + res.json(guild.welcome_screen); }); router.post("/", check(GuildAddChannelToWelcomeScreenSchema), async (req: Request, res: Response) => { const guild_id = req.params.guild_id; const body = req.body as GuildAddChannelToWelcomeScreenSchema; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); var channelObject = { ...body @@ -36,12 +36,12 @@ router.post("/", check(GuildAddChannelToWelcomeScreenSchema), async (req: Reques if (guild.welcome_screen.welcome_channels.some((channel) => channel.channel_id === body.channel_id)) throw new Error("Welcome Channel exists"); - await GuildModel.findOneAndUpdate( + await Guild.findOneOrFailAndUpdate( { id: guild_id }, { $push: { "welcome_screen.welcome_channels": channelObject } } - ).exec(); + ); res.sendStatus(204); }); diff --git a/api/src/routes/guilds/#guild_id/widget.json.ts b/api/src/routes/guilds/#guild_id/widget.json.ts index 8719bd85..10bc3ac0 100644 --- a/api/src/routes/guilds/#guild_id/widget.json.ts +++ b/api/src/routes/guilds/#guild_id/widget.json.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { Config, Permissions, GuildModel, InviteModel, ChannelModel, MemberModel } from "@fosscord/util"; +import { Config, Permissions, Guild, InviteModel, Channel, Member } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { random } from "../../../util/RandomInviteID"; @@ -17,11 +17,11 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); if (!guild.widget_enabled) throw new HTTPError("Widget Disabled", 404); // Fetch existing widget invite for widget channel - var invite = await InviteModel.findOne({ channel_id: guild.widget_channel_id, inviter_id: { $type: 10 } }).exec(); + var invite = await Invite.findOneOrFail({ channel_id: guild.widget_channel_id, inviter_id: { $type: 10 } }); if (guild.widget_channel_id && !invite) { // Create invite for channel if none exists // TODO: Refactor invite create code to a shared function @@ -45,8 +45,7 @@ router.get("/", async (req: Request, res: Response) => { // Fetch voice channels, and the @everyone permissions object let channels: any[] = []; - await ChannelModel.find({ guild_id: guild_id, type: 2 }, { permission_overwrites: { $elemMatch: { id: guild_id } } }) - .lean() + await Channel.find({ guild_id: guild_id, type: 2 }, { permission_overwrites: { $elemMatch: { id: guild_id } } }) .select("id name position permission_overwrites") .sort({ position: 1 }) .cursor() @@ -67,8 +66,7 @@ router.get("/", async (req: Request, res: Response) => { // Fetch members // TODO: Understand how Discord's max 100 random member sample works, and apply to here (see top of this file) let members: any[] = []; - await MemberModel.find({ guild_id: guild_id }) - .lean() + await Member.find({ guild_id: guild_id }) .populate({ path: "user", select: { _id: 0, username: 1, avatar: 1, presence: 1 } }) .select("id user nick deaf mute") .cursor() diff --git a/api/src/routes/guilds/#guild_id/widget.png.ts b/api/src/routes/guilds/#guild_id/widget.png.ts index 80dc9f2b..89b31153 100644 --- a/api/src/routes/guilds/#guild_id/widget.png.ts +++ b/api/src/routes/guilds/#guild_id/widget.png.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { GuildModel } from "@fosscord/util"; +import { Guild } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import fs from "fs"; import path from "path"; @@ -13,7 +13,7 @@ const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { const { guild_id } = req.params; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); if (!guild.widget_enabled) throw new HTTPError("Unknown Guild", 404); // Fetch guild information diff --git a/api/src/routes/guilds/#guild_id/widget.ts b/api/src/routes/guilds/#guild_id/widget.ts index 85eed5e9..fcf71402 100644 --- a/api/src/routes/guilds/#guild_id/widget.ts +++ b/api/src/routes/guilds/#guild_id/widget.ts @@ -1,5 +1,5 @@ import { Request, Response, Router } from "express"; -import { getPermission, GuildModel } from "@fosscord/util"; +import { getPermission, Guild } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "../../../util/instanceOf"; import { WidgetModifySchema } from "../../../schema/Widget"; @@ -13,7 +13,7 @@ router.get("/", async (req: Request, res: Response) => { const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); return res.json({ enabled: guild.widget_enabled || false, channel_id: guild.widget_channel_id || null }); }); @@ -26,7 +26,7 @@ router.patch("/", check(WidgetModifySchema), async (req: Request, res: Response) const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - await GuildModel.updateOne({ id: guild_id }, { widget_enabled: body.enabled, widget_channel_id: body.channel_id }).exec(); + await Guild.update({ id: guild_id }, { widget_enabled: body.enabled, widget_channel_id: body.channel_id }); // Widget invite for the widget_channel_id gets created as part of the /guilds/{guild.id}/widget.json request return res.json(body); diff --git a/api/src/routes/guilds/index.ts b/api/src/routes/guilds/index.ts index 92feed4e..05be07d9 100644 --- a/api/src/routes/guilds/index.ts +++ b/api/src/routes/guilds/index.ts @@ -1,5 +1,5 @@ import { Router, Request, Response } from "express"; -import { RoleModel, GuildModel, Snowflake, Guild, RoleDocument, Config } from "@fosscord/util"; +import { Role, Guild, Snowflake, Guild, RoleDocument, Config } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { check } from "./../../util/instanceOf"; import { GuildCreateSchema } from "../../schema/Guild"; @@ -65,8 +65,8 @@ router.post("/", check(GuildCreateSchema), async (req: Request, res: Response) = }; const [guild_doc, role] = await Promise.all([ - new GuildModel(guild).save(), - new RoleModel({ + new Guild(guild).save(), + new Role({ id: guild_id, guild_id: guild_id, color: 0, diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts index 7fed3c5d..ad8b676b 100644 --- a/api/src/routes/guilds/templates/index.ts +++ b/api/src/routes/guilds/templates/index.ts @@ -1,6 +1,6 @@ import { Request, Response, Router } from "express"; const router: Router = Router(); -import { TemplateModel, GuildModel, toObject, UserModel, RoleModel, Snowflake, Guild, Config } from "@fosscord/util"; +import { TemplateModel, Guild, toObject, User, Role, Snowflake, Guild, Config } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { GuildTemplateCreateSchema } from "../../../schema/Guild"; import { getPublicUser } from "../../../util/User"; @@ -10,9 +10,9 @@ import { addMember } from "../../../util/Member"; router.get("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const template = await TemplateModel.findOne({ code: code }).exec(); + const template = await Template.findOneOrFail({ code: code }); - res.json(toObject(template)).send(); + res.json(template)).send(; }); router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res: Response) => { @@ -26,7 +26,7 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res throw new HTTPError(`Maximum number of guilds reached ${maxGuilds}`, 403); } - const template = await TemplateModel.findOne({ code: code }).exec(); + const template = await Template.findOneOrFail({ code: code }); const guild_id = Snowflake.generate(); @@ -38,8 +38,8 @@ router.post("/:code", check(GuildTemplateCreateSchema), async (req: Request, res }; const [guild_doc, role] = await Promise.all([ - new GuildModel(guild).save(), - new RoleModel({ + new Guild(guild).save(), + new Role({ id: guild_id, guild_id: guild_id, color: 0, diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts index e7543dbb..e871af86 100644 --- a/api/src/routes/invites/index.ts +++ b/api/src/routes/invites/index.ts @@ -1,5 +1,5 @@ import { Router, Request, Response } from "express"; -import { getPermission, GuildModel, InviteModel, toObject } from "@fosscord/util"; +import { getPermission, Guild, InviteModel, toObject } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { addMember } from "../../util/Member"; const router: Router = Router(); @@ -7,42 +7,40 @@ const router: Router = Router(); router.get("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOne({ code }).exec(); + const invite = await Invite.findOneOrFail({ code }); if (!invite) throw new HTTPError("Unknown Invite", 404); - res.status(200).send(toObject(invite)); + res.status(200).send(invite); }); router.post("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }).exec(); + const invite = await Invite.findOneOrFailAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }); if (!invite) throw new HTTPError("Unknown Invite", 404); - if (invite.uses >= invite.max_uses) await InviteModel.deleteOne({ code }); + if (invite.uses >= invite.max_uses) await Invite.deleteOne({ code }); await addMember(req.user_id, invite.guild_id); - res.status(200).send(toObject(invite)); + res.status(200).send(invite); }); router.delete("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOne({ code }).exec(); + const invite = await Invite.findOneOrFail({ code }); const { guild_id, channel_id } = invite; - const guild = await GuildModel.findOne({ id: guild_id }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }); const permission = await getPermission(req.user_id, guild_id, channel_id, { guild }); if (!permission.has("MANAGE_GUILD") && !permission.has("MANAGE_CHANNELS")) throw new HTTPError("You missing the MANAGE_GUILD or MANAGE_CHANNELS permission", 401); - await InviteModel.deleteOne({ code }).exec(); + await Invite.deleteOne({ code }); - await GuildModel.updateOne({ vanity_url_code: code }, { $unset: { vanity_url_code: 1 } }) - .exec() - .catch((e) => {}); + await Guild.update({ vanity_url_code: code }, { $unset: { vanity_url_code: 1 } }).catch((e) => {}); - res.status(200).send({ invite: toObject(invite) }); + res.status(200).send({ invite: invite) }; }); export default router; diff --git a/api/src/routes/users/#id/profile.ts b/api/src/routes/users/#id/profile.ts index 4b4b9439..46c96698 100644 --- a/api/src/routes/users/#id/profile.ts +++ b/api/src/routes/users/#id/profile.ts @@ -4,24 +4,24 @@ import { getPublicUser } from "../../../util/User"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await getPublicUser(req.params.id, { user_data: true }) + const user = await getPublicUser(req.params.id, { data: true }); - res.json({ - connected_accounts: user.user_data.connected_accounts, - premium_guild_since: null, // TODO - premium_since: null, // TODO - user: { - username: user.username, - discriminator: user.discriminator, - id: user.id, - public_flags: user.public_flags, - avatar: user.avatar, - accent_color: user.accent_color, - banner: user.banner, - bio: req.user_bot ? null : user.bio, - bot: user.bot, - } - }); + res.json({ + connected_accounts: user.data.connected_accounts, + premium_guild_since: null, // TODO + premium_since: null, // TODO + user: { + username: user.username, + discriminator: user.discriminator, + id: user.id, + public_flags: user.public_flags, + avatar: user.avatar, + accent_color: user.accent_color, + banner: user.banner, + bio: req.user_bot ? null : user.bio, + bot: user.bot + } + }); }); export default router; diff --git a/api/src/routes/users/@me/channels.ts b/api/src/routes/users/@me/channels.ts index db9f8832..28e77dd9 100644 --- a/api/src/routes/users/@me/channels.ts +++ b/api/src/routes/users/@me/channels.ts @@ -1,6 +1,6 @@ import { Router, Request, Response } from "express"; import { - ChannelModel, + Channel, ChannelCreateEvent, toObject, ChannelType, @@ -8,7 +8,7 @@ import { trimSpecial, Channel, DMChannel, - UserModel, + User, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; @@ -19,9 +19,9 @@ import { check } from "../../../util/instanceOf"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - var channels = await ChannelModel.find({ recipient_ids: req.user_id }).exec(); + var channels = await Channel.find({ recipient_ids: req.user_id }); - res.json(toObject(channels)); + res.json(channels); }); router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Response) => { @@ -29,14 +29,14 @@ router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Respons body.recipients = body.recipients.filter((x) => x !== req.user_id).unique(); - if (!(await Promise.all(body.recipients.map((x) => UserModel.exists({ id: x })))).every((x) => x)) { + if (!(await Promise.all(body.recipients.map((x) => User.exists({ id: x })))).every((x) => x)) { throw new HTTPError("Recipient not found"); } const type = body.recipients.length === 1 ? ChannelType.DM : ChannelType.GROUP_DM; const name = trimSpecial(body.name); - const channel = await new ChannelModel({ + const channel = await new Channel({ name, type, owner_id: req.user_id, @@ -46,9 +46,9 @@ router.post("/", check(DmChannelCreateSchema), async (req: Request, res: Respons recipient_ids: [...body.recipients, req.user_id] }).save(); - await emitEvent({ event: "CHANNEL_CREATE", data: toObject(channel), user_id: req.user_id } as ChannelCreateEvent); + await emitEvent({ event: "CHANNEL_CREATE", data: channel), user_id: req.user_id } as ChannelCreateEvent; - res.json(toObject(channel)); + res.json(channel); }); export default router; diff --git a/api/src/routes/users/@me/delete.ts b/api/src/routes/users/@me/delete.ts index f863237d..fa8134cc 100644 --- a/api/src/routes/users/@me/delete.ts +++ b/api/src/routes/users/@me/delete.ts @@ -1,16 +1,16 @@ import { Router, Request, Response } from "express"; -import { GuildModel, MemberModel, UserModel } from "@fosscord/util"; +import { Guild, Member, User } from "@fosscord/util"; import bcrypt from "bcrypt"; const router = Router(); router.post("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }).exec(); //User object + const user = await User.findOneOrFail({ id: req.user_id }); //User object - let correctpass = await bcrypt.compare(req.body.password, user!.user_data.hash); //Not sure if user typed right password :/ + let correctpass = await bcrypt.compare(req.body.password, user!.data.hash); //Not sure if user typed right password :/ if (correctpass) { await Promise.all([ - UserModel.deleteOne({ id: req.user_id }).exec(), //Yeetus user deletus - MemberModel.deleteMany({ id: req.user_id }).exec() + User.deleteOne({ id: req.user_id }), //Yeetus user deletus + Member.deleteMany({ id: req.user_id }) ]); res.sendStatus(204); diff --git a/api/src/routes/users/@me/disable.ts b/api/src/routes/users/@me/disable.ts index 2d3a9850..a40c9e59 100644 --- a/api/src/routes/users/@me/disable.ts +++ b/api/src/routes/users/@me/disable.ts @@ -1,15 +1,15 @@ -import { UserModel } from "@fosscord/util"; +import { User } from "@fosscord/util"; import { Router, Response, Request } from "express"; import bcrypt from "bcrypt"; const router = Router(); router.post("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }).exec(); //User object + const user = await User.findOneOrFail({ id: req.user_id }); //User object - let correctpass = await bcrypt.compare(req.body.password, user!.user_data.hash); //Not sure if user typed right password :/ + let correctpass = await bcrypt.compare(req.body.password, user!.data.hash); //Not sure if user typed right password :/ if (correctpass) { - await UserModel.updateOne({ id: req.user_id }, { disabled: true }).exec(); + await User.update({ id: req.user_id }, { disabled: true }); res.sendStatus(204); } else { diff --git a/api/src/routes/users/@me/guilds.ts b/api/src/routes/users/@me/guilds.ts index a9b53b75..e40bfec9 100644 --- a/api/src/routes/users/@me/guilds.ts +++ b/api/src/routes/users/@me/guilds.ts @@ -1,5 +1,5 @@ import { Router, Request, Response } from "express"; -import { GuildModel, MemberModel, UserModel, GuildDeleteEvent, GuildMemberRemoveEvent, toObject, emitEvent } from "@fosscord/util"; +import { Guild, Member, User, GuildDeleteEvent, GuildMemberRemoveEvent, toObject, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { getPublicUser } from "../../../util/User"; @@ -7,28 +7,25 @@ import { getPublicUser } from "../../../util/User"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }, { guilds: true }).exec(); + const user = await User.findOneOrFail({ id: req.user_id }, { guilds: true }); if (!user) throw new HTTPError("User not found", 404); var guildIDs = user.guilds || []; - var guild = await GuildModel.find({ id: { $in: guildIDs } }) - .populate({ path: "joined_at", match: { id: req.user_id } }) - .exec(); - - res.json(toObject(guild)); + var guild = await Guild.find({ id: { $in: guildIDs } }).populate({ path: "joined_at", match: { id: req.user_id } }); + res.json(guild); }); // user send to leave a certain guild router.delete("/:id", async (req: Request, res: Response) => { const guild_id = req.params.id; - const guild = await GuildModel.findOne({ id: guild_id }, { guild_id: true }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }, { guild_id: true }); 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 Promise.all([ - MemberModel.deleteOne({ id: req.user_id, guild_id: guild_id }).exec(), - UserModel.updateOne({ id: req.user_id }, { $pull: { guilds: guild_id } }).exec(), + Member.deleteOne({ id: req.user_id, guild_id: guild_id }), + User.update({ id: req.user_id }, { $pull: { guilds: guild_id } }), emitEvent({ event: "GUILD_DELETE", data: { diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts index 6ebc6634..903b24aa 100644 --- a/api/src/routes/users/@me/index.ts +++ b/api/src/routes/users/@me/index.ts @@ -1,5 +1,5 @@ import { Router, Request, Response } from "express"; -import { UserModel, toObject, PublicUserProjection } from "@fosscord/util"; +import { User, toObject, PublicUserProjection } from "@fosscord/util"; import { getPublicUser } from "../../../util/User"; import { UserModifySchema } from "../../../schema/User"; import { check } from "../../../util/instanceOf"; @@ -38,10 +38,10 @@ router.patch("/", check(UserModifySchema), async (req: Request, res: Response) = if (body.avatar) body.avatar = await handleFile(`/avatars/${req.user_id}`, body.avatar as string); if (body.banner) body.banner = await handleFile(`/banners/${req.user_id}`, body.banner as string); - const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: UserUpdateProjection, new: true }).exec(); + const user = await User.findOneOrFailAndUpdate({ id: req.user_id }, body, { projection: UserUpdateProjection, new: true }); // TODO: dispatch user update event - res.json(toObject(user)); + res.json(user); }); export default router; diff --git a/api/src/routes/users/@me/profile.ts b/api/src/routes/users/@me/profile.ts index b67d1964..fdb969dc 100644 --- a/api/src/routes/users/@me/profile.ts +++ b/api/src/routes/users/@me/profile.ts @@ -4,24 +4,24 @@ import { getPublicUser } from "../../../util/User"; const router: Router = Router(); router.get("/", async (req: Request, res: Response) => { - const user = await getPublicUser(req.user_id, { user_data: true }) + const user = await getPublicUser(req.user_id, { data: true }); - res.json({ - connected_accounts: user.user_data.connected_accounts, - premium_guild_since: null, // TODO - premium_since: null, // TODO - user: { - username: user.username, - discriminator: user.discriminator, - id: user.id, - public_flags: user.public_flags, - avatar: user.avatar, - accent_color: user.accent_color, - banner: user.banner, - bio: user.bio, - bot: user.bot, - } - }); + res.json({ + connected_accounts: user.data.connected_accounts, + premium_guild_since: null, // TODO + premium_since: null, // TODO + user: { + username: user.username, + discriminator: user.discriminator, + id: user.id, + public_flags: user.public_flags, + avatar: user.avatar, + accent_color: user.accent_color, + banner: user.banner, + bio: user.bio, + bot: user.bot + } + }); }); export default router; diff --git a/api/src/routes/users/@me/relationships.ts b/api/src/routes/users/@me/relationships.ts index 642ee5f9..9b8d6199 100644 --- a/api/src/routes/users/@me/relationships.ts +++ b/api/src/routes/users/@me/relationships.ts @@ -1,6 +1,6 @@ import { RelationshipAddEvent, - UserModel, + User, PublicUserProjection, toObject, RelationshipType, @@ -18,18 +18,18 @@ const router = Router(); const userProjection = { "user_data.relationships": true, ...PublicUserProjection }; router.get("/", async (req: Request, res: Response) => { - const user = await UserModel.findOne({ id: req.user_id }, { user_data: { relationships: true } }) - .populate({ path: "user_data.relationships.id", model: UserModel }) - .exec(); - - return res.json(toObject(user.user_data.relationships)); + const user = await User.findOneOrFail({ id: req.user_id }, { user_data: { relationships: true } }).populate({ + path: "user_data.relationships.id", + model: User + }); + return res.json(user.user_data.relationships); }); async function addRelationship(req: Request, res: Response, friend: UserDocument, type: RelationshipType) { const id = friend.id; if (id === req.user_id) throw new HTTPError("You can't add yourself as a friend"); - const user = await UserModel.findOne({ id: req.user_id }, userProjection).exec(); + const user = await User.findOneOrFail({ id: req.user_id }, userProjection); const newUserRelationships = [...user.user_data.relationships]; const newFriendRelationships = [...friend.user_data.relationships]; @@ -48,7 +48,7 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument if (friendRequest && friendRequest.type !== RelationshipType.blocked) { newFriendRelationships.remove(friendRequest); await Promise.all([ - UserModel.updateOne({ id: friend.id }, { "user_data.relationships": newFriendRelationships }).exec(), + User.update({ id: friend.id }, { "user_data.relationships": newFriendRelationships }), emitEvent({ event: "RELATIONSHIP_REMOVE", data: friendRequest, @@ -58,12 +58,12 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument } await Promise.all([ - UserModel.updateOne({ id: req.user_id }, { "user_data.relationships": newUserRelationships }).exec(), + User.update({ id: req.user_id }, { "user_data.relationships": newUserRelationships }), emitEvent({ event: "RELATIONSHIP_ADD", data: { - ...toObject(relationship), - user: { ...toObject(friend), user_data: undefined } + ...relationship, + user: { ...friend, user_data: undefined } }, user_id: req.user_id } as RelationshipAddEvent) @@ -91,22 +91,22 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument } else newUserRelationships.push(outgoing_relationship); await Promise.all([ - UserModel.updateOne({ id: req.user_id }, { "user_data.relationships": newUserRelationships }).exec(), - UserModel.updateOne({ id: friend.id }, { "user_data.relationships": newFriendRelationships }).exec(), + User.update({ id: req.user_id }, { "user_data.relationships": newUserRelationships }), + User.update({ id: friend.id }, { "user_data.relationships": newFriendRelationships }), emitEvent({ event: "RELATIONSHIP_ADD", data: { ...outgoing_relationship, - user: { ...toObject(friend), user_data: undefined } + user: { ...friend, user_data: undefined } }, user_id: req.user_id } as RelationshipAddEvent), emitEvent({ event: "RELATIONSHIP_ADD", data: { - ...toObject(incoming_relationship), + ...incoming_relationship, should_notify: true, - user: { ...toObject(user), user_data: undefined } + user: { ...user, user_data: undefined } }, user_id: id } as RelationshipAddEvent) @@ -116,14 +116,14 @@ async function addRelationship(req: Request, res: Response, friend: UserDocument } router.put("/:id", check({ $type: new Length(Number, 1, 4) }), async (req: Request, res: Response) => { - return await addRelationship(req, res, await UserModel.findOne({ id: req.params.id }), req.body.type); + return await addRelationship(req, res, await User.findOneOrFail({ id: req.params.id }), req.body.type); }); router.post("/", check({ discriminator: String, username: String }), async (req: Request, res: Response) => { return await addRelationship( req, res, - await UserModel.findOne(req.body as { discriminator: string; username: string }).exec(), + await User.findOneOrFail(req.body as { discriminator: string; username: string }), req.body.type ); }); @@ -132,10 +132,10 @@ router.delete("/:id", async (req: Request, res: Response) => { const { id } = req.params; if (id === req.user_id) throw new HTTPError("You can't remove yourself as a friend"); - const user = await UserModel.findOne({ id: req.user_id }).exec(); + const user = await User.findOneOrFail({ id: req.user_id }); if (!user) throw new HTTPError("Invalid token", 400); - const friend = await UserModel.findOne({ id }, userProjection).exec(); + const friend = await User.findOneOrFail({ id }, userProjection); if (!friend) throw new HTTPError("User not found", 404); const relationship = user.user_data.relationships.find((x) => x.id === id); diff --git a/api/src/routes/users/@me/settings.ts b/api/src/routes/users/@me/settings.ts index f045a010..90ee6372 100644 --- a/api/src/routes/users/@me/settings.ts +++ b/api/src/routes/users/@me/settings.ts @@ -1,5 +1,5 @@ import { Router, Response, Request } from "express"; -import { UserModel, UserSettings } from "@fosscord/util"; +import { User, UserSettings } from "@fosscord/util"; import { check } from "../../../util/instanceOf"; import { UserSettingsSchema } from "../../../schema/User"; @@ -9,7 +9,7 @@ router.patch("/", check(UserSettingsSchema), async (req: Request, res: Response) const body = req.body as UserSettings; // only users can update user settings - await UserModel.updateOne({ id: req.user_id, bot: false }, body).exec(); + await User.update({ id: req.user_id, bot: false }, { settings: body }); res.sendStatus(204); }); diff --git a/api/src/test/mongo_test.ts b/api/src/test/mongo_test.ts index 44b04c5b..bf203ea6 100644 --- a/api/src/test/mongo_test.ts +++ b/api/src/test/mongo_test.ts @@ -2,12 +2,12 @@ import mongoose, { Schema, Types } from "mongoose"; require("mongoose-long")(mongoose); const userSchema = new Schema({ - id: String, + id: String }); const messageSchema = new Schema({ id: String, - content: String, + content: String }); const message = mongoose.model("message", messageSchema, "messages"); const user = mongoose.model("user", userSchema, "users"); @@ -16,7 +16,7 @@ messageSchema.virtual("u", { ref: user, localField: "id", foreignField: "id", - justOne: true, + justOne: true }); messageSchema.set("toObject", { virtuals: true }); @@ -25,14 +25,14 @@ messageSchema.set("toJSON", { virtuals: true }); async function main() { const conn = await mongoose.connect("mongodb://localhost:27017/lambert?readPreference=secondaryPreferred", { useNewUrlParser: true, - useUnifiedTopology: false, + useUnifiedTopology: false }); console.log("connected"); // const u = await new user({ name: "test" }).save(); // await new message({ user: u._id, content: "test" }).save(); - const test = await message.findOne({}).populate("u").exec(); + const test = await message.findOneOrFail({}).populate("u"); // @ts-ignore console.log(test?.toJSON()); } diff --git a/api/src/util/Channel.ts b/api/src/util/Channel.ts index fb6f9c8c..a618d2df 100644 --- a/api/src/util/Channel.ts +++ b/api/src/util/Channel.ts @@ -1,10 +1,10 @@ import { ChannelCreateEvent, - ChannelModel, + Channel, ChannelType, emitEvent, getPermission, - GuildModel, + Guild, Snowflake, TextChannel, toObject, @@ -29,7 +29,7 @@ export async function createChannel( case ChannelType.GUILD_TEXT: case ChannelType.GUILD_VOICE: if (channel.parent_id && !opts?.skipExistsCheck) { - const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec(); + const exists = await Channel.findOneOrFail({ id: channel.parent_id }, { guild_id: true }); if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild"); } @@ -49,7 +49,7 @@ export async function createChannel( if (!channel.permission_overwrites) channel.permission_overwrites = []; // TODO: auto generate position - channel = await new ChannelModel({ + channel = await new Channel({ ...channel, ...(!opts?.keepId && { id: Snowflake.generate() }), created_at: new Date(), @@ -57,7 +57,7 @@ export async function createChannel( recipient_ids: null }).save(); - await emitEvent({ event: "CHANNEL_CREATE", data: toObject(channel), guild_id: channel.guild_id } as ChannelCreateEvent); + await emitEvent({ event: "CHANNEL_CREATE", data: channel), guild_id: channel.guild_id } as ChannelCreateEvent; return channel; } diff --git a/api/src/util/Member.ts b/api/src/util/Member.ts index da02735c..6cb14d71 100644 --- a/api/src/util/Member.ts +++ b/api/src/util/Member.ts @@ -5,11 +5,11 @@ import { GuildMemberAddEvent, GuildMemberRemoveEvent, GuildMemberUpdateEvent, - GuildModel, - MemberModel, - RoleModel, + Guild, + Member, + Role, toObject, - UserModel, + User, GuildDocument, Config, emitEvent @@ -32,7 +32,7 @@ export const PublicMemberProjection = { }; export async function isMember(user_id: string, guild_id: string) { - const exists = await MemberModel.exists({ id: user_id, guild_id }); + const exists = await Member.exists({ id: user_id, guild_id }); if (!exists) throw new HTTPError("You are not a member of this guild", 403); return exists; } @@ -45,11 +45,11 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui throw new HTTPError(`You are at the ${maxGuilds} server limit.`, 403); } - const guild = cache?.guild || (await GuildModel.findOne({ id: guild_id }).exec()); + const guild = cache?.guild || (await Guild.findOneOrFail({ id: guild_id })); if (!guild) throw new HTTPError("Guild not found", 404); - if (await MemberModel.exists({ id: user.id, guild_id })) throw new HTTPError("You are already a member of this guild", 400); + if (await Member.exists({ id: user.id, guild_id })) throw new HTTPError("You are already a member of this guild", 400); const member = { id: user_id, @@ -64,7 +64,7 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui }; await Promise.all([ - new MemberModel({ + new Member({ ...member, read_state: {}, settings: { @@ -79,8 +79,8 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui } }).save(), - UserModel.updateOne({ id: user_id }, { $push: { guilds: guild_id } }).exec(), - GuildModel.updateOne({ id: guild_id }, { $inc: { member_count: 1 } }).exec(), + User.update({ id: user_id }, { $push: { guilds: guild_id } }), + Guild.update({ id: guild_id }, { $inc: { member_count: 1 } }), emitEvent({ event: "GUILD_MEMBER_ADD", @@ -95,12 +95,10 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui await emitEvent({ event: "GUILD_CREATE", - data: toObject( - await guild - .populate({ path: "members", match: { guild_id } }) - .populate({ path: "joined_at", match: { id: user.id } }) - .execPopulate() - ), + data: await guild + .populate({ path: "members", match: { guild_id } }) + .populate({ path: "joined_at", match: { id: user.id } }) + .execPopulate(), user_id } as GuildCreateEvent); } @@ -108,19 +106,19 @@ export async function addMember(user_id: string, guild_id: string, cache?: { gui export async function removeMember(user_id: string, guild_id: string) { const user = await getPublicUser(user_id); - const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec(); + const guild = await Guild.findOneOrFail({ id: guild_id }, { owner_id: true }); if (!guild) throw new HTTPError("Guild not found", 404); if (guild.owner_id === user_id) throw new Error("The owner cannot be removed of the guild"); - if (!(await MemberModel.exists({ id: user.id, guild_id }))) throw new HTTPError("Is not member of this guild", 404); + if (!(await Member.exists({ id: user.id, guild_id }))) throw new HTTPError("Is not member of this guild", 404); // use promise all to execute all promises at the same time -> save time return Promise.all([ - MemberModel.deleteOne({ + Member.deleteOne({ id: user_id, guild_id: guild_id - }).exec(), - UserModel.updateOne({ id: user.id }, { $pull: { guilds: guild_id } }).exec(), - GuildModel.updateOne({ id: guild_id }, { $inc: { member_count: -1 } }).exec(), + }), + User.update({ id: user.id }, { $pull: { guilds: guild_id } }), + Guild.update({ id: guild_id }, { $inc: { member_count: -1 } }), emitEvent({ event: "GUILD_DELETE", @@ -143,17 +141,17 @@ export async function removeMember(user_id: string, guild_id: string) { 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(); + const role = await Role.findOneOrFail({ id: role_id, guild_id: guild_id }); if (!role) throw new HTTPError("role not found", 404); - var memberObj = await MemberModel.findOneAndUpdate( + var memberObj = await Member.findOneOrFailAndUpdate( { id: user_id, guild_id: guild_id }, { $push: { roles: role_id } }, { new: true } - ).exec(); + ); if (!memberObj) throw new HTTPError("Member not found", 404); @@ -171,17 +169,17 @@ export async function addRole(user_id: string, guild_id: string, role_id: string 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(); + const role = await Role.findOneOrFail({ id: role_id, guild_id: guild_id }); if (!role) throw new HTTPError("role not found", 404); - var memberObj = await MemberModel.findOneAndUpdate( + var memberObj = await Member.findOneOrFailAndUpdate( { id: user_id, guild_id: guild_id }, { $pull: { roles: role_id } }, { new: true } - ).exec(); + ); if (!memberObj) throw new HTTPError("Member not found", 404); @@ -199,14 +197,14 @@ export async function removeRole(user_id: string, guild_id: string, role_id: str export async function changeNickname(user_id: string, guild_id: string, nickname: string) { const user = await getPublicUser(user_id); - var memberObj = await MemberModel.findOneAndUpdate( + var memberObj = await Member.findOneOrFailAndUpdate( { id: user_id, guild_id: guild_id }, { nick: nickname }, { new: true } - ).exec(); + ); if (!memberObj) throw new HTTPError("Member not found", 404); diff --git a/api/src/util/Message.ts b/api/src/util/Message.ts index 8a1e959e..5561904b 100644 --- a/api/src/util/Message.ts +++ b/api/src/util/Message.ts @@ -1,5 +1,5 @@ import { - ChannelModel, + Channel, Embed, emitEvent, Message, @@ -7,13 +7,11 @@ import { MessageUpdateEvent, getPermission, CHANNEL_MENTION, - toObject, - MessageModel, Snowflake, PublicMemberProjection, USER_MENTION, ROLE_MENTION, - RoleModel, + Role, EVERYONE_MENTION, HERE_MENTION } from "@fosscord/util"; @@ -37,13 +35,11 @@ const DEFAULT_FETCH_OPTIONS: any = { method: "GET" }; -export async function handleMessage(opts: Partial<Message>) { - const channel = await ChannelModel.findOne( +export async function handleMessage(opts: Partial<Message>): Promise<Message> { + const channel = await Channel.findOneOrFail( { id: opts.channel_id }, - { guild_id: true, type: true, permission_overwrites: true, recipient_ids: true, owner_id: true } - ) - .lean() // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids - .exec(); + { select: ["guild_id", "type", "permission_overwrites", "recipient_ids", "owner_id"] } + ); // lean is needed, because we don't want to populate .recipients that also auto deletes .recipient_ids if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); // TODO: are tts messages allowed in dm channels? should permission be checked? @@ -83,7 +79,7 @@ export async function handleMessage(opts: Partial<Message>) { await Promise.all( Array.from(content.matchAll(ROLE_MENTION)).map(async ([_, mention]) => { - const role = await RoleModel.findOne({ id: mention, guild_id: channel.guild_id }).exec(); + const role = await Role.findOneOrFail({ id: mention, guild_id: channel.guild_id }); if (role.mentionable || permission.has("MANAGE_ROLES")) { mention_role_ids.push(mention); } @@ -160,16 +156,17 @@ export async function postHandleMessage(message: Message) { channel_id: message.channel_id, data } as MessageUpdateEvent), - MessageModel.updateOne({ id: message.id, channel_id: message.channel_id }, data).exec() + Message.update({ id: message.id, channel_id: message.channel_id }, data) ]); } export async function sendMessage(opts: Partial<Message>) { const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() }); - const data = toObject( - await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).populate("referenced_message").save() - ); + const data = await new Message(message) + .populate({ path: "member", select: PublicMemberProjection }) + .populate("referenced_message") + .save(); await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data } as MessageCreateEvent); diff --git a/api/src/util/User.ts b/api/src/util/User.ts index 392c7101..4d9065c4 100644 --- a/api/src/util/User.ts +++ b/api/src/util/User.ts @@ -1,16 +1,16 @@ -import { toObject, UserModel, PublicUserProjection } from "@fosscord/util"; +import { toObject, User, PublicUserProjection } from "@fosscord/util"; import { HTTPError } from "lambert-server"; export { PublicUserProjection }; export async function getPublicUser(user_id: string, additional_fields?: any) { - const user = await UserModel.findOne( + const user = await User.findOneOrFail( { id: user_id }, { ...PublicUserProjection, ...additional_fields } - ).exec(); + ); if (!user) throw new HTTPError("User not found", 404); - return toObject(user); + return user; } |