From 1239db9ac8e100c88ffe3a04c8881aba965bd0bf Mon Sep 17 00:00:00 2001 From: fruye Date: Fri, 17 Mar 2023 13:58:01 +0000 Subject: [PATCH] fix: Parse mastoAPI `limit` argument in more places & Improve converting arguments to boolean (#9716) Reviewed-on: https://codeberg.org/calckey/calckey/pulls/9716 Authored-by: fruye Signed-off-by: Cleo John Co-authored-by: fruye Co-committed-by: fruye --- .../backend/src/server/api/mastodon/endpoints/account.ts | 8 ++++---- .../backend/src/server/api/mastodon/endpoints/status.ts | 4 +++- .../backend/src/server/api/mastodon/endpoints/timeline.ts | 7 +++++-- 3 files changed, 12 insertions(+), 7 deletions(-) diff --git a/packages/backend/src/server/api/mastodon/endpoints/account.ts b/packages/backend/src/server/api/mastodon/endpoints/account.ts index 1c5e31fe8d..bd1a2e6485 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/account.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/account.ts @@ -359,7 +359,7 @@ export function apiAccountMastodon(router: Router): void { const accessTokens = ctx.headers.authorization; const client = getClient(BASE_URL, accessTokens); try { - const data = (await client.getBookmarks(ctx.query as any)) as any; + const data = (await client.getBookmarks(limitToInt(ctx.query as any))) as any; let resp = data.data; for (let statIdx = 0; statIdx < resp.length; statIdx++) { resp[statIdx].id = convertId(resp[statIdx].id, IdType.MastodonId); @@ -383,7 +383,7 @@ export function apiAccountMastodon(router: Router): void { const accessTokens = ctx.headers.authorization; const client = getClient(BASE_URL, accessTokens); try { - const data = await client.getFavourites(ctx.query as any); + const data = await client.getFavourites(limitToInt(ctx.query as any)); let resp = data.data; for (let statIdx = 0; statIdx < resp.length; statIdx++) { resp[statIdx].id = convertId(resp[statIdx].id, IdType.MastodonId); @@ -407,7 +407,7 @@ export function apiAccountMastodon(router: Router): void { const accessTokens = ctx.headers.authorization; const client = getClient(BASE_URL, accessTokens); try { - const data = await client.getMutes(ctx.query as any); + const data = await client.getMutes(limitToInt(ctx.query as any)); let resp = data.data; for (let acctIdx = 0; acctIdx < resp.length; acctIdx++) { resp[acctIdx].id = convertId(resp[acctIdx].id, IdType.MastodonId); @@ -425,7 +425,7 @@ export function apiAccountMastodon(router: Router): void { const accessTokens = ctx.headers.authorization; const client = getClient(BASE_URL, accessTokens); try { - const data = await client.getBlocks(ctx.query as any); + const data = await client.getBlocks(limitToInt(ctx.query as any)); let resp = data.data; for (let acctIdx = 0; acctIdx < resp.length; acctIdx++) { resp[acctIdx].id = convertId(resp[acctIdx].id, IdType.MastodonId); diff --git a/packages/backend/src/server/api/mastodon/endpoints/status.ts b/packages/backend/src/server/api/mastodon/endpoints/status.ts index d04b7a8b95..0128201400 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/status.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/status.ts @@ -4,6 +4,8 @@ import { emojiRegexAtStartToEnd } from "@/misc/emoji-regex.js"; import axios from "axios"; import querystring from 'node:querystring' import qs from 'qs' +import { limitToInt } from "./timeline.js"; + function normalizeQuery(data: any) { const str = querystring.stringify(data); return qs.parse(str); @@ -101,7 +103,7 @@ export function apiStatusMastodon(router: Router): void { const client = getClient(BASE_URL, accessTokens); try { const id = ctx.params.id; - const data = await client.getStatusContext(id, ctx.query as any); + const data = await client.getStatusContext(id, limitToInt(ctx.query as any)); const status = await client.getStatus(id); const reactionsAxios = await axios.get( `${BASE_URL}/api/notes/reactions?noteId=${id}`, diff --git a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts index 58ca8d6771..23729e4804 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/timeline.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/timeline.ts @@ -15,13 +15,16 @@ export function limitToInt(q: ParsedUrlQuery) { } export function argsToBools(q: ParsedUrlQuery) { + // Values taken from https://docs.joinmastodon.org/client/intro/#boolean + const toBoolean = (value: string) => !['0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].includes(value); + let object: any = q; if (q.only_media) if (typeof q.only_media === "string") - object.only_media = q.only_media.toLowerCase() === "true"; + object.only_media = toBoolean(q.only_media); if (q.exclude_replies) if (typeof q.exclude_replies === "string") - object.exclude_replies = q.exclude_replies.toLowerCase() === "true"; + object.exclude_replies = toBoolean(q.exclude_replies); return q; }