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-09 23:21:50 +01:00
|
|
|
import { ParsedUrlQuery } from "querystring";
|
2023-04-30 21:34:52 +02:00
|
|
|
import { convertAccount, convertList, convertStatus } from "../converters.js";
|
|
|
|
import { convertId, IdType } from "../../index.js";
|
2023-02-09 23:21:50 +01:00
|
|
|
|
2023-02-13 18:54:38 +01:00
|
|
|
export function limitToInt(q: ParsedUrlQuery) {
|
2023-02-11 23:12:14 +01:00
|
|
|
let object: any = q;
|
2023-02-11 00:41:19 +01:00
|
|
|
if (q.limit)
|
2023-02-11 23:12:14 +01:00
|
|
|
if (typeof q.limit === "string") object.limit = parseInt(q.limit, 10);
|
2023-02-28 17:23:04 +01:00
|
|
|
if (q.offset)
|
|
|
|
if (typeof q.offset === "string") object.offset = parseInt(q.offset, 10);
|
|
|
|
return object;
|
2023-02-09 23:21:50 +01:00
|
|
|
}
|
|
|
|
|
2023-02-13 19:39:14 +01:00
|
|
|
export function argsToBools(q: ParsedUrlQuery) {
|
2023-03-17 14:58:01 +01:00
|
|
|
// Values taken from https://docs.joinmastodon.org/client/intro/#boolean
|
2023-03-31 04:10:03 +02:00
|
|
|
const toBoolean = (value: string) =>
|
|
|
|
!["0", "f", "F", "false", "FALSE", "off", "OFF"].includes(value);
|
2023-03-17 14:58:01 +01:00
|
|
|
|
2023-02-13 19:39:14 +01:00
|
|
|
let object: any = q;
|
|
|
|
if (q.only_media)
|
2023-02-13 20:17:07 +01:00
|
|
|
if (typeof q.only_media === "string")
|
2023-03-17 14:58:01 +01:00
|
|
|
object.only_media = toBoolean(q.only_media);
|
2023-02-13 19:39:14 +01:00
|
|
|
if (q.exclude_replies)
|
2023-02-13 20:17:07 +01:00
|
|
|
if (typeof q.exclude_replies === "string")
|
2023-03-17 14:58:01 +01:00
|
|
|
object.exclude_replies = toBoolean(q.exclude_replies);
|
2023-02-13 19:39:14 +01:00
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2023-04-30 21:34:52 +02:00
|
|
|
export function convertTimelinesArgsId(q: ParsedUrlQuery) {
|
|
|
|
if (typeof q.min_id === "string")
|
|
|
|
q.min_id = convertId(q.min_id, IdType.CalckeyId);
|
|
|
|
if (typeof q.max_id === "string")
|
|
|
|
q.max_id = convertId(q.max_id, IdType.CalckeyId);
|
|
|
|
if (typeof q.since_id === "string")
|
|
|
|
q.since_id = convertId(q.since_id, IdType.CalckeyId);
|
|
|
|
return q;
|
|
|
|
}
|
|
|
|
|
2023-02-09 23:21:50 +01:00
|
|
|
export function apiTimelineMastodon(router: Router): void {
|
2023-02-11 00:41:19 +01:00
|
|
|
router.get("/v1/timelines/public", async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
|
|
|
const query: any = ctx.query;
|
2023-07-07 19:23:47 +02:00
|
|
|
const data =
|
|
|
|
query.local === "true"
|
|
|
|
? await client.getLocalTimeline(
|
|
|
|
convertTimelinesArgsId(argsToBools(limitToInt(query))),
|
|
|
|
)
|
|
|
|
: await client.getPublicTimeline(
|
|
|
|
convertTimelinesArgsId(argsToBools(limitToInt(query))),
|
|
|
|
);
|
2023-07-06 04:00:44 +02:00
|
|
|
ctx.body = data.data.map((status) => convertStatus(status));
|
2023-02-11 00:41:19 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
router.get<{ Params: { hashtag: string } }>(
|
|
|
|
"/v1/timelines/tag/:hashtag",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
|
|
|
const data = await client.getTagTimeline(
|
|
|
|
ctx.params.hashtag,
|
2023-04-30 21:34:52 +02:00
|
|
|
convertTimelinesArgsId(argsToBools(limitToInt(ctx.query))),
|
2023-02-11 00:41:19 +01:00
|
|
|
);
|
2023-07-06 04:00:44 +02:00
|
|
|
ctx.body = data.data.map((status) => convertStatus(status));
|
2023-02-11 00:41:19 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-03-31 04:10:03 +02:00
|
|
|
router.get("/v1/timelines/home", async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-05-02 05:32:18 +02:00
|
|
|
const data = await client.getHomeTimeline(
|
|
|
|
convertTimelinesArgsId(limitToInt(ctx.query)),
|
|
|
|
);
|
2023-07-06 04:00:44 +02:00
|
|
|
ctx.body = data.data.map((status) => convertStatus(status));
|
2023-03-31 04:10:03 +02:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
2023-02-11 00:41:19 +01:00
|
|
|
router.get<{ Params: { listId: string } }>(
|
|
|
|
"/v1/timelines/list/:listId",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
|
|
|
const data = await client.getListTimeline(
|
2023-04-30 21:34:52 +02:00
|
|
|
convertId(ctx.params.listId, IdType.CalckeyId),
|
|
|
|
convertTimelinesArgsId(limitToInt(ctx.query)),
|
2023-02-11 00:41:19 +01:00
|
|
|
);
|
2023-07-06 04:00:44 +02:00
|
|
|
ctx.body = data.data.map((status) => convertStatus(status));
|
2023-02-11 00:41:19 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.get("/v1/conversations", async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-05-02 05:32:18 +02:00
|
|
|
const data = await client.getConversationTimeline(
|
|
|
|
convertTimelinesArgsId(limitToInt(ctx.query)),
|
|
|
|
);
|
2023-02-11 00:41:19 +01:00
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
router.get("/v1/lists", async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
|
|
|
const data = await client.getLists();
|
2023-05-02 05:32:18 +02:00
|
|
|
ctx.body = data.data.map((list) => convertList(list));
|
2023-02-11 00:41:19 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
router.get<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-04-30 21:34:52 +02:00
|
|
|
const data = await client.getList(
|
|
|
|
convertId(ctx.params.id, IdType.CalckeyId),
|
|
|
|
);
|
|
|
|
ctx.body = convertList(data.data);
|
2023-02-11 00:41:19 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.post("/v1/lists", async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-04-27 22:31:23 +02:00
|
|
|
const data = await client.createList((ctx.request.body as any).title);
|
2023-04-30 21:34:52 +02:00
|
|
|
ctx.body = convertList(data.data);
|
2023-02-11 00:41:19 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
router.put<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-04-30 21:34:52 +02:00
|
|
|
const data = await client.updateList(
|
|
|
|
convertId(ctx.params.id, IdType.CalckeyId),
|
2023-05-02 05:32:18 +02:00
|
|
|
(ctx.request.body as any).title,
|
2023-04-30 21:34:52 +02:00
|
|
|
);
|
|
|
|
ctx.body = convertList(data.data);
|
2023-02-11 00:41:19 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.delete<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-04-30 21:34:52 +02:00
|
|
|
const data = await client.deleteList(
|
|
|
|
convertId(ctx.params.id, IdType.CalckeyId),
|
|
|
|
);
|
2023-02-11 00:41:19 +01:00
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.get<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id/accounts",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
|
|
|
const data = await client.getAccountsInList(
|
2023-04-30 21:34:52 +02:00
|
|
|
convertId(ctx.params.id, IdType.CalckeyId),
|
|
|
|
convertTimelinesArgsId(ctx.query as any),
|
2023-02-11 00:41:19 +01:00
|
|
|
);
|
2023-05-02 05:32:18 +02:00
|
|
|
ctx.body = data.data.map((account) => convertAccount(account));
|
2023-02-11 00:41:19 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.post<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id/accounts",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
|
|
|
const data = await client.addAccountsToList(
|
2023-04-30 21:34:52 +02:00
|
|
|
convertId(ctx.params.id, IdType.CalckeyId),
|
2023-05-02 05:32:18 +02:00
|
|
|
(ctx.query.account_ids as string[]).map((id) =>
|
|
|
|
convertId(id, IdType.CalckeyId),
|
|
|
|
),
|
2023-02-11 00:41:19 +01:00
|
|
|
);
|
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
|
|
|
router.delete<{ Params: { id: string } }>(
|
|
|
|
"/v1/lists/:id/accounts",
|
|
|
|
async (ctx, reply) => {
|
|
|
|
const BASE_URL = `${ctx.protocol}://${ctx.hostname}`;
|
|
|
|
const accessTokens = ctx.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
|
|
|
const data = await client.deleteAccountsFromList(
|
2023-04-30 21:34:52 +02:00
|
|
|
convertId(ctx.params.id, IdType.CalckeyId),
|
2023-05-02 05:32:18 +02:00
|
|
|
(ctx.query.account_ids as string[]).map((id) =>
|
|
|
|
convertId(id, IdType.CalckeyId),
|
|
|
|
),
|
2023-02-11 00:41:19 +01:00
|
|
|
);
|
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
console.error(e.response.data);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-02-09 23:21:50 +01:00
|
|
|
}
|
|
|
|
function escapeHTML(str: string) {
|
2023-02-11 00:41:19 +01:00
|
|
|
if (!str) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
return str
|
|
|
|
.replace(/&/g, "&")
|
|
|
|
.replace(/</g, "<")
|
|
|
|
.replace(/>/g, ">")
|
|
|
|
.replace(/"/g, """)
|
|
|
|
.replace(/'/g, "'");
|
2023-02-09 23:21:50 +01:00
|
|
|
}
|
|
|
|
function nl2br(str: string) {
|
2023-02-11 00:41:19 +01:00
|
|
|
if (!str) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
str = str.replace(/\r\n/g, "<br />");
|
|
|
|
str = str.replace(/(\n|\r)/g, "<br />");
|
|
|
|
return str;
|
2023-02-09 23:21:50 +01:00
|
|
|
}
|