Files
sofa/app/api/settings/webhooks/regenerate-token/route.ts
T
jakeandClaude Opus 4.6 67356e04c6 Add Plex and Jellyfin webhook integration for automatic watch tracking
When a user finishes watching on their media server, a webhook fires and
Sofa logs it as watched, triggering all existing auto-transitions. Each
user configures their connection in settings and gets a unique webhook URL.

- Add webhookConnections and webhookEventLog schema tables
- Add TMDB findByExternalId for resolving IMDB/TVDB IDs
- Add source parameter to tracking functions (plex/jellyfin)
- Add webhook processing service with payload parsers, title resolution,
  and deduplication
- Add public webhook receiver route (token-based auth)
- Add authenticated settings API routes for managing connections
- Add Media Servers section to settings UI with Plex/Jellyfin cards

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-01 16:24:39 -05:00

44 lines
1.2 KiB
TypeScript

import crypto from "node:crypto";
import { and, eq } from "drizzle-orm";
import { headers } from "next/headers";
import { NextResponse } from "next/server";
import { auth } from "@/lib/auth/server";
import { db } from "@/lib/db/client";
import { webhookConnections } from "@/lib/db/schema";
export async function POST(request: Request) {
const session = await auth.api.getSession({ headers: await headers() });
if (!session)
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
const body = await request.json();
const { provider } = body;
if (!provider || !["plex", "jellyfin"].includes(provider)) {
return NextResponse.json({ error: "Invalid provider" }, { status: 400 });
}
const newToken = crypto.randomBytes(32).toString("hex");
const connection = await db
.update(webhookConnections)
.set({ token: newToken })
.where(
and(
eq(webhookConnections.userId, session.user.id),
eq(webhookConnections.provider, provider),
),
)
.returning()
.get();
if (!connection) {
return NextResponse.json(
{ error: "Connection not found" },
{ status: 404 },
);
}
return NextResponse.json({ connection });
}