2023-02-07 22:56:07 +01:00
|
|
|
import Router from "@koa/router";
|
2023-09-29 21:45:58 +02:00
|
|
|
import { getClient } from "@/server/api/mastodon/index.js";
|
|
|
|
import { convertId, IdType } from "@/misc/convert-id.js";
|
2023-07-27 00:44:38 +02:00
|
|
|
import {
|
|
|
|
convertAccount,
|
|
|
|
convertAnnouncement,
|
2023-09-29 21:45:58 +02:00
|
|
|
convertAttachment,
|
|
|
|
convertFilter
|
|
|
|
} from "@/server/api/mastodon/converters.js";
|
2023-07-22 20:15:58 +02:00
|
|
|
import { Users } from "@/models/index.js";
|
2023-09-29 21:45:58 +02:00
|
|
|
import { getInstance } from "@/server/api/mastodon/endpoints/meta.js";
|
2023-07-22 20:15:58 +02:00
|
|
|
import { IsNull } from "typeorm";
|
2023-02-07 22:56:07 +01:00
|
|
|
|
2023-09-29 21:45:58 +02:00
|
|
|
export function setupEndpointsMisc(router: Router): void {
|
2023-02-11 00:41:19 +01:00
|
|
|
router.get("/v1/custom_emojis", async (ctx) => {
|
2023-02-07 22:56:07 +01:00
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.request.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-02-09 23:21:50 +01:00
|
|
|
const data = await client.getInstanceCustomEmojis();
|
2023-02-07 22:56:07 +01:00
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
2023-02-11 00:41:19 +01:00
|
|
|
console.error(e);
|
2023-02-07 23:05:26 +01:00
|
|
|
ctx.status = 401;
|
2023-02-09 23:21:50 +01:00
|
|
|
ctx.body = e.response.data;
|
2023-02-07 22:56:07 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-02-11 00:41:19 +01:00
|
|
|
router.get("/v1/instance", async (ctx) => {
|
2023-02-07 23:05:26 +01:00
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.request.headers.authorization;
|
2023-02-11 00:41:19 +01:00
|
|
|
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
|
|
|
|
// displayed without being logged in
|
2023-02-07 22:56:07 +01:00
|
|
|
try {
|
2023-02-09 23:21:50 +01:00
|
|
|
const data = await client.getInstance();
|
2023-07-22 20:15:58 +02:00
|
|
|
const admin = await Users.findOne({
|
2023-07-27 00:44:38 +02:00
|
|
|
where: {
|
|
|
|
host: IsNull(),
|
|
|
|
isAdmin: true,
|
|
|
|
isDeleted: false,
|
|
|
|
isSuspended: false,
|
|
|
|
},
|
|
|
|
order: { id: "ASC" },
|
|
|
|
});
|
|
|
|
const contact =
|
|
|
|
admin == null
|
|
|
|
? null
|
|
|
|
: convertAccount((await client.getAccount(admin.id)).data);
|
2023-07-22 20:15:58 +02:00
|
|
|
ctx.body = await getInstance(data.data, contact);
|
2023-02-07 22:56:07 +01:00
|
|
|
} catch (e: any) {
|
2023-02-11 00:41:19 +01:00
|
|
|
console.error(e);
|
2023-02-07 23:05:26 +01:00
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
2023-02-07 22:56:07 +01:00
|
|
|
}
|
|
|
|
});
|
2023-03-22 17:43:05 +01:00
|
|
|
|
2023-04-23 22:17:02 +02:00
|
|
|
router.get("/v1/announcements", async (ctx) => {
|
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.request.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
|
|
|
const data = await client.getInstanceAnnouncements();
|
2023-05-02 05:32:18 +02:00
|
|
|
ctx.body = data.data.map((announcement) =>
|
|
|
|
convertAnnouncement(announcement),
|
|
|
|
);
|
2023-04-23 22:17:02 +02:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-04-25 00:45:34 +02:00
|
|
|
router.post<{ Params: { id: string } }>(
|
|
|
|
"/v1/announcements/:id/dismiss",
|
|
|
|
async (ctx) => {
|
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.request.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens);
|
|
|
|
try {
|
2023-04-30 21:34:52 +02:00
|
|
|
const data = await client.dismissInstanceAnnouncement(
|
2023-08-21 16:14:53 +02:00
|
|
|
convertId(ctx.params.id, IdType.IceshrimpId),
|
2023-04-30 21:34:52 +02:00
|
|
|
);
|
2023-04-25 00:45:34 +02:00
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-04-23 22:17:02 +02:00
|
|
|
|
2023-03-22 17:43:05 +01:00
|
|
|
router.get("/v1/filters", async (ctx) => {
|
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.request.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
|
|
|
|
// displayed without being logged in
|
|
|
|
try {
|
|
|
|
const data = await client.getFilters();
|
2023-05-02 05:32:18 +02:00
|
|
|
ctx.body = data.data.map((filter) => convertFilter(filter));
|
2023-03-22 17:43:05 +01:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get("/v1/trends", async (ctx) => {
|
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.request.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
|
|
|
|
// displayed without being logged in
|
|
|
|
try {
|
|
|
|
const data = await client.getInstanceTrends();
|
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
router.get("/v1/preferences", async (ctx) => {
|
|
|
|
const BASE_URL = `${ctx.request.protocol}://${ctx.request.hostname}`;
|
|
|
|
const accessTokens = ctx.request.headers.authorization;
|
|
|
|
const client = getClient(BASE_URL, accessTokens); // we are using this here, because in private mode some info isnt
|
|
|
|
// displayed without being logged in
|
|
|
|
try {
|
|
|
|
const data = await client.getPreferences();
|
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
2023-02-11 00:41:19 +01:00
|
|
|
}
|