summary refs log tree commit diff
path: root/api/src/routes
diff options
context:
space:
mode:
authorFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-11 15:13:16 +0200
committerFlam3rboy <34555296+Flam3rboy@users.noreply.github.com>2021-10-11 15:13:16 +0200
commit833dcb7c3c8d3db916821533eee74934cc2fd82f (patch)
tree126003b57acb7c4ad5cee0728b2eef4085dfd838 /api/src/routes
parentImplement GIFs (diff)
parentMerge pull request #435 from TheArcaneBrony/change-enums-to-number (diff)
downloadserver-833dcb7c3c8d3db916821533eee74934cc2fd82f.tar.xz
Merge branch 'master' into pr/Thesourtimes/430
Diffstat (limited to 'api/src/routes')
-rw-r--r--api/src/routes/channels/#channel_id/invites.ts2
-rw-r--r--api/src/routes/channels/#channel_id/messages/#message_id/ack.ts8
-rw-r--r--api/src/routes/channels/#channel_id/messages/index.ts8
-rw-r--r--api/src/routes/channels/#channel_id/permissions.ts4
-rw-r--r--api/src/routes/guilds/#guild_id/emojis.ts121
-rw-r--r--api/src/routes/guilds/#guild_id/roles.ts11
-rw-r--r--api/src/routes/guilds/#guild_id/vanity-url.ts19
-rw-r--r--api/src/routes/guilds/templates/index.ts2
-rw-r--r--api/src/routes/invites/index.ts1
9 files changed, 148 insertions, 28 deletions
diff --git a/api/src/routes/channels/#channel_id/invites.ts b/api/src/routes/channels/#channel_id/invites.ts
index 22420983..6d2c625d 100644
--- a/api/src/routes/channels/#channel_id/invites.ts
+++ b/api/src/routes/channels/#channel_id/invites.ts
@@ -2,7 +2,7 @@ import { Router, Request, Response } from "express";
 import { HTTPError } from "lambert-server";
 import { route } from "@fosscord/api";
 import { random } from "@fosscord/api";
-import { getPermission, Channel, Invite, InviteCreateEvent, emitEvent, User, Guild, PublicInviteRelation } from "@fosscord/util";
+import { Channel, Invite, InviteCreateEvent, emitEvent, User, Guild, PublicInviteRelation } from "@fosscord/util";
 import { isTextChannel } from "./messages";
 
 const router: Router = Router();
diff --git a/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts b/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts
index 786e4581..208c1da4 100644
--- a/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts
+++ b/api/src/routes/channels/#channel_id/messages/#message_id/ack.ts
@@ -1,4 +1,4 @@
-import { emitEvent, getPermission, MessageAckEvent, ReadState } from "@fosscord/util";
+import { emitEvent, getPermission, MessageAckEvent, ReadState, Snowflake } from "@fosscord/util";
 import { Request, Response, Router } from "express";
 import { route } from "@fosscord/api";
 
