From f3e8b5e1fd9b5c31aee21dc4f0e09413e7119526 Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 3 Sep 2021 15:48:09 +0200 Subject: :zap: improve invite generation --- util/src/util/Permissions.ts | 48 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 41 insertions(+), 7 deletions(-) diff --git a/util/src/util/Permissions.ts b/util/src/util/Permissions.ts index a5da377b..94fb3b51 100644 --- a/util/src/util/Permissions.ts +++ b/util/src/util/Permissions.ts @@ -1,8 +1,8 @@ // https://github.com/discordjs/discord.js/blob/master/src/util/Permissions.js // Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah -import { In } from "typeorm"; import { Channel, ChannelPermissionOverwrite, Guild, Member, Role } from "../entities"; import { BitField } from "./BitField"; +import "missing-native-js-functions"; // TODO: check role hierarchy permission var HTTPError: any; @@ -205,7 +205,19 @@ export type PermissionCache = { user_id?: string; }; -export async function getPermission(user_id?: string, guild_id?: string, channel_id?: string) { +export async function getPermission( + user_id?: string, + guild_id?: string, + channel_id?: string, + opts: { + guild_select?: (keyof Guild)[]; + guild_relations?: string[]; + channel_select?: (keyof Channel)[]; + channel_relations?: string[]; + member_select?: (keyof Member)[]; + member_relations?: string[]; + } = {} +) { if (!user_id) throw new HTTPError("User not found"); var channel: Channel | undefined; var member: Member | undefined; @@ -214,20 +226,42 @@ export async function getPermission(user_id?: string, guild_id?: string, channel if (channel_id) { channel = await Channel.findOneOrFail({ where: { id: channel_id }, - relations: ["recipients"], - select: ["id", "recipients", "permission_overwrites", "owner_id", "guild_id"], + relations: ["recipients", ...(opts.channel_relations || [])], + select: [ + "id", + "recipients", + "permission_overwrites", + "owner_id", + "guild_id", + // @ts-ignore + ...(opts.channel_select || []), + ], }); if (channel.guild_id) guild_id = channel.guild_id; // derive guild_id from the channel } if (guild_id) { - guild = await Guild.findOneOrFail({ where: { id: guild_id }, select: ["id", "owner_id"] }); + guild = await Guild.findOneOrFail({ + where: { id: guild_id }, + select: [ + "id", + "owner_id", + // @ts-ignore + ...(opts.guild_select || []), + ], + relations: opts.guild_relations, + }); if (guild.owner_id === user_id) return new Permissions(Permissions.FLAGS.ADMINISTRATOR); member = await Member.findOneOrFail({ where: { guild_id, user_id }, - relations: ["roles"], - select: ["id", "roles"], + relations: ["roles", ...(opts.member_relations || [])], + select: [ + "id", + "roles", + // @ts-ignore + ...(opts.member_select || []), + ], }); } -- cgit 1.4.1 From ebd0537c5c44f0bd9309710ba149e090027027ed Mon Sep 17 00:00:00 2001 From: Flam3rboy <34555296+Flam3rboy@users.noreply.github.com> Date: Fri, 3 Sep 2021 15:48:24 +0200 Subject: :zap: improve invite generation --- api/src/routes/channels/#channel_id/invites.ts | 38 +++++++++++++++++++------- 1 file changed, 28 insertions(+), 10 deletions(-) diff --git a/api/src/routes/channels/#channel_id/invites.ts b/api/src/routes/channels/#channel_id/invites.ts index 35006ac4..fcc8a1ad 100644 --- a/api/src/routes/channels/#channel_id/invites.ts +++ b/api/src/routes/channels/#channel_id/invites.ts @@ -6,26 +6,42 @@ import { random } from "../../../util/RandomInviteID"; import { InviteCreateSchema } from "../../../schema/Invite"; -import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent } from "@fosscord/util"; +import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent, User, Guild } from "@fosscord/util"; +import { isTextChannel } from "./messages"; const router: Router = Router(); router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => { const { user_id } = req; const { channel_id } = req.params; - const channel = await Channel.findOneOrFail({ id: channel_id }); + const channel = await Channel.findOneOrFail({ where: { id: channel_id }, select: ["id", "name", "type", "guild_id"] }); + isTextChannel(channel.type); if (!channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); } const { guild_id } = channel; - const permission = await getPermission(user_id, guild_id); + const permission = await getPermission(user_id, guild_id, undefined, { + guild_select: [ + "banner", + "description", + "features", + "icon", + "id", + "name", + "nsfw", + "nsfw_level", + "splash", + "vanity_url_code", + "verification_level" + ] as (keyof Guild)[] + }); permission.hasThrow("CREATE_INSTANT_INVITE"); const expires_at = new Date(req.body.max_age * 1000 + Date.now()); - const invite = { + const invite = await new Invite({ code: random(), temporary: req.body.temporary, uses: 0, @@ -36,12 +52,14 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) guild_id, channel_id: channel_id, inviter_id: user_id - }; - - await new Invite(invite).save(); - - await emitEvent({ event: "INVITE_CREATE", data: invite, guild_id } as InviteCreateEvent); - res.status(201).send(invite); + }).save(); + const data = invite.toJSON(); + data.inviter = await User.getPublicUser(req.user_id); + data.guild = permission.cache.guild; + data.channel = channel; + + await emitEvent({ event: "INVITE_CREATE", data, guild_id } as InviteCreateEvent); + res.status(201).send(data); }); router.get("/", async (req: Request, res: Response) => { -- cgit 1.4.1