commit
Add dedicated settings page
diff --git a/src/routes/[owner]/[name=repo]/settings/+page.server.ts b/src/routes/[owner]/[name=repo]/settings/+page.server.ts
new file mode 100644
index 0000000..bae3d6b
--- /dev/null
+++ b/src/routes/[owner]/[name=repo]/settings/+page.server.ts
@@ -0,0 +1,201 @@
+/* src/routes/[owner]/[name=repo]/settings/+page.server.ts
+ * LICENCED DASL-1.0 (c) Clove Twilight
+ */
+
+import { redirect } from "@sveltejs/kit";
+import type { Actions, PageServerLoad } from "./$types";
+import { sessionSlug } from "#lib/server/auth.ts";
+import { findUserBySlug } from "#lib/server/users.ts";
+import { siteRole } from "#lib/server/roles.ts";
+import { removeRepoImage, saveRepoImage } from "#lib/server/avatars.ts";
+import { clearRepoMetadata } from "#lib/server/repometa.ts";
+import { notifyRepoDeleted } from "#lib/server/notify.ts";
+import {
+ isLevel,
+ listCollaborators,
+ removeCollaborator,
+ setCollaborator,
+} from "#lib/server/access.ts";
+import {
+ MIRROR_KINDS,
+ mirrorHost,
+ mirrorSlug,
+ mirrorUrl,
+ type MirrorKind,
+} from "#lib/server/urls.ts";
+import { checkAllMirrors, dropMirrorStatusKind } from "#lib/server/mirror.ts";
+import {
+ description,
+ isRepoPublic,
+ readLinks,
+ setDescription,
+ setLinks,
+ setRepoPublic,
+ trashRepo,
+ type MirrorLink,
+ type RepoRef,
+} from "#lib/server/git.ts";
+import { fail, field, me, notAnImage, ownedRef, uploadedImage, writableRef } from "#lib/server/web.ts";
+
+export const load: PageServerLoad = async (event) => {
+ const ref = await writableRef(event);
+ const isOwner = sessionSlug(me(event)) === ref.owner;
+ const [rawDescription, isPublic, links] = await Promise.all([
+ description(ref),
+ isRepoPublic(ref),
+ readLinks(ref),
+ ]);
+
+ return {
+ owner: ref.owner,
+ name: ref.name,
+ isOwner,
+ rawDescription,
+ isPublic,
+ collaborators: isOwner
+ ? await Promise.all(
+ (await listCollaborators(ref)).map(async (person) => ({
+ ...person,
+ role: await siteRole(person.slug),
+ })),
+ )
+ : null,
+ canCheckMirrors: links.some((l) => !l.isPrivate),
+ mirrorFields: MIRROR_KINDS.map((kind) => {
+ const existing = links.find((l) => l.kind === kind);
+ return {
+ kind,
+ value: existing ? mirrorSlug(kind, existing.url) : "",
+ isPrivate: existing?.isPrivate ?? false,
+ };
+ }),
+ mirrorHosts: MIRROR_KINDS.map(mirrorHost).join(" or "),
+ };
+};
+
+function back(ref: RepoRef): never {
+ redirect(303, `/${ref.owner}/${ref.name}/settings`);
+}
+
+export const actions: Actions = {
+ description: async (event) => {
+ const ref = await writableRef(event);
+ const form = await event.request.formData();
+ await setDescription(ref, field(form, "description"));
+ back(ref);
+ },
+
+ image: async (event) => {
+ const ref = await ownedRef(event);
+ if (!(await saveRepoImage(ref, await uploadedImage(event, "image")))) notAnImage();
+ back(ref);
+ },
+
+ removeImage: async (event) => {
+ const ref = await ownedRef(event);
+ await removeRepoImage(ref);
+ back(ref);
+ },
+
+ visibility: async (event) => {
+ const ref = await ownedRef(event);
+ const form = await event.request.formData();
+ await setRepoPublic(ref, form.get("public") === "on");
+ back(ref);
+ },
+
+ delete: async (event) => {
+ const ref = await ownedRef(event);
+ const slug = sessionSlug(me(event));
+ const wasPublic = await isRepoPublic(ref);
+
+ const result = await trashRepo(ref, {
+ deletedAt: Math.floor(Date.now() / 1000),
+ deletedBy: slug,
+ grants: (await listCollaborators(ref)).map((g) => ({
+ slug: g.slug,
+ level: g.level,
+ })),
+ });
+ if (!result.ok) fail("could not delete", result.error ?? "unknown error");
+
+ await clearRepoMetadata(ref);
+ notifyRepoDeleted(ref, slug, wasPublic);
+ redirect(303, "/settings/deleted");
+ },
+
+ collaborator: async (event) => {
+ const ref = await ownedRef(event);
+ const form = await event.request.formData();
+ const slug = field(form, "slug").trim().toLowerCase();
+ const levelRaw = field(form, "level") || "read";
+ const level = isLevel(levelRaw) ? levelRaw : "read";
+
+ const invited = await findUserBySlug(slug);
+ if (!invited) {
+ fail(
+ "no such user",
+ `Nobody on this instance goes by "${slug}". They have to sign in ` +
+ `to this instance once before they can be added.`,
+ );
+ }
+ if (invited.slug === ref.owner) {
+ fail("already the owner", "You can't add yourself as a collaborator on your own repo.");
+ }
+
+ await setCollaborator(ref, invited.slug, level);
+ back(ref);
+ },
+
+ uncollaborate: async (event) => {
+ const ref = await ownedRef(event);
+ const form = await event.request.formData();
+ await removeCollaborator(ref, field(form, "slug"));
+ back(ref);
+ },
+
+ mirrors: async (event) => {
+ const ref = await ownedRef(event);
+ const form = await event.request.formData();
+
+ const links: MirrorLink[] = [];
+ const rejected: MirrorKind[] = [];
+ for (const kind of MIRROR_KINDS) {
+ const raw = field(form, kind).trim();
+ if (!raw) continue;
+ const url = mirrorUrl(kind, raw);
+ if (!url) {
+ rejected.push(kind);
+ continue;
+ }
+ links.push({
+ kind,
+ url,
+ isPrivate: form.get(`${kind}_private`) != null,
+ });
+ }
+
+ if (rejected.length > 0) {
+ fail(
+ "that isn't a mirror URL",
+ `The ${rejected.join(" and ")} link was refused. Give the repository as ` +
+ `owner/repo — for example doughmination/dough-git — and it is looked up on ` +
+ `${mirrorHost(rejected[0]!)}.`,
+ );
+ }
+
+ const before = await readLinks(ref);
+ await setLinks(ref, links);
+ for (const old of before) {
+ const still = links.find((l) => l.kind === old.kind && l.url === old.url);
+ if (!still) await dropMirrorStatusKind(ref, old.kind);
+ }
+ back(ref);
+ },
+
+ check: async (event) => {
+ const ref = await writableRef(event);
+ await checkAllMirrors(ref, await readLinks(ref));
+ back(ref);
+ },
+};
diff --git a/src/routes/[owner]/[name=repo]/settings/+page.svelte b/src/routes/[owner]/[name=repo]/settings/+page.svelte
new file mode 100644
index 0000000..348e4b3
--- /dev/null
+++ b/src/routes/[owner]/[name=repo]/settings/+page.svelte
@@ -0,0 +1,185 @@
+<!-- src/routes/[owner]/[name=repo]/settings/+page.svelte
+ LICENCED DASL-1.0 (c) Clove Twilight
+-->
+
+<script lang="ts">
+ import IcDelete from "~icons/ic/outline-delete";
+ import IcImage from "~icons/ic/outline-image";
+ import IcLock from "~icons/ic/outline-lock";
+ import IcLockOpen from "~icons/ic/outline-lock-open";
+ import IcPeople from "~icons/ic/outline-people";
+ import IcPerson from "~icons/ic/outline-person";
+ import IcPersonAdd from "~icons/ic/outline-person-add";
+ import IcPersonRemove from "~icons/ic/outline-person-remove";
+ import IcPublic from "~icons/ic/outline-public";
+ import IcRefresh from "~icons/ic/outline-refresh";
+ import IcSave from "~icons/ic/outline-save";
+ import IcUpload from "~icons/ic/outline-upload";
+ import IcStickyNote2 from "~icons/ic/outline-sticky-note-2";
+ import IcStorage from "~icons/ic/outline-storage";
+ import IcWarning from "~icons/ic/outline-warning";
+ import { fmtDate } from "#lib";
+ import Meta from "#lib/components/Meta.svelte";
+ import RepoNav from "#lib/components/RepoNav.svelte";
+ import Visibility from "#lib/components/Visibility.svelte";
+ import ConfirmForm from "#lib/components/ConfirmForm.svelte";
+ import RoleBadge from "#lib/components/RoleBadge.svelte";
+
+ let { data } = $props();
+
+ const title = $derived(`${data.owner}/${data.name}`);
+</script>
+
+<Meta title={`Settings · ${title}`} noindex />
+
+<div class="page">
+ <div class="page-head">
+ <p class="eyebrow">{title}</p>
+ <h1>Repository settings</h1>
+ </div>
+
+ <RepoNav owner={data.owner} name={data.name} active="settings" query="" canManage />
+
+ <h2 class="section-title"><IcStickyNote2 class="icon" width="20" height="20" />Description</h2>
+ <form method="post" action="?/description" class="panel row">
+ <label class="field-label with-icon" for="repo-description">
+ <IcStickyNote2 class="icon" width="16" height="16" />description
+ </label>
+ <input
+ id="repo-description"
+ type="text"
+ name="description"
+ maxlength="300"
+ value={data.rawDescription}
+ placeholder="what this repository is for"
+ />
+ <button type="submit"><IcSave class="icon" width="16" height="16" />save</button>
+ </form>
+
+ {#if data.isOwner || data.canCheckMirrors}
+ <h2 class="section-title"><IcStorage class="icon" width="20" height="20" />External mirrors</h2>
+ {#if data.isOwner}
+ <form method="post" action="?/mirrors" class="panel">
+ {#each data.mirrorFields as mirror (mirror.kind)}
+ <div class="row">
+ <label class="field-label with-icon" for="mirror-{mirror.kind}">
+ <IcPublic class="icon" width="16" height="16" />{mirror.kind}
+ </label>
+ <input
+ id="mirror-{mirror.kind}"
+ type="text"
+ name={mirror.kind}
+ value={mirror.value}
+ placeholder="user/repo"
+ pattern={"[^\\s/]+/[^\\s/]+/?"}
+ autocapitalize="off"
+ spellcheck="false"
+ />
+ <label class="check">
+ <input type="checkbox" name="{mirror.kind}_private" checked={mirror.isPrivate} /> private
+ </label>
+ </div>
+ {/each}
+ <p class="muted small">
+ Give the <code>user/repo</code> on {data.mirrorHosts} — not a full URL. Mark a mirror <em>private</em> to
+ skip status checks — they are anonymous, so a private mirror would otherwise always look unreachable.
+ Clear a field to remove it.
+ </p>
+ <div>
+ <button type="submit"><IcSave class="icon" width="16" height="16" />save mirrors</button>
+ </div>
+ </form>
+ {/if}
+ {#if data.canCheckMirrors}
+ <form method="post" action="?/check" class="buttons">
+ <button type="submit"><IcRefresh class="icon" width="16" height="16" />check mirror status</button>
+ </form>
+ {/if}
+ {/if}
+
+ {#if data.isOwner && data.collaborators}
+ <h2 class="section-title"><IcLock class="icon" width="20" height="20" />Visibility</h2>
+ <div class="buttons">
+ <Visibility isPublic={data.isPublic} />
+ <form method="post" action="?/visibility">
+ <input type="hidden" name="public" value={data.isPublic ? "" : "on"} />
+ <button type="submit">
+ {#if data.isPublic}
+ <IcLock class="icon" width="16" height="16" />
+ {:else}
+ <IcLockOpen class="icon" width="16" height="16" />
+ {/if}
+ make {data.isPublic ? "private" : "public"}
+ </button>
+ </form>
+ </div>
+
+ <h2 class="section-title"><IcImage class="icon" width="20" height="20" />Image</h2>
+ <p class="muted">
+ Shown beside the name and in link embeds, in place of your avatar. Private repositories keep your avatar in
+ embeds, since Discord can't fetch their image.
+ </p>
+ <section class="panel">
+ <form method="post" action="?/image" enctype="multipart/form-data" class="row">
+ <input type="file" name="image" accept="image/png,image/jpeg,image/gif,image/webp" aria-label="repository image" required />
+ <button type="submit" class="primary"><IcUpload class="icon" width="16" height="16" />upload</button>
+ </form>
+ {#if data.repoImage}
+ <ConfirmForm action="?/removeImage" prompt="Remove the image from {title}?">
+ <button type="submit" class="danger"><IcDelete class="icon" width="16" height="16" />remove image</button>
+ </ConfirmForm>
+ {/if}
+ </section>
+
+ <h2 class="section-title"><IcPeople class="icon" width="20" height="20" />Collaborators</h2>
+ <p class="muted">
+ People here can see this repository even while it is private. <code>write</code> also lets them push to it.
+ </p>
+ <form method="post" action="?/collaborator" class="panel row">
+ <label class="field-label with-icon" for="collab-slug"><IcPerson class="icon" width="16" height="16" />username</label>
+ <input id="collab-slug" type="text" name="slug" placeholder="their handle" pattern="[A-Za-z0-9._\-]+" required />
+ <select name="level" aria-label="access level">
+ <option value="read">read</option>
+ <option value="write">write</option>
+ </select>
+ <button type="submit"><IcPersonAdd class="icon" width="16" height="16" />add</button>
+ </form>
+
+ <div class="table-wrap">
+ <table>
+ <thead>
+ <tr>
+ <th>user</th>
+ <th>access</th>
+ <th>added</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ {#each data.collaborators as person (person.slug)}
+ <tr>
+ <td><IcPerson class="icon" width="14" height="14" /> <a href="/{person.slug}">{person.slug}</a> <RoleBadge role={person.role} /></td>
+ <td class="mono">{person.level}</td>
+ <td class="mono">{fmtDate(person.added_at)}</td>
+ <td>
+ <form method="post" action="?/uncollaborate">
+ <input type="hidden" name="slug" value={person.slug} />
+ <button type="submit" class="small"><IcPersonRemove class="icon" width="14" height="14" />remove</button>
+ </form>
+ </td>
+ </tr>
+ {:else}
+ <tr><td colspan="4" class="empty">nobody else has access</td></tr>
+ {/each}
+ </tbody>
+ </table>
+ </div>
+
+ <h2 class="section-title"><IcWarning class="icon" width="20" height="20" />Delete repository</h2>
+ <div class="buttons">
+ <ConfirmForm action="?/delete" prompt="Delete {title}? It moves to Recently Deleted.">
+ <button type="submit" class="danger"><IcDelete class="icon" width="16" height="16" />delete repository</button>
+ </ConfirmForm>
+ </div>
+ {/if}
+</div>