From e3dda743afea2e994c119f492e5e378daa66810b Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 23 Jun 2021 18:06:00 +0200 Subject: [PATCH 01/30] :bug: fix checkToken --- src/util/Database.ts | 1 - src/util/checkToken.ts | 3 ++- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/util/Database.ts b/src/util/Database.ts index e5323ed6..3a0f0157 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -2,7 +2,6 @@ import "./MongoBigInt"; import mongoose, { Collection, Connection, LeanDocument } from "mongoose"; import { ChangeStream, ChangeEvent, Long } from "mongodb"; import EventEmitter from "events"; -import { Document } from "mongoose"; const uri = process.env.MONGO_URL || "mongodb://localhost:27017/fosscord?readPreference=secondaryPreferred"; // TODO: auto throw error if findOne doesn't find anything diff --git a/src/util/checkToken.ts b/src/util/checkToken.ts index 73ffb670..4a60195b 100644 --- a/src/util/checkToken.ts +++ b/src/util/checkToken.ts @@ -9,7 +9,8 @@ export function checkToken(token: string, jwtSecret: string): Promise { const user = await UserModel.findOne({ id: decoded.id }, { "user_data.valid_tokens_since": true }).exec(); if (!user) return rej("Invalid Token"); - if (decoded.iat * 1000 < user.user_data.valid_tokens_since.getTime()) return rej("Invalid Token"); + // we need to round it to seconds as it saved as seconds in jwt iat and valid_tokens_since is stored in milliseconds + if (decoded.iat * 1000 < user.user_data.valid_tokens_since.setSeconds(0, 0)) return rej("Invalid Token"); if (user.disabled) return rej("User disabled"); if (user.deleted) return rej("User not found"); From 9a6c66484ddb927bb3211573113cf570cf736067 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 23 Jun 2021 18:06:10 +0200 Subject: [PATCH 02/30] 1.3.17 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index b7ab057f..a6535e54 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.16", + "version": "1.3.17", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.16", + "version": "1.3.17", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index 8d78d7e3..81c785b5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.16", + "version": "1.3.17", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From 9b3f1f5a642d9fa6e240c1d693722705e3472704 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 23 Jun 2021 18:06:41 +0200 Subject: [PATCH 03/30] 1.3.18 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index a6535e54..ee196c27 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.17", + "version": "1.3.18", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.17", + "version": "1.3.18", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index 81c785b5..60bd0a47 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.17", + "version": "1.3.18", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From ae1fc50c84bb8eec2e7f9fa80c6a5c056b20f5b7 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 24 Jun 2021 08:52:08 +0200 Subject: [PATCH 04/30] Message reply --- src/models/Message.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/models/Message.ts b/src/models/Message.ts index d3651ee2..3a9a68f2 100644 --- a/src/models/Message.ts +++ b/src/models/Message.ts @@ -342,6 +342,15 @@ MessageSchema.virtual("mention_channels", { autopopulate: { select: { id: true, guild_id: true, type: true, name: true } }, }); + +MessageSchema.virtual("referenced_message", { + ref: "Message", + localField: "message_reference.message_id", + foreignField: "id", + justOne: true, + autopopulate: true, +}); + MessageSchema.virtual("created_at").get(function (this: MessageDocument) { return new Date(Snowflake.deconstruct(this.id).timestamp); }); @@ -358,3 +367,4 @@ MessageSchema.set("removeResponse", ["mention_channel_ids", "mention_role_ids", // @ts-ignore export const MessageModel = db.model("Message", MessageSchema, "messages"); + From fe2d62cd477301402c0f5d5405b160d1242f5380 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 24 Jun 2021 08:52:24 +0200 Subject: [PATCH 05/30] :sparkles: allow bot tokens --- src/util/checkToken.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/util/checkToken.ts b/src/util/checkToken.ts index 4a60195b..d890e0e1 100644 --- a/src/util/checkToken.ts +++ b/src/util/checkToken.ts @@ -4,6 +4,7 @@ import { UserModel } from "../models"; export function checkToken(token: string, jwtSecret: string): Promise { return new Promise((res, rej) => { + token = token.replace("Bot ", ""); // TODO: proper bot support jwt.verify(token, jwtSecret, JWTOptions, async (err, decoded: any) => { if (err || !decoded) return rej("Invalid Token"); From c0032bf413cede1107ceb3f316fd488b681c30ac Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 27 Jun 2021 11:24:31 +0200 Subject: [PATCH 06/30] :sparkles: Config: ipdataApiKey + register.blockProxies --- src/util/Config.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/util/Config.ts b/src/util/Config.ts index f125bd18..30d216a4 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -88,6 +88,7 @@ export interface DefaultOptions { sitekey: string | null; secret: string | null; }; + ipdataApiKey: string | null; }; login: { requireCaptcha: boolean; @@ -107,6 +108,7 @@ export interface DefaultOptions { requireInvite: boolean; allowNewRegistration: boolean; allowMultipleAccounts: boolean; + blockProxies: boolean; password: { minLength: number; minNumbers: number; @@ -176,6 +178,7 @@ export const DefaultOptions: DefaultOptions = { sitekey: null, secret: null, }, + ipdataApiKey: "eca677b284b3bac29eb72f5e496aa9047f26543605efe99ff2ce35c9", }, login: { requireCaptcha: false, @@ -196,6 +199,7 @@ export const DefaultOptions: DefaultOptions = { requireCaptcha: true, allowNewRegistration: true, allowMultipleAccounts: true, + blockProxies: true, password: { minLength: 8, minNumbers: 2, From e8c3b57f00c9c9309e01991bd8eddccfd1792d5d Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 27 Jun 2021 11:24:35 +0200 Subject: [PATCH 07/30] 1.3.19 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index ee196c27..5b015182 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.18", + "version": "1.3.19", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.18", + "version": "1.3.19", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index 60bd0a47..16aa3e61 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.18", + "version": "1.3.19", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From 7efdbe4027e39c6cc2571b4eba8ba174dd177962 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 27 Jun 2021 23:11:04 +0200 Subject: [PATCH 08/30] :bug: fix mongoose cache --- src/util/Config.ts | 2 +- src/util/Database.ts | 29 ++++++++++++++++++++++------- 2 files changed, 23 insertions(+), 8 deletions(-) diff --git a/src/util/Config.ts b/src/util/Config.ts index 30d216a4..bd8559a8 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -4,7 +4,7 @@ import db, { MongooseCache } from "./Database"; import { Snowflake } from "./Snowflake"; import crypto from "crypto"; -var Config = new MongooseCache(db.collection("config"), [], { onlyEvents: false }); +var Config = new MongooseCache(db.collection("config"), [], { onlyEvents: false, array: false }); export default { init: async function init(defaultOpts: any = DefaultOptions) { diff --git a/src/util/Database.ts b/src/util/Database.ts index 3a0f0157..1b77629f 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -52,9 +52,11 @@ export class MongooseCache extends EventEmitter { public pipeline: Array>, public opts: { onlyEvents: boolean; + array?: boolean; } ) { super(); + if (this.opts.array == null) this.opts.array = true; } init = async () => { @@ -67,7 +69,8 @@ export class MongooseCache extends EventEmitter { if (!this.opts.onlyEvents) { const arr = await this.collection.aggregate(this.pipeline).toArray(); - this.data = arr.length ? arr[0] : arr; + if (this.opts.array) this.data = arr || []; + else this.data = arr?.[0]; } }; @@ -90,23 +93,34 @@ export class MongooseCache extends EventEmitter { change = (doc: ChangeEvent) => { try { - // @ts-ignore - if (doc.fullDocument) { - // @ts-ignore - if (!this.opts.onlyEvents) this.data = doc.fullDocument; - } - switch (doc.operationType) { case "dropDatabase": return this.destroy(); case "drop": return this.destroy(); case "delete": + if (!this.opts.onlyEvents) { + if (this.opts.array) { + this.data = this.data.filter((x: any) => doc.documentKey?._id?.equals(x._id)); + } else this.data = null; + } return this.emit("delete", doc.documentKey._id.toHexString()); case "insert": + if (!this.opts.onlyEvents) { + if (this.opts.array) this.data.push(doc.fullDocument); + else this.data = doc.fullDocument; + } return this.emit("insert", doc.fullDocument); case "update": case "replace": + if (!this.opts.onlyEvents) { + if (this.opts.array) { + const i = this.data.findIndex((x: any) => doc.fullDocument?._id?.equals(x._id)); + if (i == -1) this.data.push(doc.fullDocument); + else this.data[i] = doc.fullDocument; + } else this.data = doc.fullDocument; + } + return this.emit("change", doc.fullDocument); case "invalidate": return this.destroy(); @@ -119,6 +133,7 @@ export class MongooseCache extends EventEmitter { }; destroy = () => { + this.data = null; this.stream?.off("change", this.change); this.emit("close"); From 15de8b35639d78ac7e5e6a25d7370b99b72fe1e4 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 27 Jun 2021 23:11:18 +0200 Subject: [PATCH 09/30] 1.3.20 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 5b015182..72808d9f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.19", + "version": "1.3.20", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.19", + "version": "1.3.20", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index 16aa3e61..b481f7c0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.19", + "version": "1.3.20", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From 229e741bfdd4a6d8b7b2617eec5e88c626d29e00 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 28 Jun 2021 18:43:22 +0200 Subject: [PATCH 10/30] :sparkles: Rate Limit model --- src/models/RateLimit.ts | 26 ++++++++++++++++++++++++++ src/util/Database.ts | 28 +++++++++++++++++----------- 2 files changed, 43 insertions(+), 11 deletions(-) create mode 100644 src/models/RateLimit.ts diff --git a/src/models/RateLimit.ts b/src/models/RateLimit.ts new file mode 100644 index 00000000..7e4ca3cc --- /dev/null +++ b/src/models/RateLimit.ts @@ -0,0 +1,26 @@ +import { Schema, Document, Types } from "mongoose"; +import db from "../util/Database"; +import { ChannelModel } from "./Channel"; +import { UserModel } from "./User"; +import { GuildModel } from "./Guild"; + +export interface Bucket { + id: "global" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498 + user: string; + hits: number; + blocked: boolean; +} + +export interface BucketDocument extends Bucket, Document { + id: string; +} + +export const BucketSchema = new Schema({ + id: { type: String, required: true }, + user_id: { type: String, required: true }, // bot, user, oauth_application, webhook + hits: { type: Number, required: true }, // Number of times the user hit this bucket + blocked: { type: Boolean, required: true }, +}); + +// @ts-ignore +export const BucketModel = db.model("Bucket", BucketSchema, "ratelimits"); diff --git a/src/util/Database.ts b/src/util/Database.ts index 1b77629f..0732cb4e 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -46,6 +46,7 @@ export interface MongooseCache { export class MongooseCache extends EventEmitter { public stream: ChangeStream; public data: any; + public initalizing?: Promise; constructor( public collection: Collection, @@ -59,19 +60,24 @@ export class MongooseCache extends EventEmitter { if (this.opts.array == null) this.opts.array = true; } - init = async () => { - // @ts-ignore - this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" }); + init = () => { + if (this.initalizing) return this.initalizing; + this.initalizing = new Promise(async (resolve, reject) => { + // @ts-ignore + this.stream = this.collection.watch(this.pipeline, { fullDocument: "updateLookup" }); - this.stream.on("change", this.change); - this.stream.on("close", this.destroy); - this.stream.on("error", console.error); + this.stream.on("change", this.change); + this.stream.on("close", this.destroy); + this.stream.on("error", console.error); - if (!this.opts.onlyEvents) { - const arr = await this.collection.aggregate(this.pipeline).toArray(); - if (this.opts.array) this.data = arr || []; - else this.data = arr?.[0]; - } + if (!this.opts.onlyEvents) { + const arr = await this.collection.aggregate(this.pipeline).toArray(); + if (this.opts.array) this.data = arr || []; + else this.data = arr?.[0]; + } + resolve(); + }); + return this.initalizing; }; changeStream = (pipeline: any) => { From e13bdc31272f382773a65508ab7bb5626d1c535e Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 30 Jun 2021 17:49:19 +0200 Subject: [PATCH 11/30] :sparkles: RateLimit Bucket --- src/models/RateLimit.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/models/RateLimit.ts b/src/models/RateLimit.ts index 7e4ca3cc..6ccfafe0 100644 --- a/src/models/RateLimit.ts +++ b/src/models/RateLimit.ts @@ -9,6 +9,7 @@ export interface Bucket { user: string; hits: number; blocked: boolean; + created_at: Date; } export interface BucketDocument extends Bucket, Document { @@ -20,6 +21,7 @@ export const BucketSchema = new Schema({ user_id: { type: String, required: true }, // bot, user, oauth_application, webhook hits: { type: Number, required: true }, // Number of times the user hit this bucket blocked: { type: Boolean, required: true }, + created_at: { type: Date, required: true }, }); // @ts-ignore From 19fd29db60c567350f94a5b9da0b32d2a9b2185d Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 30 Jun 2021 17:49:49 +0200 Subject: [PATCH 12/30] 1.3.21 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 72808d9f..a964d25c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.20", + "version": "1.3.21", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.20", + "version": "1.3.21", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index b481f7c0..62cfa57f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.20", + "version": "1.3.21", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From c187c4b7ac919f0f79afe965bf776bf4ee385493 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 30 Jun 2021 21:56:25 +0200 Subject: [PATCH 13/30] :sparkles: checkToken return user data --- src/models/index.ts | 1 + src/util/checkToken.ts | 7 +++++-- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/src/models/index.ts b/src/models/index.ts index 004095db..4cc6ec2b 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -35,3 +35,4 @@ export * from "./Status"; export * from "./Role"; export * from "./User"; export * from "./VoiceState"; +export * from "./RateLimit"; diff --git a/src/util/checkToken.ts b/src/util/checkToken.ts index d890e0e1..e021a406 100644 --- a/src/util/checkToken.ts +++ b/src/util/checkToken.ts @@ -8,14 +8,17 @@ export function checkToken(token: string, jwtSecret: string): Promise { jwt.verify(token, jwtSecret, JWTOptions, async (err, decoded: any) => { if (err || !decoded) return rej("Invalid Token"); - const user = await UserModel.findOne({ id: decoded.id }, { "user_data.valid_tokens_since": true }).exec(); + const user = await UserModel.findOne( + { id: decoded.id }, + { "user_data.valid_tokens_since": true, bot: true } + ).exec(); if (!user) return rej("Invalid Token"); // we need to round it to seconds as it saved as seconds in jwt iat and valid_tokens_since is stored in milliseconds if (decoded.iat * 1000 < user.user_data.valid_tokens_since.setSeconds(0, 0)) return rej("Invalid Token"); if (user.disabled) return rej("User disabled"); if (user.deleted) return rej("User not found"); - return res(decoded); + return res({ decoded, user }); }); }); } From 54fe0c9f1a6bc67ddd5b830f8f17e91707a41b5e Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 30 Jun 2021 21:56:28 +0200 Subject: [PATCH 14/30] 1.3.22 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index a964d25c..7bf271c0 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.21", + "version": "1.3.22", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.21", + "version": "1.3.22", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index 62cfa57f..373075e1 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.21", + "version": "1.3.22", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From 3a8c7ec1c338d52b50a63d4e783820d4d5c55917 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 1 Jul 2021 11:13:12 +0200 Subject: [PATCH 15/30] :sparkles: Rate Limit Bucket --- src/models/RateLimit.ts | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/models/RateLimit.ts b/src/models/RateLimit.ts index 6ccfafe0..6a0e1ffd 100644 --- a/src/models/RateLimit.ts +++ b/src/models/RateLimit.ts @@ -1,15 +1,12 @@ import { Schema, Document, Types } from "mongoose"; import db from "../util/Database"; -import { ChannelModel } from "./Channel"; -import { UserModel } from "./User"; -import { GuildModel } from "./Guild"; export interface Bucket { - id: "global" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498 - user: string; + id: "global" | "error" | string; // channel_239842397 | guild_238927349823 | webhook_238923423498 + user_id: string; hits: number; blocked: boolean; - created_at: Date; + expires_at: Date; } export interface BucketDocument extends Bucket, Document { @@ -21,7 +18,7 @@ export const BucketSchema = new Schema({ user_id: { type: String, required: true }, // bot, user, oauth_application, webhook hits: { type: Number, required: true }, // Number of times the user hit this bucket blocked: { type: Boolean, required: true }, - created_at: { type: Date, required: true }, + expires_at: { type: Date, required: true }, }); // @ts-ignore From bf37e84e6be72e74bf08b414b9e08cecf686add1 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 1 Jul 2021 11:13:42 +0200 Subject: [PATCH 16/30] 1.3.23 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 7bf271c0..d851c58a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.22", + "version": "1.3.23", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.22", + "version": "1.3.23", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index 373075e1..f5dee681 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.22", + "version": "1.3.23", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From 8120788dcad5c4b70f400ee65eb29270ac05ab9e Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 1 Jul 2021 16:35:30 +0200 Subject: [PATCH 17/30] 1.3.24 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index d851c58a..58d19c9f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.23", + "version": "1.3.24", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.23", + "version": "1.3.24", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index f5dee681..f13988e6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.23", + "version": "1.3.24", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From 137d719ecfad4529169d96d5f45cac9f263edbe8 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 7 Jul 2021 15:21:30 +0200 Subject: [PATCH 18/30] :bug: fix Config --- src/util/Config.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/Config.ts b/src/util/Config.ts index bd8559a8..dfa942e7 100644 --- a/src/util/Config.ts +++ b/src/util/Config.ts @@ -9,7 +9,7 @@ var Config = new MongooseCache(db.collection("config"), [], { onlyEvents: false, export default { init: async function init(defaultOpts: any = DefaultOptions) { await Config.init(); - return this.set(Config.data.merge(defaultOpts)); + return this.set((Config.data || {}).merge(defaultOpts)); }, get: function get() { return Config.data; From 786d3d954a4f83ef233933c308eadb40a03548a1 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 7 Jul 2021 15:22:17 +0200 Subject: [PATCH 19/30] 1.3.25 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 58d19c9f..2044911c 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.24", + "version": "1.3.25", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.24", + "version": "1.3.25", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index f13988e6..fe7a5c26 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.24", + "version": "1.3.25", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From be7ffe5184e9406fb48f0b71a3dea86f5a2daaf6 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 8 Jul 2021 09:50:36 +0200 Subject: [PATCH 20/30] 1.3.26 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2044911c..69f5617a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.25", + "version": "1.3.26", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.25", + "version": "1.3.26", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index fe7a5c26..e8b23a30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.25", + "version": "1.3.26", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From cae178eefdd024414a3ba90b43e9d95fbc85ef3c Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 8 Jul 2021 16:44:14 +0200 Subject: [PATCH 21/30] :see_no_evil: :lock: hide db password in log --- src/util/Database.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/util/Database.ts b/src/util/Database.ts index 0732cb4e..ed596907 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -3,9 +3,10 @@ import mongoose, { Collection, Connection, LeanDocument } from "mongoose"; import { ChangeStream, ChangeEvent, Long } from "mongodb"; import EventEmitter from "events"; const uri = process.env.MONGO_URL || "mongodb://localhost:27017/fosscord?readPreference=secondaryPreferred"; +import { URL } from "url"; // TODO: auto throw error if findOne doesn't find anything -console.log(`[DB] connect: ${uri}`); +const url = new URL(uri.replace("mongodb://", "http://")); const connection = mongoose.createConnection(uri, { autoIndex: true, @@ -13,6 +14,7 @@ const connection = mongoose.createConnection(uri, { useUnifiedTopology: true, useFindAndModify: false, }); +console.log(`[DB] connect: mongodb://${url.username}@${url.host}${url.pathname}${url.search}`); export default connection; From 71c5c93e0d62b9017b7f3113e66b05a2ffdc64a0 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 8 Jul 2021 16:44:20 +0200 Subject: [PATCH 22/30] 1.3.26 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2044911c..69f5617a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.25", + "version": "1.3.26", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.25", + "version": "1.3.26", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index fe7a5c26..e8b23a30 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.25", + "version": "1.3.26", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From efa4dae2856ea2f17d92d8e861c9a2219c8b02e3 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 8 Jul 2021 16:44:34 +0200 Subject: [PATCH 23/30] 1.3.27 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 69f5617a..3d4f4cb1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.26", + "version": "1.3.27", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.26", + "version": "1.3.27", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index e8b23a30..6bb99944 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.26", + "version": "1.3.27", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From d4550a567a6bfaa14dc0c9ae09741434dec765d2 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Thu, 8 Jul 2021 16:55:22 +0200 Subject: [PATCH 24/30] add prepublish script --- package.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/package.json b/package.json index 8ed88b78..7db53b23 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "types": "dist/index.d.ts", "scripts": { "test": "echo \"Error: no test specified\" && exit 1", - "build": "tsc -b ." + "build": "tsc -b .", + "prepublish": "npm run build" }, "repository": { "type": "git", From 4c75b5cc7db95ff162fdd15ac02a41c76020a891 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 10 Jul 2021 17:51:19 +0200 Subject: [PATCH 25/30] auto throw error if findOne doesn't find any doc --- src/models/index.ts | 44 ++++++++++++++++++++++++++++++++++++++++++-- src/util/Database.ts | 1 - 2 files changed, 42 insertions(+), 3 deletions(-) diff --git a/src/models/index.ts b/src/models/index.ts index 4cc6ec2b..61c8d85a 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,7 +1,42 @@ -import mongoose from "mongoose"; -import { Schema } from "mongoose"; +import mongoose, { Schema, Document } from "mongoose"; import mongooseAutoPopulate from "mongoose-autopopulate"; +type UpdateWithAggregationPipeline = UpdateAggregationStage[]; +type UpdateAggregationStage = + | { $addFields: any } + | { $set: any } + | { $project: any } + | { $unset: any } + | { $replaceRoot: any } + | { $replaceWith: any }; +type EnforceDocument = T extends Document ? T : T & Document & TMethods; + +declare module "mongoose" { + interface Model { + // removed null -> always return document -> throw error if it doesn't exist + findOne( + filter?: FilterQuery, + projection?: any | null, + options?: QueryOptions | null, + callback?: (err: CallbackError, doc: EnforceDocument) => void + ): QueryWithHelpers, EnforceDocument, TQueryHelpers>; + findOneAndUpdate( + filter?: FilterQuery, + update?: UpdateQuery | UpdateWithAggregationPipeline, + options?: QueryOptions | null, + callback?: (err: any, doc: EnforceDocument | null, res: any) => void + ): QueryWithHelpers, EnforceDocument, TQueryHelpers>; + } +} + +var HTTPError: any; + +try { + HTTPError = require("lambert-server").HTTPError; +} catch (e) { + HTTPError = Error; +} + mongoose.plugin(mongooseAutoPopulate); mongoose.plugin((schema: Schema, opts: any) => { @@ -17,6 +52,11 @@ mongoose.plugin((schema: Schema, opts: any) => { }); }, }); + schema.post("findOne", (doc, next) => { + if (!doc) return next(new HTTPError("Not found", 404)); + // @ts-ignore + return next(); + }); }); export * from "./Activity"; diff --git a/src/util/Database.ts b/src/util/Database.ts index ed596907..16e07d3b 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -5,7 +5,6 @@ import EventEmitter from "events"; const uri = process.env.MONGO_URL || "mongodb://localhost:27017/fosscord?readPreference=secondaryPreferred"; import { URL } from "url"; -// TODO: auto throw error if findOne doesn't find anything const url = new URL(uri.replace("mongodb://", "http://")); const connection = mongoose.createConnection(uri, { From 33d4302e57bbca8c2ae76b943c69d00bc51abffd Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sat, 10 Jul 2021 17:51:32 +0200 Subject: [PATCH 26/30] 1.3.29 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index f0b4a562..dbeff062 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.28", + "version": "1.3.29", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.28", + "version": "1.3.29", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index 7db53b23..6a4ef551 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.28", + "version": "1.3.29", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From 89b5f6963a7d0ecc4341d7de916a02ff9fcd6936 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Sun, 18 Jul 2021 19:49:03 +0200 Subject: [PATCH 27/30] :loud_sound: fix log --- src/util/Database.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/util/Database.ts b/src/util/Database.ts index 16e07d3b..8c6847a8 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -13,7 +13,7 @@ const connection = mongoose.createConnection(uri, { useUnifiedTopology: true, useFindAndModify: false, }); -console.log(`[DB] connect: mongodb://${url.username}@${url.host}${url.pathname}${url.search}`); +console.log(`[Database] connect: mongodb://${url.username}@${url.host}${url.pathname}${url.search}`); export default connection; From 11ace49d8c1e75a41200bb45c0bd50c712d2e5d2 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 19 Jul 2021 22:42:07 +0200 Subject: [PATCH 28/30] :bug: fix findOne exists query --- src/models/index.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/src/models/index.ts b/src/models/index.ts index 61c8d85a..11a6fe37 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -52,10 +52,17 @@ mongoose.plugin((schema: Schema, opts: any) => { }); }, }); - schema.post("findOne", (doc, next) => { - if (!doc) return next(new HTTPError("Not found", 404)); - // @ts-ignore - return next(); + schema.post("findOne", function (doc, next) { + try { + // @ts-ignore + const isExistsQuery = JSON.stringify(this._userProvidedFields) === JSON.stringify({ _id: 1 }); + if (!doc && !isExistsQuery) return next(new HTTPError("Not found", 404)); + // @ts-ignore + return next(); + } catch (error) { + // @ts-ignore + next(); + } }); }); From 6f0ca89989269ec39cecb3824f439974825d891c Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 19 Jul 2021 22:42:18 +0200 Subject: [PATCH 29/30] 1.3.30 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index dbeff062..f9f8d375 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.29", + "version": "1.3.30", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.29", + "version": "1.3.30", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index 6a4ef551..e99f4989 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.29", + "version": "1.3.30", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts", From 32d18c5cc01daf64420928b37887c557b883849c Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Mon, 19 Jul 2021 22:44:22 +0200 Subject: [PATCH 30/30] 1.3.31 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index f9f8d375..bac5cf70 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@fosscord/server-util", - "version": "1.3.30", + "version": "1.3.31", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@fosscord/server-util", - "version": "1.3.30", + "version": "1.3.31", "license": "GPLV3", "dependencies": { "@types/jsonwebtoken": "^8.5.0", diff --git a/package.json b/package.json index e99f4989..8d69fd7c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@fosscord/server-util", - "version": "1.3.30", + "version": "1.3.31", "description": "Utility functions for the all server repositories", "main": "dist/index.js", "types": "dist/index.d.ts",