Files
opencode-manager/backend/src/routes/notifications.ts
T
Chris Scott ae644e851d feat: add push notifications with VAPID (#110)
* Add OAuth authentication support for MCP servers

* Improve mobile text sizing with responsive text classes

* refactor: improve virtualized text viewer with React Query infinite scroll and simplified buffering

* Add push notifications with VAPID and PWA support

* feat: add VAPID environment variables for push notifications

* Replace SquareFill with MicOff icon for stop recording buttons

* Send push notifications when app is not focused

* Fix stop generation button icons to use X instead of MicOff

* feat: enhance push notification logging and debugging

- Add structured logging throughout push notification flow
- Improve error handling with detailed diagnostics
- Add /api/notifications/debug endpoint for troubleshooting
- Enhanced test notification response with per-subscription results
- Add service worker logging and better install/activate handling

* Improve VAPID push notification configuration with validation and better defaults

- Add validation warnings for VAPID_SUBJECT format (requires mailto: prefix for iOS/Safari)
- Expose VAPID details via notifications API endpoint
- Update default VAPID_SUBJECT from mailto:admin@opencode.local to empty string
- Enhance VAPID logging with key lengths and subject info
- Improve documentation in .env.example with setup instructions

* Fix notification URL to use repo ID instead of filesystem path

* Clean up push notification logging and debug endpoints

* Add push notifications documentation
2026-02-06 11:23:10 -05:00

100 lines
2.8 KiB
TypeScript

import { Hono } from "hono";
import { z } from "zod";
import { PushSubscriptionRequestSchema } from "@opencode-manager/shared/schemas";
import type { NotificationService } from "../services/notification";
export function createNotificationRoutes(
notificationService: NotificationService
) {
const app = new Hono();
app.get("/vapid-public-key", (c) => {
const publicKey = notificationService.getVapidPublicKey();
if (!publicKey) {
return c.json(
{ error: "Push notifications are not configured" },
503
);
}
return c.json({ publicKey });
});
app.post("/subscribe", async (c) => {
const userId = c.req.query('userId') || 'default'
const body = await c.req.json();
const parsed = PushSubscriptionRequestSchema.safeParse(body);
if (!parsed.success) {
return c.json({ error: "Invalid subscription data", details: parsed.error.issues }, 400);
}
const { endpoint, keys, deviceName } = parsed.data;
const subscription = notificationService.saveSubscription(
userId,
endpoint,
keys.p256dh,
keys.auth,
deviceName
);
return c.json({ subscription });
});
app.delete("/subscribe", async (c) => {
const userId = c.req.query('userId') || 'default'
const body = await c.req.json();
const parsed = z.object({ endpoint: z.string().url() }).safeParse(body);
if (!parsed.success) {
return c.json({ error: "Valid endpoint URL is required", details: parsed.error.issues }, 400);
}
const removed = notificationService.removeSubscription(parsed.data.endpoint, userId);
return c.json({ success: removed });
});
app.get("/subscriptions", (c) => {
const userId = c.req.query('userId') || 'default'
const subscriptions = notificationService.getSubscriptions(userId);
return c.json({ subscriptions });
});
app.delete("/subscriptions/:id", (c) => {
const userId = c.req.query('userId') || 'default'
const id = parseInt(c.req.param("id"), 10);
if (isNaN(id)) {
return c.json({ error: "Invalid subscription ID" }, 400);
}
const removed = notificationService.removeSubscriptionById(id, userId);
return c.json({ success: removed });
});
app.post("/test", async (c) => {
const userId = c.req.query('userId') || 'default'
if (!notificationService.isConfigured()) {
return c.json(
{ error: "Push notifications are not configured" },
503
);
}
const subscriptions = notificationService.getSubscriptions(userId);
if (subscriptions.length === 0) {
return c.json(
{ error: "No push subscriptions registered" },
404
);
}
await notificationService.sendTestNotification(userId);
return c.json({ success: true, devicesNotified: subscriptions.length });
});
return app;
}