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";
|
2023-10-04 23:40:16 +02:00
|
|
|
import { MiscHelpers } from "@/server/api/mastodon/helpers/misc.js";
|
2023-10-05 00:44:33 +02:00
|
|
|
import authenticate from "@/server/api/authenticate.js";
|
|
|
|
import { argsToBools } from "@/server/api/mastodon/endpoints/timeline.js";
|
|
|
|
import { Announcements } from "@/models/index.js";
|
|
|
|
import { convertAnnouncementId } from "@/server/api/mastodon/converters.js";
|
|
|
|
import { convertId, IdType } from "@/misc/convert-id.js";
|
2023-02-07 22:56:07 +01:00
|
|
|
|
2023-09-29 21:45:58 +02:00
|
|
|
export function setupEndpointsMisc(router: Router): void {
|
2023-10-01 17:52:59 +02:00
|
|
|
router.get("/v1/custom_emojis", 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.getInstanceCustomEmojis();
|
|
|
|
ctx.body = data.data;
|
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
|
|
|
ctx.status = 401;
|
|
|
|
ctx.body = e.response.data;
|
|
|
|
}
|
|
|
|
});
|
2023-02-07 22:56:07 +01:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
router.get("/v1/instance", async (ctx) => {
|
|
|
|
try {
|
2023-10-04 23:40:16 +02:00
|
|
|
ctx.body = await MiscHelpers.getInstance();
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
|
|
|
console.error(e);
|
2023-10-04 23:40:16 +02:00
|
|
|
ctx.status = 500;
|
|
|
|
ctx.body = { error: e.message };
|
2023-10-01 17:52:59 +02:00
|
|
|
}
|
|
|
|
});
|
2023-03-22 17:43:05 +01:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
router.get("/v1/announcements", async (ctx) => {
|
|
|
|
try {
|
2023-10-05 00:44:33 +02:00
|
|
|
const auth = await authenticate(ctx.headers.authorization, null);
|
|
|
|
const user = auth[0] ?? null;
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
ctx.status = 401;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const args = argsToBools(ctx.query, ['with_dismissed']);
|
|
|
|
ctx.body = await MiscHelpers.getAnnouncements(user, args['with_dismissed'])
|
|
|
|
.then(p => p.map(x => convertAnnouncementId(x)));
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
2023-10-05 00:44:33 +02:00
|
|
|
ctx.status = 500;
|
|
|
|
ctx.body = { error: e.message };
|
2023-10-01 17:52:59 +02:00
|
|
|
}
|
|
|
|
});
|
2023-04-23 22:17:02 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
router.post<{ Params: { id: string } }>(
|
|
|
|
"/v1/announcements/:id/dismiss",
|
|
|
|
async (ctx) => {
|
|
|
|
try {
|
2023-10-05 00:44:33 +02:00
|
|
|
const auth = await authenticate(ctx.headers.authorization, null);
|
|
|
|
const user = auth[0] ?? null;
|
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
ctx.status = 401;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const id = convertId(ctx.params.id, IdType.IceshrimpId);
|
|
|
|
const announcement = await Announcements.findOneBy({id: id});
|
|
|
|
|
|
|
|
if (!announcement) {
|
|
|
|
ctx.status = 404;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
await MiscHelpers.dismissAnnouncement(announcement, user);
|
|
|
|
ctx.body = {};
|
2023-10-01 17:52:59 +02:00
|
|
|
} catch (e: any) {
|
2023-10-05 00:44:33 +02:00
|
|
|
ctx.status = 500;
|
|
|
|
ctx.body = { error: e.message };
|
2023-10-01 17:52:59 +02:00
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-04-23 22:17:02 +02:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
});
|
2023-03-22 17:43:05 +01:00
|
|
|
|
2023-10-01 17:52:59 +02:00
|
|
|
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
|
|
|
}
|