fix(api): 👽 Ignore what the Mastodon docs say, they're wrong

This commit is contained in:
Jesse Wierzbinski 2025-01-02 04:13:12 +01:00
parent 5d64ecd04f
commit cde2836982
No known key found for this signature in database
5 changed files with 35 additions and 27 deletions

View file

@ -47,7 +47,7 @@ export default apiRoute((app) =>
}), }),
async (context) => { async (context) => {
const { user, token } = context.get("auth"); const { user, token } = context.get("auth");
const { subscription, data } = context.req.valid("json"); const { subscription, data, policy } = context.req.valid("json");
if ( if (
data.alerts["admin.report"] && data.alerts["admin.report"] &&
@ -68,7 +68,7 @@ export default apiRoute((app) =>
const ps = await PushSubscription.insert({ const ps = await PushSubscription.insert({
alerts: data.alerts, alerts: data.alerts,
policy: data.policy, policy,
endpoint: subscription.endpoint, endpoint: subscription.endpoint,
publicKey: subscription.keys.p256dh, publicKey: subscription.keys.p256dh,
authSecret: subscription.keys.auth, authSecret: subscription.keys.auth,

View file

@ -28,7 +28,10 @@ export default apiRoute((app) =>
body: { body: {
content: { content: {
"application/json": { "application/json": {
schema: WebPushSubscriptionInput.shape.data, schema: WebPushSubscriptionInput.pick({
data: true,
policy: true,
}),
}, },
}, },
}, },
@ -46,7 +49,7 @@ export default apiRoute((app) =>
}), }),
async (context) => { async (context) => {
const { user, token } = context.get("auth"); const { user, token } = context.get("auth");
const { alerts, policy } = context.req.valid("json"); const { data, policy } = context.req.valid("json");
const ps = await PushSubscription.fromToken(token); const ps = await PushSubscription.fromToken(token);
@ -58,25 +61,25 @@ export default apiRoute((app) =>
} }
if ( if (
alerts["admin.report"] && data.alerts["admin.report"] &&
!user.hasPermission(RolePermissions.ManageReports) !user.hasPermission(RolePermissions.ManageReports)
) { ) {
// This shouldn't throw an error in mastodon either // This shouldn't throw an error in mastodon either
alerts["admin.report"] = false; data.alerts["admin.report"] = false;
} }
if ( if (
alerts["admin.sign_up"] && data.alerts["admin.sign_up"] &&
!user.hasPermission(RolePermissions.ManageAccounts) !user.hasPermission(RolePermissions.ManageAccounts)
) { ) {
alerts["admin.sign_up"] = false; data.alerts["admin.sign_up"] = false;
} }
await ps.update({ await ps.update({
policy, policy,
alerts: { alerts: {
...ps.data.alerts, ...ps.data.alerts,
...alerts, ...data.alerts,
}, },
}); });

View file

@ -26,8 +26,8 @@ describe("/api/v1/push/subscriptions", () => {
alerts: { alerts: {
update: true, update: true,
}, },
policy: "all",
}, },
policy: "all",
subscription: { subscription: {
endpoint: "https://example.com", endpoint: "https://example.com",
keys: { keys: {
@ -117,9 +117,11 @@ describe("/api/v1/push/subscriptions", () => {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
alerts: { data: {
update: false, alerts: {
favourite: true, update: false,
favourite: true,
},
}, },
policy: "follower", policy: "follower",
}), }),
@ -147,8 +149,8 @@ describe("/api/v1/push/subscriptions", () => {
alerts: { alerts: {
"admin.report": true, "admin.report": true,
}, },
policy: "all",
}, },
policy: "all",
subscription: { subscription: {
endpoint: "https://example.com", endpoint: "https://example.com",
keys: { keys: {
@ -183,8 +185,8 @@ describe("/api/v1/push/subscriptions", () => {
alerts: { alerts: {
"admin.report": true, "admin.report": true,
}, },
policy: "all",
}, },
policy: "all",
subscription: { subscription: {
endpoint: "https://example.com", endpoint: "https://example.com",
keys: { keys: {
@ -230,8 +232,10 @@ describe("/api/v1/push/subscriptions", () => {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
alerts: { data: {
"admin.report": true, alerts: {
"admin.report": true,
},
}, },
policy: "all", policy: "all",
}), }),
@ -277,8 +281,10 @@ describe("/api/v1/push/subscriptions", () => {
"Content-Type": "application/json", "Content-Type": "application/json",
}, },
body: JSON.stringify({ body: JSON.stringify({
alerts: { data: {
"admin.report": true, alerts: {
"admin.report": true,
},
}, },
policy: "all", policy: "all",
}), }),

View file

@ -24,20 +24,19 @@ export const WebPushSubscriptionInput = z
}) })
.strict(), .strict(),
}), }),
policy: z
.enum(["all", "followed", "follower", "none"])
.default("all")
.openapi({
description:
"Specify whether to receive push notifications from all, followed, follower, or none users.",
}),
data: z data: z
.object({ .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, alerts: PushSubscription.schema.shape.alerts,
}) })
.strict() .strict()
.default({ .default({
policy: "all",
alerts: { alerts: {
mention: false, mention: false,
favourite: false, favourite: false,