summary refs log tree commit diff
path: root/src/api/util/handlers
diff options
context:
space:
mode:
authorMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-22 22:18:59 +1000
committerMadeline <46743919+MaddyUnderStars@users.noreply.github.com>2022-08-22 22:18:59 +1000
commit0cd9a46eea260c299db2e2983f7214ab8b119d29 (patch)
tree5fbb98e7adcfeab81594732089474afdde5893f9 /src/api/util/handlers
parentMerge branch 'master' into feat/captchaVerify (diff)
parentMerge remote-tracking branch 'Puyodead1/patch/prettier-config' into staging (diff)
downloadserver-0cd9a46eea260c299db2e2983f7214ab8b119d29.tar.xz
Merge remote-tracking branch 'upstream/staging' into feat/captchaVerify
Diffstat (limited to '')
-rw-r--r--src/api/util/handlers/Instance.ts (renamed from api/src/util/handlers/Instance.ts)3
-rw-r--r--src/api/util/handlers/Message.ts (renamed from api/src/util/handlers/Message.ts)37
-rw-r--r--src/api/util/handlers/Voice.ts (renamed from api/src/util/handlers/Voice.ts)0
-rw-r--r--src/api/util/handlers/route.ts (renamed from api/src/util/handlers/route.ts)9
4 files changed, 29 insertions, 20 deletions
diff --git a/api/src/util/handlers/Instance.ts b/src/api/util/handlers/Instance.ts

