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);
|