diff --git a/api/src/Server.ts b/api/src/Server.ts index 40e7c1c2..adaf7f2c 100644 --- a/api/src/Server.ts +++ b/api/src/Server.ts @@ -12,9 +12,6 @@ import { initRateLimits } from "./middlewares/RateLimit"; import TestClient from "./middlewares/TestClient"; import { initTranslation } from "./middlewares/Translation"; -// this will return the new updated document for findOneAndUpdate -mongoose.set("returnOriginal", false); // https://mongoosejs.com/docs/api/model.html#model_Model.findOneAndUpdate - export interface FosscordServerOptions extends ServerOptions {} declare global { diff --git a/api/src/routes/channels/#channel_id/index.ts b/api/src/routes/channels/#channel_id/index.ts index 3bfa80be..fb6bcb1a 100644 --- a/api/src/routes/channels/#channel_id/index.ts +++ b/api/src/routes/channels/#channel_id/index.ts @@ -43,7 +43,7 @@ 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).exec(); + const channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, payload, { new: true }).exec(); const data = toObject(channel); 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 34d2ce3a..7da63644 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 @@ -43,7 +43,7 @@ router.delete("/", async (req: Request, res: Response) => { const permissions = await getPermission(req.user_id, undefined, channel_id); permissions.hasThrow("MANAGE_MESSAGES"); - await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }).exec(); + await MessageModel.findOneAndUpdate({ id: message_id, channel_id }, { reactions: [] }, { new: true }).exec(); await emitEvent({ event: "MESSAGE_REACTION_REMOVE_ALL", diff --git a/api/src/routes/channels/#channel_id/permissions.ts b/api/src/routes/channels/#channel_id/permissions.ts index 4cbc7522..f93075b1 100644 --- a/api/src/routes/channels/#channel_id/permissions.ts +++ b/api/src/routes/channels/#channel_id/permissions.ts @@ -5,7 +5,8 @@ import { emitEvent, getPermission, MemberModel, - RoleModel + RoleModel, + toObject } from "@fosscord/util"; import { Router, Response, Request } from "express"; import { HTTPError } from "lambert-server"; @@ -47,12 +48,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).exec(); + channel = await ChannelModel.findOneAndUpdate({ id: channel_id }, channel, { new: true }).exec(); await emitEvent({ event: "CHANNEL_UPDATE", channel_id, - data: channel + data: toObject(channel) } as ChannelUpdateEvent); return res.sendStatus(204); @@ -65,13 +66,17 @@ 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({ id: channel_id }, { $pull: { permission_overwrites: { id: overwrite_id } } }); + const channel = await ChannelModel.findOneAndUpdate( + { id: channel_id }, + { $pull: { permission_overwrites: { id: overwrite_id } } }, + { new: true } + ); if (!channel.guild_id) throw new HTTPError("Channel not found", 404); await emitEvent({ event: "CHANNEL_UPDATE", channel_id, - data: channel + data: toObject(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 18a5861b..0dd81bd3 100644 --- a/api/src/routes/channels/#channel_id/pins.ts +++ b/api/src/routes/channels/#channel_id/pins.ts @@ -57,7 +57,7 @@ router.delete("/:message_id", async (req: Request, res: Response) => { permission.hasThrow("VIEW_CHANNEL"); if (channel.guild_id) permission.hasThrow("MANAGE_MESSAGES"); - const message = toObject(await MessageModel.findOneAndUpdate({ id: message_id }, { pinned: false }).exec()); + const message = toObject(await MessageModel.findOneAndUpdate({ id: message_id }, { pinned: false }, { new: true }).exec()); await emitEvent({ event: "MESSAGE_UPDATE", diff --git a/api/src/routes/guilds/#guild_id/channels.ts b/api/src/routes/guilds/#guild_id/channels.ts index 8d97cf96..4383c79b 100644 --- a/api/src/routes/guilds/#guild_id/channels.ts +++ b/api/src/routes/guilds/#guild_id/channels.ts @@ -1,16 +1,5 @@ import { Router, Response, Request } from "express"; -import { - ChannelCreateEvent, - ChannelModel, - ChannelType, - GuildModel, - Snowflake, - toObject, - ChannelUpdateEvent, - AnyChannel, - getPermission, - emitEvent -} from "@fosscord/util"; +import { ChannelModel, toObject, ChannelUpdateEvent, getPermission, emitEvent } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import { ChannelModifySchema } from "../../../schema/Channel"; @@ -63,7 +52,7 @@ router.patch( } } - const channel = await ChannelModel.findOneAndUpdate({ id: req.body, guild_id }, opts).exec(); + const channel = await ChannelModel.findOneAndUpdate({ id: req.body, guild_id }, opts, { new: true }).exec(); await emitEvent({ event: "CHANNEL_UPDATE", data: toObject(channel), channel_id: body.id, guild_id } as ChannelUpdateEvent); diff --git a/api/src/routes/guilds/#guild_id/index.ts b/api/src/routes/guilds/#guild_id/index.ts index 1afa603f..87103caa 100644 --- a/api/src/routes/guilds/#guild_id/index.ts +++ b/api/src/routes/guilds/#guild_id/index.ts @@ -48,7 +48,7 @@ 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) + const guild = await GuildModel.findOneAndUpdate({ id: guild_id }, body, { new: true }) .populate({ path: "joined_at", match: { id: req.user_id } }) .exec(); 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 eac6684a..515434d6 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 @@ -36,7 +36,7 @@ router.patch("/", check(MemberChangeSchema), async (req: Request, res: Response) // TODO: check if user has permission to add role } - const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body).exec(); + const member = await MemberModel.findOneAndUpdate({ id: member_id, guild_id }, body, { new: true }).exec(); await emitEvent({ event: "GUILD_MEMBER_UPDATE", diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts index 36370bb4..f095c885 100644 --- a/api/src/routes/guilds/#guild_id/roles.ts +++ b/api/src/routes/guilds/#guild_id/roles.ts @@ -108,7 +108,8 @@ router.patch("/:role_id", check(RoleModifySchema), async (req: Request, res: Res guild_id: guild_id }, // @ts-ignore - body + body, + { new: true } ).exec(); await emitEvent({ diff --git a/api/src/routes/guilds/#guild_id/templates.ts b/api/src/routes/guilds/#guild_id/templates.ts index fdca1c40..e441ee12 100644 --- a/api/src/routes/guilds/#guild_id/templates.ts +++ b/api/src/routes/guilds/#guild_id/templates.ts @@ -79,7 +79,7 @@ router.put("/:code", async (req: Request, res: Response) => { const perms = await getPermission(req.user_id, guild_id); perms.hasThrow("MANAGE_GUILD"); - const template = await TemplateModel.findOneAndUpdate({ code }, { serialized_source_guild: guild }).exec(); + const template = await TemplateModel.findOneAndUpdate({ code }, { serialized_source_guild: guild }, { new: true }).exec(); res.json(toObject(template)).send(); }); @@ -91,7 +91,11 @@ 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 }).exec(); + const template = await TemplateModel.findOneAndUpdate( + { code }, + { name: req.body.name, description: req.body.description }, + { new: true } + ).exec(); res.json(toObject(template)).send(); }); diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts index 992694df..b4066bd8 100644 --- a/api/src/routes/invites/index.ts +++ b/api/src/routes/invites/index.ts @@ -16,7 +16,7 @@ router.get("/:code", async (req: Request, res: Response) => { router.post("/:code", async (req: Request, res: Response) => { const { code } = req.params; - const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }).exec(); + const invite = await InviteModel.findOneAndUpdate({ code }, { $inc: { uses: 1 } }, { new: true }).exec(); if (!invite) throw new HTTPError("Unknown Invite", 404); await addMember(req.user_id, invite.guild_id); diff --git a/api/src/routes/users/@me/index.ts b/api/src/routes/users/@me/index.ts index c073e78a..6ebc6634 100644 --- a/api/src/routes/users/@me/index.ts +++ b/api/src/routes/users/@me/index.ts @@ -38,7 +38,7 @@ 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 }).exec(); + const user = await UserModel.findOneAndUpdate({ id: req.user_id }, body, { projection: UserUpdateProjection, new: true }).exec(); // TODO: dispatch user update event res.json(toObject(user)); diff --git a/api/src/util/Member.ts b/api/src/util/Member.ts index 53ff4632..da02735c 100644 --- a/api/src/util/Member.ts +++ b/api/src/util/Member.ts @@ -151,7 +151,8 @@ export async function addRole(user_id: string, guild_id: string, role_id: string id: user_id, guild_id: guild_id }, - { $push: { roles: role_id } } + { $push: { roles: role_id } }, + { new: true } ).exec(); if (!memberObj) throw new HTTPError("Member not found", 404); @@ -178,7 +179,8 @@ export async function removeRole(user_id: string, guild_id: string, role_id: str id: user_id, guild_id: guild_id }, - { $pull: { roles: role_id } } + { $pull: { roles: role_id } }, + { new: true } ).exec(); if (!memberObj) throw new HTTPError("Member not found", 404); @@ -197,13 +199,13 @@ 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( { id: user_id, guild_id: guild_id }, - { nick: nickname } + { 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 803c37dc..8a1e959e 100644 --- a/api/src/util/Message.ts +++ b/api/src/util/Message.ts @@ -157,7 +157,6 @@ export async function postHandleMessage(message: Message) { await Promise.all([ emitEvent({ event: "MESSAGE_UPDATE", - guild_id: message.guild_id, channel_id: message.channel_id, data } as MessageUpdateEvent), @@ -172,7 +171,7 @@ export async function sendMessage(opts: Partial) { await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).populate("referenced_message").save() ); - await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: message.guild_id } as MessageCreateEvent); + await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data } as MessageCreateEvent); postHandleMessage(data).catch((e) => {}); // no await as it shouldnt block the message send function and silently catch error diff --git a/cdn/package-lock.json b/cdn/package-lock.json index cedc24bd..11ce8935 100644 --- a/cdn/package-lock.json +++ b/cdn/package-lock.json @@ -46,7 +46,8 @@ } }, "../util": { - "version": "1.3.55", + "name": "@fosscord/util", + "version": "1.0.0", "hasInstallScript": true, "license": "GPLV3", "dependencies": { diff --git a/gateway/package-lock.json b/gateway/package-lock.json index 806b02b2..e76bd71c 100644 --- a/gateway/package-lock.json +++ b/gateway/package-lock.json @@ -36,7 +36,8 @@ } }, "../util": { - "version": "1.3.55", + "name": "@fosscord/util", + "version": "1.0.0", "hasInstallScript": true, "license": "GPLV3", "dependencies": { diff --git a/util/src/util/Database.ts b/util/src/util/Database.ts index 2e04e0ff..ea517234 100644 --- a/util/src/util/Database.ts +++ b/util/src/util/Database.ts @@ -12,8 +12,12 @@ const connection = mongoose.createConnection(uri, { autoIndex: true, useNewUrlParser: true, useUnifiedTopology: true, - useFindAndModify: false, + useFindAndModify: true, }); + +// this will return the new updated document for findOneAndUpdate +mongoose.set("returnOriginal", false); // https://mongoosejs.com/docs/api/model.html#model_Model.findOneAndUpdate + console.log(`[Database] connect: mongodb://${url.username}@${url.host}${url.pathname}${url.search}`); connection.once("open", () => { console.log("[Database] connected");