summary refs log tree commit diff
path: root/api/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'api/src/util')
-rw-r--r--api/src/util/entities/blockedEmailDomains.txt (renamed from api/src/util/blockedEmailDomains.txt)0
-rw-r--r--api/src/util/entities/trustedEmailDomains.txt (renamed from api/src/util/trustedEmailDomains.txt)0
-rw-r--r--api/src/util/handlers/Instance.ts (renamed from api/src/util/Instance.ts)0
-rw-r--r--api/src/util/handlers/Message.ts (renamed from api/src/util/Message.ts)30
-rw-r--r--api/src/util/handlers/Voice.ts (renamed from api/src/util/Voice.ts)2
-rw-r--r--api/src/util/handlers/route.ts (renamed from api/src/util/route.ts)4
-rw-r--r--api/src/util/index.ts16
-rw-r--r--api/src/util/utility/Base64.ts (renamed from api/src/util/Base64.ts)0
-rw-r--r--api/src/util/utility/RandomInviteID.ts (renamed from api/src/util/RandomInviteID.ts)0
-rw-r--r--api/src/util/utility/String.ts (renamed from api/src/util/String.ts)0
-rw-r--r--api/src/util/utility/ipAddress.ts (renamed from api/src/util/ipAddress.ts)0
-rw-r--r--api/src/util/utility/passwordStrength.ts (renamed from api/src/util/passwordStrength.ts)0
12 files changed, 32 insertions, 20 deletions
diff --git a/api/src/util/blockedEmailDomains.txt b/api/src/util/entities/blockedEmailDomains.txt

