17 lines
1008 B
TypeScript
17 lines
1008 B
TypeScript
import { NextResponse } from "next/server";
|
|
import { requireUser } from "@/lib/auth";
|
|
import { db } from "@/lib/db";
|
|
import { externalUrl } from "@/lib/http";
|
|
import { queuePull } from "@/lib/sync";
|
|
import { requireSameOrigin } from "@/lib/security";
|
|
|
|
export async function POST(req: Request) {
|
|
try {
|
|
requireSameOrigin(req); const user = await requireUser(); const form = await req.formData(); const sourceId = Number(form.get("sourceId"));
|
|
const source = db.prepare("SELECT s.id FROM sources s JOIN source_members sm ON sm.source_id=s.id WHERE s.id=? AND sm.user_id=? AND s.is_enabled=1").get(sourceId, user.id);
|
|
if (!source) throw new Error("Source not available");
|
|
const created = queuePull(sourceId, "manual");
|
|
return NextResponse.redirect(externalUrl(req, `/dashboard?sync=${created ? "queued" : "already-queued"}`));
|
|
} catch (error) { return NextResponse.redirect(externalUrl(req, "/dashboard?error=" + encodeURIComponent(error instanceof Error ? error.message : "sync"))); }
|
|
}
|