diff --git a/packages/backend/src/server/api/mastodon/endpoints/search.ts b/packages/backend/src/server/api/mastodon/endpoints/search.ts index a6bfaf593..a1e8fb4f3 100644 --- a/packages/backend/src/server/api/mastodon/endpoints/search.ts +++ b/packages/backend/src/server/api/mastodon/endpoints/search.ts @@ -11,19 +11,27 @@ import { SearchHelpers } from "@/server/api/mastodon/helpers/search.js"; export function setupEndpointsSearch(router: Router): void { router.get("/v1/search", async (ctx) => { - const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`; - const accessTokens = ctx.request.headers.authorization; - const client = getClient(BASE_URL, accessTokens); - const body: any = ctx.request.body; try { - const query: any = convertPaginationArgsIds(limitToInt(ctx.query)); - const type = query.type || ""; - const data = await client.search(query.q, type, query); - ctx.body = data.data; + const auth = await authenticate(ctx.headers.authorization, null); + const user = auth[0] ?? undefined; + + if (!user) { + ctx.status = 401; + return; + } + + const args = normalizeUrlQuery(convertPaginationArgsIds(argsToBools(limitToInt(ctx.query), ['resolve', 'following', 'exclude_unreviewed']))); + const cache = UserHelpers.getFreshAccountCache(); + const result = await SearchHelpers.search(user, args.q, args.type, args.resolve, args.following, args.account_id, args['exclude_unreviewed'], args.max_id, args.min_id, args.limit, args.offset, cache); + + ctx.body = { + ...convertSearch(result), + hashtags: result.hashtags.map(p => p.name), + }; } catch (e: any) { console.error(e); - ctx.status = 401; - ctx.body = e.response.data; + ctx.status = 400; + ctx.body = { error: e.message }; } }); router.get("/v2/search", async (ctx) => {