From 48b53428d01b211360649c6907caa840a6648f04 Mon Sep 17 00:00:00 2001 From: cutestnekoaqua Date: Mon, 13 Feb 2023 19:39:14 +0100 Subject: [PATCH] Revert "parse query stuff with bools" This reverts commit 4eb695f3c3acca0510036b13562207d35c7bf7fb. --- .../server/api/mastodon/endpoints/account.ts | 4 +- .../server/api/mastodon/endpoints/timeline.ts | 9 ++++ packages/backend/src/server/index.ts | 51 ------------------- 3 files changed, 11 insertions(+), 53 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index b479d5a48d..21bb4a6fbb 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -3,7 +3,7 @@ import { resolveUser } from "@/remote/resolve-user.js"; import Router from "@koa/router"; import { FindOptionsWhere, IsNull } from "typeorm"; import { getClient } from "../ApiMastodonCompatibleService.js"; -import { limitToInt } from "./timeline.js"; +import { argsToBools, limitToInt } from "./timeline.js"; const relationshopModel = { id: "", @@ -118,7 +118,7 @@ export function apiAccountMastodon(router: Router): void { try { const data = await client.getAccountStatuses( ctx.params.id, - limitToInt(ctx.query as any), + argsToBools(limitToInt(ctx.query as any)), ); ctx.body = data.data; } catch (e: any) { diff --git a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts index f90c275445..04a15e65df 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts @@ -12,6 +12,15 @@ export function limitToInt(q: ParsedUrlQuery) { return q; } +export function argsToBools(q: ParsedUrlQuery) { + let object: any = q; + if (q.only_media) + if (typeof q.only_media === "string") object.only_media = q.only_media.toLowerCase() === "true"; + if (q.exclude_replies) + if (typeof q.exclude_replies === "string") object.exclude_replies = q.exclude_replies.toLowerCase() === "true"; + return q; +} + export function toTextWithReaction(status: Entity.Status[], host: string) { return status.map((t) => { if (!t) return statusModel(null, null, [], "no content"); diff --git a/packages/backend/src/server/index.ts b/packages/backend/src/server/index.ts index aef0d100ff..73cf58ec27 100644 --- a/packages/backend/src/server/index.ts +++ b/packages/backend/src/server/index.ts @@ -30,59 +30,9 @@ import proxyServer from "./proxy/index.js"; import webServer from "./web/index.js"; import { initializeStreamingServer } from "./api/streaming.js"; import { koaBody } from "koa-body"; -import { ParsedUrlQuery } from "node:querystring"; export const serverLogger = new Logger("server", "gray", false); -const stringToBoolean = (stringValue: string) => { - switch(stringValue?.toLowerCase()?.trim()){ - case "true": - case "yes": - case "1": - return true; - - case "false": - case "no": - case "0": - case null: - case undefined: - return false; - - default: - return JSON.parse(stringValue); - } -} - -const objectParser = (object: Object): Object => { - const newObject: any = {}; - - for (const key in object) { - const value = object[key]; - - if (typeof value === "object") { - newObject[key] = objectParser(value); - } else if (typeof value === "string") { - newObject[key] = stringToBoolean(value); - } - } - - return newObject; -}; - -const queryParser = (object: ParsedUrlQuery): ParsedUrlQuery => { - const newObject = object; - - for (const key in object) { - const value = object[key]; - - if (typeof value === "string") { - newObject[key] = stringToBoolean(value); - } - } - - return newObject; -}; - // Init app const app = new Koa(); app.proxy = true; @@ -130,7 +80,6 @@ mastoRouter.use( mastoRouter.use(async (ctx, next) => { if (ctx.request.query) { - ctx.request.query = queryParser(ctx.request.query) if (!ctx.request.body || Object.keys(ctx.request.body).length === 0) { ctx.request.body = ctx.request.query } else {