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 <fruye@unix.dog>
Signed-off-by: Cleo John <waterdev@galaxycrow.de>
Co-authored-by: fruye <fruye@unix.dog>
Co-committed-by: fruye <fruye@unix.dog>
This commit is contained in:
fruye 2023-03-17 13:58:01 +00:00 committed by Cleo
parent 177f36a2f2
commit 1239db9ac8
3 changed files with 12 additions and 7 deletions

View File

@ -359,7 +359,7 @@ export function apiAccountMastodon(router: Router): void {
const accessTokens = ctx.headers.authorization; const accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { 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; let resp = data.data;
for (let statIdx = 0; statIdx < resp.length; statIdx++) { for (let statIdx = 0; statIdx < resp.length; statIdx++) {
resp[statIdx].id = convertId(resp[statIdx].id, IdType.MastodonId); 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 accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.getFavourites(ctx.query as any); const data = await client.getFavourites(limitToInt(ctx.query as any));
let resp = data.data; let resp = data.data;
for (let statIdx = 0; statIdx < resp.length; statIdx++) { for (let statIdx = 0; statIdx < resp.length; statIdx++) {
resp[statIdx].id = convertId(resp[statIdx].id, IdType.MastodonId); 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 accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.getMutes(ctx.query as any); const data = await client.getMutes(limitToInt(ctx.query as any));
let resp = data.data; let resp = data.data;
for (let acctIdx = 0; acctIdx < resp.length; acctIdx++) { for (let acctIdx = 0; acctIdx < resp.length; acctIdx++) {
resp[acctIdx].id = convertId(resp[acctIdx].id, IdType.MastodonId); 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 accessTokens = ctx.headers.authorization;
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.getBlocks(ctx.query as any); const data = await client.getBlocks(limitToInt(ctx.query as any));
let resp = data.data; let resp = data.data;
for (let acctIdx = 0; acctIdx < resp.length; acctIdx++) { for (let acctIdx = 0; acctIdx < resp.length; acctIdx++) {
resp[acctIdx].id = convertId(resp[acctIdx].id, IdType.MastodonId); resp[acctIdx].id = convertId(resp[acctIdx].id, IdType.MastodonId);

View File

@ -4,6 +4,8 @@ import { emojiRegexAtStartToEnd } from "@/misc/emoji-regex.js";
import axios from "axios"; import axios from "axios";
import querystring from 'node:querystring' import querystring from 'node:querystring'
import qs from 'qs' import qs from 'qs'
import { limitToInt } from "./timeline.js";
function normalizeQuery(data: any) { function normalizeQuery(data: any) {
const str = querystring.stringify(data); const str = querystring.stringify(data);
return qs.parse(str); return qs.parse(str);
@ -101,7 +103,7 @@ export function apiStatusMastodon(router: Router): void {
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const id = ctx.params.id; 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 status = await client.getStatus(id);
const reactionsAxios = await axios.get( const reactionsAxios = await axios.get(
`${BASE_URL}/api/notes/reactions?noteId=${id}`, `${BASE_URL}/api/notes/reactions?noteId=${id}`,

View File

@ -15,13 +15,16 @@ export function limitToInt(q: ParsedUrlQuery) {
} }
export function argsToBools(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; let object: any = q;
if (q.only_media) if (q.only_media)
if (typeof q.only_media === "string") 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 (q.exclude_replies)
if (typeof q.exclude_replies === "string") if (typeof q.exclude_replies === "string")
object.exclude_replies = q.exclude_replies.toLowerCase() === "true"; object.exclude_replies = toBoolean(q.exclude_replies);
return q; return q;
} }