1
0
mirror of https://github.com/spacebarchat/server.git synced 2024-11-22 02:12:40 +01:00

Move isTextChannel to channel

Signed-off-by: TheArcaneBrony <myrainbowdash949@gmail.com>
This commit is contained in:
TheArcaneBrony 2023-01-14 13:25:16 +01:00
parent 909a71ff89
commit b3657f2cc8
No known key found for this signature in database
GPG Key ID: 32FC5AAADAD75A22
6 changed files with 29 additions and 47 deletions

View File

@ -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();

View File

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

View File

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

View File

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

View File

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

View File

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