parse query stuff with bools

This commit is contained in:
cutestnekoaqua 2023-02-13 19:34:41 +01:00
parent 8a69a9c90e
commit 4eb695f3c3
No known key found for this signature in database
GPG key ID: 6BF0964A5069C1E0
3 changed files with 53 additions and 11 deletions

View file

@ -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 { argsToBools, limitToInt } from "./timeline.js";
import { 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,
argsToBools(limitToInt(ctx.query as any)),
limitToInt(ctx.query as any),
);
ctx.body = data.data;
} catch (e: any) {

View file

@ -12,15 +12,6 @@ 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");

View file

@ -30,9 +30,59 @@ 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;
@ -80,6 +130,7 @@ 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 {