@@ -18,7 +18,11 @@ router.post("/", route({ body: "MessageAcknowledgeSchema" }), async (req: Reques
 	const permission = await getPermission(req.user_id, undefined, channel_id);
 	permission.hasThrow("VIEW_CHANNEL");
 
-	await ReadState.update({ user_id: req.user_id, channel_id }, { user_id: req.user_id, channel_id, last_message_id: message_id });
+	let read_state = await ReadState.findOne({ user_id: req.user_id, channel_id });
+	if (!read_state) read_state = new ReadState({ user_id: req.user_id, channel_id });
+	read_state.last_message_id = message_id;
+
+	await read_state.save();
 
 	await emitEvent({
 		event: "MESSAGE_ACK",
diff --git a/api/src/routes/channels/#channel_id/messages/index.ts b/api/src/routes/channels/#channel_id/messages/index.ts
index 1f856b80..26bb9e5d 100644
--- a/api/src/routes/channels/#channel_id/messages/index.ts
+++ b/api/src/routes/channels/#channel_id/messages/index.ts
@@ -22,7 +22,7 @@ const router: Router = Router();
 
 export default router;
 
-function isTextChannel(type: ChannelType): boolean {
+export function isTextChannel(type: ChannelType): boolean {
 	switch (type) {
 		case ChannelType.GUILD_STORE:
 		case ChannelType.GUILD_VOICE:
@@ -39,7 +39,6 @@ function isTextChannel(type: ChannelType): boolean {
 			return true;
 	}
 }
-module.exports.isTextChannel = isTextChannel;
 
 export interface MessageCreateSchema {
 	content?: string;
@@ -103,6 +102,7 @@ router.get("/", async (req: Request, res: Response) => {
 	}
 
 	const messages = await Message.find(query);
+	const endpoint = Config.get().cdn.endpointPublic;
 
 	return res.json(
 		messages.map((x) => {
@@ -115,7 +115,9 @@ router.get("/", async (req: Request, res: Response) => {
 			// @ts-ignore
 			if (!x.author) x.author = { discriminator: "0000", username: "Deleted User", public_flags: "0", avatar: null };
 			x.attachments?.forEach((x) => {
-				x.proxy_url = `${Config.get().cdn.endpointPublic || "http://localhost:3003"}${new URL(x.proxy_url).pathname}`;
+				// dynamically set attachment proxy_url in case the endpoint changed
+				const uri = x.proxy_url.startsWith("http") ? x.proxy_url : `https://example.org${x.proxy_url}`;
+				x.proxy_url = `${endpoint == null ? "http://localhost:3003" : endpoint}${new URL(uri).pathname}`;
 			});
 
 			return x;
diff --git a/api/src/routes/channels/#channel_id/permissions.ts b/api/src/routes/channels/#channel_id/permissions.ts
index 6ebf721a..2eded853 100644
--- a/api/src/routes/channels/#channel_id/permissions.ts
+++ b/api/src/routes/channels/#channel_id/permissions.ts
@@ -44,8 +44,8 @@ router.put(
 			};
 			channel.permission_overwrites!.push(overwrite);
 		}
-		overwrite.allow = String(req.permission!.bitfield & (BigInt(body.allow) || 0n));
-		overwrite.deny = String(req.permission!.bitfield & (BigInt(body.deny) || 0n));
+		overwrite.allow = String(req.permission!.bitfield & (BigInt(body.allow) || BigInt("0")));
+		overwrite.deny = String(req.permission!.bitfield & (BigInt(body.deny) || BigInt("0")));
 
 		await Promise.all([
 			channel.save(),
diff --git a/api/src/routes/guilds/#guild_id/emojis.ts b/api/src/routes/guilds/#guild_id/emojis.ts
new file mode 100644
index 00000000..ff565cd4
--- /dev/null
+++ b/api/src/routes/guilds/#guild_id/emojis.ts
@@ -0,0 +1,121 @@
+import { Router, Request, Response } from "express";
+import { Config, DiscordApiErrors, emitEvent, Emoji, GuildEmojisUpdateEvent, handleFile, Member, Snowflake, User } from "@fosscord/util";
+import { route } from "@fosscord/api";
+
+const router = Router();
+
+export interface EmojiCreateSchema {
+	name?: string;
+	image: string;
+	require_colons?: boolean | null;
+	roles?: string[];
+}
+
+export interface EmojiModifySchema {
+	name?: string;
+	roles?: string[];
+}
+
+router.get("/", route({}), async (req: Request, res: Response) => {
+	const { guild_id } = req.params;
+
+	await Member.IsInGuildOrFail(req.user_id, guild_id);
+
+	const emojis = await Emoji.find({ where: { guild_id: guild_id }, relations: ["user"] });
+
+	return res.json(emojis);
+});
+
+router.get("/:emoji_id", route({}), async (req: Request, res: Response) => {
+	const { guild_id, emoji_id } = req.params;
+
+	await Member.IsInGuildOrFail(req.user_id, guild_id);
+
+	const emoji = await Emoji.findOneOrFail({ where: { guild_id: guild_id, id: emoji_id }, relations: ["user"] });
+
+	return res.json(emoji);
+});
+
+router.post("/", route({ body: "EmojiCreateSchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => {
+	const { guild_id } = req.params;
+	const body = req.body as EmojiCreateSchema;
+
+	const emoji_count = await Emoji.count({ guild_id: guild_id });
+	const { maxEmojis } = Config.get().limits.guild;
+
+	if (emoji_count >= maxEmojis) throw DiscordApiErrors.MAXIMUM_NUMBER_OF_EMOJIS_REACHED.withParams(maxEmojis);
+
+	const id = Snowflake.generate();
+
+	if (body.require_colons == null) body.require_colons = true;
+
+	const user = await User.findOneOrFail({ id: req.user_id });
+
+	body.image = (await handleFile(`/emojis/${id}`, body.image)) as string;
+
+	const emoji = await new Emoji({
+		id: id,
+		guild_id: guild_id,
+		...body,
+		user: user,
+		managed: false,
+		animated: false, // TODO: Add support animated emojis
+		available: true,
+		roles: []
+	}).save();
+
+	await emitEvent({
+		event: "GUILD_EMOJIS_UPDATE",
+		guild_id: guild_id,
+		data: {
+			guild_id: guild_id,
+			emojis: await Emoji.find({ guild_id: guild_id })
+		}
+	} as GuildEmojisUpdateEvent);
+
+	return res.status(201).json(emoji);
+});
+
+router.patch(
+	"/:emoji_id",
+	route({ body: "EmojiModifySchema", permission: "MANAGE_EMOJIS_AND_STICKERS" }),
+	async (req: Request, res: Response) => {
+		const { emoji_id, guild_id } = req.params;
+		const body = req.body as EmojiModifySchema;
+
+		const emoji = await new Emoji({ ...body, id: emoji_id, guild_id: guild_id }).save();
+
+		await emitEvent({
+			event: "GUILD_EMOJIS_UPDATE",
+			guild_id: guild_id,
+			data: {
+				guild_id: guild_id,
+				emojis: await Emoji.find({ guild_id: guild_id })
+			}
+		} as GuildEmojisUpdateEvent);
+
+		return res.json(emoji);
+	}
+);
+
+router.delete("/:emoji_id", route({ permission: "MANAGE_EMOJIS_AND_STICKERS" }), async (req: Request, res: Response) => {
+	const { emoji_id, guild_id } = req.params;
+
+	await Emoji.delete({
+		id: emoji_id,
+		guild_id: guild_id
+	});
+
+	await emitEvent({
+		event: "GUILD_EMOJIS_UPDATE",
+		guild_id: guild_id,
+		data: {
+			guild_id: guild_id,
+			emojis: await Emoji.find({ guild_id: guild_id })
+		}
+	} as GuildEmojisUpdateEvent);
+
+	res.sendStatus(204);
+});
+
+export default router;
diff --git a/api/src/routes/guilds/#guild_id/roles.ts b/api/src/routes/guilds/#guild_id/roles.ts
index d1d60906..b1875598 100644
--- a/api/src/routes/guilds/#guild_id/roles.ts
+++ b/api/src/routes/guilds/#guild_id/roles.ts
@@ -17,7 +17,7 @@ const router: Router = Router();
 
 export interface RoleModifySchema {
 	name?: string;
-	permissions?: bigint;
+	permissions?: string;
 	color?: number;
 	hoist?: boolean; // whether the role should be displayed separately in the sidebar
 	mentionable?: boolean; // whether the role should be mentionable
@@ -57,7 +57,7 @@ router.post("/", route({ body: "RoleModifySchema", permission: "MANAGE_ROLES" })
 		...body,
 		guild_id: guild_id,
 		managed: false,
-		permissions: String(req.permission!.bitfield & (body.permissions || 0n)),
+		permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0")),
 		tags: undefined
 	});
 
@@ -105,7 +105,12 @@ router.patch("/:role_id", route({ body: "RoleModifySchema", permission: "MANAGE_
 	const { role_id, guild_id } = req.params;
 	const body = req.body as RoleModifySchema;
 
-	const role = new Role({ ...body, id: role_id, guild_id, permissions: String(req.permission!.bitfield & (body.permissions || 0n)) });
+	const role = new Role({
+		...body,
+		id: role_id,
+		guild_id,
+		permissions: String(req.permission!.bitfield & BigInt(body.permissions || "0"))
+	});
 
 	await Promise.all([
 		role.save(),
diff --git a/api/src/routes/guilds/#guild_id/vanity-url.ts b/api/src/routes/guilds/#guild_id/vanity-url.ts
index 7f2cea9e..63173345 100644
--- a/api/src/routes/guilds/#guild_id/vanity-url.ts
+++ b/api/src/routes/guilds/#guild_id/vanity-url.ts
@@ -10,10 +10,10 @@ const InviteRegex = /\W/g;
 router.get("/", route({ permission: "MANAGE_GUILD" }), async (req: Request, res: Response) => {
 	const { guild_id } = req.params;
 
-	const guild = await Guild.findOneOrFail({ where: { id: guild_id }, relations: ["vanity_url"] });
-	if (!guild.vanity_url) return res.json({ code: null });
+	const invite = await Invite.findOne({ where: { guild_id: guild_id, vanity_url: true } });
+	if (!invite) return res.json({ code: null });
 
-	return res.json({ code: guild.vanity_url_code, uses: guild.vanity_url.uses });
+	return res.json({ code: invite.code, uses: invite.uses });
 });
 
 export interface VanityUrlSchema {
@@ -33,20 +33,9 @@ router.patch("/", route({ body: "VanityUrlSchema", permission: "MANAGE_GUILD" })
 	const invite = await Invite.findOne({ code });
 	if (invite) throw new HTTPError("Invite already exists");
 
-	const guild = await Guild.findOneOrFail({ id: guild_id });
 	const { id } = await Channel.findOneOrFail({ guild_id, type: ChannelType.GUILD_TEXT });
 
-	Promise.all([
-		Guild.update({ id: guild_id }, { vanity_url_code: code }),
-		Invite.delete({ code: guild.vanity_url_code }),
-		new Invite({
-			code: code,
-			uses: 0,
-			created_at: new Date(),
-			guild_id,
-			channel_id: id
-		}).save()
-	]);
+	await Invite.update({ vanity_url: true, guild_id }, { code: code, channel_id: id });
 
 	return res.json({ code: code });
 });
diff --git a/api/src/routes/guilds/templates/index.ts b/api/src/routes/guilds/templates/index.ts
index b5e243e9..86316d23 100644
--- a/api/src/routes/guilds/templates/index.ts
+++ b/api/src/routes/guilds/templates/index.ts
@@ -47,7 +47,7 @@ router.post("/:code", route({ body: "GuildTemplateCreateSchema" }), async (req:
 			managed: true,
 			mentionable: true,
 			name: "@everyone",
-			permissions: 2251804225n,
+			permissions: BigInt("2251804225"),
 			position: 0,
 			tags: null
 		}).save()
diff --git a/api/src/routes/invites/index.ts b/api/src/routes/invites/index.ts
index 0fcf7c86..185311bc 100644
--- a/api/src/routes/invites/index.ts
+++ b/api/src/routes/invites/index.ts
@@ -33,7 +33,6 @@ router.delete("/:code", route({}), async (req: Request, res: Response) => {
 
 	await Promise.all([
 		Invite.delete({ code }),
-		Guild.update({ vanity_url_code: code }, { vanity_url_code: undefined }),
 		emitEvent({
 			event: "INVITE_DELETE",
 			guild_id: guild_id,