forum : commentaires courts sous les réponses (un niveau, 500 caractères, liens cliquables)
- table comments + comment_reports (migration idempotente, IP purgée après un an) - bouton « Commenter » dans la barre du message ; 3 derniers visibles, les autres repliés - membres connectés (profils privés compris) ; visiteurs en lecture ; jamais dans le Bulletin - hors limite VISITEUR_MAX et hors comptage des tris - signalement (page Signalements), masquage modérateur, suppression par l'auteur Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Cette révision appartient à :
1 Parent
30c14d478f
révision
c3760e2bf0
5 fichiers modifiés
+328
-6
Aucun fichier ne correspond à votre recherche
+94
-3
@@ -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
|
||||
Référencer dans un nouveau ticket
Bloquer un utilisateur