mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
fix(api): 🚑 Fix incorrect builds
Everything under api/ should be a route, or it messes up bundling
This commit is contained in:
parent
d839c274b1
commit
7b3158c102
8 changed files with 246 additions and 252 deletions
|
|
@ -1,31 +0,0 @@
|
|||
import { auth } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
||||
export const route = createRoute({
|
||||
method: "delete",
|
||||
path: "/api/v1/push/subscription",
|
||||
summary: "Remove current subscription",
|
||||
description: "Removes the current Web Push API subscription.",
|
||||
externalDocs: {
|
||||
url: "https://docs.joinmastodon.org/methods/push/#delete",
|
||||
},
|
||||
middleware: [
|
||||
auth({
|
||||
auth: true,
|
||||
permissions: [RolePermissions.UsePushNotifications],
|
||||
scopes: ["push"],
|
||||
}),
|
||||
] as const,
|
||||
responses: {
|
||||
200: {
|
||||
description:
|
||||
"PushSubscription successfully deleted or did not exist previously.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
@ -1,23 +1,53 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { apiRoute, auth } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { PushSubscription } from "@versia/kit/db";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { route } from "./index.delete.schema";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const { token } = context.get("auth");
|
||||
app.openapi(
|
||||
createRoute({
|
||||
method: "delete",
|
||||
path: "/api/v1/push/subscription",
|
||||
summary: "Remove current subscription",
|
||||
description: "Removes the current Web Push API subscription.",
|
||||
externalDocs: {
|
||||
url: "https://docs.joinmastodon.org/methods/push/#delete",
|
||||
},
|
||||
middleware: [
|
||||
auth({
|
||||
auth: true,
|
||||
permissions: [RolePermissions.UsePushNotifications],
|
||||
scopes: ["push"],
|
||||
}),
|
||||
] as const,
|
||||
responses: {
|
||||
200: {
|
||||
description:
|
||||
"PushSubscription successfully deleted or did not exist previously.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: z.object({}),
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
async (context) => {
|
||||
const { token } = context.get("auth");
|
||||
|
||||
const ps = await PushSubscription.fromToken(token);
|
||||
const ps = await PushSubscription.fromToken(token);
|
||||
|
||||
if (!ps) {
|
||||
throw new ApiError(
|
||||
404,
|
||||
"No push subscription associated with this access token",
|
||||
);
|
||||
}
|
||||
if (!ps) {
|
||||
throw new ApiError(
|
||||
404,
|
||||
"No push subscription associated with this access token",
|
||||
);
|
||||
}
|
||||
|
||||
await ps.delete();
|
||||
await ps.delete();
|
||||
|
||||
return context.json({}, 200);
|
||||
}),
|
||||
return context.json({}, 200);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,85 +0,0 @@
|
|||
import { auth } from "@/api";
|
||||
import { createRoute, z } from "@hono/zod-openapi";
|
||||
import { PushSubscription } from "@versia/kit/db";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
||||
export const WebPushSubscriptionInput = z
|
||||
.object({
|
||||
subscription: z.object({
|
||||
endpoint: z.string().url().openapi({
|
||||
example: "https://yourdomain.example/listener",
|
||||
description: "Where push alerts will be sent to.",
|
||||
}),
|
||||
keys: z
|
||||
.object({
|
||||
p256dh: z.string().base64().openapi({
|
||||
description:
|
||||
"User agent public key. Base64 encoded string of a public key from a ECDH keypair using the prime256v1 curve.",
|
||||
example:
|
||||
"MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAEoKCJeHCy69ywHcb3dAR/T8Sud5ljSFHJkuiR6it1ycqAjGTe5F1oZ0ef5QiMX/zdQ+d4jSKiO7RztIz+o/eGuQ==",
|
||||
}),
|
||||
auth: z.string().base64().length(24).openapi({
|
||||
description:
|
||||
"Auth secret. Base64 encoded string of 16 bytes of random data.",
|
||||
example: "u67u09PXZW4ncK9l9mAXkA==",
|
||||
}),
|
||||
})
|
||||
.strict(),
|
||||
}),
|
||||
data: z
|
||||
.object({
|
||||
policy: z
|
||||
.enum(["all", "followed", "follower", "none"])
|
||||
.default("all")
|
||||
.openapi({
|
||||
description:
|
||||
"Specify whether to receive push notifications from all, followed, follower, or none users.",
|
||||
}),
|
||||
alerts: PushSubscription.schema.shape.alerts,
|
||||
})
|
||||
.strict()
|
||||
.default({
|
||||
policy: "all",
|
||||
alerts: {
|
||||
mention: false,
|
||||
favourite: false,
|
||||
reblog: false,
|
||||
follow: false,
|
||||
poll: false,
|
||||
follow_request: false,
|
||||
status: false,
|
||||
update: false,
|
||||
"admin.sign_up": false,
|
||||
"admin.report": false,
|
||||
},
|
||||
}),
|
||||
})
|
||||
.strict();
|
||||
|
||||
export const route = 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": {
|
||||
schema: PushSubscription.schema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
@ -1,21 +1,51 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { apiRoute, auth } from "@/api";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { PushSubscription } from "@versia/kit/db";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { route } from "./index.get.schema";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const { token } = context.get("auth");
|
||||
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": {
|
||||
schema: PushSubscription.schema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
async (context) => {
|
||||
const { token } = context.get("auth");
|
||||
|
||||
const ps = await PushSubscription.fromToken(token);
|
||||
const ps = await PushSubscription.fromToken(token);
|
||||
|
||||
if (!ps) {
|
||||
throw new ApiError(
|
||||
404,
|
||||
"No push subscription associated with this access token",
|
||||
);
|
||||
}
|
||||
if (!ps) {
|
||||
throw new ApiError(
|
||||
404,
|
||||
"No push subscription associated with this access token",
|
||||
);
|
||||
}
|
||||
|
||||
return context.json(ps.toApi(), 200);
|
||||
}),
|
||||
return context.json(ps.toApi(), 200);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,44 +0,0 @@
|
|||
import { auth, jsonOrForm } from "@/api";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { PushSubscription } from "@versia/kit/db";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { WebPushSubscriptionInput } from "./index.get.schema";
|
||||
|
||||
export const route = createRoute({
|
||||
method: "post",
|
||||
path: "/api/v1/push/subscription",
|
||||
summary: "Subscribe to push notifications",
|
||||
description:
|
||||
"Add a Web Push API subscription to receive notifications. Each access token can have one push subscription. If you create a new subscription, the old subscription is deleted.",
|
||||
externalDocs: {
|
||||
url: "https://docs.joinmastodon.org/methods/push/#create",
|
||||
},
|
||||
middleware: [
|
||||
auth({
|
||||
auth: true,
|
||||
permissions: [RolePermissions.UsePushNotifications],
|
||||
scopes: ["push"],
|
||||
}),
|
||||
jsonOrForm(),
|
||||
] as const,
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: WebPushSubscriptionInput,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description:
|
||||
"A new PushSubscription has been generated, which will send the requested alerts to your endpoint.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: PushSubscription.schema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
@ -1,45 +1,87 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { auth, jsonOrForm } from "@/api";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { PushSubscription } from "@versia/kit/db";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { WebPushSubscriptionInput } from "~/classes/schemas/pushsubscription";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { route } from "./index.post.schema";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const { user, token } = context.get("auth");
|
||||
const { subscription, data } = context.req.valid("json");
|
||||
app.openapi(
|
||||
createRoute({
|
||||
method: "post",
|
||||
path: "/api/v1/push/subscription",
|
||||
summary: "Subscribe to push notifications",
|
||||
description:
|
||||
"Add a Web Push API subscription to receive notifications. Each access token can have one push subscription. If you create a new subscription, the old subscription is deleted.",
|
||||
externalDocs: {
|
||||
url: "https://docs.joinmastodon.org/methods/push/#create",
|
||||
},
|
||||
middleware: [
|
||||
auth({
|
||||
auth: true,
|
||||
permissions: [RolePermissions.UsePushNotifications],
|
||||
scopes: ["push"],
|
||||
}),
|
||||
jsonOrForm(),
|
||||
] as const,
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: WebPushSubscriptionInput,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description:
|
||||
"A new PushSubscription has been generated, which will send the requested alerts to your endpoint.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: PushSubscription.schema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
async (context) => {
|
||||
const { user, token } = context.get("auth");
|
||||
const { subscription, data } = context.req.valid("json");
|
||||
|
||||
if (
|
||||
data.alerts["admin.report"] &&
|
||||
!user.hasPermission(RolePermissions.ManageReports)
|
||||
) {
|
||||
throw new ApiError(
|
||||
403,
|
||||
`You do not have the '${RolePermissions.ManageReports}' permission to receive report alerts`,
|
||||
);
|
||||
}
|
||||
if (
|
||||
data.alerts["admin.report"] &&
|
||||
!user.hasPermission(RolePermissions.ManageReports)
|
||||
) {
|
||||
throw new ApiError(
|
||||
403,
|
||||
`You do not have the '${RolePermissions.ManageReports}' permission to receive report alerts`,
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
data.alerts["admin.sign_up"] &&
|
||||
!user.hasPermission(RolePermissions.ManageAccounts)
|
||||
) {
|
||||
throw new ApiError(
|
||||
403,
|
||||
`You do not have the '${RolePermissions.ManageAccounts}' permission to receive sign-up alerts`,
|
||||
);
|
||||
}
|
||||
if (
|
||||
data.alerts["admin.sign_up"] &&
|
||||
!user.hasPermission(RolePermissions.ManageAccounts)
|
||||
) {
|
||||
throw new ApiError(
|
||||
403,
|
||||
`You do not have the '${RolePermissions.ManageAccounts}' permission to receive sign-up alerts`,
|
||||
);
|
||||
}
|
||||
|
||||
await PushSubscription.clearAllOfToken(token);
|
||||
await PushSubscription.clearAllOfToken(token);
|
||||
|
||||
const ps = await PushSubscription.insert({
|
||||
alerts: data.alerts,
|
||||
policy: data.policy,
|
||||
endpoint: subscription.endpoint,
|
||||
publicKey: subscription.keys.p256dh,
|
||||
authSecret: subscription.keys.auth,
|
||||
tokenId: token.id,
|
||||
});
|
||||
const ps = await PushSubscription.insert({
|
||||
alerts: data.alerts,
|
||||
policy: data.policy,
|
||||
endpoint: subscription.endpoint,
|
||||
publicKey: subscription.keys.p256dh,
|
||||
authSecret: subscription.keys.auth,
|
||||
tokenId: token.id,
|
||||
});
|
||||
|
||||
return context.json(ps.toApi(), 200);
|
||||
}),
|
||||
return context.json(ps.toApi(), 200);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,43 +0,0 @@
|
|||
import { auth, jsonOrForm } from "@/api";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { PushSubscription } from "@versia/kit/db";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { WebPushSubscriptionInput } from "./index.get.schema";
|
||||
|
||||
export const route = createRoute({
|
||||
method: "put",
|
||||
path: "/api/v1/push/subscription",
|
||||
summary: "Change types of notifications",
|
||||
description:
|
||||
"Updates the current push subscription. Only the data part can be updated. To change fundamentals, a new subscription must be created instead.",
|
||||
externalDocs: {
|
||||
url: "https://docs.joinmastodon.org/methods/push/#update",
|
||||
},
|
||||
middleware: [
|
||||
auth({
|
||||
auth: true,
|
||||
permissions: [RolePermissions.UsePushNotifications],
|
||||
scopes: ["push"],
|
||||
}),
|
||||
jsonOrForm(),
|
||||
] as const,
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: WebPushSubscriptionInput.shape.data,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "The WebPushSubscription has been updated.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: PushSubscription.schema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
|
@ -1,51 +1,91 @@
|
|||
import { apiRoute } from "@/api";
|
||||
import { apiRoute, auth, jsonOrForm } from "@/api";
|
||||
import { createRoute } from "@hono/zod-openapi";
|
||||
import { PushSubscription } from "@versia/kit/db";
|
||||
import { ApiError } from "~/classes/errors/api-error";
|
||||
import { WebPushSubscriptionInput } from "~/classes/schemas/pushsubscription";
|
||||
import { RolePermissions } from "~/drizzle/schema";
|
||||
import { route } from "./index.put.schema";
|
||||
|
||||
export default apiRoute((app) =>
|
||||
app.openapi(route, async (context) => {
|
||||
const { user, token } = context.get("auth");
|
||||
const { alerts, policy } = context.req.valid("json");
|
||||
|
||||
const ps = await PushSubscription.fromToken(token);
|
||||
|
||||
if (!ps) {
|
||||
throw new ApiError(
|
||||
404,
|
||||
"No push subscription associated with this access token",
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
alerts["admin.report"] &&
|
||||
!user.hasPermission(RolePermissions.ManageReports)
|
||||
) {
|
||||
throw new ApiError(
|
||||
403,
|
||||
`You do not have the '${RolePermissions.ManageReports}' permission to receive report alerts`,
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
alerts["admin.sign_up"] &&
|
||||
!user.hasPermission(RolePermissions.ManageAccounts)
|
||||
) {
|
||||
throw new ApiError(
|
||||
403,
|
||||
`You do not have the '${RolePermissions.ManageAccounts}' permission to receive sign-up alerts`,
|
||||
);
|
||||
}
|
||||
|
||||
await ps.update({
|
||||
policy,
|
||||
alerts: {
|
||||
...ps.data.alerts,
|
||||
...alerts,
|
||||
app.openapi(
|
||||
createRoute({
|
||||
method: "put",
|
||||
path: "/api/v1/push/subscription",
|
||||
summary: "Change types of notifications",
|
||||
description:
|
||||
"Updates the current push subscription. Only the data part can be updated. To change fundamentals, a new subscription must be created instead.",
|
||||
externalDocs: {
|
||||
url: "https://docs.joinmastodon.org/methods/push/#update",
|
||||
},
|
||||
});
|
||||
middleware: [
|
||||
auth({
|
||||
auth: true,
|
||||
permissions: [RolePermissions.UsePushNotifications],
|
||||
scopes: ["push"],
|
||||
}),
|
||||
jsonOrForm(),
|
||||
] as const,
|
||||
request: {
|
||||
body: {
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: WebPushSubscriptionInput.shape.data,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
responses: {
|
||||
200: {
|
||||
description: "The WebPushSubscription has been updated.",
|
||||
content: {
|
||||
"application/json": {
|
||||
schema: PushSubscription.schema,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}),
|
||||
async (context) => {
|
||||
const { user, token } = context.get("auth");
|
||||
const { alerts, policy } = context.req.valid("json");
|
||||
|
||||
return context.json(ps.toApi(), 200);
|
||||
}),
|
||||
const ps = await PushSubscription.fromToken(token);
|
||||
|
||||
if (!ps) {
|
||||
throw new ApiError(
|
||||
404,
|
||||
"No push subscription associated with this access token",
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
alerts["admin.report"] &&
|
||||
!user.hasPermission(RolePermissions.ManageReports)
|
||||
) {
|
||||
throw new ApiError(
|
||||
403,
|
||||
`You do not have the '${RolePermissions.ManageReports}' permission to receive report alerts`,
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
alerts["admin.sign_up"] &&
|
||||
!user.hasPermission(RolePermissions.ManageAccounts)
|
||||
) {
|
||||
throw new ApiError(
|
||||
403,
|
||||
`You do not have the '${RolePermissions.ManageAccounts}' permission to receive sign-up alerts`,
|
||||
);
|
||||
}
|
||||
|
||||
await ps.update({
|
||||
policy,
|
||||
alerts: {
|
||||
...ps.data.alerts,
|
||||
...alerts,
|
||||
},
|
||||
});
|
||||
|
||||
return context.json(ps.toApi(), 200);
|
||||
},
|
||||
),
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue