vienalatina/apps/board/home.py
Claude 5ea6b1ac98
One dashboard, five sections, and a moderation queue
The members area had grown one navigation entry per phase, and three of the
five were inside an admin-only block in base.html — so an ordinary member
signed in to a wall and a member list, with their own profile, their own
private messages and the content editor all invisible. Everything worked and
nothing was reachable. That is the bug this starts from.

The navigation is now Inicio · Muro · Privados · Publicaciones, plus Gestión
for admins, and the account items (Mi perfil, Descargar mis datos, Salir) moved
to a menu under the member's own name — a <details> element, because the CSP has
no 'unsafe-inline' and a menu needing onclick="" silently never opens. Inicio is
a new dashboard; the wall moved from /comunidad/ to /comunidad/muro and kept its
thread URLs. test_navigation.py asserts the navigation per role from the
rendered page, so this cannot come back quietly.

Public posts are now curated. A member proposes one and it waits in a new
`submissions` table; a moderator or admin approves it — which commits it
through content.publish, the same function the editor uses, with the author
named per commit so git history says who wrote it, not who approved it — or
returns it with a reason the author reads. Moderators and admins do not queue.
Nothing a member writes reaches the repository before approval, because a
commit to it *is* publication: the webhook starts the pipeline. A proposal's
picture stays private in /data/uploads until then, and the private copy goes
when the public one is committed. There is deliberately no way to promote a wall
thread into a proposal.

The moderator role needed the role CHECK widened, and SQLite has no DROP
CONSTRAINT, so migration 4 rebuilds `members` — eight tables reference it, the
one-owner index has to be recreated by hand, and PRAGMA foreign_key_check runs
before the commit. One trap worth recording: the allowed roles can only be read
back out of sqlite_master's verbatim CREATE TABLE text, comments included, so
schema.sql's own comment explaining the rebuild made the step decide it had
already run. The start-up test caught it; comments are stripped now.

A moderator curates content and has no power over people: no creating,
suspending, promoting or erasing anybody. set_role moved from owner-only to
admin-level so moderators can be appointed routinely, and checks may_create on
the new role as well, so admins still cannot mint admins. content.py splits per
collection — posts need a moderator, the site's static pages need an admin.

Also: erasure deletes a member's submissions and their pictures and clears
reviewed_by, which the existing foreign-key guard forced; deploy-board.sh takes
a backup before restarting, since two migrations now rebuild a table in place
and one of them holds your own account; README and docs/server-setup.md updated
for the roles, the sections and migration 4.

370 tests pass.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01NizVpJ2dwzCbjCrTLCjeHn
2026-09-30 11:09:55 +00:00

112 lines
4.5 KiB
Python

"""Inicio — the one screen a member lands on.
Every section of the members area was added in its own phase, and each one added
an entry to a flat navigation until signing in meant choosing between five words
with no idea which one held anything new. This page answers that first question:
what has happened since I was last here, and what is waiting for me.
It owns no data. Every panel is a query the module responsible already runs for
its own page — the wall's newest threads, the unread count from `messages`, the
queue from `submissions` — gathered here and linked back there. Nothing on this
page is the only way to reach anything.
"""
from __future__ import annotations
from flask import Blueprint, g, render_template
from . import submissions
from .db import TOMBSTONE_LOGIN, get_db
from .render import excerpt
from .security import admin_required, login_required
bp = Blueprint("home", __name__)
RECENT = 5
@bp.route("/")
@login_required
def index():
db = get_db()
threads = db.execute(
"""SELECT t.id, t.title, t.body_md, t.created_at, m.display_name AS author,
(SELECT COUNT(*) FROM comments c
WHERE c.thread_id = t.id AND c.deleted_at IS NULL) AS replies
FROM threads t JOIN members m ON m.id = t.author_id
WHERE t.deleted_at IS NULL
ORDER BY t.created_at DESC LIMIT ?""",
(RECENT,),
).fetchall()
conversations = db.execute(
"""SELECT other.id AS other_id, other.display_name AS other_name,
(SELECT COUNT(*) FROM messages msg
WHERE msg.conversation_id = c.id AND msg.author_id != mine.member_id
AND msg.deleted_at IS NULL
AND (mine.last_read_at IS NULL OR msg.created_at > mine.last_read_at)
) AS unread,
(SELECT MAX(created_at) FROM messages
WHERE conversation_id = c.id AND deleted_at IS NULL) AS last_at
FROM conversations c
JOIN conversation_members mine
ON mine.conversation_id = c.id AND mine.member_id = :me
JOIN conversation_members theirs
ON theirs.conversation_id = c.id AND theirs.member_id != :me
JOIN members other ON other.id = theirs.member_id
ORDER BY last_at DESC NULLS LAST LIMIT :limit""",
{"me": g.member["id"], "limit": RECENT},
).fetchall()
mine = db.execute(
"""SELECT id, title, state, note FROM submissions
WHERE author_id = ? ORDER BY created_at DESC LIMIT ?""",
(g.member["id"], RECENT),
).fetchall()
# Only the people who can act on it get the queue; for everyone else the
# panel is not drawn, rather than drawn empty.
queue = []
if submissions.may_moderate():
queue = db.execute(
"""SELECT s.id, s.title, s.created_at, m.display_name AS author
FROM submissions s JOIN members m ON m.id = s.author_id
WHERE s.state = 'pending' AND s.author_id != ?
ORDER BY s.created_at LIMIT ?""",
(g.member["id"], RECENT),
).fetchall()
people = db.execute(
"""SELECT COUNT(*) AS n FROM members
WHERE active = 1 AND role != 'tombstone' AND gitea_login != ?""",
(TOMBSTONE_LOGIN,),
).fetchone()["n"]
return render_template("home.html", threads=threads, conversations=conversations,
mine=mine, queue=queue, people=people, excerpt=excerpt,
state_labels=submissions.STATE_LABELS)
@bp.route("/gestion")
@admin_required
def gestion():
"""Administration, which is two links and a count rather than a screen of
its own — Miembros and the static pages both already have their pages. It
exists so that neither of them has to sit in the navigation every member
sees, which is how *Contenido* ended up looking like a tab for everybody.
"""
db = get_db()
counts = {
"members": db.execute(
"SELECT COUNT(*) AS n FROM members WHERE role != 'tombstone'").fetchone()["n"],
"suspended": db.execute(
"SELECT COUNT(*) AS n FROM members WHERE active = 0 AND role != 'tombstone'"
).fetchone()["n"],
"moderators": db.execute(
"SELECT COUNT(*) AS n FROM members WHERE role IN ('owner', 'admin', 'moderator')"
).fetchone()["n"],
"pending": db.execute(
"SELECT COUNT(*) AS n FROM submissions WHERE state = 'pending'").fetchone()["n"],
}
return render_template("gestion.html", counts=counts)