2025-01-02 02:55:56 +01:00
|
|
|
import { apiRoute, auth } from "@/api";
|
|
|
|
|
import { createRoute } from "@hono/zod-openapi";
|
2025-01-02 01:29:33 +01:00
|
|
|
import { PushSubscription } from "@versia/kit/db";
|
|
|
|
|
import { ApiError } from "~/classes/errors/api-error";
|
2025-02-12 23:25:22 +01:00
|
|
|
import { WebPushSubscription as WebPushSubscriptionSchema } from "~/classes/schemas/pushsubscription";
|
2025-01-02 02:55:56 +01:00
|
|
|
import { RolePermissions } from "~/drizzle/schema";
|
2025-01-02 01:29:33 +01:00
|
|
|
|
|
|
|
|
export default apiRoute((app) =>
|
2025-01-02 02:55:56 +01:00
|
|
|
app.openapi(
|
|
|
|
|
createRoute({
|
|
|
|
|
method: "get",
|
|
|
|
|
path: "/api/v1/push/subscription",
|
|
|
|
|
summary: "Get current subscription",
|
|
|
|
|
description:
|
|
|
|
|
"View the PushSubscription currently associated with this access token.",
|
|
|
|
|
externalDocs: {
|
|
|
|
|
url: "https://docs.joinmastodon.org/methods/push/#get",
|
|
|
|
|
},
|
|
|
|
|
middleware: [
|
|
|
|
|
auth({
|
|
|
|
|
auth: true,
|
|
|
|
|
permissions: [RolePermissions.UsePushNotifications],
|
|
|
|
|
scopes: ["push"],
|
|
|
|
|
}),
|
|
|
|
|
] as const,
|
|
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "WebPushSubscription",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2025-02-12 23:25:22 +01:00
|
|
|
schema: WebPushSubscriptionSchema,
|
2025-01-02 02:55:56 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
async (context) => {
|
|
|
|
|
const { token } = context.get("auth");
|
2025-01-02 01:29:33 +01:00
|
|
|
|
2025-01-02 02:55:56 +01:00
|
|
|
const ps = await PushSubscription.fromToken(token);
|
2025-01-02 01:29:33 +01:00
|
|
|
|
2025-01-02 02:55:56 +01:00
|
|
|
if (!ps) {
|
|
|
|
|
throw new ApiError(
|
|
|
|
|
404,
|
|
|
|
|
"No push subscription associated with this access token",
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-01-02 01:29:33 +01:00
|
|
|
|
2025-01-02 02:55:56 +01:00
|
|
|
return context.json(ps.toApi(), 200);
|
|
|
|
|
},
|
|
|
|
|
),
|
2025-01-02 01:29:33 +01:00
|
|
|
);
|