Mail rules cross over
Browse files at this commit714d109b98053ddcd0b03242a44f70b73af19c5dParent: b1e5b90- feat
- mail rules cross over
1 changed file +724 -1
crates/izlek-topcoat/src/rules.rsmodified+724 -1
| @@ -1 +1,724 @@ | ||
| 1 | -//! Lands in the page-port fan-out. | |
| 1 | +//! Mail rules, ported from `izlek-web/src/rules.rs` onto topcoat's | |
| 2 | +//! server-rendered pages. | |
| 3 | +//! | |
| 4 | +//! The screen is the admin's, and so is every call behind it: a rule sends mail | |
| 5 | +//! on everyone's behalf, from the one workspace sender, so writing one is not | |
| 6 | +//! something a Member may do even if they reach the endpoint directly. The | |
| 7 | +//! chip in the artboard says "Admin only"; the guard is in the handlers. | |
| 8 | +//! | |
| 9 | +//! There is no client-side signal here to hold which row is being edited — | |
| 10 | +//! every topcoat page is rendered fresh, server-side, on every request. Which | |
| 11 | +//! rule (if any) is open for editing rides `?edit=<rule_id>` on `/rules` | |
| 12 | +//! itself: the row renders as `RuleForm` server-side instead of the display | |
| 13 | +//! row, no script required. The mutating endpoints preserve that query pair | |
| 14 | +//! across their redirect (see `crate::server::refusal_of`'s `carrying`), so a | |
| 15 | +//! failed edit lands back on the same open row rather than closing it. | |
| 16 | + | |
| 17 | +use std::sync::Arc; | |
| 18 | + | |
| 19 | +use serde::{Deserialize, Serialize}; | |
| 20 | +use topcoat::Result; | |
| 21 | +use topcoat::context::Cx; | |
| 22 | +use topcoat::router::content::{Form, Json}; | |
| 23 | +use topcoat::router::request::headers; | |
| 24 | +use topcoat::router::{HeaderName, StatusCode, header, page, query_params, route}; | |
| 25 | +use topcoat::view::view; | |
| 26 | + | |
| 27 | +use izlek_core::board::Column; | |
| 28 | +use izlek_core::store::{Audience, Store, Trigger, User, Workspace}; | |
| 29 | + | |
| 30 | +use crate::server::{Refusal, accounts, refusal_of, require_admin}; | |
| 31 | + | |
| 32 | +/// One rule as the screen reads it: a sentence, a switch and a stamp. | |
| 33 | +/// | |
| 34 | +/// The trigger arrives already split into the words the artboard prints — | |
| 35 | +/// "When status becomes" / "Done" — because the two halves are chosen from | |
| 36 | +/// different vocabularies and only the server knows the column names. | |
| 37 | +#[derive(Clone, Debug, Serialize)] | |
| 38 | +struct RuleLine { | |
| 39 | + id: String, | |
| 40 | + /// "When status becomes", or "When a task stops being". | |
| 41 | + when: String, | |
| 42 | + /// "Done", or "blocked", or empty when the trigger has no second half. | |
| 43 | + what: String, | |
| 44 | + subject: String, | |
| 45 | + /// "assignees", "everyone on board", or "its creator". | |
| 46 | + audience: String, | |
| 47 | + /// The trigger's own word — "status", "unblocked", "created" — as the | |
| 48 | + /// edit form's select expects it, not as the sentence prints it. | |
| 49 | + trigger_kind: String, | |
| 50 | + /// The named column, for a status trigger's edit form. `None` for every | |
| 51 | + /// other trigger. | |
| 52 | + column_id: Option<String>, | |
| 53 | + /// The audience's own word — "assignees", "board", "creator" — as the | |
| 54 | + /// edit form's select expects it. | |
| 55 | + audience_kind: String, | |
| 56 | + enabled: bool, | |
| 57 | + /// `Aug 24 14:02`, or nothing when no mail from this rule has ever been | |
| 58 | + /// accepted by the mail server. | |
| 59 | + last_sent: Option<String>, | |
| 60 | + /// `Aug 24 14:02` of the rule's last decision, sent or not, or nothing | |
| 61 | + /// when the rule has never been evaluated. | |
| 62 | + last_fired: Option<String>, | |
| 63 | +} | |
| 64 | + | |
| 65 | +/// A column, for the composer's status list. | |
| 66 | +#[derive(Clone, Debug, Serialize)] | |
| 67 | +struct ColumnChoice { | |
| 68 | + id: String, | |
| 69 | + name: String, | |
| 70 | +} | |
| 71 | + | |
| 72 | +/// The screen in one answer. | |
| 73 | +#[derive(Clone, Debug, Serialize)] | |
| 74 | +struct RulesSnapshot { | |
| 75 | + rules: Vec<RuleLine>, | |
| 76 | + columns: Vec<ColumnChoice>, | |
| 77 | + /// Whether a sender is connected. Rules that fire with no sender queue | |
| 78 | + /// rather than fail, and the screen says so instead of leaving somebody to | |
| 79 | + /// wonder why nothing arrived. | |
| 80 | + sender_connected: bool, | |
| 81 | +} | |
| 82 | + | |
| 83 | +/// The word for an audience, as the artboard writes it. | |
| 84 | +fn audience_word(audience: Audience) -> &'static str { | |
| 85 | + match audience { | |
| 86 | + Audience::Assignees => "assignees", | |
| 87 | + Audience::Board => "everyone on board", | |
| 88 | + Audience::Creator => "its creator", | |
| 89 | + } | |
| 90 | +} | |
| 91 | + | |
| 92 | +/// The audience's own word, as the edit form's select names it. | |
| 93 | +fn audience_kind(audience: Audience) -> &'static str { | |
| 94 | + match audience { | |
| 95 | + Audience::Assignees => "assignees", | |
| 96 | + Audience::Board => "board", | |
| 97 | + Audience::Creator => "creator", | |
| 98 | + } | |
| 99 | +} | |
| 100 | + | |
| 101 | +/// Parses a trigger from the words a form sends: the trigger's own kind, and | |
| 102 | +/// a column id only a status trigger carries. Every other trigger must arrive | |
| 103 | +/// with no column — a column id on, say, "created" is a form gone wrong, not | |
| 104 | +/// something to guess past. | |
| 105 | +/// | |
| 106 | +/// Shared by `create_rule` and `update_rule` so the vocabulary is matched | |
| 107 | +/// once. | |
| 108 | +fn trigger_of(kind: &str, column_id: Option<String>) -> Option<Trigger> { | |
| 109 | + match (kind, column_id) { | |
| 110 | + ("status", Some(column_id)) => Some(Trigger::StatusBecomes(column_id)), | |
| 111 | + ("status", None) => None, | |
| 112 | + ("unblocked", None) => Some(Trigger::Unblocked), | |
| 113 | + ("created", None) => Some(Trigger::Created), | |
| 114 | + ("assigned", None) => Some(Trigger::Assigned), | |
| 115 | + ("unassigned", None) => Some(Trigger::Unassigned), | |
| 116 | + ("commented", None) => Some(Trigger::Commented), | |
| 117 | + ("deadline_set", None) => Some(Trigger::DeadlineSet), | |
| 118 | + ("deadline_cleared", None) => Some(Trigger::DeadlineCleared), | |
| 119 | + ("retitled", None) => Some(Trigger::Retitled), | |
| 120 | + ("linked", None) => Some(Trigger::Linked), | |
| 121 | + ("unlinked", None) => Some(Trigger::Unlinked), | |
| 122 | + ("deleted", None) => Some(Trigger::Deleted), | |
| 123 | + _ => None, | |
| 124 | + } | |
| 125 | +} | |
| 126 | + | |
| 127 | +/// The audience a form's own word names, or the refusal for anything else — | |
| 128 | +/// shared by `create_rule` and `update_rule`. | |
| 129 | +fn audience_of(word: &str) -> std::result::Result<Audience, Refusal> { | |
| 130 | + match word { | |
| 131 | + "assignees" => Ok(Audience::Assignees), | |
| 132 | + "board" => Ok(Audience::Board), | |
| 133 | + "creator" => Ok(Audience::Creator), | |
| 134 | + _ => Err(Refusal::Forbidden), | |
| 135 | + } | |
| 136 | +} | |
| 137 | + | |
| 138 | +/// Whether a sender is connected, read straight off the raw workspace row — | |
| 139 | +/// this module does not reach into `crate::settings::Sender` so it stays | |
| 140 | +/// independent of that screen's own shape. | |
| 141 | +fn sender_connected(workspace: &Workspace) -> bool { | |
| 142 | + !workspace.smtp_host.as_deref().unwrap_or_default().trim().is_empty() | |
| 143 | + && !workspace.smtp_username.as_deref().unwrap_or_default().trim().is_empty() | |
| 144 | + && !workspace.smtp_from_address.as_deref().unwrap_or_default().trim().is_empty() | |
| 145 | + && workspace.smtp_password_set | |
| 146 | +} | |
| 147 | + | |
| 148 | +/// Splits a trigger into the sentence halves the screen prints, ported | |
| 149 | +/// verbatim from `current_rules`'s match in `izlek-web/src/rules.rs`. | |
| 150 | +fn sentence_of(trigger: &Trigger, columns: &[Column]) -> (String, String, String, Option<String>) { | |
| 151 | + match trigger { | |
| 152 | + Trigger::StatusBecomes(column_id) => ( | |
| 153 | + "When status becomes".to_string(), | |
| 154 | + columns | |
| 155 | + .iter() | |
| 156 | + .find(|column| &column.id == column_id) | |
| 157 | + // A rule can outlive the column it names. Saying so is | |
| 158 | + // better than printing an id nobody can read. | |
| 159 | + .map(|column| column.name.clone()) | |
| 160 | + .unwrap_or_else(|| "a column that is gone".to_string()), | |
| 161 | + "status".to_string(), | |
| 162 | + Some(column_id.clone()), | |
| 163 | + ), | |
| 164 | + Trigger::Unblocked => ( | |
| 165 | + "When a task stops being".to_string(), | |
| 166 | + "blocked".to_string(), | |
| 167 | + "unblocked".to_string(), | |
| 168 | + None, | |
| 169 | + ), | |
| 170 | + Trigger::Created => { | |
| 171 | + ("When a task is created".to_string(), String::new(), "created".to_string(), None) | |
| 172 | + } | |
| 173 | + Trigger::Assigned => { | |
| 174 | + ("When someone is assigned".to_string(), String::new(), "assigned".to_string(), None) | |
| 175 | + } | |
| 176 | + Trigger::Unassigned => ( | |
| 177 | + "When someone is unassigned".to_string(), | |
| 178 | + String::new(), | |
| 179 | + "unassigned".to_string(), | |
| 180 | + None, | |
| 181 | + ), | |
| 182 | + Trigger::Commented => ( | |
| 183 | + "When a comment is written".to_string(), | |
| 184 | + String::new(), | |
| 185 | + "commented".to_string(), | |
| 186 | + None, | |
| 187 | + ), | |
| 188 | + Trigger::DeadlineSet => ( | |
| 189 | + "When a deadline is set".to_string(), | |
| 190 | + String::new(), | |
| 191 | + "deadline_set".to_string(), | |
| 192 | + None, | |
| 193 | + ), | |
| 194 | + Trigger::DeadlineCleared => ( | |
| 195 | + "When a deadline is removed".to_string(), | |
| 196 | + String::new(), | |
| 197 | + "deadline_cleared".to_string(), | |
| 198 | + None, | |
| 199 | + ), | |
| 200 | + Trigger::Retitled => { | |
| 201 | + ("When a task is renamed".to_string(), String::new(), "retitled".to_string(), None) | |
| 202 | + } | |
| 203 | + Trigger::Linked => { | |
| 204 | + ("When a task is linked".to_string(), String::new(), "linked".to_string(), None) | |
| 205 | + } | |
| 206 | + Trigger::Unlinked => ( | |
| 207 | + "When a task is unlinked".to_string(), | |
| 208 | + String::new(), | |
| 209 | + "unlinked".to_string(), | |
| 210 | + None, | |
| 211 | + ), | |
| 212 | + Trigger::Deleted => { | |
| 213 | + ("When a task is deleted".to_string(), String::new(), "deleted".to_string(), None) | |
| 214 | + } | |
| 215 | + } | |
| 216 | +} | |
| 217 | + | |
| 218 | +/// Every rule on the board, switched-off ones included, with the columns a new | |
| 219 | +/// rule may name — shared by the page and `current_rules`, so the two never | |
| 220 | +/// drift apart. | |
| 221 | +async fn snapshot_of(store: &Arc<dyn Store>, user: &User) -> std::result::Result<RulesSnapshot, Refusal> { | |
| 222 | + let Some(board) = store.board(&user.workspace_id).await.map_err(|_| Refusal::Unavailable)? else { | |
| 223 | + return Err(Refusal::Unavailable); | |
| 224 | + }; | |
| 225 | + let columns = store.columns(&board.id).await.map_err(|_| Refusal::Unavailable)?; | |
| 226 | + let rules = store.mail_rules(&board.id).await.map_err(|_| Refusal::Unavailable)?; | |
| 227 | + let last_sent = store.mail_rule_last_sent(&board.id).await.map_err(|_| Refusal::Unavailable)?; | |
| 228 | + let last_decision = store.mail_rule_last_decision().await.map_err(|_| Refusal::Unavailable)?; | |
| 229 | + let sender_connected = store | |
| 230 | + .workspace() | |
| 231 | + .await | |
| 232 | + .map_err(|_| Refusal::Unavailable)? | |
| 233 | + .as_ref() | |
| 234 | + .is_some_and(sender_connected); | |
| 235 | + | |
| 236 | + let lines = rules | |
| 237 | + .into_iter() | |
| 238 | + .map(|rule| { | |
| 239 | + let (when, what, trigger_kind, column_id) = sentence_of(&rule.trigger, &columns); | |
| 240 | + RuleLine { | |
| 241 | + when, | |
| 242 | + what, | |
| 243 | + subject: rule.subject.clone(), | |
| 244 | + audience: audience_word(rule.audience).to_string(), | |
| 245 | + trigger_kind, | |
| 246 | + column_id, | |
| 247 | + audience_kind: audience_kind(rule.audience).to_string(), | |
| 248 | + enabled: rule.enabled, | |
| 249 | + last_sent: last_sent | |
| 250 | + .iter() | |
| 251 | + .find(|(id, _)| id == &rule.id) | |
| 252 | + .map(|(_, at)| izlek_core::detail::moment_label(*at)), | |
| 253 | + last_fired: last_decision | |
| 254 | + .iter() | |
| 255 | + .find(|(id, _)| id == &rule.id) | |
| 256 | + .map(|(_, at)| izlek_core::detail::moment_label(*at)), | |
| 257 | + id: rule.id, | |
| 258 | + } | |
| 259 | + }) | |
| 260 | + .collect(); | |
| 261 | + | |
| 262 | + Ok(RulesSnapshot { | |
| 263 | + rules: lines, | |
| 264 | + columns: columns | |
| 265 | + .into_iter() | |
| 266 | + .map(|column| ColumnChoice { id: column.id, name: column.name }) | |
| 267 | + .collect(), | |
| 268 | + sender_connected, | |
| 269 | + }) | |
| 270 | +} | |
| 271 | + | |
| 272 | +/// The admin's store, once the rule id has been shown to name a rule on this | |
| 273 | +/// workspace's own board. | |
| 274 | +/// | |
| 275 | +/// A rule id is opaque and arrives from the browser, so "not yours" and "not a | |
| 276 | +/// rule" are the same answer: neither tells the caller whether the id exists | |
| 277 | +/// somewhere else. | |
| 278 | +async fn rule_of_this_workspace(cx: &Cx, rule_id: &str) -> std::result::Result<Arc<dyn Store>, Refusal> { | |
| 279 | + let user = require_admin(cx).await?; | |
| 280 | + let store = accounts(cx).store().clone(); | |
| 281 | + let board = store | |
| 282 | + .board(&user.workspace_id) | |
| 283 | + .await | |
| 284 | + .map_err(|_| Refusal::Unavailable)? | |
| 285 | + .ok_or(Refusal::Unavailable)?; | |
| 286 | + let rule = store | |
| 287 | + .mail_rule(rule_id) | |
| 288 | + .await | |
| 289 | + .map_err(|_| Refusal::Unavailable)? | |
| 290 | + .ok_or(Refusal::NotFound)?; | |
| 291 | + if rule.board_id != board.id { | |
| 292 | + return Err(Refusal::NotFound); | |
| 293 | + } | |
| 294 | + Ok(store) | |
| 295 | +} | |
| 296 | + | |
| 297 | +/// Every rule on the board, as JSON — kept for callers that post rather than | |
| 298 | +/// render, such as the http test suite. | |
| 299 | +#[route(POST "/api/current_rules")] | |
| 300 | +async fn current_rules(cx: &Cx) -> Result<Json<std::result::Result<RulesSnapshot, Refusal>>> { | |
| 301 | + let user = match require_admin(cx).await { | |
| 302 | + Ok(user) => user, | |
| 303 | + Err(refusal) => return Ok(Json(Err(refusal))), | |
| 304 | + }; | |
| 305 | + let store = accounts(cx).store().clone(); | |
| 306 | + Ok(Json(snapshot_of(&store, &user).await)) | |
| 307 | +} | |
| 308 | + | |
| 309 | +/// The page a browser without script is sent back to on a 303: the page the | |
| 310 | +/// form was posted from, or home when there is no `Referer` to read. | |
| 311 | +fn back_to(cx: &Cx) -> String { | |
| 312 | + headers(cx) | |
| 313 | + .get(header::REFERER) | |
| 314 | + .and_then(|value| value.to_str().ok()) | |
| 315 | + .unwrap_or("/") | |
| 316 | + .to_string() | |
| 317 | +} | |
| 318 | + | |
| 319 | +/// A 303 to [`back_to`], carrying `refusal` as the body for | |
| 320 | +/// `crate::server::carry_refusal_on_redirect` to read and copy onto the query. | |
| 321 | +type Redirect = Result<(StatusCode, [(HeaderName, String); 1], Json<Option<Refusal>>)>; | |
| 322 | + | |
| 323 | +fn redirect(cx: &Cx, refusal: Option<Refusal>) -> Redirect { | |
| 324 | + Ok((StatusCode::SEE_OTHER, [(header::LOCATION, back_to(cx))], Json(refusal))) | |
| 325 | +} | |
| 326 | + | |
| 327 | +#[derive(Deserialize)] | |
| 328 | +struct CreateRuleForm { | |
| 329 | + trigger: String, | |
| 330 | + column_id: String, | |
| 331 | + subject: String, | |
| 332 | + audience: String, | |
| 333 | +} | |
| 334 | + | |
| 335 | +/// Writes one rule. | |
| 336 | +/// | |
| 337 | +/// `column_id` is only read for the status trigger, and is checked against | |
| 338 | +/// this workspace's own board rather than trusted — a column id from | |
| 339 | +/// somewhere else would otherwise be a way to hang a rule off another | |
| 340 | +/// workspace's board. | |
| 341 | +#[route(POST "/api/create_rule")] | |
| 342 | +async fn create_rule(cx: &Cx, Form(input): Form<CreateRuleForm>) -> Redirect { | |
| 343 | + let user = match require_admin(cx).await { | |
| 344 | + Ok(user) => user, | |
| 345 | + Err(refusal) => return redirect(cx, Some(refusal)), | |
| 346 | + }; | |
| 347 | + let subject = input.subject.trim().to_string(); | |
| 348 | + if subject.is_empty() { | |
| 349 | + return redirect(cx, Some(Refusal::EmptySubject)); | |
| 350 | + } | |
| 351 | + let audience = match audience_of(&input.audience) { | |
| 352 | + Ok(audience) => audience, | |
| 353 | + Err(refusal) => return redirect(cx, Some(refusal)), | |
| 354 | + }; | |
| 355 | + | |
| 356 | + let store = accounts(cx).store().clone(); | |
| 357 | + let board = match store.board(&user.workspace_id).await { | |
| 358 | + Ok(Some(board)) => board, | |
| 359 | + _ => return redirect(cx, Some(Refusal::Unavailable)), | |
| 360 | + }; | |
| 361 | + let column_id = if input.column_id.trim().is_empty() { None } else { Some(input.column_id) }; | |
| 362 | + if input.trigger == "status" { | |
| 363 | + let Some(column_id) = &column_id else { return redirect(cx, Some(Refusal::Forbidden)) }; | |
| 364 | + let columns = match store.columns(&board.id).await { | |
| 365 | + Ok(columns) => columns, | |
| 366 | + Err(_) => return redirect(cx, Some(Refusal::Unavailable)), | |
| 367 | + }; | |
| 368 | + if !columns.iter().any(|column| &column.id == column_id) { | |
| 369 | + return redirect(cx, Some(Refusal::Forbidden)); | |
| 370 | + } | |
| 371 | + } | |
| 372 | + let Some(trigger) = trigger_of(&input.trigger, column_id) else { | |
| 373 | + return redirect(cx, Some(Refusal::Forbidden)); | |
| 374 | + }; | |
| 375 | + | |
| 376 | + if store | |
| 377 | + .create_mail_rule(&board.id, &trigger, &subject, audience, time::OffsetDateTime::now_utc()) | |
| 378 | + .await | |
| 379 | + .is_err() | |
| 380 | + { | |
| 381 | + return redirect(cx, Some(Refusal::Unavailable)); | |
| 382 | + } | |
| 383 | + redirect(cx, None) | |
| 384 | +} | |
| 385 | + | |
| 386 | +#[derive(Deserialize)] | |
| 387 | +struct UpdateRuleForm { | |
| 388 | + rule_id: String, | |
| 389 | + trigger: String, | |
| 390 | + column_id: String, | |
| 391 | + subject: String, | |
| 392 | + audience: String, | |
| 393 | +} | |
| 394 | + | |
| 395 | +/// Rewrites a rule's sentence in place. Guarded exactly like `create_rule` — | |
| 396 | +/// same subject rule, same column-belongs-to-this-board check — plus the | |
| 397 | +/// rule itself has to be this workspace's, or an id from elsewhere could be | |
| 398 | +/// rewritten by an admin who never owned it. | |
| 399 | +#[route(POST "/api/update_rule")] | |
| 400 | +async fn update_rule(cx: &Cx, Form(input): Form<UpdateRuleForm>) -> Redirect { | |
| 401 | + let user = match require_admin(cx).await { | |
| 402 | + Ok(user) => user, | |
| 403 | + Err(refusal) => return redirect(cx, Some(refusal)), | |
| 404 | + }; | |
| 405 | + let subject = input.subject.trim().to_string(); | |
| 406 | + if subject.is_empty() { | |
| 407 | + return redirect(cx, Some(Refusal::EmptySubject)); | |
| 408 | + } | |
| 409 | + let audience = match audience_of(&input.audience) { | |
| 410 | + Ok(audience) => audience, | |
| 411 | + Err(refusal) => return redirect(cx, Some(refusal)), | |
| 412 | + }; | |
| 413 | + | |
| 414 | + let store = accounts(cx).store().clone(); | |
| 415 | + let board = match store.board(&user.workspace_id).await { | |
| 416 | + Ok(Some(board)) => board, | |
| 417 | + _ => return redirect(cx, Some(Refusal::Unavailable)), | |
| 418 | + }; | |
| 419 | + match store.mail_rule(&input.rule_id).await { | |
| 420 | + Ok(Some(rule)) if rule.board_id == board.id => {} | |
| 421 | + Ok(_) => return redirect(cx, Some(Refusal::NotFound)), | |
| 422 | + Err(_) => return redirect(cx, Some(Refusal::Unavailable)), | |
| 423 | + } | |
| 424 | + | |
| 425 | + let column_id = if input.column_id.trim().is_empty() { None } else { Some(input.column_id) }; | |
| 426 | + if input.trigger == "status" { | |
| 427 | + let Some(column_id) = &column_id else { return redirect(cx, Some(Refusal::Forbidden)) }; | |
| 428 | + let columns = match store.columns(&board.id).await { | |
| 429 | + Ok(columns) => columns, | |
| 430 | + Err(_) => return redirect(cx, Some(Refusal::Unavailable)), | |
| 431 | + }; | |
| 432 | + if !columns.iter().any(|column| &column.id == column_id) { | |
| 433 | + return redirect(cx, Some(Refusal::Forbidden)); | |
| 434 | + } | |
| 435 | + } | |
| 436 | + let Some(trigger) = trigger_of(&input.trigger, column_id) else { | |
| 437 | + return redirect(cx, Some(Refusal::Forbidden)); | |
| 438 | + }; | |
| 439 | + | |
| 440 | + if store.update_mail_rule(&input.rule_id, &trigger, &subject, audience).await.is_err() { | |
| 441 | + return redirect(cx, Some(Refusal::Unavailable)); | |
| 442 | + } | |
| 443 | + redirect(cx, None) | |
| 444 | +} | |
| 445 | + | |
| 446 | +#[derive(Deserialize)] | |
| 447 | +struct SetRuleEnabledForm { | |
| 448 | + rule_id: String, | |
| 449 | + enabled: String, | |
| 450 | +} | |
| 451 | + | |
| 452 | +/// Turns one rule on or off. Switching off stops it firing from here on; mail | |
| 453 | +/// it already owes has been written to the ledger and is still owed. | |
| 454 | +#[route(POST "/api/set_rule_enabled")] | |
| 455 | +async fn set_rule_enabled(cx: &Cx, Form(input): Form<SetRuleEnabledForm>) -> Redirect { | |
| 456 | + let store = match rule_of_this_workspace(cx, &input.rule_id).await { | |
| 457 | + Ok(store) => store, | |
| 458 | + Err(refusal) => return redirect(cx, Some(refusal)), | |
| 459 | + }; | |
| 460 | + if store.set_mail_rule_enabled(&input.rule_id, input.enabled == "true").await.is_err() { | |
| 461 | + return redirect(cx, Some(Refusal::Unavailable)); | |
| 462 | + } | |
| 463 | + redirect(cx, None) | |
| 464 | +} | |
| 465 | + | |
| 466 | +#[derive(Deserialize)] | |
| 467 | +struct DeleteRuleForm { | |
| 468 | + rule_id: String, | |
| 469 | +} | |
| 470 | + | |
| 471 | +/// Removes a rule. What it has already sent stays in the ledger: the rule is | |
| 472 | +/// gone, the record of what went out is not. | |
| 473 | +#[route(POST "/api/delete_rule")] | |
| 474 | +async fn delete_rule(cx: &Cx, Form(input): Form<DeleteRuleForm>) -> Redirect { | |
| 475 | + let store = match rule_of_this_workspace(cx, &input.rule_id).await { | |
| 476 | + Ok(store) => store, | |
| 477 | + Err(refusal) => return redirect(cx, Some(refusal)), | |
| 478 | + }; | |
| 479 | + if store.delete_mail_rule(&input.rule_id).await.is_err() { | |
| 480 | + return redirect(cx, Some(Refusal::Unavailable)); | |
| 481 | + } | |
| 482 | + redirect(cx, None) | |
| 483 | +} | |
| 484 | + | |
| 485 | +/// Which rule (if any) `/rules` renders open for editing. | |
| 486 | +#[query_params(error = redirect("/rules"))] | |
| 487 | +struct RulesQuery { | |
| 488 | + edit: Option<String>, | |
| 489 | +} | |
| 490 | + | |
| 491 | +/// The trigger/column/subject/audience form: one view for both writing a new | |
| 492 | +/// rule and rewriting an existing one, so the two never drift apart. | |
| 493 | +/// | |
| 494 | +/// `existing` is `None` for a fresh rule and `Some` to edit one in place. The | |
| 495 | +/// column select only shows for the status trigger; every other trigger | |
| 496 | +/// carries `column_id` as a hidden field instead, the same trick | |
| 497 | +/// `izlek-web/src/rules.rs` used to hide it live — here it is decided once, | |
| 498 | +/// server-side, from whichever trigger the form starts on (the existing | |
| 499 | +/// rule's, or "status" for a fresh one), since there is no script to flip it | |
| 500 | +/// as the admin changes the select. | |
| 501 | +async fn rule_form(cx: &Cx, columns: &[ColumnChoice], existing: Option<&RuleLine>, refusal: Option<&Refusal>) -> Result { | |
| 502 | + let is_edit = existing.is_some(); | |
| 503 | + let action = if is_edit { "/api/update_rule" } else { "/api/create_rule" }; | |
| 504 | + let trigger_kind = existing.map(|rule| rule.trigger_kind.as_str()).unwrap_or("status"); | |
| 505 | + let column_id = existing | |
| 506 | + .and_then(|rule| rule.column_id.as_deref()) | |
| 507 | + .or_else(|| columns.first().map(|column| column.id.as_str())) | |
| 508 | + .unwrap_or_default(); | |
| 509 | + let subject = existing.map(|rule| rule.subject.as_str()).unwrap_or_default(); | |
| 510 | + let audience_kind = existing.map(|rule| rule.audience_kind.as_str()).unwrap_or("assignees"); | |
| 511 | + | |
| 512 | + view! { | |
| 513 | + cx => | |
| 514 | + <form method="post" action=(action) class="rule-new-body"> | |
| 515 | + if let Some(rule) = existing { | |
| 516 | + <input type="hidden" name="rule_id" value=(rule.id.clone())> | |
| 517 | + } | |
| 518 | + <label class="rule-field"> | |
| 519 | + <span class="field-label">"WHEN"</span> | |
| 520 | + <select class="field-input" name="trigger"> | |
| 521 | + <option value="status" selected=(trigger_kind == "status")>"status becomes"</option> | |
| 522 | + <option value="unblocked" selected=(trigger_kind == "unblocked")>"a task stops being blocked"</option> | |
| 523 | + <option value="created" selected=(trigger_kind == "created")>"a task is created"</option> | |
| 524 | + <option value="assigned" selected=(trigger_kind == "assigned")>"someone is assigned"</option> | |
| 525 | + <option value="unassigned" selected=(trigger_kind == "unassigned")>"someone is unassigned"</option> | |
| 526 | + <option value="commented" selected=(trigger_kind == "commented")>"a comment is written"</option> | |
| 527 | + <option value="deadline_set" selected=(trigger_kind == "deadline_set")>"a deadline is set"</option> | |
| 528 | + <option value="deadline_cleared" selected=(trigger_kind == "deadline_cleared")>"a deadline is removed"</option> | |
| 529 | + <option value="retitled" selected=(trigger_kind == "retitled")>"a task is renamed"</option> | |
| 530 | + <option value="linked" selected=(trigger_kind == "linked")>"a task is linked"</option> | |
| 531 | + <option value="unlinked" selected=(trigger_kind == "unlinked")>"a task is unlinked"</option> | |
| 532 | + <option value="deleted" selected=(trigger_kind == "deleted")>"a task is deleted"</option> | |
| 533 | + </select> | |
| 534 | + </label> | |
| 535 | + if trigger_kind == "status" { | |
| 536 | + <label class="rule-field"> | |
| 537 | + <span class="field-label">"COLUMN"</span> | |
| 538 | + <select class="field-input" name="column_id"> | |
| 539 | + for column in columns { | |
| 540 | + <option value=(column.id.clone()) selected=(column.id == column_id)>(column.name.clone())</option> | |
| 541 | + } | |
| 542 | + </select> | |
| 543 | + </label> | |
| 544 | + } else { | |
| 545 | + <input type="hidden" name="column_id" value=(column_id)> | |
| 546 | + } | |
| 547 | + <label class="rule-field"> | |
| 548 | + <span class="field-label">"SEND"</span> | |
| 549 | + <input | |
| 550 | + class="field-input" | |
| 551 | + type="text" | |
| 552 | + name="subject" | |
| 553 | + placeholder="Task completed" | |
| 554 | + maxlength="120" | |
| 555 | + required="" | |
| 556 | + value=(subject) | |
| 557 | + > | |
| 558 | + </label> | |
| 559 | + <label class="rule-field"> | |
| 560 | + <span class="field-label">"TO"</span> | |
| 561 | + <select class="field-input" name="audience"> | |
| 562 | + <option value="assignees" selected=(audience_kind == "assignees")>"assignees"</option> | |
| 563 | + <option value="board" selected=(audience_kind == "board")>"everyone on board"</option> | |
| 564 | + <option value="creator" selected=(audience_kind == "creator")>"its creator"</option> | |
| 565 | + </select> | |
| 566 | + </label> | |
| 567 | + <div class="panel-foot"> | |
| 568 | + if let Some(refusal) = refusal { | |
| 569 | + <span class="field-error">(refusal.message())</span> | |
| 570 | + } | |
| 571 | + if is_edit { | |
| 572 | + <a class="rule-delete" href="/rules">"Cancel"</a> | |
| 573 | + } | |
| 574 | + <button class="primary" type="submit">(if is_edit { "Save rule" } else { "Add rule" })</button> | |
| 575 | + </div> | |
| 576 | + </form> | |
| 577 | + } | |
| 578 | +} | |
| 579 | + | |
| 580 | +/// The "New rule" control and the form inside it. A `<details>` rather than | |
| 581 | +/// anything script-driven, so a browser with no script can still open it and | |
| 582 | +/// post the form. | |
| 583 | +async fn composer(cx: &Cx, columns: &[ColumnChoice], refusal: Option<&Refusal>) -> Result { | |
| 584 | + view! { | |
| 585 | + cx => | |
| 586 | + <details class="rule-new"> | |
| 587 | + <summary class="rule-new-open">"New rule"</summary> | |
| 588 | + (rule_form(cx, columns, None, refusal).await?) | |
| 589 | + </details> | |
| 590 | + } | |
| 591 | +} | |
| 592 | + | |
| 593 | +/// One rule's row: the form in place of it when `editing`, its display | |
| 594 | +/// otherwise. | |
| 595 | +async fn rule_row(cx: &Cx, rule: &RuleLine, columns: &[ColumnChoice], editing: bool, refusal: Option<&Refusal>) -> Result { | |
| 596 | + if editing { | |
| 597 | + return view! { | |
| 598 | + cx => | |
| 599 | + <div class="rule-row"> | |
| 600 | + (rule_form(cx, columns, Some(rule), refusal).await?) | |
| 601 | + </div> | |
| 602 | + }; | |
| 603 | + } | |
| 604 | + | |
| 605 | + let row_class = if rule.enabled { "rule-row" } else { "rule-row rule-row-off" }; | |
| 606 | + let switch_class = if rule.enabled { "rule-switch rule-switch-on" } else { "rule-switch" }; | |
| 607 | + let switch_label = if rule.enabled { "Switch this rule off" } else { "Switch this rule on" }; | |
| 608 | + let stamp = rule | |
| 609 | + .last_sent | |
| 610 | + .as_ref() | |
| 611 | + .map(|moment| format!("last sent {moment}")) | |
| 612 | + .or_else(|| rule.last_fired.as_ref().map(|moment| format!("last fired {moment}"))) | |
| 613 | + .unwrap_or_else(|| "never fired".to_string()); | |
| 614 | + | |
| 615 | + view! { | |
| 616 | + cx => | |
| 617 | + <div class=(row_class)> | |
| 618 | + <form method="post" action="/api/set_rule_enabled" class="rule-switch-form"> | |
| 619 | + <input type="hidden" name="rule_id" value=(rule.id.clone())> | |
| 620 | + <input type="hidden" name="enabled" value=((!rule.enabled).to_string())> | |
| 621 | + <button class=(switch_class) type="submit" title=(switch_label)> | |
| 622 | + <span class="rule-switch-knob"></span> | |
| 623 | + <span class="visually-hidden">(switch_label)</span> | |
| 624 | + </button> | |
| 625 | + </form> | |
| 626 | + | |
| 627 | + <div class="rule-sentence"> | |
| 628 | + <span>(rule.when.clone())</span> | |
| 629 | + <span class="rule-term">(rule.what.clone())</span> | |
| 630 | + <span>"send"</span> | |
| 631 | + <span class="rule-term">(rule.subject.clone())</span> | |
| 632 | + <span>"to"</span> | |
| 633 | + <span class="rule-term">(rule.audience.clone())</span> | |
| 634 | + </div> | |
| 635 | + | |
| 636 | + <span class="rule-stamp">(stamp)</span> | |
| 637 | + | |
| 638 | + <a class="rule-delete" href=(format!("/rules?edit={}", rule.id)) title="Edit this rule">"Edit"</a> | |
| 639 | + | |
| 640 | + <form method="post" action="/api/delete_rule"> | |
| 641 | + <input type="hidden" name="rule_id" value=(rule.id.clone())> | |
| 642 | + <button class="rule-delete" type="submit" title="Delete this rule">"Delete"</button> | |
| 643 | + </form> | |
| 644 | + </div> | |
| 645 | + } | |
| 646 | +} | |
| 647 | + | |
| 648 | +#[page("/rules")] | |
| 649 | +async fn rules_page(cx: &Cx) -> Result { | |
| 650 | + let user = match require_admin(cx).await { | |
| 651 | + Ok(user) => user, | |
| 652 | + Err(refusal) => { | |
| 653 | + return view! { | |
| 654 | + <main class="scaffold-note"> | |
| 655 | + <p>(refusal.message())</p> | |
| 656 | + <p><a href="/">"Back to the board"</a></p> | |
| 657 | + </main> | |
| 658 | + }; | |
| 659 | + } | |
| 660 | + }; | |
| 661 | + let store = accounts(cx).store().clone(); | |
| 662 | + let snapshot = match snapshot_of(&store, &user).await { | |
| 663 | + Ok(snapshot) => snapshot, | |
| 664 | + Err(refusal) => { | |
| 665 | + return view! { | |
| 666 | + <main class="scaffold-note"> | |
| 667 | + <p>(refusal.message())</p> | |
| 668 | + <p><a href="/">"Back to the board"</a></p> | |
| 669 | + </main> | |
| 670 | + }; | |
| 671 | + } | |
| 672 | + }; | |
| 673 | + let edit_id = query_params::<RulesQuery>(cx)?.edit.clone(); | |
| 674 | + let create_refusal = refusal_of(cx, "create_rule"); | |
| 675 | + let update_refusal = refusal_of(cx, "update_rule"); | |
| 676 | + | |
| 677 | + view! { | |
| 678 | + <header class="topbar"> | |
| 679 | + <div class="wordmark"> | |
| 680 | + <span class="wordmark-text">"izlek"</span> | |
| 681 | + <span class="wordmark-dot"></span> | |
| 682 | + </div> | |
| 683 | + <div class="topbar-divider"></div> | |
| 684 | + <span class="board-name">"Mail rules"</span> | |
| 685 | + <div class="spacer"></div> | |
| 686 | + <span class="topbar-who" title=(user.email.clone())>(user.display_name.clone())</span> | |
| 687 | + </header> | |
| 688 | + | |
| 689 | + <div class="settings-shell"> | |
| 690 | + <nav class="sidenav"> | |
| 691 | + <a class="sidenav-item" href="/">"Board"</a> | |
| 692 | + <a class="sidenav-item sidenav-item-on" href="/rules">"Mail rules"</a> | |
| 693 | + <a class="sidenav-item" href="/logs">"Logs"</a> | |
| 694 | + <a class="sidenav-item" href="/settings">"Settings"</a> | |
| 695 | + </nav> | |
| 696 | + | |
| 697 | + <main class="settings-stage"> | |
| 698 | + <div class="settings-head"> | |
| 699 | + <h1 class="settings-title">"Mail rules"</h1> | |
| 700 | + <span class="chip chip-admin">"Admin only"</span> | |
| 701 | + </div> | |
| 702 | + | |
| 703 | + if !snapshot.sender_connected { | |
| 704 | + <p class="rules-quiet"> | |
| 705 | + "No sender connected — mail waits in the queue until you set one in " | |
| 706 | + <a href="/settings">"Settings"</a> | |
| 707 | + "." | |
| 708 | + </p> | |
| 709 | + } | |
| 710 | + | |
| 711 | + (composer(cx, &snapshot.columns, create_refusal.as_ref()).await?) | |
| 712 | + | |
| 713 | + <div class="rule-list"> | |
| 714 | + for rule in &snapshot.rules { | |
| 715 | + (rule_row(cx, rule, &snapshot.columns, edit_id.as_deref() == Some(rule.id.as_str()), update_refusal.as_ref()).await?) | |
| 716 | + } | |
| 717 | + if snapshot.rules.is_empty() { | |
| 718 | + <p class="rules-quiet">"No rules yet."</p> | |
| 719 | + } | |
| 720 | + </div> | |
| 721 | + </main> | |
| 722 | + </div> | |
| 723 | + } | |
| 724 | +} |