2025-05-01 16:27:34 +02:00
|
|
|
import {
|
|
|
|
|
RolePermission,
|
|
|
|
|
WebPushSubscription as WebPushSubscriptionSchema,
|
|
|
|
|
} from "@versia/client/schemas";
|
2025-06-15 23:50:34 +02:00
|
|
|
import { ApiError } from "@versia-server/kit";
|
|
|
|
|
import { apiRoute, auth } from "@versia-server/kit/api";
|
|
|
|
|
import { PushSubscription } from "@versia-server/kit/db";
|
2025-03-29 03:30:06 +01:00
|
|
|
import { describeRoute } from "hono-openapi";
|
|
|
|
|
import { resolver } from "hono-openapi/zod";
|
2025-01-02 01:29:33 +01:00
|
|
|
|
|
|
|
|
export default apiRoute((app) =>
|
2025-03-29 03:30:06 +01:00
|
|
|
app.get(
|
|
|
|
|
"/api/v1/push/subscription",
|
|
|
|
|
describeRoute({
|
2025-01-02 02:55:56 +01:00
|
|
|
summary: "Get current subscription",
|
|
|
|
|
description:
|
|
|
|
|
"View the PushSubscription currently associated with this access token.",
|
|
|
|
|
externalDocs: {
|
|
|
|
|
url: "https://docs.joinmastodon.org/methods/push/#get",
|
|
|
|
|
},
|
2025-02-14 16:44:32 +01:00
|
|
|
tags: ["Push Notifications"],
|
2025-01-02 02:55:56 +01:00
|
|
|
responses: {
|
|
|
|
|
200: {
|
|
|
|
|
description: "WebPushSubscription",
|
|
|
|
|
content: {
|
|
|
|
|
"application/json": {
|
2025-03-29 03:30:06 +01:00
|
|
|
schema: resolver(WebPushSubscriptionSchema),
|
2025-01-02 02:55:56 +01:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
2025-03-24 14:42:09 +01:00
|
|
|
401: ApiError.missingAuthentication().schema,
|
|
|
|
|
422: ApiError.validationFailed().schema,
|
2025-01-02 02:55:56 +01:00
|
|
|
},
|
|
|
|
|
}),
|
2025-03-29 03:30:06 +01:00
|
|
|
auth({
|
|
|
|
|
auth: true,
|
|
|
|
|
permissions: [RolePermission.UsePushNotifications],
|
|
|
|
|
scopes: ["push"],
|
|
|
|
|
}),
|
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) {
|
2025-03-24 14:42:09 +01:00
|
|
|
throw ApiError.pushSubscriptionNotFound();
|
2025-01-02 02:55:56 +01:00
|
|
|
}
|
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
|
|
|
);
|