2023-07-22 13:11:32 +02:00
|
|
|
import megalodon, { MegalodonInterface } from "megalodon";
|
2023-02-09 23:21:50 +01:00
|
|
|
import Router from "@koa/router";
|
2023-02-11 00:41:19 +01:00
|
|
|
import { getClient } from "../ApiMastodonCompatibleService.js";
|
2023-02-28 17:23:04 +01:00
|
|
|
import axios from "axios";
|
2023-07-22 13:11:32 +02:00
|
|
|
import { Converter } from "megalodon";
|
2023-09-28 22:37:54 +02:00
|
|
|
import { convertPaginationArgsIds, limitToInt } from "./timeline.js";
|
2023-04-30 21:34:52 +02:00
|
|
|
import { convertAccount, convertStatus } from "../converters.js";
|
2023-02-09 23:21:50 +01:00
|
|
|
|
|
|
|
export function apiSearchMastodon(router: Router): void {
|
2023-02-11 00:41:19 +01:00
|
|
|
router.get("/v1/search", async (ctx) => {
|
2023-02-09 23:21:50 +01:00
|
|
|
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 {
|
2023-09-28 22:37:54 +02:00
|
|
|
const query: any = convertPaginationArgsIds(limitToInt(ctx.query));
|
2023-02-11 00:41:19 +01:00
|
|
|
const type = query.type || "";
|
2023-02-09 23:21:50 +01:00
|
|
|
const data = await client.search(query.q, type, query);
|
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
2023-02-11 00:41:19 +01:00
|
|
|
console.error(e);
|
2023-02-09 23:21:50 +01:00
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
2023-02-28 17:23:04 +01:00
|
|
|
router.get("/v2/search", async (ctx) => {
|
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-09-28 22:37:54 +02:00
|
|
|
const query: any = convertPaginationArgsIds(limitToInt(ctx.query));
|
2023-02-28 17:23:04 +01:00
|
|
|
const type = query.type;
|
2023-07-07 23:42:19 +02:00
|
|
|
const acct =
|
|
|
|
!type || type === "accounts"
|
|
|
|
? await client.search(query.q, "accounts", query)
|
|
|
|
: null;
|
|
|
|
const stat =
|
|
|
|
!type || type === "statuses"
|
|
|
|
? await client.search(query.q, "statuses", query)
|
|
|
|
: null;
|
|
|
|
const tags =
|
|
|
|
!type || type === "hashtags"
|
|
|
|
? await client.search(query.q, "hashtags", query)
|
|
|
|
: null;
|
2023-07-08 03:24:11 +02:00
|
|
|
|
2023-07-07 23:42:19 +02:00
|
|
|
ctx.body = {
|
|
|
|
accounts:
|
|
|
|
acct?.data?.accounts.map((account) => convertAccount(account)) ?? [],
|
|
|
|
statuses:
|
|
|
|
stat?.data?.statuses.map((status) => convertStatus(status)) ?? [],
|
|
|
|
hashtags: tags?.data?.hashtags ?? [],
|
|
|
|
};
|
2023-02-28 17:23:04 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
2023-03-31 04:10:03 +02:00
|
|
|
ctx.status = 401;
|
2023-02-28 17:38:48 +01:00
|
|
|
ctx.body = e.response.data;
|
2023-02-28 17:23:04 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
router.get("/v1/trends/statuses", async (ctx) => {
|
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
try {
|
2023-03-31 04:10:03 +02:00
|
|
|
const data = await getHighlight(
|
|
|
|
BASE_URL,
|
|
|
|
ctx.request.hostname,
|
|
|
|
accessTokens,
|
|
|
|
);
|
2023-05-02 05:32:18 +02:00
|
|
|
ctx.body = data.map((status) => convertStatus(status));
|
2023-02-28 17:23:04 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
2023-03-31 04:10:03 +02:00
|
|
|
ctx.status = 401;
|
2023-02-28 17:23:04 +01:00
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
router.get("/v2/suggestions", async (ctx) => {
|
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
try {
|
|
|
|
const query: any = ctx.query;
|
2023-04-30 21:34:52 +02:00
|
|
|
let data = await getFeaturedUser(
|
2023-02-28 17:23:04 +01:00
|
|
|
BASE_URL,
|
|
|
|
ctx.request.hostname,
|
|
|
|
accessTokens,
|
|
|
|
query.limit || 20,
|
|
|
|
);
|
2023-05-02 05:32:18 +02:00
|
|
|
data = data.map((suggestion) => {
|
2023-04-30 21:34:52 +02:00
|
|
|
suggestion.account = convertAccount(suggestion.account);
|
|
|
|
return suggestion;
|
|
|
|
});
|
2023-02-28 17:23:04 +01:00
|
|
|
console.log(data);
|
|
|
|
ctx.body = data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
2023-03-31 04:10:03 +02:00
|
|
|
ctx.status = 401;
|
2023-02-28 17:23:04 +01:00
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
async function getHighlight(
|
|
|
|
BASE_URL: string,
|
|
|
|
domain: string,
|
|
|
|
accessTokens: string | undefined,
|
|
|
|
) {
|
|
|
|
const accessTokenArr = accessTokens?.split(" ") ?? [null];
|
|
|
|
const accessToken = accessTokenArr[accessTokenArr.length - 1];
|
|
|
|
try {
|
|
|
|
const api = await axios.post(`${BASE_URL}/api/notes/featured`, {
|
|
|
|
i: accessToken,
|
|
|
|
});
|
|
|
|
const data: MisskeyEntity.Note[] = api.data;
|
2023-07-02 18:41:01 +02:00
|
|
|
return data.map((note) => new Converter(BASE_URL).note(note, domain));
|
2023-02-28 17:23:04 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.log(e);
|
|
|
|
console.log(e.response.data);
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
async function getFeaturedUser(
|
|
|
|
BASE_URL: string,
|
|
|
|
host: string,
|
|
|
|
accessTokens: string | undefined,
|
|
|
|
limit: number,
|
|
|
|
) {
|
|
|
|
const accessTokenArr = accessTokens?.split(" ") ?? [null];
|
|
|
|
const accessToken = accessTokenArr[accessTokenArr.length - 1];
|
|
|
|
try {
|
|
|
|
const api = await axios.post(`${BASE_URL}/api/users`, {
|
|
|
|
i: accessToken,
|
|
|
|
limit,
|
|
|
|
origin: "local",
|
|
|
|
sort: "+follower",
|
|
|
|
state: "alive",
|
|
|
|
});
|
|
|
|
const data: MisskeyEntity.UserDetail[] = api.data;
|
|
|
|
console.log(data);
|
|
|
|
return data.map((u) => {
|
|
|
|
return {
|
|
|
|
source: "past_interactions",
|
2023-07-02 18:41:01 +02:00
|
|
|
account: new Converter(BASE_URL).userDetail(u, host),
|
2023-02-28 17:23:04 +01:00
|
|
|
};
|
|
|
|
});
|
|
|
|
} catch (e: any) {
|
|
|
|
console.log(e);
|
|
|
|
console.log(e.response.data);
|
|
|
|
return [];
|
|
|
|
}
|
2023-02-09 23:21:50 +01:00
|
|
|
}
|