2023-10-01 17:52:59 +02:00
|
|
|
import { Converter } from "megalodon";
|
2023-02-09 23:21:50 +01:00
|
|
|
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";
|
2023-10-04 23:54:45 +02:00
|
|
|
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";
|
2023-02-09 23:21:50 +01:00
|
|
|
|
2023-09-29 21:45:58 +02:00
|
|
|
export function setupEndpointsSearch(router: Router): void {
|
2023-10-01 17:52:59 +02:00
|
|
|
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
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
if (!user) {
|
|
|
|
ctx.status = 401;
|
|
|
|
return;
|
|
|
|
}
|
2023-10-01 01:30:09 +02:00
|
|
|
|
2023-10-01 17:52:59 +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
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
ctx.body = {
|
2023-10-04 23:54:45 +02:00
|
|
|
...convertSearchIds(result),
|
2023-10-01 17:52:59 +02:00
|
|
|
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;
|
2023-07-08 03:24:11 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
if (!user) {
|
|
|
|
ctx.status = 401;
|
|
|
|
return;
|
|
|
|
}
|
2023-09-30 23:57:44 +02:00
|
|
|
|
2023-10-01 17:52:59 +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
|
|
|
|
2023-10-04 23:54:45 +02:00
|
|
|
ctx.body = convertSearchIds(result);
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
ctx.status = 400;
|
|
|
|
ctx.body = {error: e.message};
|
|
|
|
}
|
|
|
|
});
|
2023-10-05 01:38:18 +02:00
|
|
|
}
|