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
246 lines
9.5 KiB
Python
246 lines
9.5 KiB
Python
"""Private messages between two members.
|
|
|
|
An inbox, not a chat. Each conversation has exactly two people, each keeps
|
|
their own unread mark, and either can block the other — after which neither can
|
|
write. That symmetry is deliberate: a block that silences only one side is a
|
|
one-way megaphone, which is worse than having no block at all.
|
|
|
|
Every rule here is enforced in the handler, not only in the template. A hidden
|
|
button is a courtesy to somebody using the site normally; it is not a rule, and
|
|
the difference matters most for exactly the person a block exists to stop.
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from flask import (Blueprint, abort, flash, g, redirect, render_template,
|
|
request, url_for)
|
|
|
|
from . import uploads
|
|
from .db import TOMBSTONE_LOGIN, get_db
|
|
from .render import to_html
|
|
from .security import login_required
|
|
|
|
bp = Blueprint("messages", __name__)
|
|
|
|
BODY_MAX = 20_000
|
|
|
|
|
|
def blocked_between(a: int, b: int) -> bool:
|
|
"""A block in either direction stops both directions."""
|
|
return get_db().execute(
|
|
"""SELECT 1 FROM blocks
|
|
WHERE (blocker_id = ? AND blocked_id = ?)
|
|
OR (blocker_id = ? AND blocked_id = ?)""",
|
|
(a, b, b, a),
|
|
).fetchone() is not None
|
|
|
|
|
|
def existing_conversation(other_id: int) -> int | None:
|
|
"""The conversation between us and `other_id`, or None. Creates nothing."""
|
|
row = get_db().execute(
|
|
"""SELECT a.conversation_id AS id
|
|
FROM conversation_members a
|
|
JOIN conversation_members b ON b.conversation_id = a.conversation_id
|
|
WHERE a.member_id = ? AND b.member_id = ?""",
|
|
(g.member["id"], other_id),
|
|
).fetchone()
|
|
return row["id"] if row else None
|
|
|
|
|
|
def conversation_with(other_id: int) -> int:
|
|
"""The conversation between the signed-in member and `other_id`, made if
|
|
it does not exist yet."""
|
|
db = get_db()
|
|
row = db.execute(
|
|
"""SELECT a.conversation_id AS id
|
|
FROM conversation_members a
|
|
JOIN conversation_members b ON b.conversation_id = a.conversation_id
|
|
WHERE a.member_id = ? AND b.member_id = ?""",
|
|
(g.member["id"], other_id),
|
|
).fetchone()
|
|
if row:
|
|
return row["id"]
|
|
|
|
conversation_id = db.execute(
|
|
"INSERT INTO conversations DEFAULT VALUES").lastrowid
|
|
for member_id in (g.member["id"], other_id):
|
|
db.execute(
|
|
"INSERT INTO conversation_members (conversation_id, member_id) VALUES (?, ?)",
|
|
(conversation_id, member_id),
|
|
)
|
|
return conversation_id
|
|
|
|
|
|
def unread_count() -> int:
|
|
"""For the badge in the navigation. One query, not one per conversation."""
|
|
if g.member is None:
|
|
return 0
|
|
return get_db().execute(
|
|
"""SELECT COUNT(*) AS n
|
|
FROM messages m
|
|
JOIN conversation_members cm
|
|
ON cm.conversation_id = m.conversation_id AND cm.member_id = ?
|
|
WHERE m.author_id != ?
|
|
AND m.deleted_at IS NULL
|
|
AND (cm.last_read_at IS NULL OR m.created_at > cm.last_read_at)""",
|
|
(g.member["id"], g.member["id"]),
|
|
).fetchone()["n"]
|
|
|
|
|
|
# --- routes ---------------------------------------------------------------
|
|
|
|
@bp.route("/privados")
|
|
@login_required
|
|
def inbox():
|
|
rows = get_db().execute(
|
|
"""SELECT c.id,
|
|
other.display_name AS other_name,
|
|
other.id AS other_id,
|
|
(SELECT body_md FROM messages
|
|
WHERE conversation_id = c.id AND deleted_at IS NULL
|
|
ORDER BY created_at DESC LIMIT 1) AS last_body,
|
|
(SELECT created_at FROM messages
|
|
WHERE conversation_id = c.id AND deleted_at IS NULL
|
|
ORDER BY created_at DESC LIMIT 1) AS last_at,
|
|
(SELECT COUNT(*) FROM messages m
|
|
WHERE m.conversation_id = c.id AND m.author_id != mine.member_id
|
|
AND m.deleted_at IS NULL
|
|
AND (mine.last_read_at IS NULL OR m.created_at > mine.last_read_at)
|
|
) AS unread
|
|
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""",
|
|
{"me": g.member["id"]},
|
|
).fetchall()
|
|
|
|
# Anyone you could start writing to: every active member but yourself, the
|
|
# tombstone, and anyone either of you has blocked.
|
|
people = get_db().execute(
|
|
"""SELECT m.id, m.display_name FROM members m
|
|
WHERE m.id != :me AND m.active = 1
|
|
AND m.role != 'tombstone'
|
|
AND m.gitea_login != :ghost
|
|
AND NOT EXISTS (SELECT 1 FROM blocks
|
|
WHERE (blocker_id = :me AND blocked_id = m.id)
|
|
OR (blocker_id = m.id AND blocked_id = :me))
|
|
ORDER BY m.display_name COLLATE NOCASE""",
|
|
{"me": g.member["id"], "ghost": TOMBSTONE_LOGIN},
|
|
).fetchall()
|
|
|
|
return render_template("inbox.html", conversations=rows, people=people,
|
|
excerpt=lambda text: (text or "")[:120])
|
|
|
|
|
|
@bp.route("/privados/con")
|
|
@login_required
|
|
def pick():
|
|
"""The select posts here as a plain GET and is sent on to the conversation.
|
|
|
|
A form cannot put its own value into a path, and rewriting the action with
|
|
JavaScript is not available: the Content-Security-Policy has no
|
|
'unsafe-inline', so an inline handler is ignored without a word. One tiny
|
|
redirect avoids the whole question.
|
|
"""
|
|
return redirect(url_for("messages.talk",
|
|
member_id=request.args.get("member_id", type=int) or 0))
|
|
|
|
|
|
@bp.route("/privados/con/<int:member_id>", methods=["GET", "POST"])
|
|
@login_required
|
|
def talk(member_id: int):
|
|
"""One page for a conversation, whether or not it exists yet.
|
|
|
|
There used to be an "Abrir conversación" step that created an empty
|
|
conversation and sent you to it. It bought nothing — you still had to type
|
|
a message afterwards — and it littered the inbox with rows saying "Sin
|
|
mensajes todavía" for people nobody had actually written to. The
|
|
conversation is now created by the first message, not by looking at
|
|
somebody's name.
|
|
"""
|
|
if member_id == g.member["id"]:
|
|
abort(400)
|
|
other = get_db().execute(
|
|
"""SELECT * FROM members
|
|
WHERE id = ? AND active = 1 AND role != 'tombstone'""",
|
|
(member_id,),
|
|
).fetchone()
|
|
if other is None:
|
|
abort(404)
|
|
blocked = blocked_between(g.member["id"], member_id)
|
|
if blocked and request.method == "POST":
|
|
abort(403)
|
|
|
|
db = get_db()
|
|
conversation_id = existing_conversation(member_id)
|
|
|
|
if request.method == "POST":
|
|
body = request.form.get("body", "").strip()[:BODY_MAX]
|
|
try:
|
|
staged = uploads.stage(request.files.getlist("pictures"))
|
|
except uploads.RejectedUpload as exc:
|
|
flash(str(exc), "error")
|
|
return redirect(url_for("messages.talk", member_id=member_id))
|
|
if not body and not staged:
|
|
flash("Escribe algo o adjunta una imagen.", "error")
|
|
return redirect(url_for("messages.talk", member_id=member_id))
|
|
|
|
# Only now does the conversation come into being.
|
|
if conversation_id is None:
|
|
conversation_id = conversation_with(member_id)
|
|
cursor = db.execute(
|
|
"INSERT INTO messages (conversation_id, author_id, body_md) VALUES (?, ?, ?)",
|
|
(conversation_id, g.member["id"], body),
|
|
)
|
|
uploads.save(staged, g.member["id"], message_id=cursor.lastrowid)
|
|
return redirect(url_for("messages.talk", member_id=member_id) + "#final")
|
|
|
|
rows = []
|
|
if conversation_id is not None:
|
|
rows = db.execute(
|
|
"""SELECT m.*, a.display_name AS author
|
|
FROM messages m JOIN members a ON a.id = m.author_id
|
|
WHERE m.conversation_id = ? AND m.deleted_at IS NULL
|
|
ORDER BY m.created_at""",
|
|
(conversation_id,),
|
|
).fetchall()
|
|
db.execute(
|
|
"""UPDATE conversation_members SET last_read_at = datetime('now')
|
|
WHERE conversation_id = ? AND member_id = ?""",
|
|
(conversation_id, g.member["id"]),
|
|
)
|
|
|
|
return render_template(
|
|
"conversation.html", other=other, messages=rows, to_html=to_html,
|
|
images=uploads.for_messages([row["id"] for row in rows]),
|
|
blocked=blocked,
|
|
)
|
|
|
|
|
|
@bp.route("/privados/bloquear/<int:member_id>", methods=["POST"])
|
|
@login_required
|
|
def block(member_id: int):
|
|
if member_id == g.member["id"]:
|
|
abort(400)
|
|
get_db().execute(
|
|
"""INSERT INTO blocks (blocker_id, blocked_id) VALUES (?, ?)
|
|
ON CONFLICT DO NOTHING""",
|
|
(g.member["id"], member_id),
|
|
)
|
|
flash("Bloqueado. Ninguno de los dos puede escribir al otro.", "ok")
|
|
return redirect(url_for("messages.inbox"))
|
|
|
|
|
|
@bp.route("/privados/desbloquear/<int:member_id>", methods=["POST"])
|
|
@login_required
|
|
def unblock(member_id: int):
|
|
"""Only your own block. Removing somebody else's would let the blocked
|
|
person undo the thing that was done to protect against them."""
|
|
get_db().execute("DELETE FROM blocks WHERE blocker_id = ? AND blocked_id = ?",
|
|
(g.member["id"], member_id))
|
|
flash("Desbloqueado.", "ok")
|
|
return redirect(url_for("messages.inbox"))
|