// /mods/submit: signs in with GitHub through the registry's service, then sends the page and the file. The service // checks the file and answers with the pull request it opened, or with what to fix. import { API, entryFrom } from "./mods.js"; import { setUpMenu, setUpTheme } from "./chrome.js"; import { escape } from "./markdown.js"; const auth = document.getElementById("submit-auth"); const form = document.getElementById("submit-form"); const result = document.getElementById("submit-result"); let csrf = ""; // The form's own controls by name (form.id and form.name are the form's attributes). const control = (name) => form.elements.namedItem(name); function signedOut(message) { auth.innerHTML = `

${message}

Sign in with GitHub

It only reads your public profile: your GitHub name goes on the pull request and in the project's owners.

`; form.hidden = true; } async function start() { let me; try { const r = await fetch(`${API}/me`, { credentials: "same-origin" }); if (r.status === 401) return signedOut("Sign in to submit: reviewers and updates go through your GitHub account."); if (!r.ok) throw new Error(String(r.status)); me = await r.json(); } catch { auth.innerHTML = `

The form isn't open right now. You can open a pull request by hand.

`; return; } csrf = me.csrf; auth.innerHTML = `

Signed in as ${escape(me.login)}.

`; document.getElementById("sign-out").addEventListener("click", async () => { await fetch(`${API}/auth/logout`, { method: "POST", credentials: "same-origin" }); signedOut("Signed out."); }); form.hidden = false; } // A file named -. fills in the two fields and the kind. form.querySelector("#f-file").addEventListener("change", (e) => { const name = e.target.files[0]?.name || ""; const m = name.match(/^([a-z0-9_]{1,64})-([A-Za-z0-9][A-Za-z0-9._-]*)\.(hmod|hpack|zip)$/); const kind = { hmod: "mod", hpack: "modpack", zip: "resourcepack" }[name.split(".").pop()]; if (kind) control("kind").value = kind; if (m) { if (!control("id").value) control("id").value = m[1]; if (!control("version").value) control("version").value = m[2]; } }); form.addEventListener("submit", async (e) => { e.preventDefault(); const send = form.querySelector("#f-send"); const values = Object.fromEntries(["id", "kind", "name", "summary", "description", "authors", "license", "source", "icon", "key", "version", "game", "changelog"].map((k) => [k, control(k).value])); const sig = form.querySelector("#f-signature").files[0]; values.signature = sig ? await sig.text() : ""; const body = new FormData(); body.append("entry", JSON.stringify(entryFrom(values))); body.append("file", form.querySelector("#f-file").files[0]); send.disabled = true; result.innerHTML = `

Checking the file and opening the pull request…

`; try { const r = await fetch(`${API}/submit`, { method: "POST", body, credentials: "same-origin", headers: { "X-CSRF-Token": csrf } }); const json = await r.json().catch(() => ({})); if (r.ok && /^https:\/\/github\.com\//.test(json.pull_request || "")) { result.innerHTML = `

Submitted. Follow the review in its pull request: the checks' report is there, and reviewers answer there.

`; form.reset(); } else { result.innerHTML = `

${escape(json.error || `The service answered ${r.status}.`)}

`; } } catch { result.innerHTML = `

The service didn't answer. Try again in a minute.

`; } finally { send.disabled = false; } }); setUpTheme(); setUpMenu(); start();