Merge branch 'r2-web'

# Conflicts:
#	app/static/roles.js
#	app/web/api.py
#	tests/test_web.py
This commit is contained in:
Wolf committed 2026-09-29 15:31:35 +02:00
commit a629eda8de
13 files changed
+548 -203

No files matched your search

+16 -4
View File
@@ -14,9 +14,10 @@
* forgetting the header is the failure that looks like a server bug.
*
* Beside `api` on `window`, the few helpers more than one page script uses:
* `count`, `cell`, `debounce`, and the company list with its affinity control
* (`loadCompanies`, `companies`, `affinityControl`, `setAffinity`). One copy
* each, since two copies of a helper is one of them being fixed.
* `count`, `cell`, `debounce`, `safeUrl`, and the company list with its
* affinity control (`loadCompanies`, `companies`, `affinityControl`,
* `setAffinity`). One copy each, since two copies of a helper is one of them
* being fixed.
*
* Affinity is per search agent. The three company helpers take the agent's
* id as an optional last argument and send it along; a caller that passes
@@ -104,6 +105,17 @@
};
}
/** An `http(s)` URL, or "" for anything else — `javascript:`, a bare path,
empty. Every link whose address came from a feed, a board, discovery or
a person (a search result's `url`, a company's `careers_url`, a saved
application's `url`, a role's `original_url`) goes through this before
it lands in an `href`, so a board that wrote something other than a URL
cannot make this page navigate to it. Moved here from roles.js, which
first needed it for the reader's outbound links. */
function safeUrl(u) {
return /^https?:\/\//i.test(u || "") ? u : "";
}
// --------------------------------------------------------------- companies
/** @type {Map<string, object>} company_key -> row from /api/companies. One
@@ -170,6 +182,6 @@
}
Object.assign(window, {
api, count, cell, debounce, companies, loadCompanies, affinityControl, setAffinity,
api, count, cell, debounce, safeUrl, companies, loadCompanies, affinityControl, setAffinity,
});
})();
+4 -3
View File
@@ -9,7 +9,7 @@
(function () {
"use strict";
const { api, count, cell, companies, loadCompanies } = window;
const { api, count, cell, safeUrl, companies, loadCompanies } = window;
const $ = (sel, root = document) => root.querySelector(sel);
// `ROLEDESK_STATUSES` is set by applications.html from the same `STATUSES`
@@ -41,13 +41,14 @@
body.replaceChildren();
for (const application of applications) {
const tr = document.createElement("tr");
if (application.role_uid || application.url) {
const externalUrl = safeUrl(application.url);
if (application.role_uid || externalUrl) {
const link = document.createElement("a");
link.textContent = application.role_title || "(untitled)";
if (application.role_uid) {
link.href = `/roles/${encodeURIComponent(application.role_uid)}`;
} else {
link.href = application.url;
link.href = externalUrl;
link.target = "_blank";
link.rel = "noopener";
}
+45 -9
View File
@@ -21,7 +21,7 @@
(function () {
"use strict";
const { api, count, debounce, companies, loadCompanies, affinityControl } = window;
const { api, count, debounce, safeUrl, companies, loadCompanies, affinityControl } = window;
const $ = (sel, root = document) => root.querySelector(sel);
/** @type {object[]} the search agents, by id */
@@ -79,9 +79,10 @@
// where this company posts its roles.
sub.append("board not found");
}
if (company.careers_url && !company.board) {
const careersUrl = safeUrl(company.careers_url);
if (careersUrl && !company.board) {
const link = document.createElement("a");
link.href = company.careers_url;
link.href = careersUrl;
link.target = "_blank";
link.rel = "noopener";
link.textContent = "careers page";
@@ -150,13 +151,22 @@
form.addEventListener("submit", async (event) => {
event.preventDefault();
const board = `${host.value}:${slug.value.trim()}`;
// Replacing a different board closes its roles (see set_board in
// app/jobs/sources.py); ask first, naming both boards and the count,
// the same as Detach and a removed search already do.
if (company.board && company.board !== board
&& !window.confirm(`Replace ${company.name}'s board (${company.board}) `
+ `with ${board}? Its ${count(company.open_roles, "open role")} close.`)) {
return;
}
save.disabled = true;
try {
const result = await api(`/api/companies/${encodeURIComponent(company.key)}/board`,
{ method: "POST", body: { board: `${host.value}:${slug.value.trim()}` } });
{ method: "POST", body: { board } });
const closed = result.closed ? `, ${count(result.closed, "old role")} closed` : "";
window.toast.info(`${company.name}: reading ${result.source.target}, `
+ `${count(result.found, "role")} listed${closed}.`);
window.toast.info(`${company.name}: ${result.source.target} listed `
+ `${count(result.found, "role")}${closed}. Reading now, or with the pull right after.`);
await done();
} catch (err) {
save.disabled = false;
@@ -168,6 +178,11 @@
detach.className = "linky";
detach.textContent = "Detach";
detach.addEventListener("click", async () => {
if (company.open_roles
&& !window.confirm(`Detach ${company.name}'s board (${company.board})? `
+ `Its ${count(company.open_roles, "open role")} close.`)) {
return;
}
detach.disabled = true;
try {
const result = await api(`/api/companies/${encodeURIComponent(company.key)}/board`,
@@ -193,6 +208,14 @@
box.checked = Boolean(company.on);
box.setAttribute("aria-label", `Read ${company.name}'s board`);
box.addEventListener("change", async () => {
// Switching off closes every open role in one tap; ask first, naming
// the count, as removing a search and deleting an agent already do.
if (!box.checked && company.open_roles
&& !window.confirm(`Switch off ${company.name}'s board? `
+ `Its ${count(company.open_roles, "open role")} close.`)) {
box.checked = true;
return;
}
box.disabled = true;
try {
const result = await api(`/api/sources/${company.subscription_id}/switch`,
@@ -200,6 +223,8 @@
company.on = result.source.on;
if (!box.checked && result.closed) {
window.toast.info(`${company.name}: ${count(result.closed, "open role")} closed.`);
} else if (box.checked) {
window.toast.info(`${company.name}: reading now, or with the pull right after.`);
}
} catch (err) {
box.checked = !box.checked;
@@ -263,19 +288,30 @@
event.preventDefault();
const status = $("#board-status");
const button = $("#board-form button[type=submit]");
const name = $("#b-name").value.trim();
const board = `${$("#b-host").value}:${$("#b-slug").value.trim()}`;
// This name may already be a company with a different board: `set_board`
// would replace it and close its roles, the same as the row's own
// editor does, so it asks first too.
const existing = [...companies.values()]
.find((c) => c.name.toLowerCase() === name.toLowerCase());
if (existing && existing.board && existing.board !== board
&& !window.confirm(`Replace ${existing.name}'s board (${existing.board}) `
+ `with ${board}? Its ${count(existing.open_roles, "open role")} close.`)) {
return;
}
button.disabled = true;
status.textContent = "Reading the board once before following it…";
const board = `${$("#b-host").value}:${$("#b-slug").value.trim()}`;
try {
const result = await api("/api/boards", {
method: "POST",
body: { board, name: $("#b-name").value.trim() },
body: { board, name },
});
await loadCompanies(agentId());
render();
$("#board-form").reset();
status.textContent = `Following ${result.source.label}: `
+ `${count(result.found, "role")} listed. They arrive with the next pull.`;
+ `${count(result.found, "role")} listed. Reading now, or with the pull right after.`;
} catch (err) {
status.textContent = "";
}
+92 -42
View File
@@ -47,7 +47,7 @@
(function () {
"use strict";
const { api, esc, debounce, companies, loadCompanies, affinityControl, judgeButtons,
const { api, esc, debounce, safeUrl, companies, loadCompanies, affinityControl, judgeButtons,
loadTracked } = window;
const $ = (sel, root = document) => root.querySelector(sel);
@@ -107,10 +107,9 @@
pushedReader: false, // we own a history entry, so closing is `history.back()`
summarising: null, // the uid whose summary is being waited for
listY: 0, // where the page was scrolled when the reader replaced the feed
feedError: false, // the feed's own fetch failed; the empty state offers a retry
};
const safeUrl = (u) => (/^https?:\/\//i.test(u || "") ? u : "");
function ago(iso) {
const mins = (Date.now() - new Date(iso)) / 60000;
if (!Number.isFinite(mins)) return "";
@@ -202,11 +201,20 @@
try {
data = await api(`/api/roles?${params}`);
} catch (err) {
// An append failure leaves what is already on screen; a first load or
// a reload has nothing to show, so the empty state offers a retry
// rather than leaving the feed blank for good.
if (!append && request === feedRequest) {
state.feedError = true;
state.loaded = false;
renderFeed();
}
return;
} finally {
if (request === feedRequest) state.loading = false;
}
if (request !== feedRequest) return;
state.feedError = false;
const held = append ? state.items[state.selected]?.uid : state.readingUid;
state.total = data.total;
@@ -475,15 +483,23 @@
for (const el of had.values()) el.remove();
}
/* The summary when there is one, else the description's opening, cut at a
word. */
/* Built on the server (`app.models.excerpt`) and served as `role.excerpt`,
so the list response never carries the full description a row would
otherwise need to cut this from — see docs/decisions.md, "The pages". */
function excerpt(role) {
if (role.summary) return role.summary;
const text = (role.description || "").replace(/\s+/g, " ").trim();
if (text.length <= 160) return text;
const cut = text.slice(0, 160);
return role.excerpt || "";
}
/* The same cut, run here instead: for the one row `merge` refreshes after
a fetch or a summarise, off the full role that call answers with, which
is the only time this page ever sees a role's `summary` or
`description` directly. */
function excerptOf(role) {
const text = (role.summary || role.description || "").replace(/\s+/g, " ").trim();
if (text.length <= 200) return text;
const cut = text.slice(0, 200);
const space = cut.lastIndexOf(" ");
return `${(space > 100 ? cut.slice(0, space) : cut).replace(/[\s,.;:–-]+$/, "")}…`;
return `${(space > 120 ? cut.slice(0, space) : cut).replace(/[\s,.;:–-]+$/, "")}…`;
}
function tagChips(tags) {
@@ -584,21 +600,29 @@
sync($("#roles-list"), state.items.map((role, i) => [role.uid, { role, i }]), makeRow, fillRow);
const empty = $("#feed-empty");
empty.hidden = !(state.loaded && !state.items.length);
empty.hidden = !((state.loaded || state.feedError) && !state.items.length);
if (!empty.hidden) {
const f = state.filter;
const filtered = state.q || f.tags.size || f.period !== "all" || !f.showRead
|| f.company.trim() || f.place.trim() || f.function || f.remote || f.pay || f.judged;
empty.innerHTML = filtered
? `<h2>Nothing matches</h2>
<p>No open role fits these filters.
<button type="button" class="linky" data-clear>Clear them</button> and start again.</p>`
: `<h2>No roles yet</h2>
<p>Nothing has been pulled yet. <a href="/settings/sources#runs">Runs</a> starts one;
<a href="/settings/sources#job-boards">Job boards</a> and
<a href="/settings/sources#company-boards">Company boards</a> decide what it brings in.</p>`;
const clear = empty.querySelector("[data-clear]");
if (clear) clear.onclick = () => { state.q = ""; $("#search").value = ""; clearFilters(); };
if (state.feedError) {
empty.innerHTML = `<h2>Could not load the feed</h2>
<p>The server did not answer. <button type="button" class="linky" data-retry
>Try again</button>.</p>`;
const retry = empty.querySelector("[data-retry]");
if (retry) retry.onclick = () => loadFeed();
} else {
const f = state.filter;
const filtered = state.q || f.tags.size || f.period !== "all" || !f.showRead
|| f.company.trim() || f.place.trim() || f.function || f.remote || f.pay || f.judged;
empty.innerHTML = filtered
? `<h2>Nothing matches</h2>
<p>No open role fits these filters.
<button type="button" class="linky" data-clear>Clear them</button> and start again.</p>`
: `<h2>No roles yet</h2>
<p>Nothing has been pulled yet. <a href="/settings/sources#runs">Runs</a> starts one;
<a href="/settings/sources#job-boards">Job boards</a> and
<a href="/settings/sources#company-boards">Company boards</a> decide what it brings in.</p>`;
const clear = empty.querySelector("[data-clear]");
if (clear) clear.onclick = () => { state.q = ""; $("#search").value = ""; clearFilters(); };
}
}
// The end of the list says which end it is: a list that has run out and
@@ -755,9 +779,13 @@
}
if (!open) { el.replaceChildren(); return; }
// The list row is already in memory, and a list row is the whole role, so
// the pane paints from it at once and the fetch only brings it up to date.
// The list row is already in memory, and carries what a row draws —
// title, company, place, tags, pay — so the pane paints those at once;
// the body, the summary state and the original link wait on the fetch
// (`loaded`), since the list row no longer carries them (see
// `Store.roles_page`).
const stub = state.items.find((r) => r.uid === state.readingUid);
const loaded = Boolean(state.reading) && state.reading.uid === state.readingUid;
const r = state.reading || stub || {};
const same = el.dataset.uid === state.readingUid;
const scrolled = same ? el.querySelector(".reader-body")?.scrollTop || 0 : 0;
@@ -768,13 +796,13 @@
const text = (r.article_text || r.description || "").trim();
const body = error
? `<p class="reader-note">This role could not be loaded: ${esc(error)}</p>`
: !r.uid
: !loaded
? `<p class="reader-note">Loading…</p>`
: text ? paragraphs(text)
: `<p class="reader-note">The board gave no description for this one.</p>`;
const pay = payDisplay(r);
const waiting = state.summarising === state.readingUid;
const teaser = r.uid && !r.article_text && out && (r.description || "").length < TEASER;
const teaser = loaded && !r.article_text && out && (r.description || "").length < TEASER;
el.innerHTML = `
<div class="reader-bar">
@@ -801,7 +829,7 @@
${r.summary ? `<section class="reader-summary">
<h2 class="reader-label">Summary</h2><p>${esc(r.summary)}</p></section>` : ""}
<div class="reader-tools">
${r.uid && !r.summary && !waiting
${loaded && !r.summary && !waiting
? `<button type="button" class="secondary" data-tool="summarise">Summarise</button>` : ""}
${teaser ? `<button type="button" class="secondary" data-tool="fetch">Fetch the posting</button>` : ""}
<span class="muted small" role="status">${waiting
@@ -855,9 +883,9 @@
else state.reading = updated;
const item = state.items.find((x) => x.uid === updated.uid);
if (item) {
Object.assign(item, { summary: updated.summary, article_text: updated.article_text });
item.excerpt = excerptOf(updated);
const row = $(`#roles-list .row[data-uid="${CSS.escape(updated.uid)}"]`);
if (row) row.role = null; // drawn again from the new text
if (row) row.role = null; // drawn again from the new excerpt
renderFeed();
}
renderReader();
@@ -1116,19 +1144,41 @@
wire();
renderRail();
renderActive();
// The agent first: the companies, the feed and the counts are its.
try {
await Promise.all([loadTracked(), loadAgents()]);
chooseAgent();
await loadCompanies(state.filter.agent);
} catch (err) {
return;
}
renderRail();
renderActive();
loadFilters().catch(() => {});
// The feed is the page; agents and companies are enhancements that fill
// in when they arrive, not something it waits on — a slow or failed
// /api/agents or /api/companies must not leave it blank. Each one drawn
// here is greyed or missing until `enhance` lands, which is what "no
// agent" (renderAgent), an unjoined company (paintRow) and the tag row
// being empty already mean, so nothing here is a new state to handle.
const before = state.filter.agent;
const enhance = (async () => {
try {
await Promise.all([loadTracked(), loadAgents()]);
chooseAgent();
await loadCompanies(state.filter.agent);
} catch (err) {
return;
}
renderRail();
renderActive();
loadFilters().catch(() => {});
// The feed's first request went out with whatever agent `readUrl` had
// (often none): reload for the score and votes once the real one is
// known, and either way bring the reader's affinity control and judge
// buttons up to date, since `companies` was still empty when it first
// painted.
if (state.filter.agent !== before) reload();
else renderFeed();
if (state.readingUid) {
if (state.filter.agent !== before) refreshReader(state.readingUid);
else renderReader();
}
})();
route();
await loadFeed({ keep: was ? was.count : 0 });
if (was && !state.readingUid) window.scrollTo(0, was.y || 0);
await enhance;
});
})();
+26 -10
View File
@@ -14,7 +14,7 @@
(function () {
"use strict";
const { api, count, debounce } = window;
const { api, count, debounce, safeUrl } = window;
const $ = (sel, root = document) => root.querySelector(sel);
/** @type {{searches: object[], adzuna_key: boolean}} */
@@ -34,6 +34,14 @@
box.checked = source.on;
box.setAttribute("aria-label", `Run ${source.label}`);
box.addEventListener("change", async () => {
// Switching off closes every open role in one tap; ask first, naming
// the count, as removing a search and deleting an agent already do.
if (!box.checked && source.open_roles
&& !window.confirm(`Switch off ${source.label}? `
+ `Its ${count(source.open_roles, "open role")} close.`)) {
box.checked = true;
return;
}
box.disabled = true;
try {
const result = await api(`/api/sources/${source.id}/switch`,
@@ -41,6 +49,8 @@
Object.assign(source, result.source);
if (!box.checked && result.closed) {
window.toast.info(`${source.label}: ${count(result.closed, "open role")} closed.`);
} else if (box.checked) {
window.toast.info(`${source.label}: reading now, or with the pull right after.`);
}
} catch (err) {
box.checked = !box.checked;
@@ -158,12 +168,18 @@
const list = $("#search-sample");
for (const row of result.sample) {
const li = document.createElement("li");
const link = document.createElement("a");
link.href = row.url;
link.target = "_blank";
link.rel = "noopener";
link.textContent = row.title;
li.append(link, ` — ${[row.company, row.place].filter(Boolean).join(", ")}`);
const url = safeUrl(row.url);
if (url) {
const link = document.createElement("a");
link.href = url;
link.target = "_blank";
link.rel = "noopener";
link.textContent = row.title;
li.appendChild(link);
} else {
li.append(row.title);
}
li.append(` — ${[row.company, row.place].filter(Boolean).join(", ")}`);
list.appendChild(li);
}
} catch (err) {
@@ -184,7 +200,7 @@
renderSearches();
fillSearchForm(null);
status.textContent = `Saved ${result.source.label}. ${found(result)} `
+ "Its roles arrive with the next pull.";
+ "Reading now, or with the pull right after.";
} catch (err) {
status.textContent = "";
}
@@ -208,8 +224,8 @@
// Sources shows one section: the one the fragment names or contains, or the
// first when there is none or it names none, so `/settings/sources#runs` (a
// rail link, an old path's 301, the Search agents page's "follow it under
// Runs") opens Runs, and so does `#messages`, the log at its end. The rest
// rail link, or the Search agents page's "follow it under Runs") opens
// Runs, and so does `#messages`, the log at its end. The rest
// are hidden, not removed, so their scripts still find their roots. It runs
// as the script loads rather than on DOMContentLoaded, so the page never
// paints all three first.