A held opening post hides its whole topic from all but author and staff

4ed675a · gmorell · 2026-12-03 04:03

32 files +391 -43
Message
{commit_body(@commit)}

Files changed

modified migration/src/lib.rs
+2 −0
@@ -23,6 +23,7 @@ mod m20240101_000020_user_ips;
23 23 mod m20240101_000021_category_reactions;
24 24 mod m20240101_000022_post_holds;
25 25 mod m20240101_000023_search_and_hot_indexes;
26 +mod m20240101_000024_topic_held;
26 27
27 28 pub struct Migrator;
28 29
@@ -57,6 +58,7 @@ impl MigratorTrait for Migrator {
57 58 Box::new(m20240101_000021_category_reactions::Migration),
58 59 Box::new(m20240101_000022_post_holds::Migration),
59 60 Box::new(m20240101_000023_search_and_hot_indexes::Migration),
61 + Box::new(m20240101_000024_topic_held::Migration),
60 62 ]
61 63 }
62 64 }
added migration/src/m20240101_000024_topic_held.rs
+31 −0
@@ -0,0 +1,31 @@
1 +//! `topics.held_at`: set while the topic's opening post has a pending
2 +//! lameness hold. The topic is then shown only to its author and staff,
3 +//! everywhere a topic or its title can appear. It's denormalised from
4 +//! `post_holds` so every topic list filters on one column. Only
5 +//! `moderation::sync_topic_held` writes it.
6 +use sea_orm_migration::prelude::*;
7 +
8 +#[derive(DeriveMigrationName)]
9 +pub struct Migration;
10 +
11 +const UP: &str = r#"
12 +ALTER TABLE topics ADD COLUMN held_at TIMESTAMPTZ NULL;
13 +UPDATE topics t SET held_at = h.created_at
14 + FROM post_holds h JOIN posts p ON p.id = h.post_id
15 + WHERE p.topic_id = t.id AND p.post_number = 1 AND h.resolved_at IS NULL;
16 +"#;
17 +
18 +const DOWN: &str = r#"
19 +ALTER TABLE topics DROP COLUMN IF EXISTS held_at;
20 +"#;
21 +
22 +#[async_trait::async_trait]
23 +impl MigrationTrait for Migration {
24 + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
25 + manager.get_connection().execute_unprepared(UP).await.map(|_| ())
26 + }
27 +
28 + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
29 + manager.get_connection().execute_unprepared(DOWN).await.map(|_| ())
30 + }
31 +}
modified src/actions.rs
+5 −2
@@ -193,7 +193,7 @@ async fn apply_lameness(
193 193
194 194 /// The read capability of a signed-in user.
195 195 pub fn viewer(user: &users::Model) -> Viewer {
196 − Viewer { level: Some(user.trust_level), staff: user.is_staff }
196 + Viewer { level: Some(user.trust_level), staff: user.is_staff, id: Some(user.id) }
197 197 }
198 198
199 199 /// A verified address (where the site requires one) — enough for private
@@ -340,7 +340,9 @@ pub async fn visible_topic(
340 340 .await?
341 341 .ok_or(ActionError::NotFound)?;
342 342 let tree = CategoryTree::load(state.db.as_ref()).await?;
343 − if !tree.can_view(topic.category_id, viewer) {
343 + // A held topic is invisible to everyone but its author and staff: the
344 + // same 404 as a hidden category.
345 + if !tree.can_view(topic.category_id, viewer) || !viewer.sees_topic(&topic) {
344 346 return Err(ActionError::NotFound);
345 347 }
346 348 Ok((topic, tree))
@@ -408,6 +410,7 @@ pub async fn create_topic(
408 410 closed_at: Set(None),
409 411 closed_by_id: Set(None),
410 412 closed_reason: Set(None),
413 + held_at: Set(None),
411 414 }
412 415 .insert(state.db.as_ref())
413 416 .await?;
modified src/category_tree.rs
+58 −5
@@ -12,17 +12,70 @@ use uuid::Uuid;
12 12 /// Maximum nesting depth (top level = 1).
13 13 pub const MAX_DEPTH: i32 = 3;
14 14
15 −/// A viewer's read capability: their trust level (None = anonymous) and whether
16 −/// they're staff (staff see everything).
15 +/// A viewer's read capability: their trust level (None = anonymous), whether
16 +/// they're staff (staff see everything) and who they are (their own held
17 +/// topics).
17 18 #[derive(Clone, Copy)]
18 19 pub struct Viewer {
19 20 pub level: Option<i32>,
20 21 pub staff: bool,
22 + pub id: Option<Uuid>,
21 23 }
22 24
23 25 impl Viewer {
24 26 pub fn anonymous() -> Self {
25 − Self { level: None, staff: false }
27 + Self { level: None, staff: false, id: None }
28 + }
29 +
30 + /// The topic rule beyond its category: a topic whose opening post is
31 + /// held for review (`held_at`) is seen only by its author and staff.
32 + /// Deleted topics are each caller's business (staff see tombstones).
33 + pub fn sees_topic(&self, t: &crate::entities::topics::Model) -> bool {
34 + t.held_at.is_none() || self.staff || self.id == Some(t.user_id)
35 + }
36 +
37 + /// [`Self::sees_topic`] for topic queries (sea-orm).
38 + pub fn topic_condition(&self) -> Condition {
39 + use crate::entities::topics;
40 + let mut c = Condition::any().add(topics::Column::HeldAt.is_null());
41 + if self.staff {
42 + return Condition::all();
43 + }
44 + if let Some(id) = self.id {
45 + c = c.add(topics::Column::UserId.eq(id));
46 + }
47 + c
48 + }
49 +
50 + /// [`Self::sees_topic`] as SQL for raw queries, on topics aliased `t`.
51 + pub fn topic_sql(&self, t: &str) -> String {
52 + match (self.staff, self.id) {
53 + (true, _) => "TRUE".into(),
54 + (false, Some(id)) => format!("({t}.held_at IS NULL OR {t}.user_id = '{id}')"),
55 + (false, None) => format!("{t}.held_at IS NULL"),
56 + }
57 + }
58 +}
59 +
60 +/// The categories a viewer may read, and the viewer (for the topic rule):
61 +/// what topic lists filter by. Derefs to the category id set.
62 +#[derive(Clone)]
63 +pub struct Visible {
64 + pub cats: HashSet<Uuid>,
65 + pub viewer: Viewer,
66 +}
67 +
68 +impl Visible {
69 + /// The viewer may see this topic: its category, and [`Viewer::sees_topic`].
70 + pub fn sees(&self, t: &crate::entities::topics::Model) -> bool {
71 + self.cats.contains(&t.category_id) && self.viewer.sees_topic(t)
72 + }
73 +}
74 +
75 +impl std::ops::Deref for Visible {
76 + type Target = HashSet<Uuid>;
77 + fn deref(&self) -> &HashSet<Uuid> {
78 + &self.cats
26 79 }
27 80 }
28 81
@@ -201,7 +254,7 @@ impl CategoryTree {
201 254 }
202 255
203 256 /// The set of category ids a viewer can read (self + ancestors all admit).
204 − pub fn visible_category_ids(&self, v: Viewer) -> HashSet<Uuid> {
257 + pub fn visible_category_ids(&self, v: Viewer) -> Visible {
205 258 let mut vis = HashSet::new();
206 259 // flatten() is preorder, so a parent is decided before its children.
207 260 for (id, _) in self.flatten() {
@@ -212,7 +265,7 @@ impl CategoryTree {
212 265 }
213 266 }
214 267 }
215 − vis
268 + Visible { cats: vis, viewer: v }
216 269 }
217 270
218 271 /// `id` plus all its descendants — the set whose topics roll up into `id`.
modified src/entities/topics.rs
+3 −0
@@ -25,6 +25,9 @@ pub struct Model {
25 25 pub closed_at: Option<DateTimeWithTimeZone>,
26 26 pub closed_by_id: Option<Uuid>,
27 27 pub closed_reason: Option<String>,
28 + /// The opening post is held by the lameness filter: only the author and
29 + /// staff see the topic ([`crate::category_tree::Viewer::sees_topic`]).
30 + pub held_at: Option<DateTimeWithTimeZone>,
28 31 }
29 32
30 33 #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
modified src/handlers/bookmarks.rs
+1 −1

Click to load diff…

modified src/handlers/focus.rs
+6 −2

Click to load diff…

modified src/handlers/forum.rs
+33 −2

Click to load diff…

modified src/handlers/landing.rs
+7 −6

Click to load diff…

modified src/handlers/notifications.rs
+2 −2

Click to load diff…

modified src/handlers/reactions.rs
+6 −5

Click to load diff…

modified src/handlers/tags.rs
+21 −15

Click to load diff…

modified src/moderation.rs
+45 −2

Click to load diff…

modified src/policy.rs
+1 −0

Click to load diff…

modified src/seed.rs
+1 −0

Click to load diff…

added templates/forum/_held_badge.html
+3 −0

Click to load diff…

added templates/forum/_held_banner.html
+16 −0

Click to load diff…

modified templates/forum/_reader.html
+1 −0

Click to load diff…

modified templates/forum/_topic_list.html
+1 −0

Click to load diff…

modified templates/forum/_topic_square.html
+1 −1

Click to load diff…

modified templates/forum/_topic_tile.html
+1 −0

Click to load diff…

modified templates/forum/topic.html
+1 −0

Click to load diff…

modified templates/forum/topic_focus.html
+1 −0

Click to load diff…

modified tests/moderation_db.rs
+144 −0

Click to load diff…

Parents: 5daaf12