From 8a69a9c90e45b5150203acd9e5ed7bd639168fb9 Mon Sep 17 00:00:00 2001 From: cutestnekoaqua Date: Mon, 13 Feb 2023 18:54:38 +0100 Subject: [PATCH] initial bool parsing in mastodon --- .../server/api/mastodon/endpoints/account.ts | 4 ++-- .../server/api/mastodon/endpoints/timeline.ts | 23 +++++++++++++------ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index 34a02a9d2..21bb4a6fb 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 { toLimitToInt } 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, - toLimitToInt(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 bb91a5604..04a15e65d 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts @@ -5,13 +5,22 @@ import { statusModel } from "./status.js"; import Autolinker from "autolinker"; import { ParsedUrlQuery } from "querystring"; -export function toLimitToInt(q: ParsedUrlQuery) { +export function limitToInt(q: ParsedUrlQuery) { let object: any = q; if (q.limit) if (typeof q.limit === "string") object.limit = parseInt(q.limit, 10); 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"); @@ -62,8 +71,8 @@ export function apiTimelineMastodon(router: Router): void { try { const query: any = ctx.query; const data = query.local - ? await client.getLocalTimeline(toLimitToInt(query)) - : await client.getPublicTimeline(toLimitToInt(query)); + ? await client.getLocalTimeline(limitToInt(query)) + : await client.getPublicTimeline(limitToInt(query)); ctx.body = toTextWithReaction(data.data, ctx.hostname); } catch (e: any) { console.error(e); @@ -81,7 +90,7 @@ export function apiTimelineMastodon(router: Router): void { try { const data = await client.getTagTimeline( ctx.params.hashtag, - toLimitToInt(ctx.query), + limitToInt(ctx.query), ); ctx.body = toTextWithReaction(data.data, ctx.hostname); } catch (e: any) { @@ -99,7 +108,7 @@ export function apiTimelineMastodon(router: Router): void { const accessTokens = ctx.headers.authorization; const client = getClient(BASE_URL, accessTokens); try { - const data = await client.getHomeTimeline(toLimitToInt(ctx.query)); + const data = await client.getHomeTimeline(limitToInt(ctx.query)); ctx.body = toTextWithReaction(data.data, ctx.hostname); } catch (e: any) { console.error(e); @@ -118,7 +127,7 @@ export function apiTimelineMastodon(router: Router): void { try { const data = await client.getListTimeline( ctx.params.listId, - toLimitToInt(ctx.query), + limitToInt(ctx.query), ); ctx.body = toTextWithReaction(data.data, ctx.hostname); } catch (e: any) { @@ -135,7 +144,7 @@ export function apiTimelineMastodon(router: Router): void { const client = getClient(BASE_URL, accessTokens); try { const data = await client.getConversationTimeline( - toLimitToInt(ctx.query), + limitToInt(ctx.query), ); ctx.body = data.data; } catch (e: any) {