index eb88305d..eb88305d 100644 --- a/api/src/util/blockedEmailDomains.txt +++ b/api/src/util/entities/blockedEmailDomains.txt
diff --git a/api/src/util/trustedEmailDomains.txt b/api/src/util/entities/trustedEmailDomains.txt
index 38ffa4fa..38ffa4fa 100644 --- a/api/src/util/trustedEmailDomains.txt +++ b/api/src/util/entities/trustedEmailDomains.txt
diff --git a/api/src/util/Instance.ts b/api/src/util/handlers/Instance.ts
index 6bddfa98..6bddfa98 100644 --- a/api/src/util/Instance.ts +++ b/api/src/util/handlers/Instance.ts
diff --git a/api/src/util/Message.ts b/api/src/util/handlers/Message.ts
index 4ba93edd..21664368 100644 --- a/api/src/util/Message.ts +++ b/api/src/util/handlers/Message.ts
@@ -2,6 +2,7 @@ import { Channel, Embed, emitEvent, + Guild, Message, MessageCreateEvent, MessageUpdateEvent, @@ -17,13 +18,14 @@ import { User, Application, Webhook, - Attachment + Attachment, + Config, } from "@fosscord/util"; import { HTTPError } from "lambert-server"; import fetch from "node-fetch"; import cheerio from "cheerio"; -import { MessageCreateSchema } from "../routes/channels/#channel_id/messages"; - +import { MessageCreateSchema } from "../../routes/channels/#channel_id/messages"; +const allow_empty = false; // TODO: check webhook, application, system author, stickers // TODO: embed gifs/videos/images @@ -55,6 +57,10 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { type: opts.type ?? 0 }); + if (message.content && message.content.length > Config.get().limits.message.maxCharacters) { + throw new HTTPError("Content length over max character limit") + } + // TODO: are tts messages allowed in dm channels? should permission be checked? if (opts.author_id) { message.author = await User.getPublicUser(opts.author_id); @@ -67,7 +73,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { } const permission = await getPermission(opts.author_id, channel.guild_id, opts.channel_id); - permission.hasThrow("SEND_MESSAGES"); + permission.hasThrow("SEND_MESSAGES"); // TODO: add the rights check if (permission.cache.member) { message.member = permission.cache.member; } @@ -75,15 +81,19 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { if (opts.tts) permission.hasThrow("SEND_TTS_MESSAGES"); if (opts.message_reference) { permission.hasThrow("READ_MESSAGE_HISTORY"); - if (opts.message_reference.guild_id !== channel.guild_id) throw new HTTPError("You can only reference messages from this guild"); - if (opts.message_reference.channel_id !== opts.channel_id) throw new HTTPError("You can only reference messages from this channel"); + // code below has to be redone when we add custom message routing and cross-channel replies + const guild = await Guild.findOneOrFail({ id: channel.guild_id }); + if (!guild.features.includes("CROSS_CHANNEL_REPLIES")) { + if (opts.message_reference.guild_id !== channel.guild_id) throw new HTTPError("You can only reference messages from this guild"); + if (opts.message_reference.channel_id !== opts.channel_id) throw new HTTPError("You can only reference messages from this channel"); + } // TODO: should be checked if the referenced message exists? // @ts-ignore message.type = MessageType.REPLY; } // TODO: stickers/activity - if (!opts.content && !opts.embeds?.length && !opts.attachments?.length && !opts.sticker_ids?.length) { + if (!allow_empty && (!opts.content && !opts.embeds?.length && !opts.attachments?.length && !opts.sticker_ids?.length)) { throw new HTTPError("Empty messages are not allowed", 50006); } @@ -93,7 +103,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { var mention_user_ids = [] as string[]; var mention_everyone = false; - if (content) { + if (content) { // TODO: explicit-only mentions message.content = content.trim(); for (const [_, mention] of content.matchAll(CHANNEL_MENTION)) { if (!mention_channel_ids.includes(mention)) mention_channel_ids.push(mention); @@ -135,7 +145,7 @@ export async function postHandleMessage(message: Message) { const data = { ...message }; data.embeds = data.embeds.filter((x) => x.type !== "link"); - links = links.slice(0, 5); // embed max 5 links + links = links.slice(0, 20); // embed max 20 links — TODO: make this configurable with instance policies for (const link of links) { try { @@ -188,7 +198,7 @@ export async function sendMessage(opts: MessageOptions) { emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data: message.toJSON() } as MessageCreateEvent) ]); - postHandleMessage(message).catch((e) => {}); // no await as it shouldnt block the message send function and silently catch error + postHandleMessage(message).catch((e) => {}); // no await as it should catch error non-blockingly return message; } diff --git a/api/src/util/Voice.ts b/api/src/util/handlers/Voice.ts
index f06b1aaa..4d60eb91 100644 --- a/api/src/util/Voice.ts +++ b/api/src/util/handlers/Voice.ts
@@ -1,5 +1,5 @@ import { Config } from "@fosscord/util"; -import { distanceBetweenLocations, IPAnalysis } from "./ipAddress"; +import { distanceBetweenLocations, IPAnalysis } from "../utility/ipAddress"; export async function getVoiceRegions(ipAddress: string, vip: boolean) { const regions = Config.get().regions; diff --git a/api/src/util/route.ts b/api/src/util/handlers/route.ts
index e4794eb5..05658ad3 100644 --- a/api/src/util/route.ts +++ b/api/src/util/handlers/route.ts
@@ -18,8 +18,9 @@ import Ajv from "ajv"; import { AnyValidateFunction } from "ajv/dist/core"; import addFormats from "ajv-formats"; -const SchemaPath = path.join(__dirname, "..", "..", "assets", "schemas.json"); +const SchemaPath = path.join(__dirname, "..", "..", "..", "assets", "schemas.json"); const schemas = JSON.parse(fs.readFileSync(SchemaPath, { encoding: "utf8" })); + export const ajv = new Ajv({ allErrors: true, parseDate: true, @@ -30,6 +31,7 @@ export const ajv = new Ajv({ strict: true, strictRequired: true }); + addFormats(ajv); declare global { diff --git a/api/src/util/index.ts b/api/src/util/index.ts
index 238787c9..ffbcf24e 100644 --- a/api/src/util/index.ts +++ b/api/src/util/index.ts
@@ -1,8 +1,8 @@ -export * from "./Base64"; -export * from "./ipAddress"; -export * from "./Message"; -export * from "./passwordStrength"; -export * from "./RandomInviteID"; -export * from "./route"; -export * from "./String"; -export * from "./Voice"; +export * from "./utility/Base64"; +export * from "./utility/ipAddress"; +export * from "./handlers/Message"; +export * from "./utility/passwordStrength"; +export * from "./utility/RandomInviteID"; +export * from "./handlers/route"; +export * from "./utility/String"; +export * from "./handlers/Voice"; diff --git a/api/src/util/Base64.ts b/api/src/util/utility/Base64.ts
index 46cff77a..46cff77a 100644 --- a/api/src/util/Base64.ts +++ b/api/src/util/utility/Base64.ts
diff --git a/api/src/util/RandomInviteID.ts b/api/src/util/utility/RandomInviteID.ts
index 7ea344e0..7ea344e0 100644 --- a/api/src/util/RandomInviteID.ts +++ b/api/src/util/utility/RandomInviteID.ts
diff --git a/api/src/util/String.ts b/api/src/util/utility/String.ts
index 982b7e11..982b7e11 100644 --- a/api/src/util/String.ts +++ b/api/src/util/utility/String.ts
diff --git a/api/src/util/ipAddress.ts b/api/src/util/utility/ipAddress.ts
index 13cc9603..13cc9603 100644 --- a/api/src/util/ipAddress.ts +++ b/api/src/util/utility/ipAddress.ts
diff --git a/api/src/util/passwordStrength.ts b/api/src/util/utility/passwordStrength.ts
index 047df008..047df008 100644 --- a/api/src/util/passwordStrength.ts +++ b/api/src/util/utility/passwordStrength.ts