summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-18 22:09:03 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-05-18 22:09:03 +0200
commitec4ac6f788fe284e7e97b80e1539dea97faff9e0 (patch)
treef6b9bba63b878f5530bf3da2fa3e9204bf8c8632 /src/util
parent:bug: add MessageType enum (diff)
downloadserver-ec4ac6f788fe284e7e97b80e1539dea97faff9e0.tar.xz
:bug: fix types
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Database.ts1
-rw-r--r--src/util/Permissions.ts25
2 files changed, 20 insertions, 6 deletions
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;
 }