From 7685e19835afdf0b403a676c16ada663ddcbc29d Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Tue, 6 Apr 2021 18:01:49 +0200 Subject: :art: Convert id bigint to string --- src/models/Role.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'src/models/Role.ts') diff --git a/src/models/Role.ts b/src/models/Role.ts index fe716593..adb75b0a 100644 --- a/src/models/Role.ts +++ b/src/models/Role.ts @@ -2,8 +2,8 @@ import { Schema, model, Types, Document } from "mongoose"; import db from "../util/Database"; export interface Role { - id: bigint; - guild_id: bigint; + id: string; + guild_id: string; color: number; hoist: boolean; managed: boolean; @@ -12,17 +12,17 @@ export interface Role { permissions: bigint; position: number; tags?: { - bot_id?: bigint; + bot_id?: string; }; } export interface RoleDocument extends Document, Role { - id: bigint; + id: string; } export const RoleSchema = new Schema({ - id: Types.Long, - guild_id: Types.Long, + id: String, + guild_id: String, color: Number, hoist: Boolean, managed: Boolean, @@ -31,7 +31,7 @@ export const RoleSchema = new Schema({ permissions: Types.Long, position: Number, tags: { - bot_id: Types.Long, + bot_id: String, }, }); -- cgit 1.5.1 From a6d82b466f61c1b60b1058d75b0647f2c0ac91a0 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Wed, 7 Apr 2021 20:26:19 +0200 Subject: :zap: use mongoose autopopulate --- package-lock.json | 36 +++++++++++++++++++++++++++++++++++- package.json | 4 +++- src/models/Guild.ts | 4 +++- src/models/Member.ts | 17 ++++++++++++++++- src/models/Message.ts | 16 ++++++++++++++-- src/models/Role.ts | 1 + src/models/User.ts | 7 +++++++ src/models/index.ts | 3 +++ 8 files changed, 82 insertions(+), 6 deletions(-) (limited to 'src/models/Role.ts') diff --git a/package-lock.json b/package-lock.json index 902566ed..17d70c4a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -13,10 +13,12 @@ "lambert-db": "^1.1.7", "missing-native-js-functions": "^1.2.2", "mongodb": "^3.6.6", - "mongoose": "^5.12.3" + "mongoose": "^5.12.3", + "mongoose-autopopulate": "^0.12.3" }, "devDependencies": { "@types/jsonwebtoken": "^8.5.0", + "@types/mongoose-autopopulate": "^0.10.1", "@types/mongoose-lean-virtuals": "^0.5.1", "@types/node": "^14.14.25", "typescript": "^4.1.3" @@ -58,6 +60,15 @@ "@types/node": "*" } }, + "node_modules/@types/mongoose-autopopulate": { + "version": "0.10.1", + "resolved": "https://registry.npmjs.org/@types/mongoose-autopopulate/-/mongoose-autopopulate-0.10.1.tgz", + "integrity": "sha512-L67MAIE3WEoTtt7a7/spRYk+76lgp67FAP6I38Y9NcC1kQuzwqnukTaJzodfb8180wxHZM4qt68u6x6ptuDRaQ==", + "dev": true, + "dependencies": { + "@types/mongoose": "*" + } + }, "node_modules/@types/mongoose-lean-virtuals": { "version": "0.5.1", "resolved": "https://registry.npmjs.org/@types/mongoose-lean-virtuals/-/mongoose-lean-virtuals-0.5.1.tgz", @@ -720,6 +731,14 @@ "url": "https://opencollective.com/mongoose" } }, + "node_modules/mongoose-autopopulate": { + "version": "0.12.3", + "resolved": "https://registry.npmjs.org/mongoose-autopopulate/-/mongoose-autopopulate-0.12.3.tgz", + "integrity": "sha512-yNmYsfi6OpS/GQ+48mkB0KQ199ExHmmPrt3wt3fyxPHPMtEBGts7yq3wBQR6VgKCPOQaKvCI1URbJCPOtrPeLw==", + "peerDependencies": { + "mongoose": "4.x || 5.x" + } + }, "node_modules/mongoose-legacy-pluralize": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", @@ -1194,6 +1213,15 @@ "@types/node": "*" } }, + "@types/mongoose-autopopulate": { + "version": "0.10.1", + "resolved": "https://registry.npmjs.org/@types/mongoose-autopopulate/-/mongoose-autopopulate-0.10.1.tgz", + "integrity": "sha512-L67MAIE3WEoTtt7a7/spRYk+76lgp67FAP6I38Y9NcC1kQuzwqnukTaJzodfb8180wxHZM4qt68u6x6ptuDRaQ==", + "dev": true, + "requires": { + "@types/mongoose": "*" + } + }, "@types/mongoose-lean-virtuals": { "version": "0.5.1", "resolved": "https://registry.npmjs.org/@types/mongoose-lean-virtuals/-/mongoose-lean-virtuals-0.5.1.tgz", @@ -1694,6 +1722,12 @@ } } }, + "mongoose-autopopulate": { + "version": "0.12.3", + "resolved": "https://registry.npmjs.org/mongoose-autopopulate/-/mongoose-autopopulate-0.12.3.tgz", + "integrity": "sha512-yNmYsfi6OpS/GQ+48mkB0KQ199ExHmmPrt3wt3fyxPHPMtEBGts7yq3wBQR6VgKCPOQaKvCI1URbJCPOtrPeLw==", + "requires": {} + }, "mongoose-legacy-pluralize": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/mongoose-legacy-pluralize/-/mongoose-legacy-pluralize-1.0.2.tgz", diff --git a/package.json b/package.json index b92228ff..9bf8cd16 100644 --- a/package.json +++ b/package.json @@ -24,10 +24,12 @@ "lambert-db": "^1.1.7", "missing-native-js-functions": "^1.2.2", "mongodb": "^3.6.6", - "mongoose": "^5.12.3" + "mongoose": "^5.12.3", + "mongoose-autopopulate": "^0.12.3" }, "devDependencies": { "@types/jsonwebtoken": "^8.5.0", + "@types/mongoose-autopopulate": "^0.10.1", "@types/mongoose-lean-virtuals": "^0.5.1", "@types/node": "^14.14.25", "typescript": "^4.1.3" diff --git a/src/models/Guild.ts b/src/models/Guild.ts index d3f098ea..aa520329 100644 --- a/src/models/Guild.ts +++ b/src/models/Guild.ts @@ -96,12 +96,14 @@ GuildSchema.virtual("channels", { localField: "id", foreignField: "guild_id", justOne: false, + autopopulate: true, }); GuildSchema.virtual("roles", { ref: RoleModel, localField: "id", foreignField: "guild_id", justOne: false, + autopopulate: true, }); // nested populate is needed for member users: https://gist.github.com/yangsu/5312204 @@ -117,6 +119,7 @@ GuildSchema.virtual("emojis", { localField: "id", foreignField: "guild_id", justOne: false, + autopopulate: true, }); GuildSchema.virtual("joined_at", { @@ -125,7 +128,6 @@ GuildSchema.virtual("joined_at", { foreignField: "guild_id", justOne: true, }).get((member: any, virtual: any, doc: any) => { - console.log("get", member, this); return member.joined_at; }); diff --git a/src/models/Member.ts b/src/models/Member.ts index fc9d5b99..52151235 100644 --- a/src/models/Member.ts +++ b/src/models/Member.ts @@ -1,7 +1,19 @@ -import { PublicUser, User, UserModel } from "./User"; +import { PublicUser, PublicUserProjection, User, UserModel } from "./User"; import { Schema, Types, Document } from "mongoose"; import db from "../util/Database"; +export const PublicMemberProjection = { + id: true, + guild_id: true, + nick: true, + roles: true, + joined_at: true, + pending: true, + deaf: true, + mute: true, + premium_since: true, +}; + export interface Member { id: string; guild_id: string; @@ -80,6 +92,9 @@ MemberSchema.virtual("user", { localField: "id", foreignField: "id", justOne: true, + autopopulate: { + select: PublicUserProjection, + }, }); // @ts-ignore diff --git a/src/models/Message.ts b/src/models/Message.ts index ee038f97..15ff57d3 100644 --- a/src/models/Message.ts +++ b/src/models/Message.ts @@ -1,7 +1,7 @@ import { Schema, Types, Document } from "mongoose"; import db from "../util/Database"; -import { PublicUser, UserModel } from "./User"; -import { MemberModel, PublicMember } from "./Member"; +import { PublicUser, PublicUserProjection, UserModel } from "./User"; +import { MemberModel, PublicMember, PublicMemberProjection } from "./Member"; import { Role, RoleModel } from "./Role"; import { Channel } from "./Channel"; @@ -229,6 +229,14 @@ export const MessageSchema = new Schema({ channel_id: String, guild_id: String, }, + // virtual: + // author: { + // ref: UserModel, + // localField: "author_id", + // foreignField: "id", + // justOne: true, + // autopopulate: { select: { id: true, user_data: false } }, + // }, }); MessageSchema.virtual("author", { @@ -236,6 +244,7 @@ MessageSchema.virtual("author", { localField: "author_id", foreignField: "id", justOne: true, + autopopulate: { select: PublicUserProjection }, }); MessageSchema.virtual("member", { @@ -250,6 +259,7 @@ MessageSchema.virtual("mentions", { localField: "mention_user_ids", foreignField: "id", justOne: false, + autopopulate: { select: PublicUserProjection }, }); MessageSchema.virtual("mention_roles", { @@ -257,6 +267,7 @@ MessageSchema.virtual("mention_roles", { localField: "mention_role_ids", foreignField: "id", justOne: false, + autopopulate: true, }); MessageSchema.virtual("mention_channels", { @@ -264,6 +275,7 @@ MessageSchema.virtual("mention_channels", { localField: "mention_channel_ids", foreignField: "id", justOne: false, + autopopulate: { select: { id: true, guild_id: true, type: true, name: true } }, }); MessageSchema.set("removeResponse", ["mention_channel_ids", "mention_role_ids", "mention_user_ids", "author_id"]); diff --git a/src/models/Role.ts b/src/models/Role.ts index adb75b0a..84ad55d0 100644 --- a/src/models/Role.ts +++ b/src/models/Role.ts @@ -34,6 +34,7 @@ export const RoleSchema = new Schema({ bot_id: String, }, }); +RoleSchema.set("removeResponse", ["guild_id"]); // @ts-ignore export const RoleModel = db.model("Role", RoleSchema, "roles"); diff --git a/src/models/User.ts b/src/models/User.ts index 76251e09..1f01e837 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -3,6 +3,13 @@ import { ClientStatus, Status } from "./Status"; import { Schema, Types, Document } from "mongoose"; import db from "../util/Database"; +export const PublicUserProjection = { + username: true, + discriminator: true, + id: true, + public_flags: true, + avatar: true, +}; export interface User { id: string; username: string; // username max length 32, min 2 diff --git a/src/models/index.ts b/src/models/index.ts index 03b9fe70..876e5984 100644 --- a/src/models/index.ts +++ b/src/models/index.ts @@ -1,5 +1,8 @@ import mongoose from "mongoose"; import { Schema } from "mongoose"; +import mongooseAutoPopulate from "mongoose-autopopulate"; + +mongoose.plugin(mongooseAutoPopulate); mongoose.plugin((schema: Schema, opts: any) => { schema.set("toObject", { -- cgit 1.5.1 From e8c097ef273100c6200c8e814965f2cb02de0bc9 Mon Sep 17 00:00:00 2001 From: notsapinho <52896767+notsapinho@users.noreply.github.com> Date: Thu, 8 Apr 2021 11:43:26 -0300 Subject: added toBigInt and BigInt getters to bitfields --- src/models/Channel.ts | 5 +++-- src/models/Guild.ts | 6 +++++- src/models/Role.ts | 4 +++- src/models/User.ts | 5 +++-- src/util/Database.ts | 13 ++++++++++++- src/util/MongoBigInt.ts | 2 +- src/util/checkToken.ts | 2 +- src/util/index.ts | 1 + src/util/toBigInt.ts | 3 +++ 9 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 src/util/toBigInt.ts (limited to 'src/models/Role.ts') diff --git a/src/models/Channel.ts b/src/models/Channel.ts index 82ad2999..1b9a167c 100644 --- a/src/models/Channel.ts +++ b/src/models/Channel.ts @@ -1,5 +1,6 @@ import { Schema, model, Types, Document } from "mongoose"; import db from "../util/Database"; +import toBigInt from "../util/toBigInt"; export interface AnyChannel extends Channel, DMChannel, TextChannel, VoiceChannel {} @@ -24,8 +25,8 @@ export const ChannelSchema = new Schema({ topic: String, permission_overwrites: [ { - allow: Types.Long, - deny: Types.Long, + allow: { type: String, get: toBigInt }, + deny: { type: String, get: toBigInt }, id: String, type: Number, }, diff --git a/src/models/Guild.ts b/src/models/Guild.ts index 819ac840..ef343010 100644 --- a/src/models/Guild.ts +++ b/src/models/Guild.ts @@ -32,6 +32,8 @@ export interface Guild { // channels: GuildChannel[]; // * Channels are stored in a seperate collection // emojis: Emoji[]; // * Emojis are stored in a seperate collection // voice_states: []; // * voice_states are stored in a seperate collection + //TODO: + presences?: object[]; mfa_level?: number; name: string; owner_id: string; @@ -69,6 +71,7 @@ export const GuildSchema = new Schema({ max_presences: Number, max_video_channel_users: { type: Number, default: 25 }, member_count: Number, + presences: { type: [Object], default: [] }, presence_count: Number, mfa_level: Number, name: { type: String, required: true }, @@ -98,6 +101,7 @@ GuildSchema.virtual("channels", { justOne: false, autopopulate: true, }); + GuildSchema.virtual("roles", { ref: RoleModel, localField: "id", @@ -128,7 +132,7 @@ GuildSchema.virtual("joined_at", { foreignField: "guild_id", justOne: true, }).get((member: any, virtual: any, doc: any) => { - return member.joined_at; + return member?.joined_at; }); // @ts-ignore diff --git a/src/models/Role.ts b/src/models/Role.ts index 84ad55d0..c1111c84 100644 --- a/src/models/Role.ts +++ b/src/models/Role.ts @@ -1,5 +1,6 @@ import { Schema, model, Types, Document } from "mongoose"; import db from "../util/Database"; +import toBigInt from "../util/toBigInt"; export interface Role { id: string; @@ -28,12 +29,13 @@ export const RoleSchema = new Schema({ managed: Boolean, mentionable: Boolean, name: String, - permissions: Types.Long, + permissions: { type: String, get: toBigInt }, position: Number, tags: { bot_id: String, }, }); + RoleSchema.set("removeResponse", ["guild_id"]); // @ts-ignore diff --git a/src/models/User.ts b/src/models/User.ts index 1f01e837..8511a9b0 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -2,6 +2,7 @@ import { Activity } from "./Activity"; import { ClientStatus, Status } from "./Status"; import { Schema, Types, Document } from "mongoose"; import db from "../util/Database"; +import toBigInt from "../util/toBigInt"; export const PublicUserProjection = { username: true, @@ -141,8 +142,8 @@ export const UserSchema = new Schema({ created_at: Date, verified: Boolean, email: String, - flags: Types.Long, // TODO: automatically convert Types.Long to BitField of UserFlags - public_flags: Types.Long, + flags: { type: String, get: toBigInt }, // TODO: automatically convert Types.Long to BitField of UserFlags + public_flags: { type: String, get: toBigInt }, guilds: [String], // array of guild ids the user is part of user_data: { fingerprints: [String], diff --git a/src/util/Database.ts b/src/util/Database.ts index 5d9afab9..339ac65b 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -7,7 +7,12 @@ const uri = process.env.MONGO_URL || "mongodb://localhost:27017/fosscord?readPre console.log(`[DB] connect: ${uri}`); -const connection = mongoose.createConnection(uri, { autoIndex: true, useNewUrlParser: true, useUnifiedTopology: true }); +const connection = mongoose.createConnection(uri, { + autoIndex: true, + useNewUrlParser: true, + useUnifiedTopology: true, + useFindAndModify: false, +}); export default connection; @@ -56,6 +61,12 @@ export class MongooseCache extends EventEmitter { } }; + changeStream = (pipeline: any) => { + this.pipeline = pipeline; + this.destroy(); + this.init(); + }; + convertResult = (obj: any) => { if (obj instanceof Long) return BigInt(obj.toString()); if (typeof obj === "object") { diff --git a/src/util/MongoBigInt.ts b/src/util/MongoBigInt.ts index c4e5f623..fc451925 100644 --- a/src/util/MongoBigInt.ts +++ b/src/util/MongoBigInt.ts @@ -44,7 +44,7 @@ class LongSchema extends mongoose.SchemaType { if (val instanceof Number || "number" == typeof val) return BigInt(val); if (!Array.isArray(val) && val.toString) return BigInt(val.toString()); - // @ts-ignore + //@ts-ignore throw new SchemaType.CastError("Long", val); } diff --git a/src/util/checkToken.ts b/src/util/checkToken.ts index b4635126..d5a128b4 100644 --- a/src/util/checkToken.ts +++ b/src/util/checkToken.ts @@ -4,7 +4,7 @@ import Config from "./Config"; export function checkToken(token: string): Promise { return new Promise((res, rej) => { - jwt.verify(token, Config.getAll().api.security.jwtSecret, JWTOptions, (err, decoded: any) => { + jwt.verify(token, Config.getAll()?.api?.security?.jwtSecret, JWTOptions, (err, decoded: any) => { if (err || !decoded) return rej("Invalid Token"); return res(decoded); diff --git a/src/util/index.ts b/src/util/index.ts index b0c7fe62..7e8bca20 100644 --- a/src/util/index.ts +++ b/src/util/index.ts @@ -5,3 +5,4 @@ export * from "./MessageFlags"; export * from "./Permissions"; export * from "./Snowflake"; export * from "./UserFlags"; +export * from "./toBigInt" \ No newline at end of file diff --git a/src/util/toBigInt.ts b/src/util/toBigInt.ts new file mode 100644 index 00000000..d57c4568 --- /dev/null +++ b/src/util/toBigInt.ts @@ -0,0 +1,3 @@ +export default function toBigInt(string: String): BigInt { + return BigInt(string); +} -- cgit 1.5.1