initial bool parsing in mastodon

This commit is contained in:
cutestnekoaqua 2023-02-13 18:54:38 +01:00
parent ad46ccfd33
commit 8a69a9c90e
No known key found for this signature in database
GPG key ID: 6BF0964A5069C1E0
2 changed files with 18 additions and 9 deletions

View file

@ -3,7 +3,7 @@ import { resolveUser } from "@/remote/resolve-user.js";
import Router from "@koa/router"; import Router from "@koa/router";
import { FindOptionsWhere, IsNull } from "typeorm"; import { FindOptionsWhere, IsNull } from "typeorm";
import { getClient } from "../ApiMastodonCompatibleService.js"; import { getClient } from "../ApiMastodonCompatibleService.js";
import { toLimitToInt } from "./timeline.js"; import { argsToBools, limitToInt } from "./timeline.js";
const relationshopModel = { const relationshopModel = {
id: "", id: "",
@ -118,7 +118,7 @@ export function apiAccountMastodon(router: Router): void {
try { try {
const data = await client.getAccountStatuses( const data = await client.getAccountStatuses(
ctx.params.id, ctx.params.id,
toLimitToInt(ctx.query as any), argsToBools(limitToInt(ctx.query as any)),
); );
ctx.body = data.data; ctx.body = data.data;
} catch (e: any) { } catch (e: any) {

View file

@ -5,13 +5,22 @@ import { statusModel } from "./status.js";
import Autolinker from "autolinker"; import Autolinker from "autolinker";
import { ParsedUrlQuery } from "querystring"; import { ParsedUrlQuery } from "querystring";
export function toLimitToInt(q: ParsedUrlQuery) { export function limitToInt(q: ParsedUrlQuery) {
let object: any = q; let object: any = q;
if (q.limit) if (q.limit)
if (typeof q.limit === "string") object.limit = parseInt(q.limit, 10); if (typeof q.limit === "string") object.limit = parseInt(q.limit, 10);
return q; 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) { export function toTextWithReaction(status: Entity.Status[], host: string) {
return status.map((t) => { return status.map((t) => {
if (!t) return statusModel(null, null, [], "no content"); if (!t) return statusModel(null, null, [], "no content");
@ -62,8 +71,8 @@ export function apiTimelineMastodon(router: Router): void {
try { try {
const query: any = ctx.query; const query: any = ctx.query;
const data = query.local const data = query.local
? await client.getLocalTimeline(toLimitToInt(query)) ? await client.getLocalTimeline(limitToInt(query))
: await client.getPublicTimeline(toLimitToInt(query)); : await client.getPublicTimeline(limitToInt(query));
ctx.body = toTextWithReaction(data.data, ctx.hostname); ctx.body = toTextWithReaction(data.data, ctx.hostname);
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
@ -81,7 +90,7 @@ export function apiTimelineMastodon(router: Router): void {
try { try {
const data = await client.getTagTimeline( const data = await client.getTagTimeline(
ctx.params.hashtag, ctx.params.hashtag,
toLimitToInt(ctx.query), limitToInt(ctx.query),
); );
ctx.body = toTextWithReaction(data.data, ctx.hostname); ctx.body = toTextWithReaction(data.data, ctx.hostname);
} catch (e: any) { } catch (e: any) {
@ -99,7 +108,7 @@ export function apiTimelineMastodon(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.getHomeTimeline(toLimitToInt(ctx.query)); const data = await client.getHomeTimeline(limitToInt(ctx.query));
ctx.body = toTextWithReaction(data.data, ctx.hostname); ctx.body = toTextWithReaction(data.data, ctx.hostname);
} catch (e: any) { } catch (e: any) {
console.error(e); console.error(e);
@ -118,7 +127,7 @@ export function apiTimelineMastodon(router: Router): void {
try { try {
const data = await client.getListTimeline( const data = await client.getListTimeline(
ctx.params.listId, ctx.params.listId,
toLimitToInt(ctx.query), limitToInt(ctx.query),
); );
ctx.body = toTextWithReaction(data.data, ctx.hostname); ctx.body = toTextWithReaction(data.data, ctx.hostname);
} catch (e: any) { } catch (e: any) {
@ -135,7 +144,7 @@ export function apiTimelineMastodon(router: Router): void {
const client = getClient(BASE_URL, accessTokens); const client = getClient(BASE_URL, accessTokens);
try { try {
const data = await client.getConversationTimeline( const data = await client.getConversationTimeline(
toLimitToInt(ctx.query), limitToInt(ctx.query),
); );
ctx.body = data.data; ctx.body = data.data;
} catch (e: any) { } catch (e: any) {