From b3657f2cc89bd67d1879f2a5f6589bb1cd11ff08 Mon Sep 17 00:00:00 2001 From: TheArcaneBrony Date: Sat, 14 Jan 2023 13:25:16 +0100 Subject: [PATCH] Move isTextChannel to channel Signed-off-by: TheArcaneBrony --- .../v0/channels/#channel_id/webhooks.ts | 14 ---------- .../routes/v9/channels/#channel_id/invites.ts | 6 ++--- .../v9/channels/#channel_id/messages/index.ts | 26 +------------------ .../routes/v9/channels/#channel_id/purge.ts | 3 +-- .../v9/channels/#channel_id/webhooks.ts | 3 +-- src/util/entities/Channel.ts | 24 +++++++++++++++++ 6 files changed, 29 insertions(+), 47 deletions(-) diff --git a/src/api/routes/v0/channels/#channel_id/webhooks.ts b/src/api/routes/v0/channels/#channel_id/webhooks.ts index 737ed6a8..1cd42cee 100644 --- a/src/api/routes/v0/channels/#channel_id/webhooks.ts +++ b/src/api/routes/v0/channels/#channel_id/webhooks.ts @@ -1,19 +1,5 @@ import { Router, Response, Request } from "express"; import { route } from "@fosscord/api"; -import { - Channel, - Config, - handleFile, - trimSpecial, - User, - Webhook, - WebhookCreateSchema, - WebhookType, -} from "@fosscord/util"; -import { HTTPError } from "lambert-server"; -import { isTextChannel } from "./messages/index"; -import { DiscordApiErrors } from "@fosscord/util"; -import crypto from "crypto"; const router: Router = Router(); diff --git a/src/api/routes/v9/channels/#channel_id/invites.ts b/src/api/routes/v9/channels/#channel_id/invites.ts index afa5201b..600046f2 100644 --- a/src/api/routes/v9/channels/#channel_id/invites.ts +++ b/src/api/routes/v9/channels/#channel_id/invites.ts @@ -1,7 +1,6 @@ import { Router, Request, Response } from "express"; import { HTTPError } from "lambert-server"; -import { route } from "@fosscord/api"; -import { random } from "@fosscord/api"; +import { route, random } from "@fosscord/api"; import { Channel, Invite, @@ -11,7 +10,6 @@ import { Guild, PublicInviteRelation, } from "@fosscord/util"; -import { isTextChannel } from "../../../v0/channels/#channel_id/messages"; const router: Router = Router(); @@ -29,7 +27,7 @@ router.post( where: { id: channel_id }, select: ["id", "name", "type", "guild_id"], }); - isTextChannel(channel.type); + channel.isTextChannel(); if (!channel.guild_id) { throw new HTTPError("This channel doesn't exist", 404); diff --git a/src/api/routes/v9/channels/#channel_id/messages/index.ts b/src/api/routes/v9/channels/#channel_id/messages/index.ts index 2968437d..9913d3d8 100644 --- a/src/api/routes/v9/channels/#channel_id/messages/index.ts +++ b/src/api/routes/v9/channels/#channel_id/messages/index.ts @@ -36,30 +36,6 @@ const router: Router = Router(); export default router; -export function isTextChannel(type: ChannelType): boolean { - switch (type) { - case ChannelType.GUILD_STORE: - case ChannelType.GUILD_VOICE: - case ChannelType.GUILD_STAGE_VOICE: - case ChannelType.GUILD_CATEGORY: - case ChannelType.GUILD_FORUM: - case ChannelType.DIRECTORY: - throw new HTTPError("not a text channel", 400); - case ChannelType.DM: - case ChannelType.GROUP_DM: - case ChannelType.GUILD_NEWS: - case ChannelType.GUILD_NEWS_THREAD: - case ChannelType.GUILD_PUBLIC_THREAD: - case ChannelType.GUILD_PRIVATE_THREAD: - case ChannelType.GUILD_TEXT: - case ChannelType.ENCRYPTED: - case ChannelType.ENCRYPTED_THREAD: - return true; - default: - throw new HTTPError("unimplemented", 400); - } -} - // https://discord.com/developers/docs/resources/channel#create-message // get messages router.get("/", async (req: Request, res: Response) => { @@ -67,7 +43,7 @@ router.get("/", async (req: Request, res: Response) => { const channel = await Channel.findOneOrFail({ where: { id: channel_id } }); if (!channel) throw new HTTPError("Channel not found", 404); - isTextChannel(channel.type); + channel.isTextChannel(); const around = req.query.around ? `${req.query.around}` : undefined; const before = req.query.before ? `${req.query.before}` : undefined; const after = req.query.after ? `${req.query.after}` : undefined; diff --git a/src/api/routes/v9/channels/#channel_id/purge.ts b/src/api/routes/v9/channels/#channel_id/purge.ts index 0be9ab7c..e158fb4c 100644 --- a/src/api/routes/v9/channels/#channel_id/purge.ts +++ b/src/api/routes/v9/channels/#channel_id/purge.ts @@ -1,6 +1,5 @@ import { HTTPError } from "lambert-server"; import { route } from "@fosscord/api"; -import { isTextChannel } from "../../../v0/channels/#channel_id/messages"; import { FindManyOptions, Between, Not } from "typeorm"; import { Channel, @@ -34,7 +33,7 @@ router.post( if (!channel.guild_id) throw new HTTPError("Can't purge dm channels", 400); - isTextChannel(channel.type); + channel.isTextChannel(); const rights = await getRights(req.user_id); if (!rights.has("MANAGE_MESSAGES")) { diff --git a/src/api/routes/v9/channels/#channel_id/webhooks.ts b/src/api/routes/v9/channels/#channel_id/webhooks.ts index f2923f95..9f6e5a4d 100644 --- a/src/api/routes/v9/channels/#channel_id/webhooks.ts +++ b/src/api/routes/v9/channels/#channel_id/webhooks.ts @@ -11,7 +11,6 @@ import { WebhookType, } from "@fosscord/util"; import { HTTPError } from "lambert-server"; -import { isTextChannel } from "../../../v0/channels/#channel_id/messages/index"; import { DiscordApiErrors } from "@fosscord/util"; import crypto from "crypto"; @@ -27,7 +26,7 @@ router.post( where: { id: channel_id }, }); - isTextChannel(channel.type); + channel.isTextChannel(); if (!channel.guild_id) throw new HTTPError("Not a guild channel", 400); const webhook_count = await Webhook.count({ where: { channel_id } }); diff --git a/src/util/entities/Channel.ts b/src/util/entities/Channel.ts index aaddc001..23f2ea55 100644 --- a/src/util/entities/Channel.ts +++ b/src/util/entities/Channel.ts @@ -450,6 +450,30 @@ export class Channel extends BaseClass { ]; return disallowedChannelTypes.indexOf(this.type) == -1; } + + isTextChannel(): boolean { + switch (this.type) { + case ChannelType.GUILD_STORE: + case ChannelType.GUILD_VOICE: + case ChannelType.GUILD_STAGE_VOICE: + case ChannelType.GUILD_CATEGORY: + case ChannelType.GUILD_FORUM: + case ChannelType.DIRECTORY: + throw new HTTPError("not a text channel", 400); + case ChannelType.DM: + case ChannelType.GROUP_DM: + case ChannelType.GUILD_NEWS: + case ChannelType.GUILD_NEWS_THREAD: + case ChannelType.GUILD_PUBLIC_THREAD: + case ChannelType.GUILD_PRIVATE_THREAD: + case ChannelType.GUILD_TEXT: + case ChannelType.ENCRYPTED: + case ChannelType.ENCRYPTED_THREAD: + return true; + default: + throw new HTTPError("unimplemented", 400); + } + } } export interface ChannelPermissionOverwrite {