diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-03-04 21:59:49 +0100 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-03-04 21:59:49 +0100 |
commit | 514afaca697ae5f9a02e91342c0434fe9eeffe74 (patch) | |
tree | d0b4ac2751d0903c097aa0374820dd17222ca9d2 /src | |
parent | :sparkles: AuditLog (diff) | |
download | server-514afaca697ae5f9a02e91342c0434fe9eeffe74.tar.xz |
:sparkles: AuditLog
Diffstat (limited to 'src')
-rw-r--r-- | src/models/AuditLog.ts | 2 | ||||
-rw-r--r-- | src/models/Guild.ts | 2 | ||||
-rw-r--r-- | src/models/Member.ts | 2 | ||||
-rw-r--r-- | src/models/Message.ts | 77 | ||||
-rw-r--r-- | src/models/User.ts | 4 | ||||
-rw-r--r-- | src/util/BitField.ts | 2 | ||||
-rw-r--r-- | src/util/MongoBigInt.ts | 2 | ||||
-rw-r--r-- | src/util/Permissions.ts | 6 |
8 files changed, 69 insertions, 28 deletions
diff --git a/src/models/AuditLog.ts b/src/models/AuditLog.ts index f69c4329..a0f91a82 100644 --- a/src/models/AuditLog.ts +++ b/src/models/AuditLog.ts @@ -7,7 +7,7 @@ export interface AuditLogResponse { webhooks: []; // TODO: users: PublicUser[]; audit_log_entries: AuditLogEntries[]; - integrations: []; + integrations: []; // TODO: } export interface AuditLogEntries { diff --git a/src/models/Guild.ts b/src/models/Guild.ts index effc20ee..b25be7aa 100644 --- a/src/models/Guild.ts +++ b/src/models/Guild.ts @@ -49,6 +49,7 @@ export interface Guild { } export const GuildSchema = new Schema({ + id: { type: Types.Long, required: true }, afk_channel_id: Types.Long, afk_timeout: Number, application_id: Types.Long, @@ -59,7 +60,6 @@ export const GuildSchema = new Schema({ explicit_content_filter: Number, features: { type: [String], default: [] }, icon: String, - id: { type: Types.Long, required: true }, large: Boolean, max_members: { type: Number, default: 100000 }, max_presences: Number, diff --git a/src/models/Member.ts b/src/models/Member.ts index 5e0e0014..97ed1961 100644 --- a/src/models/Member.ts +++ b/src/models/Member.ts @@ -75,7 +75,7 @@ export const MemberSchema = new Schema({ }, }); -exports.MemberSchema.virtual("user", { +MemberSchema.virtual("user", { ref: UserModel, localField: "id", foreignField: "id", diff --git a/src/models/Message.ts b/src/models/Message.ts index 6353c588..43517313 100644 --- a/src/models/Message.ts +++ b/src/models/Message.ts @@ -1,10 +1,13 @@ -import { Schema, model, Types, Document } from "mongoose"; -import { ChannelType } from "./Channel"; +import { Schema, Types, Document } from "mongoose"; import db from "../util/Database"; +import { UserModel } from "./User"; +import { MemberModel } from "./Member"; +import { RoleModel } from "./Role"; export interface Message extends Document { id: bigint; channel_id: bigint; + guild_id?: bigint; author_id?: bigint; webhook_id?: bigint; application_id: bigint; @@ -13,14 +16,9 @@ export interface Message extends Document { edited_timestamp: number; tts: boolean; mention_everyone: boolean; - mentions: bigint[]; - mention_roles: bigint[]; - mention_channels?: { - id: bigint; - guild_id: bigint; - type: ChannelType; - name: string; - }[]; + mention_user_ids: bigint[]; + mention_role_ids: bigint[]; + mention_channels_ids: bigint[]; attachments: Attachment[]; embeds: Embed[]; reactions?: Reaction[]; @@ -154,7 +152,7 @@ const Reaction = { }, }; -const Embed = { +export const Embed = { title: String, //title of embed type: String, // type of embed (always "rich" for webhook embeds) description: String, // description of embed @@ -193,22 +191,16 @@ export const MessageSchema = new Schema({ channel_id: Types.Long, author_id: Types.Long, webhook_id: Types.Long, + guild_id: Types.Long, application_id: Types.Long, content: String, timestamp: Number, edited_timestamp: Number, tts: Boolean, mention_everyone: Boolean, - mentions: [Types.Long], - mention_roles: [Types.Long], - mention_channels: [ - { - id: Types.Long, - guild_id: Types.Long, - type: { type: Number }, - name: String, - }, - ], + mention_user_ids: [Types.Long], + mention_role_ids: [Types.Long], + mention_channel_ids: [Types.Long], attachments: [Attachment], embeds: [Embed], reactions: [Reaction], @@ -228,5 +220,48 @@ export const MessageSchema = new Schema({ }, }); +MessageSchema.virtual("author", { + ref: UserModel, + localField: "author_id", + foreignField: "id", + justOne: true, +}); + +MessageSchema.virtual("member", { + ref: MemberModel, + localField: "author_id", + foreignField: "id", + justOne: true, +}); + +MessageSchema.virtual("mentions", { + ref: UserModel, + localField: "mention_user_ids", + foreignField: "id", + justOne: true, +}); + +MessageSchema.virtual("mention_roles", { + ref: RoleModel, + localField: "mention_role_ids", + foreignField: "id", + justOne: true, +}); + +MessageSchema.virtual("mention_channels", { + ref: RoleModel, + localField: "mention_role_ids", + foreignField: "id", + justOne: true, +}); + +// TODO: missing Application Model +// MessageSchema.virtual("application", { +// ref: Application, +// localField: "mention_role_ids", +// foreignField: "id", +// justOne: true, +// }); + // @ts-ignore export const MessageModel = db.model<Message>("Message", MessageSchema, "messages"); diff --git a/src/models/User.ts b/src/models/User.ts index 00b7f8c6..66849844 100644 --- a/src/models/User.ts +++ b/src/models/User.ts @@ -21,7 +21,7 @@ export interface User { mfa_enabled: boolean; // if multi factor authentication is enabled created_at: number; // registration date verified: boolean; // if the user is offically verified - email: string; // email of the user + email?: string; // email of the user flags: bigint; // UserFlags public_flags: bigint; hash: string; // hash of the password, salt is saved in password (bcrypt) @@ -88,6 +88,7 @@ export interface UserSettings { enable_tts_command: boolean; explicit_content_filter: number; friend_source_flags: { all: boolean }; + gateway_connected: boolean; gif_auto_play: boolean; guild_folders: // every top guild is displayed as a "folder" { @@ -155,6 +156,7 @@ export const UserSchema = new Schema({ enable_tts_command: Boolean, explicit_content_filter: Number, friend_source_flags: { all: Boolean }, + gateway_connected: Boolean, gif_auto_play: Boolean, // every top guild is displayed as a "folder" guild_folders: [ diff --git a/src/util/BitField.ts b/src/util/BitField.ts index aba98d75..5cccd352 100644 --- a/src/util/BitField.ts +++ b/src/util/BitField.ts @@ -137,7 +137,7 @@ export class BitField { const resolve = this.constructor?.resolve || this.resolve; return bit.map((p) => resolve(p)).reduce((prev, p) => BigInt(prev) | BigInt(p), 0n); } - if (typeof bit === "string" && typeof FLAGS[bit] !== "undefined") return this.FLAGS[bit]; + if (typeof bit === "string" && typeof FLAGS[bit] !== "undefined") return FLAGS[bit]; throw new RangeError("BITFIELD_INVALID: " + bit); } } diff --git a/src/util/MongoBigInt.ts b/src/util/MongoBigInt.ts index 28818212..c4e5f623 100644 --- a/src/util/MongoBigInt.ts +++ b/src/util/MongoBigInt.ts @@ -33,7 +33,7 @@ class LongSchema extends mongoose.SchemaType { cast(val: any, scope?: any, init?: any, type?: string) { if (null === val) return val; if ("" === val) return null; - if (typeof val === "bigint" && type === "query") { + if (typeof val === "bigint") { return mongoose.mongo.Long.fromString(val.toString()); } diff --git a/src/util/Permissions.ts b/src/util/Permissions.ts index d7233536..ae21e138 100644 --- a/src/util/Permissions.ts +++ b/src/util/Permissions.ts @@ -5,7 +5,7 @@ import { ChannelDocument, ChannelModel } from "../models/Channel"; import { ChannelPermissionOverwrite } from "../models/Channel"; import { Role, RoleModel } from "../models/Role"; import { BitField } from "./BitField"; -import { GuildDocument } from "../models/Guild"; +import { GuildDocument, GuildModel } from "../models/Guild"; export type PermissionResolvable = bigint | number | Permissions | PermissionResolvable[] | PermissionString; @@ -151,6 +151,10 @@ export async function getPermission( ) { var { channel, member } = cache || {}; + const guild = await GuildModel.findOne({ id: guild_id }, { owner_id: true }).exec(); + if (!guild) throw new Error("Guild not found"); + if (guild.owner_id === user_id) return new Permissions(Permissions.FLAGS.ADMINISTRATOR); + member = await MemberModel.findOne({ guild_id, id: user_id }, "roles").exec(); if (!member) throw new Error("Member not found"); |