diff options
author | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-05-18 22:09:03 +0200 |
---|---|---|
committer | Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> | 2021-05-18 22:09:03 +0200 |
commit | ec4ac6f788fe284e7e97b80e1539dea97faff9e0 (patch) | |
tree | f6b9bba63b878f5530bf3da2fa3e9204bf8c8632 /src | |
parent | :bug: add MessageType enum (diff) | |
download | server-ec4ac6f788fe284e7e97b80e1539dea97faff9e0.tar.xz |
:bug: fix types
Diffstat (limited to 'src')
-rw-r--r-- | src/models/Event.ts | 6 | ||||
-rw-r--r-- | src/models/Message.ts | 4 | ||||
-rw-r--r-- | src/util/Database.ts | 1 | ||||
-rw-r--r-- | src/util/Permissions.ts | 25 |
4 files changed, 26 insertions, 10 deletions
diff --git a/src/models/Event.ts b/src/models/Event.ts index 402b30ab..46161350 100644 --- a/src/models/Event.ts +++ b/src/models/Event.ts @@ -138,7 +138,7 @@ export interface ChannelPinsUpdateEvent extends Event { data: { guild_id?: string; channel_id: string; - last_pin_timestamp: number; + last_pin_timestamp?: number; }; } @@ -276,9 +276,9 @@ export interface InviteDeleteEvent extends Event { export type MessagePayload = Omit<Message, "author_id"> & { channel_id: string; guild_id?: string; - author: User; + author: PublicUser; member: PublicMember; - mentions: (User & { member: PublicMember })[]; + mentions: (PublicUser & { member: PublicMember })[]; }; export interface MessageCreateEvent extends Event { diff --git a/src/models/Message.ts b/src/models/Message.ts index 15ff57d3..636e2575 100644 --- a/src/models/Message.ts +++ b/src/models/Message.ts @@ -40,7 +40,9 @@ export interface Message { // mongoose virtuals: author?: PublicUser; member?: PublicMember; - mentions?: PublicUser[]; + mentions?: (PublicUser & { + member: PublicMember; + })[]; mention_roles?: Role[]; mention_channels?: Channel[]; } diff --git a/src/util/Database.ts b/src/util/Database.ts index 68909c97..863df663 100644 --- a/src/util/Database.ts +++ b/src/util/Database.ts @@ -5,6 +5,7 @@ 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 console.log(`[DB] connect: ${uri}`); const connection = mongoose.createConnection(uri, { diff --git a/src/util/Permissions.ts b/src/util/Permissions.ts index ab195813..588d7bb9 100644 --- a/src/util/Permissions.ts +++ b/src/util/Permissions.ts @@ -3,7 +3,7 @@ import { MemberDocument, MemberModel } from "../models/Member"; import { ChannelDocument, ChannelModel } from "../models/Channel"; import { ChannelPermissionOverwrite } from "../models/Channel"; -import { Role, RoleModel } from "../models/Role"; +import { Role, RoleDocument, RoleModel } from "../models/Role"; import { BitField } from "./BitField"; import { GuildDocument, GuildModel } from "../models/Guild"; // TODO: check role hierarchy permission @@ -54,6 +54,8 @@ type PermissionString = const CUSTOM_PERMISSION_OFFSET = 1n << 48n; // 16 free custom permission bits, and 16 for discord to add new ones export class Permissions extends BitField { + cache: PermissionCache = {}; + static FLAGS = { CREATE_INSTANT_INVITE: 1n << 0n, KICK_MEMBERS: 1n << 1n, @@ -186,14 +188,20 @@ export class Permissions extends BitField { } } +export type PermissionCache = { + channel?: ChannelDocument | null; + member?: MemberDocument | null; + guild?: GuildDocument | null; + roles?: RoleDocument[] | null; +}; + export async function getPermission( user_id?: string, guild_id?: string, channel_id?: string, - cache?: { channel?: ChannelDocument | null; member?: MemberDocument | null; guild?: GuildDocument | null } + cache: PermissionCache = {} ) { - var { channel, member, guild } = cache || {}; - var roles; + var { channel, member, guild, roles } = cache; if (!user_id) throw new HTTPError("User not found"); @@ -214,7 +222,7 @@ export async function getPermission( if (!member) member = await MemberModel.findOne({ guild_id, id: user_id }, "roles").exec(); if (!member) throw new HTTPError("Member not found"); - roles = await RoleModel.find({ guild_id, id: { $in: member.roles } }).exec(); + if (!roles) roles = await RoleModel.find({ guild_id, id: { $in: member.roles } }).exec(); } var permission = Permissions.finalPermission({ @@ -232,5 +240,10 @@ export async function getPermission( }, }); - return new Permissions(permission); + const obj = new Permissions(permission); + + // pass cache to permission for possible future getPermission calls + obj.cache = { guild, member, channel, roles }; + + return obj; } |