neiam
/recourse
public · Issues · Pulls · Labels · Forks · Compare · Actions queued · Badges · Packages
⭐
Log in to mark this repository.
Add topic tag editing (reconstructs final session step)
7d724b1 · gmorell · 2026-12-02 15:56
Message
{commit_body(@commit)}
Files changed
modified
src/handlers/forum.rs
+155
−43
@@ -241,6 +241,84 @@ async fn tags_for_topic(state: &AppState, topic_id: Uuid) -> Result<Vec<TagChip>
| 241 | 241 | .collect()) |
| 242 | 242 | } |
| 243 | 243 | |
| 244 | +/// Maximum number of tags on one topic. | |
| 245 | +const MAX_TAGS: usize = 5; | |
| 246 | + | |
| 247 | +/// Parse a comma-separated tag list into `(name, slug)` pairs: trimmed, | |
| 248 | +/// slugified, deduplicated by slug, at most [`MAX_TAGS`]. | |
| 249 | +fn parse_tag_list(raw: &str) -> Vec<(String, String)> { | |
| 250 | + let mut seen = std::collections::HashSet::new(); | |
| 251 | + let mut out = Vec::new(); | |
| 252 | + for piece in raw.split(',') { | |
| 253 | + let name = piece.trim(); | |
| 254 | + if name.is_empty() || name.chars().count() > 64 { | |
| 255 | + continue; | |
| 256 | + } | |
| 257 | + let slug = tag_slug(name); | |
| 258 | + if slug.is_empty() || !seen.insert(slug.clone()) { | |
| 259 | + continue; | |
| 260 | + } | |
| 261 | + out.push((name.to_string(), slug)); | |
| 262 | + if out.len() >= MAX_TAGS { | |
| 263 | + break; | |
| 264 | + } | |
| 265 | + } | |
| 266 | + out | |
| 267 | +} | |
| 268 | + | |
| 269 | +/// Attach the tags in `raw` to a topic. Existing tags are reused; brand-new | |
| 270 | +/// tags are only minted when `user` meets the trust gate (others are silently | |
| 271 | +/// skipped). Does not remove tags already on the topic. | |
| 272 | +async fn apply_tags( | |
| 273 | + state: &AppState, | |
| 274 | + topic_id: Uuid, | |
| 275 | + raw: &str, | |
| 276 | + user: &users::Model, | |
| 277 | +) -> Result<(), StatusCode> { | |
| 278 | + let may_create_tags = crate::trust::can_create_tags(user, state.config.min_trust_to_create_tag); | |
| 279 | + let now = Utc::now(); | |
| 280 | + for (name, slug) in parse_tag_list(raw) { | |
| 281 | + let tag = match tags::Entity::find() | |
| 282 | + .filter(tags::Column::Slug.eq(&slug)) | |
| 283 | + .one(state.db.as_ref()) | |
| 284 | + .await | |
| 285 | + .map_err(ise("look up tag"))? | |
| 286 | + { | |
| 287 | + Some(t) => Some(t), | |
| 288 | + None if may_create_tags => tags::ActiveModel { | |
| 289 | + id: Set(Uuid::now_v7()), | |
| 290 | + name: Set(name), | |
| 291 | + slug: Set(slug), | |
| 292 | + created_at: Set(now.into()), | |
| 293 | + } | |
| 294 | + .insert(state.db.as_ref()) | |
| 295 | + .await | |
| 296 | + .ok(), | |
| 297 | + None => None, // not allowed to mint new tags | |
| 298 | + }; | |
| 299 | + if let Some(tag) = tag { | |
| 300 | + let already = topic_tags::Entity::find() | |
| 301 | + .filter(topic_tags::Column::TopicId.eq(topic_id)) | |
| 302 | + .filter(topic_tags::Column::TagId.eq(tag.id)) | |
| 303 | + .one(state.db.as_ref()) | |
| 304 | + .await | |
| 305 | + .map_err(ise("check topic tag"))? | |
| 306 | + .is_some(); | |
| 307 | + if !already { | |
| 308 | + topic_tags::ActiveModel { | |
| 309 | + id: Set(Uuid::now_v7()), | |
| 310 | + topic_id: Set(topic_id), | |
| 311 | + tag_id: Set(tag.id), | |
| 312 | + } | |
| 313 | + .insert(state.db.as_ref()) | |
| 314 | + .await | |
| 315 | + .map_err(ise("insert topic tag"))?; | |
| 316 | + } | |
| 317 | + } | |
| 318 | + } | |
| 319 | + Ok(()) | |
| 320 | +} | |
| 321 | + | |
| 244 | 322 | /// Build display rows for a set of loaded topics (category circle, author, |
| 245 | 323 | /// tags). Reused by category, tag, and search listings. |
| 246 | 324 | pub async fn build_topic_rows( |
@@ -680,6 +758,12 @@ pub async fn topic_show(
| 680 | 758 | } |
| 681 | 759 | |
| 682 | 760 | let topic_tag_chips = tags_for_topic(&state, topic.id).await?; |
| 761 | + // Tag editing: topic author or staff, on a live topic. | |
| 762 | + let can_edit_tags = | |
| 763 | + !topic_deleted && current.is_some_and(|(uid, staff)| staff || uid == topic.user_id); | |
| 764 | + let may_create_tags = user_context.as_ref().is_some_and(|c| { | |
| 765 | + crate::trust::can_create_tags(&c.user, state.config.min_trust_to_create_tag) | |
| 766 | + }); | |
| 683 | 767 | |
| 684 | 768 | render(&state, "forum/topic.html", context! { |
| 685 | 769 | title => topic.title, |
@@ -689,6 +773,9 @@ pub async fn topic_show(
| 689 | 773 | topic => topic, |
| 690 | 774 | posts => rows, |
| 691 | 775 | tags => topic_tag_chips, |
| 776 | + can_edit_tags => can_edit_tags, | |
| 777 | + may_create_tags => may_create_tags, | |
| 778 | + min_trust => state.config.min_trust_to_create_tag, | |
| 692 | 779 | participants => participants, |
| 693 | 780 | topic_deleted => topic_deleted, |
| 694 | 781 | can_delete_topic => can_delete_topic, |
@@ -805,50 +892,8 @@ pub async fn create_topic(
| 805 | 892 | |
| 806 | 893 | // Free-form tags (created on demand; up to 5). Minting NEW tags requires a |
| 807 | 894 | // trust level; lower-tier users may still apply existing tags. |
| 808 | − let may_create_tags = | |
| 809 | − crate::trust::can_create_tags(&user_context.user, state.config.min_trust_to_create_tag); | |
| 810 | 895 | if let Some(raw) = form.tags.as_deref() { |
| 811 | − let mut seen = std::collections::HashSet::new(); | |
| 812 | − for piece in raw.split(',') { | |
| 813 | − let name = piece.trim(); | |
| 814 | − if name.is_empty() || name.chars().count() > 64 { | |
| 815 | − continue; | |
| 816 | − } | |
| 817 | − let slug = tag_slug(name); | |
| 818 | − if slug.is_empty() || !seen.insert(slug.clone()) { | |
| 819 | − continue; | |
| 820 | − } | |
| 821 | − if seen.len() > 5 { | |
| 822 | − break; | |
| 823 | − } | |
| 824 | − let tag = match tags::Entity::find() | |
| 825 | − .filter(tags::Column::Slug.eq(&slug)) | |
| 826 | − .one(state.db.as_ref()) | |
| 827 | − .await | |
| 828 | − { | |
| 829 | − Ok(Some(t)) => Some(t), | |
| 830 | − Ok(None) if may_create_tags => tags::ActiveModel { | |
| 831 | − id: Set(Uuid::now_v7()), | |
| 832 | − name: Set(name.to_string()), | |
| 833 | − slug: Set(slug), | |
| 834 | − created_at: Set(now.into()), | |
| 835 | − } | |
| 836 | − .insert(state.db.as_ref()) | |
| 837 | − .await | |
| 838 | − .ok(), | |
| 839 | − Ok(None) => None, // not allowed to mint new tags | |
| 840 | − Err(_) => None, | |
| 841 | − }; | |
| 842 | − if let Some(tag) = tag { | |
| 843 | − let _ = topic_tags::ActiveModel { | |
| 844 | − id: Set(Uuid::now_v7()), | |
| 845 | − topic_id: Set(topic_id), | |
| 846 | − tag_id: Set(tag.id), | |
| 847 | − } | |
| 848 | − .insert(state.db.as_ref()) | |
| 849 | − .await; | |
| 850 | − } | |
| 851 | − } | |
| 896 | + apply_tags(&state, topic_id, raw, &user_context.user).await?; | |
| 852 | 897 | } |
| 853 | 898 | |
| 854 | 899 | // Notify watchers of this category and its ancestors (best-effort). |
@@ -974,6 +1019,73 @@ pub async fn create_post(
| 974 | 1019 | } |
| 975 | 1020 | } |
| 976 | 1021 | |
| 1022 | +#[derive(Debug, Deserialize)] | |
| 1023 | +pub struct TopicTagsForm { | |
| 1024 | + /// Comma-separated tag names; replaces the topic's current tags. | |
| 1025 | + #[serde(default)] | |
| 1026 | + pub tags: String, | |
| 1027 | +} | |
| 1028 | + | |
| 1029 | +/// Replace a topic's tags (topic author or staff). Same rules as at creation: | |
| 1030 | +/// up to 5, slug-deduplicated, and minting brand-new tags needs the trust gate | |
| 1031 | +/// (existing tags can always be applied). htmx gets the re-rendered tag bar; | |
| 1032 | +/// plain form posts redirect back to the topic. | |
| 1033 | +pub async fn update_topic_tags( | |
| 1034 | + State(state): State<AppState>, | |
| 1035 | + Path(id): Path<Uuid>, | |
| 1036 | + HxRequest(is_htmx): HxRequest, | |
| 1037 | + user_context: UserContext, | |
| 1038 | + Form(form): Form<TopicTagsForm>, | |
| 1039 | +) -> Result<axum::response::Response, StatusCode> { | |
| 1040 | + let topic = topics::Entity::find_by_id(id) | |
| 1041 | + .one(state.db.as_ref()) | |
| 1042 | + .await | |
| 1043 | + .map_err(ise("load topic for tag edit"))? | |
| 1044 | + .ok_or(StatusCode::NOT_FOUND)?; | |
| 1045 | + | |
| 1046 | + { | |
| 1047 | + let tree = CategoryTree::load(state.db.as_ref()).await.map_err(ise("load category tree"))?; | |
| 1048 | + if !tree.can_view(topic.category_id, viewer_user(&user_context)) { | |
| 1049 | + return Err(StatusCode::NOT_FOUND); | |
| 1050 | + } | |
| 1051 | + } | |
| 1052 | + if topic.deleted_at.is_some() { | |
| 1053 | + return Err(StatusCode::NOT_FOUND); | |
| 1054 | + } | |
| 1055 | + if !(user_context.user.is_staff || user_context.user.id == topic.user_id) { | |
| 1056 | + return Err(StatusCode::FORBIDDEN); | |
| 1057 | + } | |
| 1058 | + | |
| 1059 | + // Replace: drop the current links, then apply the submitted list. Tag rows | |
| 1060 | + // themselves are kept (other topics may use them; /tags lists zero counts). | |
| 1061 | + topic_tags::Entity::delete_many() | |
| 1062 | + .filter(topic_tags::Column::TopicId.eq(topic.id)) | |
| 1063 | + .exec(state.db.as_ref()) | |
| 1064 | + .await | |
| 1065 | + .map_err(ise("clear topic tags"))?; | |
| 1066 | + apply_tags(&state, topic.id, &form.tags, &user_context.user).await?; | |
| 1067 | + | |
| 1068 | + let mut t: topics::ActiveModel = topic.clone().into(); | |
| 1069 | + t.updated_at = Set(Utc::now().into()); | |
| 1070 | + t.update(state.db.as_ref()).await.map_err(ise("touch topic"))?; | |
| 1071 | + | |
| 1072 | + if is_htmx { | |
| 1073 | + let tag_chips = tags_for_topic(&state, topic.id).await?; | |
| 1074 | + let may_create_tags = | |
| 1075 | + crate::trust::can_create_tags(&user_context.user, state.config.min_trust_to_create_tag); | |
| 1076 | + let html = render(&state, "forum/_topic_tags.html", context! { | |
| 1077 | + topic => topic, | |
| 1078 | + tags => tag_chips, | |
| 1079 | + can_edit_tags => true, | |
| 1080 | + may_create_tags => may_create_tags, | |
| 1081 | + min_trust => state.config.min_trust_to_create_tag, | |
| 1082 | + })?; | |
| 1083 | + Ok(html.into_response()) | |
| 1084 | + } else { | |
| 1085 | + Ok(Redirect::to(&format!("/t/{}", topic.id)).into_response()) | |
| 1086 | + } | |
| 1087 | +} | |
| 1088 | + | |
| 977 | 1089 | /// Soft-delete a topic (author or staff). The row stays so `/t/{id}` keeps |
| 978 | 1090 | /// resolving; it just drops out of listings and shows a tombstone. |
| 979 | 1091 | pub async fn delete_topic( |
modified
src/handlers/mod.rs
+1
−1
@@ -30,7 +30,7 @@ pub use categories::{
| 30 | 30 | }; |
| 31 | 31 | pub use forum::{ |
| 32 | 32 | category_show, create_post, create_topic, delete_topic, home, new_topic_form, restore_topic, |
| 33 | − topic_show, | |
| 33 | + topic_show, update_topic_tags, | |
| 34 | 34 | }; |
| 35 | 35 | pub use posts::{ |
| 36 | 36 | delete_post, edit_post_form, post_history, post_permalink, restore_post, update_post, |
modified
src/main.rs
+2
−0
@@ -121,6 +121,8 @@ async fn main() -> Result<(), anyhow::Error> {
| 121 | 121 | // Topic soft-delete / restore (delete: author or staff; restore: staff-only) |
| 122 | 122 | .route("/t/{id}/delete", post(handlers::delete_topic)) |
| 123 | 123 | .route("/t/{id}/restore", post(handlers::restore_topic)) |
| 124 | + // Topic tag editing (author or staff; minting new tags is trust-gated) | |
| 125 | + .route("/t/{id}/tags", post(handlers::update_topic_tags)) | |
| 124 | 126 | .route("/c/{slug}/watch", post(handlers::toggle_watch)) |
| 125 | 127 | // Post edit / soft-delete / restore (author or staff; restore staff-only) |
| 126 | 128 | .route("/p/{id}/edit", get(handlers::edit_post_form)) |
added
templates/forum/_topic_tags.html
+26
−0
@@ -0,0 +1,26 @@
| 1 | +{# Topic tag bar: chips plus, for the author/staff, an inline editor that | |
| 2 | + htmx-swaps this whole block. Context: topic, tags, can_edit_tags, | |
| 3 | + may_create_tags, min_trust. #} | |
| 4 | +<div id="topic-tags" class="flex flex-wrap items-center gap-1"> | |
| 5 | + {% for tag in tags %}<a href="/tag/{{ tag.slug }}" class="badge badge-ghost">{{ tag.name }}</a>{% endfor %} | |
| 6 | + {% if can_edit_tags %} | |
| 7 | + <details class="w-full sm:w-auto"> | |
| 8 | + <summary class="btn btn-ghost btn-xs list-none"> | |
| 9 | + <i class="fas fa-tags mr-1"></i>{% if tags %}Edit tags{% else %}Add tags{% endif %} | |
| 10 | + </summary> | |
| 11 | + <form method="post" action="/t/{{ topic.id }}/tags" | |
| 12 | + hx-post="/t/{{ topic.id }}/tags" hx-target="#topic-tags" hx-swap="outerHTML" | |
| 13 | + class="flex flex-col gap-1 mt-2"> | |
| 14 | + <div class="join w-full"> | |
| 15 | + <input type="text" name="tags" class="input input-bordered input-sm join-item w-full" | |
| 16 | + value="{% for tag in tags %}{{ tag.name }}{% if not loop.last %}, {% endif %}{% endfor %}" | |
| 17 | + placeholder="rust, help, async"/> | |
| 18 | + <button type="submit" class="btn btn-primary btn-sm join-item">Save</button> | |
| 19 | + </div> | |
| 20 | + <span class="label-text-alt opacity-60"> | |
| 21 | + Comma-separated, up to 5.{% if not may_create_tags %} You can apply existing tags; creating new tags requires trust level {{ min_trust }} ({{ tier_name(min_trust) }}).{% endif %} | |
| 22 | + </span> | |
| 23 | + </form> | |
| 24 | + </details> | |
| 25 | + {% endif %} | |
| 26 | +</div> |
modified
templates/forum/topic.html
+1
−5
@@ -28,11 +28,7 @@
| 28 | 28 | </div> |
| 29 | 29 | </div> |
| 30 | 30 | |
| 31 | − {% if tags %} | |
| 32 | − <div class="flex flex-wrap gap-1"> | |
| 33 | − {% for tag in tags %}<a href="/tag/{{ tag.slug }}" class="badge badge-ghost">{{ tag.name }}</a>{% endfor %} | |
| 34 | − </div> | |
| 35 | − {% endif %} | |
| 31 | + {% if tags or can_edit_tags %}{% include "forum/_topic_tags.html" %}{% endif %} | |
| 36 | 32 | |
| 37 | 33 | {% if participants %} |
| 38 | 34 | <div class="hidden lg:flex items-center gap-2 text-sm"> |
Parents: 2264215