1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-11 13:14:06 +01:00

🐛 fix types

This commit is contained in:
Flam3rboy 2021-05-18 22:09:03 +02:00
parent 9fb7bf9491
commit ec4ac6f788
4 changed files with 26 additions and 10 deletions

View File

@ -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 {

View File

@ -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[];
}

View File

@ -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, {

View File

@ -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;
}