Roles is newsdesk's feed: rail, feed and reader, with read state and tags
The Roles tab takes newsdesk's shell, ported from its index.html and
app.css rather than rewritten: a filter rail (Show read, Best first or
Newest first, first seen, tag chips with counts, the old filters behind a
disclosure), the feed in newsdesk's row layout (score gutter, title,
company, place, badges, age, tags, the summary as excerpt, judge buttons)
and the reader as a third column that replaces the list on a phone. The
router pushes /roles/{uid}, which the server also renders with the reader
open, so the separate read page and role.js go. j/k, u/n/d, S, Enter,
Escape, o, /, f and c work as in newsdesk. The phone rules come with their
comments.
Two things the row lacked: roles.read_at, stamped when the reader opens a
role or a vote is cast, behind include_read on /api/roles; and role_tags,
written by summarise alongside the summary and served with counts by
/api/filters, filtered in SQL before LIMIT. Migration 5.
PyCharm run configurations in .run/ for serve, pull, seed and pytest.
core.autocrlf=input is set locally so a Windows-side checkout commits LF.
Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
515d2250fc
commit
a9290a6926
23 files changed
+2252
-715
No files matched your search
@@ -71,6 +71,9 @@ CREATE TABLE IF NOT EXISTS roles (
|
||||
-- neutral, 1 like. It moves the rank at read time and never hides a role.
|
||||
vote INTEGER CHECK (vote IN (-1, 0, 1)),
|
||||
voted_at TEXT,
|
||||
-- NULL unread, else when the feed first opened it or judged it: a vote
|
||||
-- stamps this too, see Store.set_vote, because voting is reading.
|
||||
read_at TEXT,
|
||||
-- About 60 words from summarise, and when it wrote them.
|
||||
summary TEXT NOT NULL DEFAULT '',
|
||||
summarised_at TEXT,
|
||||
@@ -81,6 +84,15 @@ CREATE TABLE IF NOT EXISTS roles (
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- The open vocabulary summarise reads off a posting: the role family, the
|
||||
-- technologies and skills it names, and the domain. What the feed's filter
|
||||
-- row is built from; see Store.set_tags and Store.tag_counts.
|
||||
CREATE TABLE IF NOT EXISTS role_tags (
|
||||
uid TEXT NOT NULL REFERENCES roles(uid) ON DELETE CASCADE,
|
||||
tag TEXT NOT NULL,
|
||||
PRIMARY KEY (uid, tag)
|
||||
);
|
||||
|
||||
-- A company as a target, whether or not any board of it is read. key is
|
||||
-- models.company_key(name), the slug roles.company_key holds.
|
||||
CREATE TABLE IF NOT EXISTS companies (
|
||||
@@ -208,6 +220,7 @@ CREATE INDEX IF NOT EXISTS idx_application_events
|
||||
ON application_events(application_id, at);
|
||||
CREATE INDEX IF NOT EXISTS idx_scores_agent ON scores(agent_id, score DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_vectors_model ON vectors(model);
|
||||
CREATE INDEX IF NOT EXISTS idx_role_tags_tag ON role_tags(tag);
|
||||
"""
|
||||
|
||||
#: The table whose absence marks a file that has never been a roledesk
|
||||
@@ -244,6 +257,14 @@ ALTER TABLE roles ADD COLUMN summary TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE roles ADD COLUMN summarised_at TEXT;
|
||||
ALTER TABLE roles ADD COLUMN article_text TEXT NOT NULL DEFAULT '';
|
||||
ALTER TABLE roles ADD COLUMN article_at TEXT;
|
||||
"""),
|
||||
(5, "roles.read_at and role_tags, for the feed's read state and tags", """
|
||||
ALTER TABLE roles ADD COLUMN read_at TEXT;
|
||||
CREATE TABLE role_tags (
|
||||
uid TEXT NOT NULL REFERENCES roles(uid) ON DELETE CASCADE,
|
||||
tag TEXT NOT NULL,
|
||||
PRIMARY KEY (uid, tag)
|
||||
);
|
||||
"""),
|
||||
)
|
||||
|
||||
|
||||
+163
-25
@@ -69,6 +69,29 @@ _COMPANY_FIELDS = ("name", "domain", "affinity", "status", "careers_url", "notes
|
||||
_SUBSCRIPTION_FIELDS = ("label", "company", "board", "params", "subscribed", "fetch",
|
||||
"show", "target_id")
|
||||
|
||||
#: A tag's longest stored form, and how many a role may carry. Long enough for
|
||||
#: "senior backend engineer" and short enough that the filter row stays a row.
|
||||
_MAX_TAG_LEN = 40
|
||||
_MAX_TAGS = 12
|
||||
|
||||
|
||||
def _normalise_tags(tags: Sequence[str]) -> list[str]:
|
||||
"""Each tag stripped, lowercased, its internal whitespace collapsed to one
|
||||
space and cut to `_MAX_TAG_LEN`; empties and repeats dropped, order kept,
|
||||
capped at `_MAX_TAGS`. What `set_tags` and `set_summary` store, so the
|
||||
filter row never has to trust what a model or a person typed."""
|
||||
seen: set[str] = set()
|
||||
out: list[str] = []
|
||||
for tag in tags:
|
||||
cleaned = " ".join(str(tag).split()).lower()[:_MAX_TAG_LEN]
|
||||
if not cleaned or cleaned in seen:
|
||||
continue
|
||||
seen.add(cleaned)
|
||||
out.append(cleaned)
|
||||
if len(out) >= _MAX_TAGS:
|
||||
break
|
||||
return out
|
||||
|
||||
|
||||
def _iso(moment: datetime) -> str:
|
||||
"""UTC, to the second. Every timestamp column is compared as text, so every
|
||||
@@ -282,30 +305,92 @@ class Store:
|
||||
|
||||
def role(self, uid: str) -> Role | None:
|
||||
row = self._row("SELECT * FROM roles WHERE uid=?", (uid,))
|
||||
return Role.from_row(row) if row else None
|
||||
if row is None:
|
||||
return None
|
||||
role = Role.from_row(row)
|
||||
role.tags = self._tags_of(uid)
|
||||
return role
|
||||
|
||||
def set_vote(self, uid: str, value: int | None) -> Role:
|
||||
"""Judge one role: 1 like, 0 neither way, -1 pass, None unjudged again.
|
||||
Stamps `voted_at`, or clears it with the vote. A vote never hides a
|
||||
role; it moves it in a ranking (see `roles`). Raises ValueError for
|
||||
another value and KeyError for an unknown uid."""
|
||||
if isinstance(value, bool) or value not in VOTES:
|
||||
raise ValueError(f"a vote is -1, 0 or 1, not {value!r}")
|
||||
stamp = None if value is None else self.now()
|
||||
if not self._write("UPDATE roles SET vote=?, voted_at=? WHERE uid=?",
|
||||
(value, stamp, uid)):
|
||||
def set_read(self, uid: str, read: bool = True) -> Role:
|
||||
"""Mark a role read, or unread again. Stamps `read_at` only the first
|
||||
time — a role already read keeps the moment it was first opened —
|
||||
and clears it outright when `read` is False. Raises KeyError for an
|
||||
unknown uid."""
|
||||
if read:
|
||||
changed = self._write(
|
||||
"UPDATE roles SET read_at = COALESCE(read_at, ?) WHERE uid = ?",
|
||||
(self.now(), uid))
|
||||
else:
|
||||
changed = self._write("UPDATE roles SET read_at = NULL WHERE uid = ?", (uid,))
|
||||
if not changed:
|
||||
raise KeyError(uid)
|
||||
role = self.role(uid)
|
||||
assert role is not None
|
||||
return role
|
||||
|
||||
def set_vote(self, uid: str, value: int | None) -> Role:
|
||||
"""Judge one role: 1 like, 0 neither way, -1 pass, None unjudged again.
|
||||
Stamps `voted_at`, or clears it with the vote. A vote never hides a
|
||||
role; it moves it in a ranking (see `roles`). Voting is reading, so
|
||||
it stamps `read_at` too, the same once-only way `set_read` does;
|
||||
clearing a vote is not un-reading, and leaves it alone. Raises
|
||||
ValueError for another value and KeyError for an unknown uid."""
|
||||
if isinstance(value, bool) or value not in VOTES:
|
||||
raise ValueError(f"a vote is -1, 0 or 1, not {value!r}")
|
||||
stamp = None if value is None else self.now()
|
||||
if not self._write(
|
||||
"UPDATE roles SET vote=?, voted_at=?, read_at=COALESCE(read_at, ?) "
|
||||
"WHERE uid=?", (value, stamp, stamp, uid)):
|
||||
raise KeyError(uid)
|
||||
role = self.role(uid)
|
||||
assert role is not None
|
||||
return role
|
||||
|
||||
def set_tags(self, uid: str, tags: Sequence[str]) -> list[str]:
|
||||
"""Replace one role's tags in one transaction. See `_normalise_tags`
|
||||
for how each one is cleaned up. Returns what was stored, sorted the
|
||||
way `role` and `roles` read it back. Raises KeyError for an unknown
|
||||
uid."""
|
||||
with self.transaction() as conn:
|
||||
if conn.execute("SELECT 1 FROM roles WHERE uid=?", (uid,)).fetchone() is None:
|
||||
raise KeyError(uid)
|
||||
return self._replace_tags(conn, uid, tags)
|
||||
|
||||
def _replace_tags(self, conn: sqlite3.Connection, uid: str,
|
||||
tags: Sequence[str]) -> list[str]:
|
||||
"""`set_tags`'s write, against an already-open transaction so
|
||||
`set_summary` can share it."""
|
||||
cleaned = _normalise_tags(tags)
|
||||
conn.execute("DELETE FROM role_tags WHERE uid=?", (uid,))
|
||||
conn.executemany("INSERT INTO role_tags (uid, tag) VALUES (?,?)",
|
||||
[(uid, tag) for tag in cleaned])
|
||||
return sorted(cleaned)
|
||||
|
||||
def _tags_of(self, uid: str) -> list[str]:
|
||||
return [str(r["tag"]) for r in self._rows(
|
||||
"SELECT tag FROM role_tags WHERE uid=? ORDER BY tag", (uid,))]
|
||||
|
||||
def _tags_by_uid(self, uids: Sequence[str]) -> dict[str, list[str]]:
|
||||
"""Every tag of these roles, by uid, sorted — one query for a whole
|
||||
page rather than one per role."""
|
||||
if not uids:
|
||||
return {}
|
||||
out: dict[str, list[str]] = {}
|
||||
rows = self._rows(
|
||||
"SELECT uid, tag FROM role_tags WHERE uid IN (SELECT value FROM json_each(?)) "
|
||||
"ORDER BY uid, tag", (json.dumps(list(uids)),))
|
||||
for r in rows:
|
||||
out.setdefault(r["uid"], []).append(str(r["tag"]))
|
||||
return out
|
||||
|
||||
def roles(self, limit: int = 100, company: str | None = None,
|
||||
place: str | None = None, open_only: bool = True,
|
||||
q: str | None = None, function: str | None = None,
|
||||
remote: str | None = None, days: int | None = None,
|
||||
hide_muted: bool = False, agent: int | None = None,
|
||||
gated: bool = False, judged: str | None = None,
|
||||
offset: int = 0) -> list[ScoredRole]:
|
||||
hide_muted: bool = False, include_read: bool = True,
|
||||
tags: Sequence[str] = (), tag_mode: str = "any",
|
||||
agent: int | None = None, gated: bool = False,
|
||||
judged: str | None = None, offset: int = 0) -> list[ScoredRole]:
|
||||
"""Newest first by `first_seen`. `company` and `place` are
|
||||
case-insensitive substrings of the columns a board wrote — not the
|
||||
key, so a search does not have to know the exact slug a company folds
|
||||
@@ -322,6 +407,12 @@ class Store:
|
||||
leaving nothing to show for a default query that would otherwise
|
||||
have plenty.
|
||||
|
||||
`include_read` set False excludes a role `set_read` or `set_vote` has
|
||||
stamped, the same way and for the same reason as `hide_muted`:
|
||||
before `limit`, not after. `tags` keeps only roles carrying at least
|
||||
one of them (`tag_mode="any"`, the default) or every one
|
||||
(`tag_mode="all"`), also before `limit`.
|
||||
|
||||
`agent` orders by that agent's score instead, highest first, and
|
||||
carries `score` and `gated_reason` on each row. Only roles it has
|
||||
scored are listed, and a gated one only when `gated` is set. The score
|
||||
@@ -329,37 +420,45 @@ class Store:
|
||||
shows in the order at once and needs no rescore.
|
||||
|
||||
`offset` skips that many rows of the same order, for the page that
|
||||
loads the next 60 as it is scrolled; `count_roles` is the total."""
|
||||
loads the next 60 as it is scrolled; `count_roles` is the total.
|
||||
Every returned role carries its tags, read with one extra query for
|
||||
the whole page rather than one per role."""
|
||||
sql, params = self._roles_from(
|
||||
company=company, place=place, open_only=open_only, q=q, function=function,
|
||||
remote=remote, days=days, hide_muted=hide_muted, agent=agent, gated=gated,
|
||||
judged=judged)
|
||||
remote=remote, days=days, hide_muted=hide_muted, include_read=include_read,
|
||||
tags=tags, tag_mode=tag_mode, agent=agent, gated=gated, judged=judged)
|
||||
if agent is None:
|
||||
select = "SELECT roles.* "
|
||||
order = "roles.first_seen DESC, roles.uid"
|
||||
else:
|
||||
select = f"SELECT roles.*, {_VOTED_SCORE} AS score, s.gated_reason "
|
||||
order = "score DESC, roles.first_seen DESC, roles.uid"
|
||||
return [ScoredRole.from_row(r) for r in self._rows(
|
||||
found = [ScoredRole.from_row(r) for r in self._rows(
|
||||
f"{select}{sql} ORDER BY {order} LIMIT ? OFFSET ?", [*params, limit, offset])]
|
||||
by_uid = self._tags_by_uid([r.uid for r in found])
|
||||
for r in found:
|
||||
r.tags = by_uid.get(r.uid, [])
|
||||
return found
|
||||
|
||||
def count_roles(self, company: str | None = None, place: str | None = None,
|
||||
open_only: bool = True, q: str | None = None,
|
||||
function: str | None = None, remote: str | None = None,
|
||||
days: int | None = None, hide_muted: bool = False,
|
||||
agent: int | None = None, gated: bool = False,
|
||||
judged: str | None = None) -> int:
|
||||
include_read: bool = True, tags: Sequence[str] = (),
|
||||
tag_mode: str = "any", agent: int | None = None,
|
||||
gated: bool = False, judged: str | None = None) -> int:
|
||||
"""How many roles `roles` would list with the same filters and no limit."""
|
||||
sql, params = self._roles_from(
|
||||
company=company, place=place, open_only=open_only, q=q, function=function,
|
||||
remote=remote, days=days, hide_muted=hide_muted, agent=agent, gated=gated,
|
||||
judged=judged)
|
||||
remote=remote, days=days, hide_muted=hide_muted, include_read=include_read,
|
||||
tags=tags, tag_mode=tag_mode, agent=agent, gated=gated, judged=judged)
|
||||
row = self._row(f"SELECT COUNT(*) AS n {sql}", params)
|
||||
return int(row["n"]) if row else 0
|
||||
|
||||
def _roles_from(self, *, company: str | None, place: str | None, open_only: bool,
|
||||
q: str | None, function: str | None, remote: str | None,
|
||||
days: int | None, hide_muted: bool, agent: int | None,
|
||||
days: int | None, hide_muted: bool, include_read: bool,
|
||||
tags: Sequence[str], tag_mode: str, agent: int | None,
|
||||
gated: bool, judged: str | None) -> tuple[str, list[Any]]:
|
||||
"""The `FROM ... WHERE ...` that `roles` and `count_roles` share, with
|
||||
its parameters in order."""
|
||||
@@ -392,6 +491,22 @@ class Store:
|
||||
if hide_muted:
|
||||
where.append("company_key NOT IN "
|
||||
"(SELECT key FROM companies WHERE affinity = -1)")
|
||||
if not include_read:
|
||||
where.append("roles.read_at IS NULL")
|
||||
if tags:
|
||||
marks = ", ".join("?" for _ in tags)
|
||||
if tag_mode == "all":
|
||||
where.append(
|
||||
f"(SELECT COUNT(DISTINCT rt.tag) FROM role_tags rt "
|
||||
f"WHERE rt.uid = roles.uid AND rt.tag IN ({marks})) = ?")
|
||||
params += [*tags, len(tags)]
|
||||
elif tag_mode == "any":
|
||||
where.append(
|
||||
f"EXISTS (SELECT 1 FROM role_tags rt "
|
||||
f"WHERE rt.uid = roles.uid AND rt.tag IN ({marks}))")
|
||||
params += list(tags)
|
||||
else:
|
||||
raise ValueError(f"tag_mode is any or all, not {tag_mode!r}")
|
||||
joins: list[Any] = []
|
||||
if agent is None:
|
||||
sql = "FROM roles"
|
||||
@@ -404,6 +519,23 @@ class Store:
|
||||
sql += " WHERE " + " AND ".join(where)
|
||||
return sql, [*joins, *params]
|
||||
|
||||
def tag_counts(self, show_muted: bool = False) -> list[tuple[str, int, int]]:
|
||||
"""`(tag, open_count, unread_count)` over open roles, most-used tag
|
||||
first and then alphabetically. What `/api/filters` serves; muted
|
||||
companies are left out unless `show_muted`, the same default as
|
||||
`roles`."""
|
||||
where = "r.closed_at IS NULL"
|
||||
if not show_muted:
|
||||
where += (" AND r.company_key NOT IN "
|
||||
"(SELECT key FROM companies WHERE affinity = -1)")
|
||||
rows = self._rows(
|
||||
f"""SELECT rt.tag AS tag, COUNT(*) AS n, SUM(r.read_at IS NULL) AS unread
|
||||
FROM role_tags rt JOIN roles r ON r.uid = rt.uid
|
||||
WHERE {where}
|
||||
GROUP BY rt.tag
|
||||
ORDER BY n DESC, rt.tag ASC""")
|
||||
return [(str(r["tag"]), int(r["n"]), int(r["unread"])) for r in rows]
|
||||
|
||||
def mark_closed(self, source: str, seen_ids: Sequence[str],
|
||||
subscription_id: int) -> int:
|
||||
"""Close every open role of one board that a complete fetch of it did not
|
||||
@@ -561,15 +693,21 @@ class Store:
|
||||
f"SELECT r.* FROM roles r WHERE {where} "
|
||||
"ORDER BY r.first_seen DESC, r.uid LIMIT ?", [*params, limit])]
|
||||
|
||||
def set_summary(self, uid: str, summary: str) -> bool:
|
||||
def set_summary(self, uid: str, summary: str, tags: Sequence[str] = ()) -> bool:
|
||||
"""Write a role's summary and stamp `summarised_at`. Only over an empty
|
||||
one, so a second batch that carried the same role does not replace the
|
||||
first answer. False for an unknown uid or one already summarised."""
|
||||
first answer. Given `tags`, they replace the role's tags in the same
|
||||
transaction — see `_replace_tags` — but only when the summary was
|
||||
actually written, so a rejected late answer cannot plant tags on a
|
||||
role it did not get to summarise. False for an unknown uid or one
|
||||
already summarised."""
|
||||
with self.transaction() as conn:
|
||||
written = conn.execute(
|
||||
"UPDATE roles SET summary = ?, summarised_at = ? "
|
||||
"WHERE uid = ? AND summary = ''", (summary, self.now(), uid)).rowcount
|
||||
conn.execute("DELETE FROM state WHERE key = ?", (self._SUMMARY_MISSED + uid,))
|
||||
if written and tags:
|
||||
self._replace_tags(conn, uid, tags)
|
||||
return bool(written)
|
||||
|
||||
def summarise_missed(self, uids: Sequence[str], why: str = "") -> int:
|
||||
|
||||
Reference in new issue
Block a user