commit
Add site owner, moderator list and role badges
The site owner (MINIGIT_SITE_OWNER, default "clove") manages a list of moderators from Settings > moderation. Owner and mod badges show in the header, on profiles and in collaborator lists. Co-Authored-By: Sandrone <[email protected]>
diff --git a/.env.example b/.env.example
index e1ab9b2..8b6d0e6 100644
--- a/.env.example
+++ b/.env.example
@@ -14,6 +14,9 @@ MINIGIT_FAVICON=https://git.doughmination.gay/favicon.png
MINIGIT_DESCRIPTION=A minimal self-hosted git mirror.
MINIGIT_THEME_COLOR=#eb459e
+# Namespace slug of the site owner, who can add and remove moderators
+MINIGIT_SITE_OWNER=clove
+
# Auto-create a bare repo on first authenticated push (set "false" to disable).
MINIGIT_AUTO_CREATE=true
diff --git a/migrations/0002_site_mods.sql b/migrations/0002_site_mods.sql
new file mode 100644
index 0000000..ad0ab80
--- /dev/null
+++ b/migrations/0002_site_mods.sql
@@ -0,0 +1,8 @@
+-- migrations/0002_site_mods.sql
+-- LICENCED DASL-1.0 (c) Clove Twilight
+
+-- Site moderators, keyed by namespace slug. The site owner comes from MINIGIT_SITE_OWNER, not this table
+CREATE TABLE IF NOT EXISTS site_mods (
+ slug TEXT PRIMARY KEY,
+ added_at INTEGER NOT NULL
+);
diff --git a/src/lib/components/Header.svelte b/src/lib/components/Header.svelte
index 26f0642..e4f5b4c 100644
--- a/src/lib/components/Header.svelte
+++ b/src/lib/components/Header.svelte
@@ -11,6 +11,7 @@
import { page } from "$app/state";
import { displayName } from "#lib";
import Avatar from "#lib/components/Avatar.svelte";
+ import RoleBadge from "#lib/components/RoleBadge.svelte";
const site = $derived(page.data.site);
const user = $derived(page.data.user);
@@ -31,6 +32,7 @@
{#if user}
<a href="/{user.slug}">
<Avatar profile={user} size="sm" />{displayName(user)}
+ <RoleBadge role={user.role} />
</a>
<a href="/new">
<IcAddCircle class="icon nav-icon" width="24" height="24" />New
diff --git a/src/lib/components/OwnerHead.svelte b/src/lib/components/OwnerHead.svelte
index 8113a4f..8fc08f3 100644
--- a/src/lib/components/OwnerHead.svelte
+++ b/src/lib/components/OwnerHead.svelte
@@ -5,8 +5,9 @@
<script lang="ts">
import IcForkRight from "~icons/ic/outline-fork-right";
import IcPerson from "~icons/ic/outline-person";
- import { displayName, fmtDate } from "#lib";
+ import { displayName, fmtDate, type SiteRole } from "#lib";
import Avatar from "#lib/components/Avatar.svelte";
+ import RoleBadge from "#lib/components/RoleBadge.svelte";
import Tabs from "#lib/components/Tabs.svelte";
type Profile = {
@@ -15,6 +16,7 @@
username: string | null;
picture: string | null;
createdAt: number;
+ role: SiteRole | null;
};
let {
@@ -40,7 +42,7 @@
<section class="owner-head">
<Avatar profile={shown} size="lg" />
<div>
- <h1>{displayName(shown)}</h1>
+ <h1>{displayName(shown)} <RoleBadge role={profile?.role} /></h1>
{#if profile}
<p class="muted">
@{profile.slug}{#if handle} · {handle}{/if} · joined {fmtDate(profile.createdAt).slice(0, 10)}
diff --git a/src/lib/components/RoleBadge.svelte b/src/lib/components/RoleBadge.svelte
new file mode 100644
index 0000000..1a5004f
--- /dev/null
+++ b/src/lib/components/RoleBadge.svelte
@@ -0,0 +1,17 @@
+<!-- src/lib/components/RoleBadge.svelte
+ LICENCED DASL-1.0 (c) Clove Twilight
+-->
+
+<script lang="ts">
+ import IcShield from "~icons/ic/outline-shield";
+ import IcVerified from "~icons/ic/outline-verified";
+ import type { SiteRole } from "#lib";
+
+ let { role }: { role: SiteRole | null | undefined } = $props();
+</script>
+
+{#if role === "owner"}
+ <span class="badge accent" title="site owner"><IcVerified class="icon" width="12" height="12" /> owner</span>
+{:else if role === "mod"}
+ <span class="badge" title="site moderator"><IcShield class="icon" width="12" height="12" /> mod</span>
+{/if}
diff --git a/src/lib/index.ts b/src/lib/index.ts
index cd1f528..23eef28 100644
--- a/src/lib/index.ts
+++ b/src/lib/index.ts
@@ -24,6 +24,8 @@ export function ago(seconds: number): string {
return `${Math.round(seconds / 86400)}d`;
}
+export type SiteRole = "owner" | "mod";
+
export interface Profile {
name: string | null;
username: string | null;
diff --git a/src/lib/server/config.ts b/src/lib/server/config.ts
index cf9bd32..1760180 100644
--- a/src/lib/server/config.ts
+++ b/src/lib/server/config.ts
@@ -25,6 +25,9 @@ export const config = {
themeColor: optional("MINIGIT_THEME_COLOR", "#eb459e"),
+ // Namespace slug of the site owner, who manages the moderator list
+ siteOwner: optional("MINIGIT_SITE_OWNER", "clove"),
+
reposRoot: optional("MINIGIT_REPOS_ROOT", "./data/repos"),
dbPath: optional("MINIGIT_DB_PATH", "./data/dough-git.db"),
publicMarker: optional("MINIGIT_PUBLIC_MARKER", "minigit-public"),
diff --git a/src/lib/server/owner.ts b/src/lib/server/owner.ts
index f84b705..99b9b9e 100644
--- a/src/lib/server/owner.ts
+++ b/src/lib/server/owner.ts
@@ -4,6 +4,7 @@
import { canRead } from "./access.ts";
import { findUserBySlug } from "./users.ts";
+import { siteRole } from "./roles.ts";
import { renderMarkdown, plainSummary } from "./markdown.ts";
import {
PROFILE_REPO,
@@ -66,6 +67,7 @@ export async function ownerPage(owner: string, viewer: string | null, withReadme
username: profile.username,
picture: profile.picture,
createdAt: profile.created_at,
+ role: await siteRole(profile.slug),
}
: null,
repos,
diff --git a/src/lib/server/roles.ts b/src/lib/server/roles.ts
new file mode 100644
index 0000000..aa0fad3
--- /dev/null
+++ b/src/lib/server/roles.ts
@@ -0,0 +1,51 @@
+/* src/lib/server/roles.ts
+ * LICENCED DASL-1.0 (c) Clove Twilight
+ */
+
+import type { SiteRole } from "../index.ts";
+import { config } from "./config.ts";
+import { db, now } from "./db.ts";
+import { ownerSlug } from "./gitref.ts";
+
+export interface ModRow {
+ slug: string;
+ added_at: number;
+}
+
+export async function siteRole(slug: string | null): Promise<SiteRole | null> {
+ if (!slug) return null;
+ if (slug === config.siteOwner) return "owner";
+
+ const row = await db()
+ .prepare("SELECT slug FROM site_mods WHERE slug = ?")
+ .bind(slug)
+ .first<{ slug: string }>();
+ return row ? "mod" : null;
+}
+
+export async function listMods(): Promise<ModRow[]> {
+ const { results } = await db()
+ .prepare("SELECT slug, added_at FROM site_mods ORDER BY slug")
+ .all<ModRow>();
+ return results;
+}
+
+/** False when the slug is empty, malformed, or the site owner */
+export async function addMod(raw: string): Promise<boolean> {
+ const slug = raw.trim().toLowerCase();
+ if (!slug || slug !== ownerSlug(slug) || slug === config.siteOwner) return false;
+
+ await db()
+ .prepare("INSERT INTO site_mods (slug, added_at) VALUES (?, ?) ON CONFLICT (slug) DO NOTHING")
+ .bind(slug, now())
+ .run();
+ return true;
+}
+
+export async function removeMod(slug: string): Promise<boolean> {
+ const result = await db()
+ .prepare("DELETE FROM site_mods WHERE slug = ?")
+ .bind(slug)
+ .run();
+ return result.meta.changes > 0;
+}
diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts
index cdf67f4..5fae624 100644
--- a/src/routes/+layout.server.ts
+++ b/src/routes/+layout.server.ts
@@ -5,9 +5,11 @@
import type { LayoutServerLoad } from "./$types";
import { config } from "#lib/server/config.ts";
import { sessionSlug } from "#lib/server/auth.ts";
+import { siteRole } from "#lib/server/roles.ts";
-export const load: LayoutServerLoad = ({ locals }) => {
+export const load: LayoutServerLoad = async ({ locals }) => {
const user = locals.user;
+ const slug = user ? sessionSlug(user) : null;
return {
site: {
title: config.title,
@@ -22,6 +24,7 @@ export const load: LayoutServerLoad = ({ locals }) => {
name: user.name,
username: user.username,
picture: user.picture,
+ role: await siteRole(slug),
}
: null,
};
diff --git a/src/routes/[owner]/[name=repo]/+page.server.ts b/src/routes/[owner]/[name=repo]/+page.server.ts
index 5ab6502..c54ecc5 100644
--- a/src/routes/[owner]/[name=repo]/+page.server.ts
+++ b/src/routes/[owner]/[name=repo]/+page.server.ts
@@ -8,6 +8,7 @@ import type { Actions, PageServerLoad } from "./$types";
import { config } from "#lib/server/config.ts";
import { sessionSlug } from "#lib/server/auth.ts";
import { findUserBySlug } from "#lib/server/users.ts";
+import { siteRole } from "#lib/server/roles.ts";
import { getSettings } from "#lib/server/settings.ts";
import { clearRepoMetadata } from "#lib/server/repometa.ts";
import { notifyRepoDeleted } from "#lib/server/notify.ts";
@@ -114,7 +115,14 @@ export const load: PageServerLoad = async (event) => {
: null,
canPush,
isOwner,
- collaborators: isOwner ? await listCollaborators(ref) : null,
+ collaborators: isOwner
+ ? await Promise.all(
+ (await listCollaborators(ref)).map(async (person) => ({
+ ...person,
+ role: await siteRole(person.slug),
+ })),
+ )
+ : null,
mirrors: links.map((link) => describeMirror(link, statuses.get(link.kind) ?? null, nowSec)),
canCheckMirrors: canPush && links.some((l) => !l.isPrivate),
mirrorFields: MIRROR_KINDS.map((kind) => {
diff --git a/src/routes/[owner]/[name=repo]/+page.svelte b/src/routes/[owner]/[name=repo]/+page.svelte
index 1613141..5e011d1 100644
--- a/src/routes/[owner]/[name=repo]/+page.svelte
+++ b/src/routes/[owner]/[name=repo]/+page.svelte
@@ -33,6 +33,7 @@
import RevPicker from "#lib/components/RevPicker.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();
@@ -235,7 +236,7 @@ git push --mirror mirror</code></pre>
<tbody>
{#each data.collaborators as person (person.slug)}
<tr>
- <td><IcPerson class="icon" width="14" height="14" /> <a href="/{person.slug}">{person.slug}</a></td>
+ <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>
diff --git a/src/routes/settings/+layout.server.ts b/src/routes/settings/+layout.server.ts
index 31bafbc..f637ed6 100644
--- a/src/routes/settings/+layout.server.ts
+++ b/src/routes/settings/+layout.server.ts
@@ -3,9 +3,14 @@
*/
import type { LayoutServerLoad } from "./$types";
+import { sessionSlug } from "#lib/server/auth.ts";
+import { config } from "#lib/server/config.ts";
import { me } from "#lib/server/web.ts";
// Every settings page needs a signed-in user
export const load: LayoutServerLoad = (event) => {
- me(event);
+ const user = me(event);
+ return {
+ isSiteOwner: sessionSlug(user) === config.siteOwner,
+ };
};
diff --git a/src/routes/settings/+layout.svelte b/src/routes/settings/+layout.svelte
index 4a26203..fa523f9 100644
--- a/src/routes/settings/+layout.svelte
+++ b/src/routes/settings/+layout.svelte
@@ -6,17 +6,20 @@
import IcDelete from "~icons/ic/outline-delete";
import IcKey from "~icons/ic/outline-key";
import IcPerson from "~icons/ic/outline-person";
+ import IcShield from "~icons/ic/outline-shield";
import { page } from "$app/state";
import Tabs from "#lib/components/Tabs.svelte";
- let { children } = $props();
+ let { data, children } = $props();
const active = $derived(
page.url.pathname.startsWith("/settings/tokens")
? "tokens"
: page.url.pathname.startsWith("/settings/deleted")
? "deleted"
- : "account",
+ : page.url.pathname.startsWith("/settings/moderation")
+ ? "moderation"
+ : "account",
);
</script>
@@ -47,6 +50,16 @@
icon: IcDelete,
href: "/settings/deleted",
},
+ ...(data.isSiteOwner
+ ? [
+ {
+ id: "moderation",
+ label: "moderation",
+ icon: IcShield,
+ href: "/settings/moderation",
+ },
+ ]
+ : []),
]}
/>
diff --git a/src/routes/settings/moderation/+page.server.ts b/src/routes/settings/moderation/+page.server.ts
new file mode 100644
index 0000000..038e1c3
--- /dev/null
+++ b/src/routes/settings/moderation/+page.server.ts
@@ -0,0 +1,39 @@
+/* src/routes/settings/moderation/+page.server.ts
+ * LICENCED DASL-1.0 (c) Clove Twilight
+ */
+
+import { redirect, type RequestEvent } from "@sveltejs/kit";
+import type { Actions, PageServerLoad } from "./$types";
+import { sessionSlug } from "#lib/server/auth.ts";
+import { config } from "#lib/server/config.ts";
+import { addMod, listMods, removeMod } from "#lib/server/roles.ts";
+import { fail, field, me } from "#lib/server/web.ts";
+
+// Only the site owner manages moderators; everyone else gets a plain 404
+function ownerOnly(event: RequestEvent): void {
+ if (sessionSlug(me(event)) !== config.siteOwner) fail("not found", "No such page.", 404);
+}
+
+export const load: PageServerLoad = async (event) => {
+ ownerOnly(event);
+ return {
+ owner: config.siteOwner,
+ mods: await listMods(),
+ };
+};
+
+export const actions: Actions = {
+ add: async (event) => {
+ ownerOnly(event);
+ const form = await event.request.formData();
+ if (!(await addMod(field(form, "slug")))) fail("can't add", "That isn't a valid username, or it's the site owner.");
+ redirect(303, "/settings/moderation");
+ },
+
+ remove: async (event) => {
+ ownerOnly(event);
+ const form = await event.request.formData();
+ await removeMod(field(form, "slug"));
+ redirect(303, "/settings/moderation");
+ },
+};
diff --git a/src/routes/settings/moderation/+page.svelte b/src/routes/settings/moderation/+page.svelte
new file mode 100644
index 0000000..743ef7a
--- /dev/null
+++ b/src/routes/settings/moderation/+page.svelte
@@ -0,0 +1,57 @@
+<!-- src/routes/settings/moderation/+page.svelte
+ LICENCED DASL-1.0 (c) Clove Twilight
+-->
+
+<script lang="ts">
+ import IcPerson from "~icons/ic/outline-person";
+ import IcPersonAdd from "~icons/ic/outline-person-add";
+ import IcPersonRemove from "~icons/ic/outline-person-remove";
+ import IcShield from "~icons/ic/outline-shield";
+ import { fmtDate } from "#lib";
+ import Meta from "#lib/components/Meta.svelte";
+ import RoleBadge from "#lib/components/RoleBadge.svelte";
+
+ let { data } = $props();
+</script>
+
+<Meta title="moderation" description="Manage site moderators." noindex />
+
+<h2 class="section-title"><IcShield class="icon" width="20" height="20" />Moderators</h2>
+<p class="muted">
+ Moderators get a <RoleBadge role="mod" /> badge everywhere their name shows. You, <code>{data.owner}</code>, always
+ carry <RoleBadge role="owner" />.
+</p>
+
+<form method="post" action="?/add" class="panel row">
+ <label class="field-label with-icon" for="mod-slug"><IcPerson class="icon" width="16" height="16" />username</label>
+ <input id="mod-slug" type="text" name="slug" placeholder="their handle" pattern="[A-Za-z0-9._\-]+" required />
+ <button type="submit"><IcPersonAdd class="icon" width="16" height="16" />add moderator</button>
+</form>
+
+<div class="table-wrap">
+ <table>
+ <thead>
+ <tr>
+ <th>user</th>
+ <th>added</th>
+ <th></th>
+ </tr>
+ </thead>
+ <tbody>
+ {#each data.mods as mod (mod.slug)}
+ <tr>
+ <td><IcPerson class="icon" width="14" height="14" /> <a href="/{mod.slug}">{mod.slug}</a></td>
+ <td class="mono">{fmtDate(mod.added_at)}</td>
+ <td>
+ <form method="post" action="?/remove">
+ <input type="hidden" name="slug" value={mod.slug} />
+ <button type="submit" class="small"><IcPersonRemove class="icon" width="14" height="14" />remove</button>
+ </form>
+ </td>
+ </tr>
+ {:else}
+ <tr><td colspan="3" class="empty">no moderators yet</td></tr>
+ {/each}
+ </tbody>
+ </table>
+</div>
diff --git a/test/roles.test.mjs b/test/roles.test.mjs
new file mode 100644
index 0000000..ba9a930
--- /dev/null
+++ b/test/roles.test.mjs
@@ -0,0 +1,43 @@
+/* test/roles.test.mjs
+ * LICENCED DASL-1.0 (c) Clove Twilight
+ */
+
+import { mkdtempSync, rmSync } from "node:fs";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+
+const root = mkdtempSync(join(tmpdir(), "dough-roles-"));
+process.env.MINIGIT_REPOS_ROOT = root;
+process.env.MINIGIT_DB_PATH = join(root, "test.db");
+process.env.MINIGIT_SITE_OWNER = "clove";
+
+const {
+ siteRole,
+ addMod,
+ removeMod,
+ listMods,
+} = await import("../src/lib/server/roles.ts");
+
+let failures = 0;
+function check(label, cond) {
+ console.log(`${cond ? "PASS" : "FAIL"} ${label}`);
+ if (!cond) failures++;
+}
+
+check("the configured slug is the site owner", await siteRole("clove") === "owner");
+check("a stranger has no role", await siteRole("sam") === null);
+check("signed-out viewers have no role", await siteRole(null) === null);
+
+check("the owner can't be added as a mod", await addMod("clove") === false);
+check("a malformed handle is refused", await addMod("not a handle") === false);
+check("an empty handle is refused", await addMod(" ") === false);
+
+check("adding a mod succeeds", await addMod("Ari") === true);
+check("the handle is stored lowercased", await siteRole("ari") === "mod");
+check("adding twice is harmless", await addMod("ari") === true && (await listMods()).length === 1);
+
+check("removing a mod succeeds", await removeMod("ari") === true);
+check("a removed mod has no role", await siteRole("ari") === null);
+
+rmSync(root, { recursive: true, force: true });
+if (failures) Deno.exit(1);