index 6bddfa98..7c337270 100644 --- a/api/src/util/handlers/Instance.ts +++ b/src/api/util/handlers/Instance.ts
@@ -1,4 +1,5 @@ import { Config, Guild, Session } from "@fosscord/util"; +import { createQueryBuilder } from "typeorm"; export async function initInstance() { // TODO: clean up database and delete tombstone data @@ -9,7 +10,7 @@ export async function initInstance() { const { autoJoin } = Config.get().guild; if (autoJoin.enabled && !autoJoin.guilds?.length) { - let guild = await Guild.findOne({}); + let guild = await Guild.findOne({where: {}, order: {id: "ASC"}}); if (guild) { // @ts-ignore await Config.set({ guild: { autoJoin: { guilds: [guild.id] } } }); diff --git a/api/src/util/handlers/Message.ts b/src/api/util/handlers/Message.ts
index 48f87dfe..ff5ece75 100644 --- a/api/src/util/handlers/Message.ts +++ b/src/api/util/handlers/Message.ts
@@ -21,11 +21,13 @@ import { Webhook, Attachment, Config, + MessageCreateSchema, } from "@fosscord/util"; -import { HTTPError } from "lambert-server"; +import { HTTPError } from "@fosscord/util"; import fetch from "node-fetch"; import cheerio from "cheerio"; -import { MessageCreateSchema } from "../../routes/channels/#channel_id/messages"; +import { OrmUtils } from "@fosscord/util"; + const allow_empty = false; // TODO: check webhook, application, system author, stickers // TODO: embed gifs/videos/images @@ -47,7 +49,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { const channel = await Channel.findOneOrFail({ where: { id: opts.channel_id }, relations: ["recipients"] }); if (!channel || !opts.channel_id) throw new HTTPError("Channel not found", 404); - const message = new Message({ + const message = OrmUtils.mergeDeep(new Message(), { ...opts, sticker_items: opts.sticker_ids?.map((x) => ({ id: x })), guild_id: channel.guild_id, @@ -68,10 +70,10 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { rights.hasThrow("SEND_MESSAGES"); } if (opts.application_id) { - message.application = await Application.findOneOrFail({ id: opts.application_id }); + message.application = await Application.findOneOrFail({ where: { id: opts.application_id } }); } if (opts.webhook_id) { - message.webhook = await Webhook.findOneOrFail({ id: opts.webhook_id }); + message.webhook = await Webhook.findOneOrFail({ where: { id: opts.webhook_id } }); } const permission = await getPermission(opts.author_id, channel.guild_id, opts.channel_id); @@ -85,7 +87,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { permission.hasThrow("READ_MESSAGE_HISTORY"); // code below has to be redone when we add custom message routing if (message.guild_id !== null) { - const guild = await Guild.findOneOrFail({ id: channel.guild_id }); + const guild = await Guild.findOneOrFail({ where: { 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"); @@ -102,11 +104,11 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { throw new HTTPError("Empty messages are not allowed", 50006); } - var content = opts.content; - var mention_channel_ids = [] as string[]; - var mention_role_ids = [] as string[]; - var mention_user_ids = [] as string[]; - var mention_everyone = false; + let content = opts.content; + let mention_channel_ids = [] as string[]; + let mention_role_ids = [] as string[]; + let mention_user_ids = [] as string[]; + let mention_everyone = false; if (content) { // TODO: explicit-only mentions message.content = content.trim(); @@ -120,7 +122,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { await Promise.all( Array.from(content.matchAll(ROLE_MENTION)).map(async ([_, mention]) => { - const role = await Role.findOneOrFail({ id: mention, guild_id: channel.guild_id }); + 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); } @@ -132,9 +134,9 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { } } - message.mention_channels = mention_channel_ids.map((x) => new Channel({ id: x })); - message.mention_roles = mention_role_ids.map((x) => new Role({ id: x })); - message.mentions = mention_user_ids.map((x) => new User({ id: x })); + message.mention_channels = mention_channel_ids.map((x) => OrmUtils.mergeDeep(new Channel(), { id: x })); + message.mention_roles = mention_role_ids.map((x) => OrmUtils.mergeDeep(new Role(), { id: x })); + message.mentions = mention_user_ids.map((x) => OrmUtils.mergeDeep(new User(), { id: x })); message.mention_everyone = mention_everyone; // TODO: check and put it all in the body @@ -144,7 +146,7 @@ export async function handleMessage(opts: MessageOptions): Promise<Message> { // TODO: cache link result in db export async function postHandleMessage(message: Message) { - var links = message.content?.match(LINK_REGEX); + let links = message.content?.match(LINK_REGEX); if (!links) return; const data = { ...message }; @@ -201,9 +203,10 @@ export async function postHandleMessage(message: Message) { export async function sendMessage(opts: MessageOptions) { const message = await handleMessage({ ...opts, timestamp: new Date() }); + //TODO: check this, removed toJSON call await Promise.all([ Message.insert(message), - emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data: message.toJSON() } as MessageCreateEvent) + emitEvent({ event: "MESSAGE_CREATE", channel_id: opts.channel_id, data: message } as MessageCreateEvent) ]); postHandleMessage(message).catch((e) => {}); // no await as it should catch error non-blockingly diff --git a/api/src/util/handlers/Voice.ts b/src/api/util/handlers/Voice.ts
index 4d60eb91..4d60eb91 100644 --- a/api/src/util/handlers/Voice.ts +++ b/src/api/util/handlers/Voice.ts
diff --git a/api/src/util/handlers/route.ts b/src/api/util/handlers/route.ts
index 3d3bbc37..71e14955 100644 --- a/api/src/util/handlers/route.ts +++ b/src/api/util/handlers/route.ts
@@ -19,7 +19,7 @@ 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({ @@ -87,7 +87,7 @@ const normalizeBody = (body: any = {}) => { }; export function route(opts: RouteOptions) { - var validate: AnyValidateFunction<any> | undefined; + let validate: AnyValidateFunction<any> | undefined; if (opts.body) { validate = ajv.getSchema(opts.body); if (!validate) throw new Error(`Body schema ${opts.body} not found`); @@ -117,6 +117,11 @@ export function route(opts: RouteOptions) { const valid = validate(normalizeBody(req.body)); if (!valid) { const fields: Record<string, { code?: string; message: string }> = {}; + if(process.env.LOG_INVALID_BODY) { + console.log(`Got invalid request: ${req.method} ${req.originalUrl}`) + console.log(req.body) + validate.errors?.forEach(x => console.log(x.params)) + } validate.errors?.forEach((x) => (fields[x.instancePath.slice(1)] = { code: x.keyword, message: x.message || "" })); throw FieldErrors(fields); }