1import type { Context, Hono } from "hono";2import { syncAll } from "../bucket/sync.ts";3import { openMirror, quarantinedPaths } from "../mirror/store.ts";4import { html } from "../pages/html.ts";5import { formatDate, plural } from "../pages/layout.ts";6import { type AdminContext, adminPage, formField, notice, textFields } from "./console.ts";7import { type AdminRepo, allRepos, findRepo, type RepoEdit, updateRepo } from "./store.ts";89const MAX_FIELD = 500;10const HTTP_URL = /^https?:\/\/[^\s]+$/i;11const SORT_ORDER = /^-?\d{1,6}$/;121314export function registerRepos(app: Hono, ctx: AdminContext): void {15 app.get("/admin/repos", (c) => c.html(reposPage(c, ctx, "")));1617 app.post("/admin/sync", async (c) => {18 await syncAll(ctx.sync);19 return c.redirect("/admin/repos", 303);20 });2122 app.post("/admin/repos/:repo", async (c) => {23 const repo = findRepo(ctx.kepr, c.req.param("repo"));24 if (!repo) return c.notFound();25 const fields = await textFields(c);26 const text = (name: string) => (fields[name] ?? "").trim();27 const edit: RepoEdit = {28 public: fields.public === "1" ? 1 : 0,29 category: text("category"),30 homepage: text("homepage"),31 sort_order: Number(text("sort_order") || "0"),32 install1: text("install1"),33 install2: text("install2"),34 install3: text("install3"),35 };36 const refuse = (error: string) => c.html(reposPage(c, ctx, `${repo.name}: ${error}`), 400);37 if (Object.values(fields).some((value) => value.length > MAX_FIELD)) {38 return refuse(`keep each field under ${MAX_FIELD} characters.`);39 }40 if (edit.homepage !== "" && !HTTP_URL.test(edit.homepage)) {41 return refuse("the homepage must be an http or https address.");42 }43 if (!SORT_ORDER.test(text("sort_order") || "0")) return refuse("the order must be a number.");44 if ([edit.install1, edit.install2, edit.install3].some((line) => /[\r\n]/.test(line))) {45 return refuse("an install line is one line.");46 }47 updateRepo(ctx.kepr, repo.name, edit);48 return c.redirect(`/admin/repos#repo-${repo.name}`, 303);49 });50}5152function reposPage(c: Context, ctx: AdminContext, error: string): string {53 const repos = allRepos(ctx.kepr);54 const body = html`${notice(error)}55<form method="post" action="/admin/sync">56${formField(c)}<button class="issues-submit" type="submit">sync now</button></form>57${repos.length === 0 && html`<p class="muted mono save-empty">no repos found in the bucket yet</p>`}58${repos.map((repo) => repoForm(c, repo, quarantinedPaths(openMirror(ctx.mirrorDir, repo.name))))}`;59 return adminPage(c, ctx, "repos", body);60}6162function repoForm(c: Context, repo: AdminRepo, quarantined: string[]) {63 const input = (name: keyof RepoEdit, label: string) =>64 html`<label class="issues-label" for="${repo.name}-${name}">${label}</label>65<input class="issues-input" id="${repo.name}-${name}" name="${name}" maxlength="${MAX_FIELD}" value="${String(repo[name])}">`;66 const synced =67 repo.last_sync_at === null ? "never synced" : `last sync ${formatDate(repo.last_sync_at)} UTC`;68 return html`<article class="issue-post" id="repo-${repo.name}">69<p class="issue-post-head mono">70<span class="issue-author">71${repo.public ? html`<a href="/${repo.name}">${repo.name}</a>` : repo.name}</span>72<span>${repo.public ? "public" : "private"}</span><span class="history-when">${synced}</span></p>73${74 repo.last_sync_error && html`<p class="issues-error mono">sync error: ${repo.last_sync_error}</p>`75}76${77 quarantined.length > 0 &&78 html`<p class="issues-error mono">${plural(quarantined.length, "path")} quarantined: ${quarantined.join(", ")}</p>`79}80<form class="issues-form" method="post" action="/admin/repos/${repo.name}">81${formField(c)}82<label class="issues-label">83<input type="checkbox" name="public" value="1"${repo.public ? html` checked` : ""}> public</label>84${input("category", "category")}85${input("homepage", "homepage")}86${input("sort_order", "card order")}87${input("install1", "install line 1")}88${input("install2", "install line 2")}89${input("install3", "install line 3")}90<button class="issues-submit" type="submit">save ${repo.name}</button>91</form>92</article>`;93}