Commentaire #%d
" + "Motif
" + "Extrait du commentaire
" + "diff --git a/forum/db.py b/forum/db.py index d8bd849..7d6463e 100644 --- a/forum/db.py +++ b/forum/db.py @@ -163,6 +163,16 @@ def connecter(chemin=":memory:"): " user_id INTEGER NOT NULL REFERENCES users(id), username TEXT NOT NULL," " jusqu_au TEXT NOT NULL DEFAULT (datetime('now')))") con.execute("CREATE INDEX IF NOT EXISTS idx_usernames_anciens ON usernames_anciens(user_id, id)") + # Commentaires aux réponses (06/11/2026) : un seul niveau, texte simple ; ils ne comptent pas comme des messages + # (limite des visiteurs, tris). Tables à part : rien ne change pour posts et reports. + con.execute("CREATE TABLE IF NOT EXISTS comments (id INTEGER PRIMARY KEY," + " post_id INTEGER NOT NULL REFERENCES posts(id), author_id INTEGER NOT NULL REFERENCES users(id)," + " body TEXT NOT NULL, created_at TEXT NOT NULL DEFAULT (datetime('now'))," + " hidden INTEGER NOT NULL DEFAULT 0, ip TEXT)") + con.execute("CREATE INDEX IF NOT EXISTS idx_comments_post ON comments(post_id, id)") + con.execute("CREATE TABLE IF NOT EXISTS comment_reports (id INTEGER PRIMARY KEY," + " comment_id INTEGER NOT NULL REFERENCES comments(id), reporter_id INTEGER NOT NULL REFERENCES users(id)," + " reason TEXT NOT NULL, created_at TEXT NOT NULL DEFAULT (datetime('now')), UNIQUE (comment_id, reporter_id))") con.execute("INSERT OR IGNORE INTO users(hub_id, display_name) VALUES (?, ?)", (SYSTEME, "vercors.me")) if con.execute("SELECT 1 FROM topics WHERE fixed=1").fetchone() is None: sid = con.execute("SELECT id FROM users WHERE hub_id=?", (SYSTEME,)).fetchone()[0] diff --git a/forum/forum.py b/forum/forum.py index 3ee810c..4ca66fb 100644 --- a/forum/forum.py +++ b/forum/forum.py @@ -25,6 +25,7 @@ SESSION_JOURS, ETAT_MINUTES = 30, 10 # SESSION_JOURS : durée du cookie (« se # coupent donc le forum en 1 h au plus. SESSION_MINUTES = 60 MODIF_JOURS = 7 # un message se modifie une fois, pendant une semaine +COMMENTAIRE_MAX = 500 # commentaire sous une réponse : texte simple, liens cliquables, pas de photo def nom_sql(u="u"): @@ -660,6 +661,94 @@ class Forum: self.con.commit() self.version += 1 + # --- commentaires aux réponses (un seul niveau, texte simple) --------- + def _commentaire(self, comment_id): + c = self.con.execute("SELECT * FROM comments WHERE id=?", (comment_id,)).fetchone() + if c is None: + raise Invalide("commentaire introuvable") + return c + + def peut_commenter(self, user, t, p): + """Membre connecté (profil privé compris), message visible, sujet publié et ouvert ; jamais dans le Bulletin.""" + if user is None or not self._peut(user, "commenter"): + return False + return not (p["hidden"] or t["fixed"] or t["locked"] or t["hidden"] or t["status"] != "publie") + + def commenter(self, user, post_id, corps, ip=None): + self._exiger(user, "commenter") + self._frais(user) + p = self._post(post_id) + t, _ = self.sujet(user, p["topic_id"]) + if not self.peut_commenter(user, t, p): + raise Refus("commentaire impossible ici") + corps = self._texte((corps or "").replace("\r\n", "\n"), COMMENTAIRE_MAX, "commentaire") + if re.search(r"[\x00-\x08\x0b\x0c\x0e-\x1f\x7f]", corps): + raise Invalide("commentaire : caractères de contrôle refusés") + cur = self.con.execute("INSERT INTO comments(post_id, author_id, body, ip) VALUES (?, ?, ?, ?)", + (post_id, user["id"], corps, ip)) + self.con.commit() + return cur.lastrowid + + def commentaires(self, user, post_ids): + """{post_id: [commentaires]} dans l'ordre ; masqués seulement pour les modérateurs et propriétaires. + Mêmes colonnes d'auteur que les messages (nom_sql, avatar, profil).""" + ids = [int(i) for i in post_ids] + out = {i: [] for i in ids} + if not ids: + return out + voit = self._peut(user, "voir_masques") + rows = self.con.execute( + "SELECT c.id, c.post_id, c.author_id, c.body, c.created_at, c.hidden, " + nom_sql() + " AS auteur," + " u.username AS auteur_username, u.profil AS auteur_profil, u.photo AS auteur_photo," + " u.avatar_illustre AS auteur_avatar, u.supprime AS auteur_supprime" + " FROM comments c JOIN users u ON u.id=c.author_id" + " WHERE c.post_id IN (%s) AND (c.hidden=0 OR ?) ORDER BY c.id" % ",".join("?" * len(ids)), + ids + [int(voit)]).fetchall() + for r in rows: + out[r["post_id"]].append(r) + return out + + def _sujet_du_commentaire(self, user, c): + """Le commentaire doit être dans un sujet visible pour cet utilisateur ; retourne (topic_id, post_id).""" + p = self._post(c["post_id"]) + self.sujet(user, p["topic_id"]) + return p["topic_id"], p["id"] + + def supprimer_commentaire(self, user, comment_id): + """L'auteur supprime son commentaire (effacé de la base, signalements compris).""" + c = self._commentaire(comment_id) + if user is None or c["author_id"] != user["id"]: + raise Refus("pas ton commentaire") + lieu = self._sujet_du_commentaire(user, c) + with self.con: + self.con.execute("DELETE FROM comment_reports WHERE comment_id=?", (comment_id,)) + self.con.execute("DELETE FROM comments WHERE id=?", (comment_id,)) + return lieu + + def signaler_commentaire(self, user, comment_id, motif): + self._exiger(user, "signaler") + c = self._commentaire(comment_id) + if c["hidden"]: + raise Invalide("commentaire introuvable") + lieu = self._sujet_du_commentaire(user, c) + self.con.execute("INSERT OR IGNORE INTO comment_reports(comment_id, reporter_id, reason) VALUES (?, ?, ?)", + (comment_id, user["id"], self._texte(motif, 500, "motif"))) + self.con.commit() + return lieu + + def signalements_commentaires(self, user): + self._exiger(user, "voir_signalements") + return self.con.execute("SELECT r.*, c.body, c.post_id, c.hidden, p.topic_id FROM comment_reports r" + " JOIN comments c ON c.id=r.comment_id JOIN posts p ON p.id=c.post_id" + " ORDER BY r.id DESC").fetchall() + + def masquer_commentaire(self, user, comment_id, masque=True): + self._exiger(user, "masquer") + lieu = self._sujet_du_commentaire(user, self._commentaire(comment_id)) + self.con.execute("UPDATE comments SET hidden=? WHERE id=?", (int(masque), comment_id)) + self.con.commit() + return lieu + def verrouiller(self, user, topic_id, verrou=True): self._exiger(user, "verrouiller") self.sujet(user, topic_id) @@ -946,7 +1035,9 @@ class Forum: # --- RGPD ---------------------------------------------------------- def purger_ips(self, mois=12): """Efface les adresses IP de plus de `mois` mois (obligation légale : un an). Retourne le nombre de lignes.""" - cur = self.con.execute("UPDATE posts SET ip=NULL WHERE ip IS NOT NULL AND created_at < datetime('now', ?)", - ("-%d months" % mois,)) + n = 0 + for table in ("posts", "comments"): + n += self.con.execute("UPDATE %s SET ip=NULL WHERE ip IS NOT NULL AND created_at < datetime('now', ?)" % table, + ("-%d months" % mois,)).rowcount self.con.commit() - return cur.rowcount + return n diff --git a/forum/roles.py b/forum/roles.py index 136d450..1ac0715 100644 --- a/forum/roles.py +++ b/forum/roles.py @@ -38,6 +38,7 @@ DROITS = { "lire": None, "repondre": {MEMBRE}, "signaler": {MEMBRE}, + "commenter": {MEMBRE}, # commentaires courts sous une réponse (profils privés compris) "modifier_son_message": {MEMBRE}, "creer_sujet": _CREATEURS, "publier_bulletin": _CREATEURS, # écrire dans un sujet fermé aux réponses (Bulletin de la semaine) diff --git a/forum/server.py b/forum/server.py index 83e726e..d77147b 100644 --- a/forum/server.py +++ b/forum/server.py @@ -36,7 +36,7 @@ import markdown_sur # noqa: E402 import profil as profil_mod # noqa: E402 import roles # noqa: E402 from db import SYSTEME # noqa: E402 -from forum import PLUS_TARD_MAX # noqa: E402 +from forum import PLUS_TARD_MAX, COMMENTAIRE_MAX # noqa: E402 from forum import CORPS_MAX, TITRE_MAX, FICHE_PHOTOS_MAX, OCTETS_MAX, PHOTOS_MAX, SESSION_JOURS, ETAT_MINUTES, Forum, Invalide, Refus # noqa: E402 @@ -287,6 +287,16 @@ details.ancien{margin:10px 0 0;color:var(--gris)}details.ancien summary{cursor:p .avatar.petit .couronne{top:-7px;width:20px;height:10px}.avatar.grand .couronne{top:-17px;width:56px;height:28px} .avatar .bouclier{position:absolute;right:-6px;bottom:-6px;width:18px;height:19px}.avatar.grand .bouclier{width:30px;height:32px} .msg aside .avatar{margin:0 0 10px}.msg aside a.auteur{color:var(--roche);text-decoration:none}.msg aside a.auteur:hover{text-decoration:underline} +.avatar.mini img{width:28px;height:28px}.avatar.mini .couronne{top:-6px;width:16px;height:8px}.avatar.mini .bouclier{width:12px;height:13px;right:-4px;bottom:-4px} +.commentaires{margin:16px 0 0;border-left:3px solid var(--pierre);padding-left:14px}.commentaires ul{list-style:none;margin:0;padding:0} +.commentaire{display:flex;gap:10px;align-items:flex-start;padding:8px 0;border-top:1px solid #e3e6df}.commentaire:first-child{border-top:0} +.commentaire>div{min-width:0;flex:1}.commentaire .meta{margin:0}.commentaire .meta b{font-size:15px}.commentaire .meta a.auteur{color:var(--roche)} +.commentaire .texte{margin:2px 0 0;overflow-wrap:anywhere;font-size:15px}.commentaire.cache{opacity:.75;border-left:3px solid var(--gris);padding-left:8px} +.commentaire .outils{display:flex;gap:12px;flex-wrap:wrap;align-items:flex-start;font-size:14px}.commentaire .outils form{margin:0} +.commentaire .outils summary{padding:2px 0}.commentaire .outils details[open]{flex-basis:100%} +button.lien{background:none;border:0;padding:2px 0;color:var(--sapin);font-weight:600;cursor:pointer;margin:0}button.lien:hover{text-decoration:underline;background:none} +.plus-commentaires>summary{cursor:pointer;color:var(--sapin);font-weight:600;padding:4px 0} +.actions .commenter textarea{min-height:90px} .compte-menu .entete .avatar{margin-top:8px} .bienvenue{max-width:780px} .etapes{display:flex;gap:8px;margin:0 0 18px;color:var(--gris)}.etapes span{flex:1;border-bottom:3px solid var(--pierre);padding-bottom:4px} @@ -428,6 +438,7 @@ def heure_paris(epoch): PAR_PAGE = 10 VISITEUR_MAX = 100 # messages visibles sans compte dans un sujet (le 1er + les 99 derniers) +COMMENTAIRES_VISIBLES = 3 # sous une réponse ; les plus anciens sont repliés (« Voir les N autres commentaires ») def entier(texte, defaut=1): @@ -528,6 +539,14 @@ def signalements_html(forum, acteur, lien): "
Ouvrir le message dans le forum
" % (signalement["post_id"], e(date_fr(signalement["created_at"])), signalement["topic_id"], e(signalement["reason"]), e(extrait), e(lien), signalement["topic_id"], signalement["post_id"])) + for s in forum.signalements_commentaires(acteur): + corps = s["body"] + cartes.append("Motif
" + "Extrait du commentaire
" + "%s
%sFiche de la station sur vercors.me : %d photo%s épinglée%s sur %d. " "Choisis-les avec « Épingler sur la fiche » sous chaque photo.
" % (len(fiche), "s" if len(fiche) > 1 else "", "s" if len(fiche) > 1 else "", FICHE_PHOTOS_MAX)) + comms = {} if t["fixed"] else f.commentaires(user, [m["id"] for m in msgs]) # pas de commentaires dans le Bulletin for i, m in enumerate(msgs): if i == 1 and caches: out.append("%d message%s plus ancien%s : connecte-toi pour les voir.
" % (caches, "s" if caches > 1 else "", "s" if caches > 1 else "")) gal = galerie_html(f.photos_de(m["id"]), "/photo", None if fiche is None or m["hidden"] else fiche) outils = "" lien = "Copier le lien" % m["id"] + if f.peut_commenter(user, t, m): + lien += ("" + "
Voir %s
%s