summary refs log tree commit diff
path: root/src/api/util/handlers
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/api/util/handlers/Message.ts63
-rw-r--r--src/api/util/handlers/Voice.ts9
-rw-r--r--src/api/util/handlers/route.ts24
3 files changed, 27 insertions, 69 deletions
diff --git a/src/api/util/handlers/Message.ts b/src/api/util/handlers/Message.ts

index 6172a3d0..24e307ca 100644 --- a/src/api/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts
@@ -58,12 +58,9 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { where: { id: opts.channel_id }, relations: ["recipients"], }); - if (!channel || !opts.channel_id) - throw new HTTPError("Channel not found", 404); + if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); - const stickers = opts.sticker_ids - ? await Sticker.find({ where: { id: In(opts.sticker_ids) } }) - : undefined; + const stickers = opts.sticker_ids ? await Sticker.find({ where: { id: In(opts.sticker_ids) } }) : undefined; const message = Message.create({ ...opts, sticker_items: stickers, @@ -75,10 +72,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { type: opts.type ?? 0, }); - if ( - message.content && - message.content.length > Config.get().limits.message.maxCharacters - ) { + if (message.content && message.content.length > Config.get().limits.message.maxCharacters) { throw new HTTPError("Content length over max character limit"); } @@ -98,11 +92,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { }); } - const permission = await getPermission( - opts.author_id, - channel.guild_id, - opts.channel_id, - ); + const permission = await getPermission(opts.author_id, channel.guild_id, opts.channel_id); permission.hasThrow("SEND_MESSAGES"); if (permission.cache.member) { message.member = permission.cache.member; @@ -118,13 +108,9 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { }); 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", - ); + 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", - ); + throw new HTTPError("You can only reference messages from this channel"); } } /** Q: should be checked if the referenced message exists? ANSWER: NO @@ -162,27 +148,22 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { }*/ for (const [, mention] of content.matchAll(USER_MENTION)) { - if (!mention_user_ids.includes(mention)) - mention_user_ids.push(mention); + if (!mention_user_ids.includes(mention)) mention_user_ids.push(mention); } await Promise.all( - Array.from(content.matchAll(ROLE_MENTION)).map( - async ([, mention]) => { - const role = await Role.findOneOrFail({ - where: { id: mention, guild_id: channel.guild_id }, - }); - if (role.mentionable || permission.has("MANAGE_ROLES")) { - mention_role_ids.push(mention); - } - }, - ), + Array.from(content.matchAll(ROLE_MENTION)).map(async ([, mention]) => { + const role = await Role.findOneOrFail({ + where: { id: mention, guild_id: channel.guild_id }, + }); + if (role.mentionable || permission.has("MANAGE_ROLES")) { + mention_role_ids.push(mention); + } + }) ); if (permission.has("MENTION_EVERYONE")) { - mention_everyone = - !!content.match(EVERYONE_MENTION) || - !!content.match(HERE_MENTION); + mention_everyone = !!content.match(EVERYONE_MENTION) || !!content.match(HERE_MENTION); } } @@ -222,8 +203,7 @@ export async function postHandleMessage(message: Message) { } // bit gross, but whatever! - const endpointPublic = - Config.get().cdn.endpointPublic || "http://127.0.0.1"; // lol + const endpointPublic = Config.get().cdn.endpointPublic || "http://127.0.0.1"; // lol const handler = url.hostname == new URL(endpointPublic).hostname ? EmbedHandlers["self"] @@ -262,10 +242,7 @@ export async function postHandleMessage(message: Message) { channel_id: message.channel_id, data, } as MessageUpdateEvent), - Message.update( - { id: message.id, channel_id: message.channel_id }, - { embeds: data.embeds }, - ), + Message.update({ id: message.id, channel_id: message.channel_id }, { embeds: data.embeds }), ...cachePromises, ]); } @@ -283,9 +260,7 @@ export async function sendMessage(opts: MessageOptions) { ]); // no await as it should catch error non-blockingly - postHandleMessage(message).catch((e) => - console.error("[Message] post-message handler failed", e), - ); + postHandleMessage(message).catch((e) => console.error("[Message] post-message handler failed", e)); return message; } diff --git a/src/api/util/handlers/Voice.ts b/src/api/util/handlers/Voice.ts
index db06bd33..1407a180 100644 --- a/src/api/util/handlers/Voice.ts +++ b/src/api/util/handlers/Voice.ts
@@ -21,9 +21,7 @@ import { distanceBetweenLocations, IPAnalysis } from "../utility/ipAddress"; export async function getVoiceRegions(ipAddress: string, vip: boolean) { const regions = Config.get().regions; - const availableRegions = regions.available.filter((ar) => - vip ? true : !ar.vip, - ); + const availableRegions = regions.available.filter((ar) => (vip ? true : !ar.vip)); let optimalId = regions.default; if (!regions.useDefaultAsOptimal) { @@ -33,10 +31,7 @@ export async function getVoiceRegions(ipAddress: string, vip: boolean) { for (const ar of availableRegions) { //TODO the endpoint location should be saved in the database if not already present to prevent IPAnalysis call - const dist = distanceBetweenLocations( - clientIpAnalysis, - ar.location || (await IPAnalysis(ar.endpoint)), - ); + const dist = distanceBetweenLocations(clientIpAnalysis, ar.location || (await IPAnalysis(ar.endpoint))); if (dist < min) { min = dist; diff --git a/src/api/util/handlers/route.ts b/src/api/util/handlers/route.ts
index 5a0b48e6..9668b09b 100644 --- a/src/api/util/handlers/route.ts +++ b/src/api/util/handlers/route.ts
@@ -84,24 +84,17 @@ export function route(opts: RouteOptions) { let validate: AnyValidateFunction | undefined; if (opts.requestBody) { validate = ajv.getSchema(opts.requestBody); - if (!validate) - throw new Error(`Body schema ${opts.requestBody} not found`); + if (!validate) throw new Error(`Body schema ${opts.requestBody} not found`); } return async (req: Request, res: Response, next: NextFunction) => { if (opts.permission) { const required = new Permissions(opts.permission); - req.permission = await getPermission( - req.user_id, - req.params.guild_id, - req.params.channel_id, - ); + req.permission = await getPermission(req.user_id, req.params.guild_id, req.params.channel_id); // bitfield comparison: check if user lacks certain permission if (!req.permission.has(required)) { - throw DiscordApiErrors.MISSING_PERMISSIONS.withParams( - opts.permission as string, - ); + throw DiscordApiErrors.MISSING_PERMISSIONS.withParams(opts.permission as string); } } @@ -110,25 +103,20 @@ export function route(opts: RouteOptions) { req.rights = await getRights(req.user_id); if (!req.rights || !req.rights.has(required)) { - throw SpacebarApiErrors.MISSING_RIGHTS.withParams( - opts.right as string, - ); + throw SpacebarApiErrors.MISSING_RIGHTS.withParams(opts.right as string); } } if (validate) { const valid = validate(normalizeBody(req.body)); if (!valid) { - const fields: Record< - string, - { code?: string; message: string } - > = {}; + const fields: Record<string, { code?: string; message: string }> = {}; validate.errors?.forEach( (x) => (fields[x.instancePath.slice(1)] = { code: x.keyword, message: x.message || "", - }), + }) ); throw FieldErrors(fields); }