iceshrimp/packages/backend/src/server/api/mastodon/endpoints/search.ts

57 lines
2.5 KiB
TypeScript
Raw Normal View History

import { Converter } from "megalodon";
import Router from "@koa/router";
2023-02-28 17:23:04 +01:00
import axios from "axios";
2023-09-30 23:57:44 +02:00
import { argsToBools, convertPaginationArgsIds, limitToInt, normalizeUrlQuery } from "./timeline.js";
import { convertAccountId, convertSearchIds, convertStatusIds } from "../converters.js";
2023-09-30 23:57:44 +02:00
import authenticate from "@/server/api/authenticate.js";
import { UserHelpers } from "@/server/api/mastodon/helpers/user.js";
import { SearchHelpers } from "@/server/api/mastodon/helpers/search.js";
2023-10-05 01:38:18 +02:00
import { MiscHelpers } from "@/server/api/mastodon/helpers/misc.js";
export function setupEndpointsSearch(router: Router): void {
router.get("/v1/search", async (ctx) => {
try {
const auth = await authenticate(ctx.headers.authorization, null);
const user = auth[0] ?? undefined;
2023-10-01 01:30:09 +02:00
if (!user) {
ctx.status = 401;
return;
}
2023-10-01 01:30:09 +02:00
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);
2023-10-01 01:30:09 +02:00
ctx.body = {
...convertSearchIds(result),
hashtags: result.hashtags.map(p => p.name),
};
} catch (e: any) {
console.error(e);
ctx.status = 400;
ctx.body = {error: e.message};
}
});
router.get("/v2/search", async (ctx) => {
try {
const auth = await authenticate(ctx.headers.authorization, null);
const user = auth[0] ?? undefined;
if (!user) {
ctx.status = 401;
return;
}
2023-09-30 23:57:44 +02:00
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);
2023-09-30 23:57:44 +02:00
ctx.body = convertSearchIds(result);
} catch (e: any) {
console.error(e);
ctx.status = 400;
ctx.body = {error: e.message};
}
});
2023-10-05 01:38:18 +02:00
}