Merge branch 'master' of https://github.com/fosscord/fosscord-server
This commit is contained in:
commit
77a87acfe6
@ -6,26 +6,42 @@ import { random } from "../../../util/RandomInviteID";
|
|||||||
|
|
||||||
import { InviteCreateSchema } from "../../../schema/Invite";
|
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();
|
const router: Router = Router();
|
||||||
|
|
||||||
router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => {
|
router.post("/", check(InviteCreateSchema), async (req: Request, res: Response) => {
|
||||||
const { user_id } = req;
|
const { user_id } = req;
|
||||||
const { channel_id } = req.params;
|
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) {
|
if (!channel.guild_id) {
|
||||||
throw new HTTPError("This channel doesn't exist", 404);
|
throw new HTTPError("This channel doesn't exist", 404);
|
||||||
}
|
}
|
||||||
const { guild_id } = channel;
|
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");
|
permission.hasThrow("CREATE_INSTANT_INVITE");
|
||||||
|
|
||||||
const expires_at = new Date(req.body.max_age * 1000 + Date.now());
|
const expires_at = new Date(req.body.max_age * 1000 + Date.now());
|
||||||
|
|
||||||
const invite = {
|
const invite = await new Invite({
|
||||||
code: random(),
|
code: random(),
|
||||||
temporary: req.body.temporary,
|
temporary: req.body.temporary,
|
||||||
uses: 0,
|
uses: 0,
|
||||||
@ -36,12 +52,14 @@ router.post("/", check(InviteCreateSchema), async (req: Request, res: Response)
|
|||||||
guild_id,
|
guild_id,
|
||||||
channel_id: channel_id,
|
channel_id: channel_id,
|
||||||
inviter_id: user_id
|
inviter_id: user_id
|
||||||
};
|
}).save();
|
||||||
|
const data = invite.toJSON();
|
||||||
|
data.inviter = await User.getPublicUser(req.user_id);
|
||||||
|
data.guild = permission.cache.guild;
|
||||||
|
data.channel = channel;
|
||||||
|
|
||||||
await new Invite(invite).save();
|
await emitEvent({ event: "INVITE_CREATE", data, guild_id } as InviteCreateEvent);
|
||||||
|
res.status(201).send(data);
|
||||||
await emitEvent({ event: "INVITE_CREATE", data: invite, guild_id } as InviteCreateEvent);
|
|
||||||
res.status(201).send(invite);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
router.get("/", async (req: Request, res: Response) => {
|
router.get("/", async (req: Request, res: Response) => {
|
||||||
|
@ -1,8 +1,8 @@
|
|||||||
// https://github.com/discordjs/discord.js/blob/master/src/util/Permissions.js
|
// https://github.com/discordjs/discord.js/blob/master/src/util/Permissions.js
|
||||||
// Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah
|
// Apache License Version 2.0 Copyright 2015 - 2021 Amish Shah
|
||||||
import { In } from "typeorm";
|
|
||||||
import { Channel, ChannelPermissionOverwrite, Guild, Member, Role } from "../entities";
|
import { Channel, ChannelPermissionOverwrite, Guild, Member, Role } from "../entities";
|
||||||
import { BitField } from "./BitField";
|
import { BitField } from "./BitField";
|
||||||
|
import "missing-native-js-functions";
|
||||||
// TODO: check role hierarchy permission
|
// TODO: check role hierarchy permission
|
||||||
|
|
||||||
var HTTPError: any;
|
var HTTPError: any;
|
||||||
@ -205,7 +205,19 @@ export type PermissionCache = {
|
|||||||
user_id?: string;
|
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");
|
if (!user_id) throw new HTTPError("User not found");
|
||||||
var channel: Channel | undefined;
|
var channel: Channel | undefined;
|
||||||
var member: Member | undefined;
|
var member: Member | undefined;
|
||||||
@ -214,20 +226,42 @@ export async function getPermission(user_id?: string, guild_id?: string, channel
|
|||||||
if (channel_id) {
|
if (channel_id) {
|
||||||
channel = await Channel.findOneOrFail({
|
channel = await Channel.findOneOrFail({
|
||||||
where: { id: channel_id },
|
where: { id: channel_id },
|
||||||
relations: ["recipients"],
|
relations: ["recipients", ...(opts.channel_relations || [])],
|
||||||
select: ["id", "recipients", "permission_overwrites", "owner_id", "guild_id"],
|
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 (channel.guild_id) guild_id = channel.guild_id; // derive guild_id from the channel
|
||||||
}
|
}
|
||||||
|
|
||||||
if (guild_id) {
|
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);
|
if (guild.owner_id === user_id) return new Permissions(Permissions.FLAGS.ADMINISTRATOR);
|
||||||
|
|
||||||
member = await Member.findOneOrFail({
|
member = await Member.findOneOrFail({
|
||||||
where: { guild_id, user_id },
|
where: { guild_id, user_id },
|
||||||
relations: ["roles"],
|
relations: ["roles", ...(opts.member_relations || [])],
|
||||||
select: ["id", "roles"],
|
select: [
|
||||||
|
"id",
|
||||||
|
"roles",
|
||||||
|
// @ts-ignore
|
||||||
|
...(opts.member_select || []),
|
||||||
|
],
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user