diff options
author | notsapinho <52896767+notsapinho@users.noreply.github.com> | 2021-04-08 11:43:26 -0300 |
---|---|---|
committer | notsapinho <52896767+notsapinho@users.noreply.github.com> | 2021-04-08 11:43:26 -0300 |
commit | e8c097ef273100c6200c8e814965f2cb02de0bc9 (patch) | |
tree | 6cdf25485d11d7aae1a2a4efeab10e2b12ae8b86 | |
parent | Merge branch 'main' of https://github.com/fosscord/fosscord-server-util (diff) | |
download | server-e8c097ef273100c6200c8e814965f2cb02de0bc9.tar.xz |
added toBigInt and BigInt getters to bitfields
-rw-r--r-- | src/models/Channel.ts | 5 | ||||
-rw-r--r-- | src/models/Guild.ts | 6 | ||||
-rw-r--r-- | src/models/Role.ts | 4 | ||||
-rw-r--r-- | src/models/User.ts | 5 | ||||
-rw-r--r-- | src/util/Database.ts | 13 | ||||
-rw-r--r-- | src/util/MongoBigInt.ts | 2 | ||||
-rw-r--r-- | src/util/checkToken.ts | 2 | ||||
-rw-r--r-- | src/util/index.ts | 1 | ||||
-rw-r--r-- | src/util/toBigInt.ts | 3 |
9 files changed, 32 insertions, 9 deletions
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>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<any> { 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); +} |