summary refs log tree commit diff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/Channel.ts54
-rw-r--r--src/util/Message.ts12
-rw-r--r--src/util/instanceOf.ts17
3 files changed, 70 insertions, 13 deletions
diff --git a/src/util/Channel.ts b/src/util/Channel.ts
new file mode 100644

index 00000000..c8df85bc --- /dev/null +++ b/src/util/Channel.ts
@@ -0,0 +1,54 @@ +import { + ChannelCreateEvent, + ChannelModel, + ChannelType, + getPermission, + GuildModel, + Snowflake, + TextChannel, + VoiceChannel +} from "@fosscord/server-util"; +import { HTTPError } from "lambert-server"; +import { emitEvent } from "./Event"; + +// TODO: DM channel +export async function createChannel(channel: Partial<TextChannel | VoiceChannel>, user_id: string = "0") { + if (!channel.permission_overwrites) channel.permission_overwrites = []; + + switch (channel.type) { + case ChannelType.GUILD_TEXT: + case ChannelType.GUILD_VOICE: + break; + case ChannelType.DM: + case ChannelType.GROUP_DM: + throw new HTTPError("You can't create a dm channel in a guild"); + // TODO: check if guild is community server + case ChannelType.GUILD_STORE: + case ChannelType.GUILD_NEWS: + default: + throw new HTTPError("Not yet supported"); + } + + const permissions = await getPermission(user_id, channel.guild_id); + permissions.hasThrow("MANAGE_CHANNELS"); + + if (channel.parent_id) { + const exists = await ChannelModel.findOne({ id: channel.parent_id }, { guild_id: true }).exec(); + if (!exists) throw new HTTPError("Parent id channel doesn't exist", 400); + if (exists.guild_id !== channel.guild_id) throw new HTTPError("The category channel needs to be in the guild"); + } + + // TODO: auto generate position + + channel = await new ChannelModel({ + ...channel, + id: Snowflake.generate(), + created_at: new Date(), + // @ts-ignore + recipients: null + }).save(); + + await emitEvent({ event: "CHANNEL_CREATE", data: channel, guild_id: channel.guild_id } as ChannelCreateEvent); + + return channel; +} diff --git a/src/util/Message.ts b/src/util/Message.ts
index 8e9f98cd..0d3cdac7 100644 --- a/src/util/Message.ts +++ b/src/util/Message.ts
@@ -9,7 +9,7 @@ import { HTTPError } from "lambert-server"; import { emitEvent } from "./Event"; // TODO: check webhook, application, system author -export async function sendMessage(opts: Partial<Message>) { +export async function handleMessage(opts: Partial<Message>) { const channel = await ChannelModel.findOne({ id: opts.channel_id }, { guild_id: true, type: true, permission_overwrites: true }).exec(); if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); // TODO: are tts messages allowed in dm channels? should permission be checked? @@ -28,10 +28,8 @@ export async function sendMessage(opts: Partial<Message>) { } // TODO: check and put it all in the body - const message: Message = { + return { ...opts, - id: Snowflake.generate(), - timestamp: new Date(), guild_id: channel.guild_id, channel_id: opts.channel_id, // TODO: generate mentions and check permissions @@ -43,10 +41,14 @@ export async function sendMessage(opts: Partial<Message>) { reactions: opts.reactions || [], type: opts.type ?? 0 }; +} + +export async function sendMessage(opts: Partial<Message>) { + const message = await handleMessage({ ...opts, id: Snowflake.generate(), timestamp: new Date() }); const data = toObject(await new MessageModel(message).populate({ path: "member", select: PublicMemberProjection }).save()); - await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: channel.guild_id } as MessageCreateEvent); + await emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data, guild_id: message.guild_id } as MessageCreateEvent); return data; } diff --git a/src/util/instanceOf.ts b/src/util/instanceOf.ts
index e4e58092..b67bde27 100644 --- a/src/util/instanceOf.ts +++ b/src/util/instanceOf.ts
@@ -5,7 +5,8 @@ import { Tuple } from "lambert-server"; import "missing-native-js-functions"; export const OPTIONAL_PREFIX = "$"; -export const EMAIL_REGEX = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; +export const EMAIL_REGEX = + /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/; export function check(schema: any) { return (req: Request, res: Response, next: NextFunction) => { @@ -27,9 +28,9 @@ export function FieldErrors(fields: Record<string, { code?: string; message: str _errors: [ { message, - code: code || "BASE_TYPE_INVALID", - }, - ], + code: code || "BASE_TYPE_INVALID" + } + ] })) ); } @@ -68,7 +69,7 @@ export function instanceOf( optional = false, errors = {}, req, - ref, + ref }: { path?: string; optional?: boolean; errors?: any; req: Request; ref?: { key: string | number; obj: any } } ): Boolean { if (!ref) ref = { obj: null, key: "" }; @@ -131,7 +132,7 @@ export function instanceOf( optional, errors: errors[i], req, - ref: { key: i, obj: value }, + ref: { key: i, obj: value } }) === true ) { delete errors[i]; @@ -153,7 +154,7 @@ export function instanceOf( throw new FieldError( "BASE_TYPE_BAD_LENGTH", req.t("common:field.BASE_TYPE_BAD_LENGTH", { - length: `${type.min} - ${type.max}`, + length: `${type.min} - ${type.max}` }) ); } @@ -185,7 +186,7 @@ export function instanceOf( optional: OPTIONAL, errors: errors[newKey], req, - ref: { key: newKey, obj: value }, + ref: { key: newKey, obj: value } }) === true ) { delete errors[newKey];