| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | |
| 17 | use topcoat::Result; |
| 18 | use topcoat::context::Cx; |
| 19 | use topcoat::router::content::Form; |
| 20 | use topcoat::router::{HeaderMap, HeaderValue, StatusCode, header, page, route}; |
| 21 | use topcoat::view::{View, ViewExt, view}; |
| 22 | |
| 23 | use iz_core::detail::ActivityKind; |
| 24 | use iz_core::store::{NewSender, SenderCheck, SenderTest, StorageBackend, Store, StoreError, User}; |
| 25 | |
| 26 | use crate::i18n::{Key, Lang, t}; |
| 27 | use crate::server::{PUBLIC_URL_KEY, Refusal, config, mail, require_admin, require_user, store}; |
| 28 | |
| 29 | |
| 30 | |
| 31 | |
| 32 | |
| 33 | |
| 34 | |
| 35 | pub const WIDEST_ATTACHMENT_MB: u64 = 500; |
| 36 | |
| 37 | |
| 38 | pub const LONGEST_BATCH_MINUTES: u32 = 120; |
| 39 | |
| 40 | const MB: u64 = 1024 * 1024; |
| 41 | |
| 42 | |
| 43 | |
| 44 | |
| 45 | fn took_label(ms: u64) -> String { |
| 46 | if ms >= 1000 { |
| 47 | format!("{:.1} s", ms as f64 / 1000.0) |
| 48 | } else { |
| 49 | format!("{ms} ms") |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | |
| 54 | |
| 55 | |
| 56 | |
| 57 | fn is_address(raw: &str) -> bool { |
| 58 | let mut halves = raw.split('@'); |
| 59 | let (Some(local), Some(domain), None) = (halves.next(), halves.next(), halves.next()) else { |
| 60 | return false; |
| 61 | }; |
| 62 | !local.is_empty() |
| 63 | && domain.contains('.') |
| 64 | && !domain.starts_with('.') |
| 65 | && !domain.ends_with('.') |
| 66 | && !raw.contains(char::is_whitespace) |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | |
| 73 | |
| 74 | |
| 75 | |
| 76 | fn parse_types(raw: &str) -> Option<Vec<String>> { |
| 77 | let mut types: Vec<String> = Vec::new(); |
| 78 | for piece in raw.split([',', ' ', '\n']) { |
| 79 | let piece = piece.trim().trim_start_matches('.').to_lowercase(); |
| 80 | if piece.is_empty() { |
| 81 | continue; |
| 82 | } |
| 83 | if piece.len() > 12 || !piece.chars().all(|c| c.is_ascii_alphanumeric()) { |
| 84 | return None; |
| 85 | } |
| 86 | if !types.contains(&piece) { |
| 87 | types.push(piece); |
| 88 | } |
| 89 | } |
| 90 | Some(types) |
| 91 | } |
| 92 | |
| 93 | #[cfg(test)] |
| 94 | mod codec_tests { |
| 95 | use super::{parse_types, took_label}; |
| 96 | #[test] |
| 97 | fn a_list_is_lowercased_undotted_and_deduplicated() { |
| 98 | assert_eq!( |
| 99 | parse_types(".PNG, png, jpg").unwrap(), |
| 100 | vec!["png".to_string(), "jpg".to_string()] |
| 101 | ); |
| 102 | } |
| 103 | |
| 104 | #[test] |
| 105 | fn an_empty_list_means_every_type() { |
| 106 | assert_eq!(parse_types(" ").unwrap(), Vec::<String>::new()); |
| 107 | } |
| 108 | |
| 109 | #[test] |
| 110 | fn nothing_that_is_not_an_extension_is_stored_as_one() { |
| 111 | assert!(parse_types("image/png").is_none()); |
| 112 | assert!(parse_types("../etc").is_none()); |
| 113 | assert!(parse_types("*").is_none()); |
| 114 | assert!(parse_types("tar.gz").is_none()); |
| 115 | } |
| 116 | |
| 117 | #[test] |
| 118 | fn a_send_is_timed_the_way_the_artboard_writes_it() { |
| 119 | assert_eq!(took_label(1234), "1.2 s"); |
| 120 | assert_eq!(took_label(1000), "1.0 s"); |
| 121 | assert_eq!(took_label(999), "999 ms"); |
| 122 | assert_eq!(took_label(0), "0 ms"); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | |
| 127 | fn redirect_to(query: &str) -> (StatusCode, HeaderMap, Vec<u8>) { |
| 128 | let mut headers = HeaderMap::new(); |
| 129 | let location = if query.is_empty() { |
| 130 | "/settings".to_string() |
| 131 | } else { |
| 132 | format!("/settings?{query}") |
| 133 | }; |
| 134 | if let Ok(value) = HeaderValue::from_str(&location) { |
| 135 | headers.insert(header::LOCATION, value); |
| 136 | } |
| 137 | (StatusCode::SEE_OTHER, headers, Vec::new()) |
| 138 | } |
| 139 | |
| 140 | |
| 141 | |
| 142 | fn section_of_call(call: &str) -> &'static str { |
| 143 | match call { |
| 144 | "save_sender" | "send_test_mail" | "check_sender" => "outgoing", |
| 145 | "save_limits" | "save_storage" => "limits", |
| 146 | "save_public_url" => "server", |
| 147 | "set_role" | "set_disabled" | "add_member" => "members", |
| 148 | "send_message" => "message", |
| 149 | _ => "profile", |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | |
| 154 | |
| 155 | |
| 156 | |
| 157 | pub(crate) fn saved_or_refused( |
| 158 | call: &str, |
| 159 | refusal: Option<Refusal>, |
| 160 | ) -> (StatusCode, HeaderMap, Vec<u8>) { |
| 161 | let section = section_of_call(call); |
| 162 | match refusal { |
| 163 | |
| 164 | |
| 165 | |
| 166 | Some(Refusal::BadSender(problem)) => redirect_to(&format!( |
| 167 | "refusal=bad-sender&on={call}&why={}§ion={section}", |
| 168 | qsencode(&problem) |
| 169 | )), |
| 170 | Some(refusal) => redirect_to(&format!( |
| 171 | "refusal={}&on={call}§ion={section}", |
| 172 | refusal.code() |
| 173 | )), |
| 174 | None => redirect_to(&format!("saved={call}§ion={section}")), |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | fn qsencode(text: &str) -> String { |
| 183 | text.bytes() |
| 184 | .map(|b| match b { |
| 185 | b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => { |
| 186 | (b as char).to_string() |
| 187 | } |
| 188 | b' ' => "+".to_string(), |
| 189 | _ => format!("%{b:02X}"), |
| 190 | }) |
| 191 | .collect() |
| 192 | } |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | fn qsdecode(text: &str) -> String { |
| 198 | let bytes = text.as_bytes(); |
| 199 | let mut out = Vec::with_capacity(bytes.len()); |
| 200 | let mut i = 0; |
| 201 | while i < bytes.len() { |
| 202 | match bytes[i] { |
| 203 | b'+' => { |
| 204 | out.push(b' '); |
| 205 | i += 1; |
| 206 | } |
| 207 | b'%' if i + 2 < bytes.len() => match u8::from_str_radix(&text[i + 1..i + 3], 16) { |
| 208 | Ok(byte) => { |
| 209 | out.push(byte); |
| 210 | i += 3; |
| 211 | } |
| 212 | Err(_) => { |
| 213 | out.push(b'%'); |
| 214 | i += 1; |
| 215 | } |
| 216 | }, |
| 217 | b => { |
| 218 | out.push(b); |
| 219 | i += 1; |
| 220 | } |
| 221 | } |
| 222 | } |
| 223 | String::from_utf8_lossy(&out).into_owned() |
| 224 | } |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | #[derive(serde::Deserialize)] |
| 231 | struct SaveLimitsForm { |
| 232 | attachment_limit_mb: u64, |
| 233 | allowed_file_types: String, |
| 234 | mail_batch_minutes: u32, |
| 235 | #[serde(default)] |
| 236 | reminder_minutes: Option<String>, |
| 237 | } |
| 238 | |
| 239 | |
| 240 | |
| 241 | |
| 242 | |
| 243 | |
| 244 | #[route(POST "/api/save_limits")] |
| 245 | async fn save_limits( |
| 246 | cx: &Cx, |
| 247 | Form(input): Form<SaveLimitsForm>, |
| 248 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 249 | let admin = match require_admin(cx).await { |
| 250 | Ok(admin) => admin, |
| 251 | Err(refusal) => return Ok(saved_or_refused("save_limits", Some(refusal))), |
| 252 | }; |
| 253 | if input.attachment_limit_mb == 0 |
| 254 | || input.attachment_limit_mb > WIDEST_ATTACHMENT_MB |
| 255 | || input.mail_batch_minutes > LONGEST_BATCH_MINUTES |
| 256 | { |
| 257 | return Ok(saved_or_refused("save_limits", Some(Refusal::BadLimit))); |
| 258 | } |
| 259 | let reminder_minutes = match input.reminder_minutes.as_deref().map(str::trim) { |
| 260 | |
| 261 | |
| 262 | |
| 263 | None | Some("") => match store(cx).workspace().await { |
| 264 | Ok(Some(workspace)) => workspace.reminder_minutes, |
| 265 | Ok(None) => 0, |
| 266 | Err(problem) => { |
| 267 | eprintln!("store error: {problem}"); |
| 268 | return Ok(saved_or_refused("save_limits", Some(Refusal::Unavailable))); |
| 269 | } |
| 270 | }, |
| 271 | Some(raw) => match raw.parse::<u32>() { |
| 272 | Ok(minutes) => minutes, |
| 273 | Err(_) => return Ok(saved_or_refused("save_limits", Some(Refusal::BadLimit))), |
| 274 | }, |
| 275 | }; |
| 276 | let Some(types) = parse_types(&input.allowed_file_types) else { |
| 277 | return Ok(saved_or_refused("save_limits", Some(Refusal::BadFileType))); |
| 278 | }; |
| 279 | let outcome = store(cx) |
| 280 | .set_limits( |
| 281 | &admin.workspace_id, |
| 282 | input.attachment_limit_mb * MB, |
| 283 | &types, |
| 284 | input.mail_batch_minutes, |
| 285 | reminder_minutes, |
| 286 | ) |
| 287 | .await; |
| 288 | let refusal = match outcome { |
| 289 | Ok(()) => { |
| 290 | let _ = store(cx) |
| 291 | .record_event( |
| 292 | Some(&admin.id), |
| 293 | &ActivityKind::LimitsSaved, |
| 294 | "", |
| 295 | time::OffsetDateTime::now_utc(), |
| 296 | ) |
| 297 | .await; |
| 298 | None |
| 299 | } |
| 300 | Err(problem) => { |
| 301 | eprintln!("store error: {problem}"); |
| 302 | Some(Refusal::Unavailable) |
| 303 | } |
| 304 | }; |
| 305 | Ok(saved_or_refused("save_limits", refusal)) |
| 306 | } |
| 307 | |
| 308 | #[derive(serde::Deserialize)] |
| 309 | struct SaveStorageForm { |
| 310 | backend: String, |
| 311 | } |
| 312 | |
| 313 | |
| 314 | |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | #[route(POST "/api/save_storage")] |
| 321 | async fn save_storage( |
| 322 | cx: &Cx, |
| 323 | Form(input): Form<SaveStorageForm>, |
| 324 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 325 | if let Err(refusal) = require_admin(cx).await { |
| 326 | return Ok(saved_or_refused("save_storage", Some(refusal))); |
| 327 | } |
| 328 | let backend = match input.backend.as_str() { |
| 329 | "local" => StorageBackend::Local, |
| 330 | "in" => { |
| 331 | if config(cx).storage_in.is_none() { |
| 332 | return Ok(saved_or_refused( |
| 333 | "save_storage", |
| 334 | Some(Refusal::StorageNotConfigured), |
| 335 | )); |
| 336 | } |
| 337 | let listed = crate::layout::family_of(cx) |
| 338 | .await |
| 339 | .iter() |
| 340 | .any(|service| service.key == "in"); |
| 341 | if !listed { |
| 342 | return Ok(saved_or_refused( |
| 343 | "save_storage", |
| 344 | Some(Refusal::StorageNotListed), |
| 345 | )); |
| 346 | } |
| 347 | StorageBackend::In |
| 348 | } |
| 349 | _ => return Ok(saved_or_refused("save_storage", Some(Refusal::NotFound))), |
| 350 | }; |
| 351 | match store(cx).set_storage_backend(backend).await { |
| 352 | Ok(()) => Ok(saved_or_refused("save_storage", None)), |
| 353 | Err(problem) => { |
| 354 | eprintln!("store error: {problem}"); |
| 355 | Ok(saved_or_refused("save_storage", Some(Refusal::Unavailable))) |
| 356 | } |
| 357 | } |
| 358 | } |
| 359 | |
| 360 | #[derive(serde::Deserialize)] |
| 361 | struct SaveSenderForm { |
| 362 | host: String, |
| 363 | port: u32, |
| 364 | username: String, |
| 365 | #[serde(default)] |
| 366 | password: String, |
| 367 | from_name: String, |
| 368 | from_address: String, |
| 369 | |
| 370 | |
| 371 | #[serde(default)] |
| 372 | public_url: String, |
| 373 | } |
| 374 | |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | |
| 380 | |
| 381 | |
| 382 | |
| 383 | |
| 384 | |
| 385 | |
| 386 | #[route(POST "/api/save_sender")] |
| 387 | async fn save_sender( |
| 388 | cx: &Cx, |
| 389 | Form(input): Form<SaveSenderForm>, |
| 390 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 391 | let admin = match require_admin(cx).await { |
| 392 | Ok(admin) => admin, |
| 393 | Err(refusal) => return Ok(saved_or_refused("save_sender", Some(refusal))), |
| 394 | }; |
| 395 | let lang = Lang::from_code(&admin.language); |
| 396 | |
| 397 | let host = input.host.trim().to_string(); |
| 398 | let username = input.username.trim().to_string(); |
| 399 | let from_name = input.from_name.trim().to_string(); |
| 400 | let from_address = input.from_address.trim().to_string(); |
| 401 | |
| 402 | |
| 403 | let public_url = input.public_url.trim().trim_end_matches('/').to_string(); |
| 404 | |
| 405 | |
| 406 | let password = (!input.password.is_empty()).then_some(input.password); |
| 407 | |
| 408 | let store = store(cx).clone(); |
| 409 | let known = store |
| 410 | .workspace() |
| 411 | .await? |
| 412 | .map(|ws| ws.smtp_password_set) |
| 413 | .unwrap_or(false); |
| 414 | |
| 415 | let complaint = if host.is_empty() { |
| 416 | Some(Key::SmtpHostRequired) |
| 417 | } else if host.contains(char::is_whitespace) || host.contains('@') || host.contains('/') { |
| 418 | Some(Key::SmtpHostInvalid) |
| 419 | } else if !(1..=65535).contains(&input.port) { |
| 420 | Some(Key::PortInvalid) |
| 421 | } else if username.is_empty() { |
| 422 | Some(Key::SmtpUsernameRequired) |
| 423 | } else if password.is_none() && !known { |
| 424 | Some(Key::PasswordNeededFirstTime) |
| 425 | } else if !is_address(&from_address) { |
| 426 | Some(Key::NotFromAddress) |
| 427 | } else if !public_url.is_empty() && !is_origin(&public_url) { |
| 428 | Some(Key::NotAnOrigin) |
| 429 | } else { |
| 430 | None |
| 431 | }; |
| 432 | if let Some(problem) = complaint { |
| 433 | return Ok(saved_or_refused( |
| 434 | "save_sender", |
| 435 | Some(Refusal::BadSender(t(lang, problem).to_string())), |
| 436 | )); |
| 437 | } |
| 438 | |
| 439 | let outcome = match store |
| 440 | .set_sender( |
| 441 | &admin.workspace_id, |
| 442 | NewSender { |
| 443 | host, |
| 444 | port: input.port, |
| 445 | username, |
| 446 | password, |
| 447 | from_name, |
| 448 | from_address, |
| 449 | }, |
| 450 | ) |
| 451 | .await |
| 452 | { |
| 453 | Ok(()) => { |
| 454 | store |
| 455 | .set_public_url( |
| 456 | &admin.workspace_id, |
| 457 | (!public_url.is_empty()).then_some(public_url.as_str()), |
| 458 | ) |
| 459 | .await |
| 460 | } |
| 461 | Err(problem) => Err(problem), |
| 462 | }; |
| 463 | if outcome.is_ok() { |
| 464 | |
| 465 | |
| 466 | |
| 467 | |
| 468 | probe_sender(mail(cx), store.clone(), admin.workspace_id.clone()); |
| 469 | } |
| 470 | let refusal = match outcome { |
| 471 | Ok(()) => { |
| 472 | let _ = store |
| 473 | .record_event( |
| 474 | Some(&admin.id), |
| 475 | &ActivityKind::SenderSaved, |
| 476 | "", |
| 477 | time::OffsetDateTime::now_utc(), |
| 478 | ) |
| 479 | .await; |
| 480 | None |
| 481 | } |
| 482 | Err(problem) => { |
| 483 | eprintln!("store error: {problem}"); |
| 484 | Some(Refusal::Unavailable) |
| 485 | } |
| 486 | }; |
| 487 | Ok(saved_or_refused("save_sender", refusal)) |
| 488 | } |
| 489 | |
| 490 | |
| 491 | |
| 492 | |
| 493 | |
| 494 | |
| 495 | |
| 496 | |
| 497 | |
| 498 | |
| 499 | |
| 500 | const PROBE_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(15); |
| 501 | |
| 502 | |
| 503 | |
| 504 | |
| 505 | |
| 506 | |
| 507 | |
| 508 | |
| 509 | |
| 510 | fn probe_sender( |
| 511 | engine: crate::server::Mail, |
| 512 | store: std::sync::Arc<dyn Store>, |
| 513 | workspace_id: String, |
| 514 | ) { |
| 515 | tokio::spawn(async move { |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | |
| 521 | |
| 522 | let dialled = tokio::time::timeout(PROBE_TIMEOUT, engine.check()).await; |
| 523 | let outcome = match dialled { |
| 524 | Ok(outcome) => outcome, |
| 525 | Err(_) => Some(Err(iz_core::MailError::retryable( |
| 526 | "the mail server did not answer", |
| 527 | ))), |
| 528 | }; |
| 529 | let Some(outcome) = outcome else { |
| 530 | |
| 531 | |
| 532 | return; |
| 533 | }; |
| 534 | let at = time::OffsetDateTime::now_utc(); |
| 535 | let check = match outcome { |
| 536 | Ok(took) => SenderCheck { |
| 537 | at, |
| 538 | took_ms: took.whole_milliseconds().max(0) as u64, |
| 539 | error: None, |
| 540 | }, |
| 541 | Err(problem) => SenderCheck { |
| 542 | at, |
| 543 | took_ms: 0, |
| 544 | |
| 545 | error: Some(problem.message.clone()), |
| 546 | }, |
| 547 | }; |
| 548 | if let Err(problem) = store.record_sender_check(&workspace_id, check).await { |
| 549 | eprintln!("store error: {problem}"); |
| 550 | } |
| 551 | }); |
| 552 | } |
| 553 | |
| 554 | #[derive(serde::Deserialize)] |
| 555 | struct SavePublicUrlForm { |
| 556 | #[serde(default)] |
| 557 | public_url: String, |
| 558 | } |
| 559 | |
| 560 | |
| 561 | |
| 562 | |
| 563 | |
| 564 | |
| 565 | |
| 566 | |
| 567 | |
| 568 | |
| 569 | |
| 570 | |
| 571 | |
| 572 | #[route(POST "/api/save_public_url")] |
| 573 | async fn save_public_url( |
| 574 | cx: &Cx, |
| 575 | Form(input): Form<SavePublicUrlForm>, |
| 576 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 577 | if let Err(refusal) = require_admin(cx).await { |
| 578 | return Ok(saved_or_refused("save_public_url", Some(refusal))); |
| 579 | } |
| 580 | |
| 581 | |
| 582 | let public_url = input.public_url.trim().trim_end_matches('/').to_string(); |
| 583 | if !public_url.is_empty() && !is_origin(&public_url) { |
| 584 | return Ok(saved_or_refused( |
| 585 | "save_public_url", |
| 586 | Some(Refusal::BadOrigin), |
| 587 | )); |
| 588 | } |
| 589 | let refusal = match store(cx).set_setting(PUBLIC_URL_KEY, &public_url).await { |
| 590 | Ok(()) => None, |
| 591 | Err(problem) => { |
| 592 | eprintln!("store error: {problem}"); |
| 593 | Some(Refusal::Unavailable) |
| 594 | } |
| 595 | }; |
| 596 | Ok(saved_or_refused("save_public_url", refusal)) |
| 597 | } |
| 598 | |
| 599 | |
| 600 | #[route(POST "/api/check_sender")] |
| 601 | async fn check_sender(cx: &Cx) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 602 | let admin = match require_admin(cx).await { |
| 603 | Ok(admin) => admin, |
| 604 | Err(refusal) => return Ok(saved_or_refused("check_sender", Some(refusal))), |
| 605 | }; |
| 606 | let store = store(cx).clone(); |
| 607 | probe_sender(mail(cx), store.clone(), admin.workspace_id.clone()); |
| 608 | let _ = store |
| 609 | .record_event( |
| 610 | Some(&admin.id), |
| 611 | &ActivityKind::Other("sender_checked".to_string()), |
| 612 | "", |
| 613 | time::OffsetDateTime::now_utc(), |
| 614 | ) |
| 615 | .await; |
| 616 | Ok(saved_or_refused("check_sender", None)) |
| 617 | } |
| 618 | |
| 619 | #[route(POST "/api/send_test_mail")] |
| 620 | async fn send_test_mail(cx: &Cx) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 621 | let admin = match require_admin(cx).await { |
| 622 | Ok(admin) => admin, |
| 623 | Err(refusal) => return Ok(saved_or_refused("send_test_mail", Some(refusal))), |
| 624 | }; |
| 625 | let lang = Lang::from_code(&admin.language); |
| 626 | |
| 627 | let store = store(cx).clone(); |
| 628 | let configured = store |
| 629 | .workspace() |
| 630 | .await? |
| 631 | .is_some_and(|ws| ws.smtp_password_set && ws.smtp_host.is_some()); |
| 632 | if !configured { |
| 633 | return Ok(saved_or_refused( |
| 634 | "send_test_mail", |
| 635 | Some(Refusal::BadSender( |
| 636 | t(lang, Key::SenderNotConfiguredYet).to_string(), |
| 637 | )), |
| 638 | )); |
| 639 | } |
| 640 | |
| 641 | let Some(outcome) = mail(cx).test(&admin.email).await else { |
| 642 | |
| 643 | |
| 644 | return Ok(saved_or_refused( |
| 645 | "send_test_mail", |
| 646 | Some(Refusal::Unavailable), |
| 647 | )); |
| 648 | }; |
| 649 | |
| 650 | let at = time::OffsetDateTime::now_utc(); |
| 651 | let test = match outcome { |
| 652 | Ok(took) => SenderTest { |
| 653 | at, |
| 654 | took_ms: took.whole_milliseconds().max(0) as u64, |
| 655 | error: None, |
| 656 | }, |
| 657 | Err(problem) => SenderTest { |
| 658 | at, |
| 659 | took_ms: 0, |
| 660 | |
| 661 | |
| 662 | error: Some(problem.message.clone()), |
| 663 | }, |
| 664 | }; |
| 665 | let sent = test.error.is_none(); |
| 666 | if let Err(problem) = store.record_sender_test(&admin.workspace_id, test).await { |
| 667 | eprintln!("store error: {problem}"); |
| 668 | return Ok(saved_or_refused( |
| 669 | "send_test_mail", |
| 670 | Some(Refusal::Unavailable), |
| 671 | )); |
| 672 | } |
| 673 | if sent { |
| 674 | let _ = store |
| 675 | .record_event( |
| 676 | Some(&admin.id), |
| 677 | &ActivityKind::TestMailSent, |
| 678 | "", |
| 679 | time::OffsetDateTime::now_utc(), |
| 680 | ) |
| 681 | .await; |
| 682 | } |
| 683 | Ok(saved_or_refused("send_test_mail", None)) |
| 684 | } |
| 685 | |
| 686 | #[derive(serde::Deserialize)] |
| 687 | struct SaveProfileForm { |
| 688 | theme: String, |
| 689 | language: String, |
| 690 | ui: String, |
| 691 | } |
| 692 | |
| 693 | |
| 694 | const THEME_OPTIONS: [&str; 2] = ["light", "dark"]; |
| 695 | |
| 696 | |
| 697 | const UI_OPTIONS: [&str; 2] = ["instrument", "ledger"]; |
| 698 | |
| 699 | |
| 700 | const LANGUAGE_OPTIONS: [&str; 2] = ["en", "tr"]; |
| 701 | |
| 702 | #[route(POST "/api/save_profile")] |
| 703 | async fn save_profile( |
| 704 | cx: &Cx, |
| 705 | Form(input): Form<SaveProfileForm>, |
| 706 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 707 | let user = match require_user(cx).await { |
| 708 | Ok(user) => user, |
| 709 | Err(refusal) => return Ok(saved_or_refused("save_profile", Some(refusal))), |
| 710 | }; |
| 711 | if !THEME_OPTIONS.contains(&input.theme.as_str()) { |
| 712 | return Ok(saved_or_refused("save_profile", Some(Refusal::BadTheme))); |
| 713 | } |
| 714 | if !UI_OPTIONS.contains(&input.ui.as_str()) { |
| 715 | return Ok(saved_or_refused("save_profile", Some(Refusal::BadUi))); |
| 716 | } |
| 717 | if !LANGUAGE_OPTIONS.contains(&input.language.as_str()) { |
| 718 | return Ok(saved_or_refused("save_profile", Some(Refusal::BadLanguage))); |
| 719 | } |
| 720 | let store = store(cx); |
| 721 | let refusal = match store |
| 722 | .set_preferences( |
| 723 | &user.id, |
| 724 | &user.timezone, |
| 725 | &input.theme, |
| 726 | &input.language, |
| 727 | &input.ui, |
| 728 | ) |
| 729 | .await |
| 730 | { |
| 731 | Ok(()) => { |
| 732 | let _ = store |
| 733 | .record_event( |
| 734 | Some(&user.id), |
| 735 | &ActivityKind::ProfileSaved, |
| 736 | "", |
| 737 | time::OffsetDateTime::now_utc(), |
| 738 | ) |
| 739 | .await; |
| 740 | None |
| 741 | } |
| 742 | Err(problem) => { |
| 743 | eprintln!("store error: {problem}"); |
| 744 | Some(Refusal::Unavailable) |
| 745 | } |
| 746 | }; |
| 747 | Ok(saved_or_refused("save_profile", refusal)) |
| 748 | } |
| 749 | |
| 750 | #[derive(serde::Deserialize)] |
| 751 | struct SetRoleForm { |
| 752 | user_id: String, |
| 753 | role: iz_core::Role, |
| 754 | } |
| 755 | |
| 756 | |
| 757 | |
| 758 | |
| 759 | #[route(POST "/api/set_role")] |
| 760 | async fn set_role( |
| 761 | cx: &Cx, |
| 762 | Form(input): Form<SetRoleForm>, |
| 763 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 764 | let admin = match require_admin(cx).await { |
| 765 | Ok(admin) => admin, |
| 766 | Err(refusal) => return Ok(saved_or_refused("set_role", Some(refusal))), |
| 767 | }; |
| 768 | if input.role == iz_core::Role::Admin { |
| 769 | return Ok(saved_or_refused("set_role", Some(Refusal::Forbidden))); |
| 770 | } |
| 771 | let store = store(cx); |
| 772 | let member = match store.user(&input.user_id).await? { |
| 773 | Some(member) => member, |
| 774 | None => return Ok(saved_or_refused("set_role", Some(Refusal::NoSuchMember))), |
| 775 | }; |
| 776 | if member.workspace_id != admin.workspace_id || member.id == admin.id { |
| 777 | return Ok(saved_or_refused("set_role", Some(Refusal::Forbidden))); |
| 778 | } |
| 779 | let owner = store.owner().await?; |
| 780 | if owner.map(|owner| owner.id) == Some(member.id.clone()) { |
| 781 | return Ok(saved_or_refused("set_role", Some(Refusal::Forbidden))); |
| 782 | } |
| 783 | let refusal = match store.set_role(&member.id, input.role).await { |
| 784 | Ok(()) => { |
| 785 | let _ = store |
| 786 | .record_event( |
| 787 | Some(&admin.id), |
| 788 | &ActivityKind::RoleChanged, |
| 789 | &format!("{} -> {}", member.display_name, input.role.as_str()), |
| 790 | time::OffsetDateTime::now_utc(), |
| 791 | ) |
| 792 | .await; |
| 793 | None |
| 794 | } |
| 795 | Err(problem) => { |
| 796 | eprintln!("store error: {problem}"); |
| 797 | Some(Refusal::Unavailable) |
| 798 | } |
| 799 | }; |
| 800 | Ok(saved_or_refused("set_role", refusal)) |
| 801 | } |
| 802 | |
| 803 | #[derive(serde::Deserialize)] |
| 804 | struct DisableForm { |
| 805 | user_id: String, |
| 806 | #[serde(default)] |
| 807 | disabled: Option<String>, |
| 808 | } |
| 809 | |
| 810 | |
| 811 | |
| 812 | |
| 813 | #[route(POST "/api/set_disabled")] |
| 814 | async fn set_disabled( |
| 815 | cx: &Cx, |
| 816 | Form(input): Form<DisableForm>, |
| 817 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 818 | let admin = match require_admin(cx).await { |
| 819 | Ok(admin) => admin, |
| 820 | Err(refusal) => return Ok(saved_or_refused("set_disabled", Some(refusal))), |
| 821 | }; |
| 822 | if input.user_id == admin.id { |
| 823 | return Ok(saved_or_refused("set_disabled", Some(Refusal::Forbidden))); |
| 824 | } |
| 825 | let disabled = match input.disabled.as_deref() { |
| 826 | None => true, |
| 827 | Some(value) => !matches!( |
| 828 | value.trim().to_lowercase().as_str(), |
| 829 | "" | "0" | "false" | "no" | "off" |
| 830 | ), |
| 831 | }; |
| 832 | let refusal = match store(cx).set_user_disabled(&input.user_id, disabled).await { |
| 833 | Ok(()) => None, |
| 834 | Err(problem) => { |
| 835 | eprintln!("store error: {problem}"); |
| 836 | Some(Refusal::Unavailable) |
| 837 | } |
| 838 | }; |
| 839 | Ok(saved_or_refused("set_disabled", refusal)) |
| 840 | } |
| 841 | |
| 842 | #[derive(serde::Deserialize)] |
| 843 | struct AddMemberForm { |
| 844 | display_name: String, |
| 845 | email: String, |
| 846 | role: iz_core::Role, |
| 847 | } |
| 848 | |
| 849 | |
| 850 | |
| 851 | |
| 852 | |
| 853 | |
| 854 | #[route(POST "/api/add_member")] |
| 855 | async fn add_member( |
| 856 | cx: &Cx, |
| 857 | Form(input): Form<AddMemberForm>, |
| 858 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 859 | let admin = match require_admin(cx).await { |
| 860 | Ok(admin) => admin, |
| 861 | Err(refusal) => return Ok(saved_or_refused("add_member", Some(refusal))), |
| 862 | }; |
| 863 | if input.role == iz_core::Role::Admin { |
| 864 | return Ok(saved_or_refused("add_member", Some(Refusal::Forbidden))); |
| 865 | } |
| 866 | if input.display_name.trim().is_empty() { |
| 867 | return Ok(saved_or_refused("add_member", Some(Refusal::EmptyName))); |
| 868 | } |
| 869 | if !is_address(input.email.trim()) { |
| 870 | return Ok(saved_or_refused("add_member", Some(Refusal::BadEmail))); |
| 871 | } |
| 872 | let refusal = match store(cx) |
| 873 | .add_member( |
| 874 | &admin.workspace_id, |
| 875 | &input.email, |
| 876 | &input.display_name, |
| 877 | input.role, |
| 878 | ) |
| 879 | .await |
| 880 | { |
| 881 | Ok(_) => None, |
| 882 | Err(StoreError::Conflict("member")) => Some(Refusal::AlreadyMember), |
| 883 | Err(problem) => { |
| 884 | eprintln!("store error: {problem}"); |
| 885 | Some(Refusal::Unavailable) |
| 886 | } |
| 887 | }; |
| 888 | Ok(saved_or_refused("add_member", refusal)) |
| 889 | } |
| 890 | |
| 891 | #[derive(serde::Deserialize)] |
| 892 | struct SendMessageForm { |
| 893 | to: String, |
| 894 | subject: String, |
| 895 | body: String, |
| 896 | } |
| 897 | |
| 898 | #[route(POST "/api/send_message")] |
| 899 | async fn send_message( |
| 900 | cx: &Cx, |
| 901 | Form(input): Form<SendMessageForm>, |
| 902 | ) -> Result<(StatusCode, HeaderMap, Vec<u8>)> { |
| 903 | let admin = match require_admin(cx).await { |
| 904 | Ok(admin) => admin, |
| 905 | Err(refusal) => return Ok(saved_or_refused("send_message", Some(refusal))), |
| 906 | }; |
| 907 | let subject = input.subject.trim(); |
| 908 | if subject.is_empty() { |
| 909 | return Ok(saved_or_refused( |
| 910 | "send_message", |
| 911 | Some(Refusal::EmptySubject), |
| 912 | )); |
| 913 | } |
| 914 | let body = input.body.trim(); |
| 915 | if body.is_empty() { |
| 916 | return Ok(saved_or_refused("send_message", Some(Refusal::EmptyBody))); |
| 917 | } |
| 918 | let store = store(cx).clone(); |
| 919 | let members = store.users(&admin.workspace_id).await?; |
| 920 | let recipients: Vec<String> = if input.to == "everyone" { |
| 921 | members |
| 922 | .into_iter() |
| 923 | .filter(|member| member.id != admin.id) |
| 924 | .map(|member| member.email) |
| 925 | .collect() |
| 926 | } else { |
| 927 | match members.into_iter().find(|member| member.id == input.to) { |
| 928 | Some(member) => vec![member.email], |
| 929 | None => { |
| 930 | return Ok(saved_or_refused( |
| 931 | "send_message", |
| 932 | Some(Refusal::NoSuchMember), |
| 933 | )); |
| 934 | } |
| 935 | } |
| 936 | }; |
| 937 | let now = time::OffsetDateTime::now_utc(); |
| 938 | for recipient in &recipients { |
| 939 | if let Err(problem) = store.queue_notice(recipient, subject, body, now).await { |
| 940 | eprintln!("store error: {problem}"); |
| 941 | return Ok(saved_or_refused("send_message", Some(Refusal::Unavailable))); |
| 942 | } |
| 943 | } |
| 944 | let _ = store |
| 945 | .record_event(Some(&admin.id), &ActivityKind::MessageSent, subject, now) |
| 946 | .await; |
| 947 | Ok(saved_or_refused("send_message", None)) |
| 948 | } |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | |
| 957 | |
| 958 | async fn connection_card<'a>(cx: &'a Cx, lang: Lang) -> Result<impl View + 'a> { |
| 959 | let config = config(cx); |
| 960 | let issuer = config.oidc.issuer.clone(); |
| 961 | let client_id = config.oidc.client_id.clone(); |
| 962 | let snap = crate::directory::health(cx).snapshot(); |
| 963 | let (state_word, state_class) = if snap.connected { |
| 964 | (t(lang, Key::ConnectionConnected), "connection-on") |
| 965 | } else { |
| 966 | (t(lang, Key::ConnectionReconnecting), "connection-wait") |
| 967 | }; |
| 968 | let event_age = crate::directory::age_text(snap.event_age); |
| 969 | let pass_age = crate::directory::age_text(snap.pass_age); |
| 970 | let never = t(lang, Key::ConnectionNever).to_string(); |
| 971 | let event_line = if event_age.is_empty() { |
| 972 | never.clone() |
| 973 | } else { |
| 974 | format!("{} {}", event_age, t(lang, Key::Ago)) |
| 975 | }; |
| 976 | let pass_line = if pass_age.is_empty() { |
| 977 | never |
| 978 | } else { |
| 979 | format!("{} {}", pass_age, t(lang, Key::Ago)) |
| 980 | }; |
| 981 | Ok(view! { |
| 982 | cx => |
| 983 | <dl class="connection-facts"> |
| 984 | <dt>(t(lang, Key::ConnectionIssuer))</dt> |
| 985 | <dd class="connection-fact">(issuer)</dd> |
| 986 | <dt>(t(lang, Key::ConnectionClientId))</dt> |
| 987 | <dd class="connection-fact">(client_id)</dd> |
| 988 | <dt>(t(lang, Key::ConnectionStream))</dt> |
| 989 | <dd class="connection-fact"> |
| 990 | <span class=(format!("connection-dot {state_class}"))></span> |
| 991 | (state_word) |
| 992 | </dd> |
| 993 | <dt>(t(lang, Key::ConnectionLastEvent))</dt> |
| 994 | <dd class="connection-fact">(event_line)</dd> |
| 995 | <dt>(t(lang, Key::ConnectionLastPass))</dt> |
| 996 | <dd class="connection-fact">(pass_line)</dd> |
| 997 | </dl> |
| 998 | } |
| 999 | .boxed()) |
| 1000 | } |
| 1001 | |
| 1002 | /// The share of a migration already moved, for the bar's width. An empty |
| 1003 | /// total stands at zero — the row only renders while rows remain, but a |
| 1004 | /// meter that could divide by nothing is a meter that never renders wrong. |
| 1005 | fn migration_percent(moved: u64, total: u64) -> u8 { |
| 1006 | if total == 0 { |
| 1007 | return 0; |
| 1008 | } |
| 1009 | (moved.saturating_mul(100) / total).min(100) as u8 |
| 1010 | } |
| 1011 | |
| 1012 | /// The Storage card's body: where the family says the Files service is, |
| 1013 | /// whether it answered the last beat, how full the account im limits |
| 1014 | /// stands, and — while rows still move — how far the drain has come. |
| 1015 | /// Read from the mirror and the beat's health only; the key |
| 1016 | /// `[storage.in]` holds is not read here, so no render path can carry it. |
| 1017 | async fn storage_card<'a>( |
| 1018 | cx: &'a Cx, |
| 1019 | lang: Lang, |
| 1020 | listed: Option<(String, String)>, |
| 1021 | snap: &crate::storage::StorageSnapshot, |
| 1022 | backend: StorageBackend, |
| 1023 | ) -> Result<impl View + 'a> { |
| 1024 | let service_line = match &listed { |
| 1025 | Some((name, url)) => format!("{name} · {url}"), |
| 1026 | None => t(lang, Key::StorageAbsent).to_string(), |
| 1027 | }; |
| 1028 | let (state_word, state_class) = if snap.connected { |
| 1029 | (t(lang, Key::ConnectionConnected), "connection-on") |
| 1030 | } else { |
| 1031 | (t(lang, Key::StorageUnreachable), "connection-wait") |
| 1032 | }; |
| 1033 | let limit_line = match (snap.used, snap.quota) { |
| 1034 | (Some(used), Some(quota)) => crate::i18n::share_of_total( |
| 1035 | lang, |
| 1036 | &crate::storage::human_bytes(used), |
| 1037 | &crate::storage::human_bytes(quota), |
| 1038 | ), |
| 1039 | _ => "—".to_string(), |
| 1040 | }; |
| 1041 | let problem_word = match snap.problem_word.as_deref() { |
| 1042 | Some("unreachable") => t(lang, Key::ProblemUnreachable).to_string(), |
| 1043 | Some("quota") => t(lang, Key::ProblemQuota).to_string(), |
| 1044 | Some(other) => other.to_string(), |
| 1045 | None => String::new(), |
| 1046 | }; |
| 1047 | let problem_line = if problem_word.is_empty() { |
| 1048 | t(lang, Key::ConnectionNever).to_string() |
| 1049 | } else { |
| 1050 | format!( |
| 1051 | "{} {} · {}", |
| 1052 | crate::directory::age_text(snap.problem_age), |
| 1053 | t(lang, Key::Ago), |
| 1054 | problem_word |
| 1055 | ) |
| 1056 | }; |
| 1057 | let migrating = snap.migrating; |
| 1058 | Ok(view! { |
| 1059 | cx => |
| 1060 | <dl class="connection-facts"> |
| 1061 | <dt>(t(lang, Key::StorageService))</dt> |
| 1062 | <dd class="connection-fact">(service_line)</dd> |
| 1063 | <dt>(t(lang, Key::StorageConnection))</dt> |
| 1064 | <dd class="connection-fact"> |
| 1065 | <span class=(format!("connection-dot {state_class}"))></span> |
| 1066 | (state_word) |
| 1067 | </dd> |
| 1068 | <dt>(t(lang, Key::StorageLimit))</dt> |
| 1069 | <dd class="connection-fact">(limit_line)</dd> |
| 1070 | if backend == StorageBackend::In && let Some((moved, total)) = migrating { |
| 1071 | <dt>(t(lang, Key::StorageMigration))</dt> |
| 1072 | <dd class="connection-fact"> |
| 1073 | (crate::i18n::share_of_total(lang, &moved.to_string(), &total.to_string())) |
| 1074 | <div class="quota-bar" role="progressbar" aria-valuenow=(migration_percent(moved, total).to_string()) aria-valuemin="0" aria-valuemax="100"> |
| 1075 | <div class="quota-fill" style=(format!("width: {}%", migration_percent(moved, total)))></div> |
| 1076 | </div> |
| 1077 | </dd> |
| 1078 | } |
| 1079 | <dt>(t(lang, Key::StorageLastProblem))</dt> |
| 1080 | <dd class="connection-fact">(problem_line)</dd> |
| 1081 | </dl> |
| 1082 | }.boxed()) |
| 1083 | } |
| 1084 | |
| 1085 | /// One row of the member list, as an admin may see it. Identity is im's to |
| 1086 | /// vouch — the row carries no password and no link token, only what the |
| 1087 | /// workspace itself decides: the role and whether the account is disabled. |
| 1088 | struct Member { |
| 1089 | id: String, |
| 1090 | display_name: String, |
| 1091 | email: String, |
| 1092 | role: iz_core::Role, |
| 1093 | disabled: bool, |
| 1094 | /// The face's version, for the row's avatar `?v=` stamp. |
| 1095 | photo_version: u64, |
| 1096 | /// The day they last signed in, as the list writes it, or nothing. |
| 1097 | last_signed_in: Option<String>, |
| 1098 | is_you: bool, |
| 1099 | /// The first account. It administers the workspace and cannot be |
| 1100 | /// removed. |
| 1101 | is_owner: bool, |
| 1102 | } |
| 1103 | |
| 1104 | async fn members_now(cx: &Cx, asking: &User) -> Result<Vec<Member>> { |
| 1105 | let store = store(cx); |
| 1106 | let owner = store.owner().await?.map(|owner| owner.id); |
| 1107 | let users = store.users(&asking.workspace_id).await?; |
| 1108 | let zone = iz_core::detail::parse_zone(&asking.timezone); |
| 1109 | Ok(users |
| 1110 | .into_iter() |
| 1111 | .map(|user| Member { |
| 1112 | disabled: user.disabled, |
| 1113 | photo_version: user.photo_version, |
| 1114 | last_signed_in: user |
| 1115 | .last_signed_in_at |
| 1116 | .map(|at| iz_core::board::day_label(at.to_offset(zone).date())), |
| 1117 | is_you: user.id == asking.id, |
| 1118 | is_owner: owner.as_deref() == Some(user.id.as_str()), |
| 1119 | id: user.id, |
| 1120 | display_name: user.display_name, |
| 1121 | email: user.email, |
| 1122 | role: user.role, |
| 1123 | }) |
| 1124 | .collect()) |
| 1125 | } |
| 1126 | |
| 1127 | async fn limits_now(cx: &Cx, workspace_id: &str) -> Result<(u64, Vec<String>, u32, u32)> { |
| 1128 | let workspace = store(cx) |
| 1129 | .workspace() |
| 1130 | .await? |
| 1131 | .filter(|workspace| workspace.id == workspace_id) |
| 1132 | .ok_or_else(|| topcoat::Error::from(std::io::Error::other("no workspace")))?; |
| 1133 | Ok(( |
| 1134 | workspace.attachment_limit_bytes / MB, |
| 1135 | workspace.allowed_file_types, |
| 1136 | workspace.mail_batch_minutes, |
| 1137 | workspace.reminder_minutes, |
| 1138 | )) |
| 1139 | } |
| 1140 | |
| 1141 | /// What the row under the test button says, once the workspace holds a |
| 1142 | /// `sender_test` to read. |
| 1143 | struct TestResult { |
| 1144 | moment: String, |
| 1145 | took: Option<String>, |
| 1146 | error: Option<String>, |
| 1147 | } |
| 1148 | |
| 1149 | /// The last handshake, rendered. |
| 1150 | struct CheckResult { |
| 1151 | moment: String, |
| 1152 | error: Option<String>, |
| 1153 | } |
| 1154 | |
| 1155 | /// The sender as the admin's screen may see it, plus the last test's |
| 1156 | /// outcome — persisted, so it survives the reload the redirect gives it |
| 1157 | /// rather than living only in this request's memory. |
| 1158 | struct Sender { |
| 1159 | host: String, |
| 1160 | port: u16, |
| 1161 | username: String, |
| 1162 | from_name: String, |
| 1163 | from_address: String, |
| 1164 | password_set: bool, |
| 1165 | test: Option<TestResult>, |
| 1166 | /// How the last handshake went, and when. `None` means nobody has asked |
| 1167 | /// the server yet since these settings were saved. |
| 1168 | check: Option<CheckResult>, |
| 1169 | /// The origin mailed links point at, empty when the configured one is |
| 1170 | /// still in use. |
| 1171 | public_url: String, |
| 1172 | } |
| 1173 | |
| 1174 | /// What the panel says about the mail server, in the order the states happen. |
| 1175 | /// |
| 1176 | /// The point of four states rather than two is that "the fields are filled in" |
| 1177 | /// and "the server let us in" are different facts, and the old chip said |
| 1178 | /// `Connected` for the first. A host typed correctly with the wrong password |
| 1179 | /// showed green while every mail was refused. |
| 1180 | #[derive(PartialEq, Eq)] |
| 1181 | enum Standing { |
| 1182 | /// Something the sender needs is missing. |
| 1183 | NotConfigured, |
| 1184 | /// Complete, but nobody has asked the server yet. |
| 1185 | Unchecked, |
| 1186 | /// The server accepted our credentials, at this moment. |
| 1187 | Connected(String), |
| 1188 | /// The server would not have us, in its own words. |
| 1189 | Refused(String), |
| 1190 | } |
| 1191 | |
| 1192 | impl Sender { |
| 1193 | /// Whether the form holds everything a sender needs. Says nothing about |
| 1194 | /// whether any of it is right — see [`Standing`]. |
| 1195 | fn is_complete(&self) -> bool { |
| 1196 | !self.host.trim().is_empty() |
| 1197 | && !self.username.trim().is_empty() |
| 1198 | && !self.from_address.trim().is_empty() |
| 1199 | && self.password_set |
| 1200 | } |
| 1201 | |
| 1202 | fn standing(&self) -> Standing { |
| 1203 | if !self.is_complete() { |
| 1204 | return Standing::NotConfigured; |
| 1205 | } |
| 1206 | match &self.check { |
| 1207 | None => Standing::Unchecked, |
| 1208 | Some(check) => match &check.error { |
| 1209 | Some(said) => Standing::Refused(said.clone()), |
| 1210 | None => Standing::Connected(check.moment.clone()), |
| 1211 | }, |
| 1212 | } |
| 1213 | } |
| 1214 | } |
| 1215 | |
| 1216 | async fn sender_now(cx: &Cx, zone: time::UtcOffset) -> Result<Sender> { |
| 1217 | let workspace = store(cx).workspace().await?; |
| 1218 | Ok(match workspace { |
| 1219 | None => Sender { |
| 1220 | host: String::new(), |
| 1221 | port: 587, |
| 1222 | username: String::new(), |
| 1223 | from_name: String::new(), |
| 1224 | from_address: String::new(), |
| 1225 | password_set: false, |
| 1226 | test: None, |
| 1227 | check: None, |
| 1228 | public_url: String::new(), |
| 1229 | }, |
| 1230 | Some(workspace) => Sender { |
| 1231 | host: workspace.smtp_host.unwrap_or_default(), |
| 1232 | port: workspace |
| 1233 | .smtp_port |
| 1234 | .and_then(|p| u16::try_from(p).ok()) |
| 1235 | .unwrap_or(587), |
| 1236 | username: workspace.smtp_username.unwrap_or_default(), |
| 1237 | from_name: workspace.smtp_from_name.unwrap_or_default(), |
| 1238 | from_address: workspace.smtp_from_address.unwrap_or_default(), |
| 1239 | password_set: workspace.smtp_password_set, |
| 1240 | test: workspace.sender_test.map(|test| TestResult { |
| 1241 | moment: iz_core::detail::moment_label_in(test.at, zone), |
| 1242 | took: test.error.is_none().then(|| took_label(test.took_ms)), |
| 1243 | error: test.error, |
| 1244 | }), |
| 1245 | check: workspace.sender_check.map(|check| CheckResult { |
| 1246 | moment: iz_core::detail::moment_label_in(check.at, zone), |
| 1247 | error: check.error, |
| 1248 | }), |
| 1249 | public_url: workspace.public_url.unwrap_or_default(), |
| 1250 | }, |
| 1251 | }) |
| 1252 | } |
| 1253 | |
| 1254 | /// Whether a typed address is an origin a link can be built on: an http or |
| 1255 | /// https scheme, a host after it, and no whitespace anywhere. A path is |
| 1256 | /// allowed — iz behind `example.com/iz` is a real deployment — but a |
| 1257 | /// bare host or a mail address is not. |
| 1258 | fn is_origin(value: &str) -> bool { |
| 1259 | let Some(rest) = value |
| 1260 | .strip_prefix("https: |
| 1261 | .or_else(|| value.strip_prefix("http://")) |
| 1262 | else { |
| 1263 | return false; |
| 1264 | }; |
| 1265 | let host = rest.split('/').next().unwrap_or_default(); |
| 1266 | !host.is_empty() && !host.contains('@') && !value.chars().any(char::is_whitespace) |
| 1267 | } |
| 1268 | |
| 1269 | |
| 1270 | |
| 1271 | fn call_state(query: &str, call: &str) -> (Option<Refusal>, bool) { |
| 1272 | let mut refusal_code = None; |
| 1273 | let mut on = None; |
| 1274 | let mut saved = false; |
| 1275 | let mut why = None; |
| 1276 | for pair in query.split('&') { |
| 1277 | let Some((key, value)) = pair.split_once('=') else { |
| 1278 | continue; |
| 1279 | }; |
| 1280 | match key { |
| 1281 | "refusal" => refusal_code = Some(value), |
| 1282 | "on" => on = Some(value), |
| 1283 | "why" => why = Some(value), |
| 1284 | "saved" if value == call => saved = true, |
| 1285 | _ => {} |
| 1286 | } |
| 1287 | } |
| 1288 | let refusal = if on == Some(call) { |
| 1289 | refusal_code |
| 1290 | .and_then(Refusal::from_code) |
| 1291 | .map(|refusal| match (refusal, why) { |
| 1292 | |
| 1293 | |
| 1294 | (Refusal::BadSender(_), Some(why)) => Refusal::BadSender(qsdecode(why)), |
| 1295 | (refusal, _) => refusal, |
| 1296 | }) |
| 1297 | } else { |
| 1298 | None |
| 1299 | }; |
| 1300 | (refusal, saved) |
| 1301 | } |
| 1302 | |
| 1303 | fn query_value<'q>(query: &'q str, key: &str) -> Option<&'q str> { |
| 1304 | query.split('&').find_map(|pair| { |
| 1305 | pair.split_once('=') |
| 1306 | .filter(|(k, _)| *k == key) |
| 1307 | .map(|(_, v)| v) |
| 1308 | }) |
| 1309 | } |
| 1310 | |
| 1311 | |
| 1312 | |
| 1313 | |
| 1314 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 1315 | enum Section { |
| 1316 | Profile, |
| 1317 | Outgoing, |
| 1318 | Limits, |
| 1319 | Server, |
| 1320 | Members, |
| 1321 | Message, |
| 1322 | } |
| 1323 | |
| 1324 | |
| 1325 | |
| 1326 | fn rail_class(current: Section, target: Section) -> &'static str { |
| 1327 | if current == target { |
| 1328 | "settings-section-link active" |
| 1329 | } else { |
| 1330 | "settings-section-link" |
| 1331 | } |
| 1332 | } |
| 1333 | |
| 1334 | |
| 1335 | #[page("/settings")] |
| 1336 | #[allow(unused_variables)] |
| 1337 | async fn settings_page(cx: &Cx) -> Result<impl View> { |
| 1338 | let user = match require_user(cx).await { |
| 1339 | Ok(user) => user, |
| 1340 | Err(refusal) => { |
| 1341 | return Ok(view! { |
| 1342 | cx => |
| 1343 | <main class="scaffold-note"> |
| 1344 | <p>(refusal.message())</p> |
| 1345 | <p><a href="/">(t(Lang::En, Key::BackToBoard))</a></p> |
| 1346 | </main> |
| 1347 | } |
| 1348 | .boxed()); |
| 1349 | } |
| 1350 | }; |
| 1351 | let lang = Lang::from_code(&user.language); |
| 1352 | let administers = user.role.can_administer(); |
| 1353 | let query = topcoat::router::request::uri(cx).query().unwrap_or(""); |
| 1354 | let section = match query_value(query, "section") { |
| 1355 | Some("outgoing") if administers => Section::Outgoing, |
| 1356 | Some("limits") if administers => Section::Limits, |
| 1357 | Some("server") if administers => Section::Server, |
| 1358 | Some("members") if administers => Section::Members, |
| 1359 | Some("message") if administers => Section::Message, |
| 1360 | _ => Section::Profile, |
| 1361 | }; |
| 1362 | |
| 1363 | let zone = iz_core::detail::parse_zone(&user.timezone); |
| 1364 | // What a link falls back to when the field is empty — the placeholder |
| 1365 | // shows it rather than an invented example. |
| 1366 | let configured_url = config(cx).listen_url(); |
| 1367 | let sender = if administers { |
| 1368 | Some(sender_now(cx, zone).await?) |
| 1369 | } else { |
| 1370 | None |
| 1371 | }; |
| 1372 | let (limits, allowed_types) = if administers { |
| 1373 | let (attachment, types, batch, reminder) = limits_now(cx, &user.workspace_id).await?; |
| 1374 | (Some((attachment, batch, reminder)), types) |
| 1375 | } else { |
| 1376 | (None, Vec::new()) |
| 1377 | }; |
| 1378 | let members = if administers { |
| 1379 | Some(members_now(cx, &user).await?) |
| 1380 | } else { |
| 1381 | None |
| 1382 | }; |
| 1383 | // What the public-address field shows: the stored app-level address, |
| 1384 | // or nothing — the placeholder then shows what the fallback chain |
| 1385 | // would use, the same way the sender panel's does. |
| 1386 | let stored_public_url = if administers { |
| 1387 | store(cx).get_setting(PUBLIC_URL_KEY).await.ok().flatten() |
| 1388 | } else { |
| 1389 | None |
| 1390 | }; |
| 1391 | |
| 1392 | let (profile_refusal, profile_saved) = call_state(query, "save_profile"); |
| 1393 | let (sender_refusal, sender_saved) = call_state(query, "save_sender"); |
| 1394 | let (test_refusal, _) = call_state(query, "send_test_mail"); |
| 1395 | let (limits_refusal, limits_saved) = call_state(query, "save_limits"); |
| 1396 | let (storage_refusal, storage_saved) = call_state(query, "save_storage"); |
| 1397 | let (server_refusal, server_saved) = call_state(query, "save_public_url"); |
| 1398 | let (role_refusal, _) = call_state(query, "set_role"); |
| 1399 | let (disabled_refusal, _) = call_state(query, "set_disabled"); |
| 1400 | let (add_refusal, add_saved) = call_state(query, "add_member"); |
| 1401 | let member_refusal = role_refusal.or(disabled_refusal).or(add_refusal); |
| 1402 | let (message_refusal, message_saved) = call_state(query, "send_message"); |
| 1403 | |
| 1404 | // What the Storage panel renders: the surface the workspace currently |
| 1405 | // keeps its bytes on, where the family mirror says the Files service |
| 1406 | // is, and the storage beat's latest stand. The key `[storage.in]` |
| 1407 | // holds is only asked about (`is_some`), never read into the page. |
| 1408 | let storage = if administers && section == Section::Limits { |
| 1409 | let backend = store(cx) |
| 1410 | .storage_backend() |
| 1411 | .await |
| 1412 | .unwrap_or(StorageBackend::Local); |
| 1413 | let listed = crate::layout::family_of(cx) |
| 1414 | .await |
| 1415 | .into_iter() |
| 1416 | .find(|service| service.key == "in") |
| 1417 | .map(|service| (service.name, service.url)); |
| 1418 | Some((backend, listed, crate::storage::health(cx).snapshot())) |
| 1419 | } else { |
| 1420 | None |
| 1421 | }; |
| 1422 | |
| 1423 | let me = crate::detail::Me::from(&user); |
| 1424 | Ok(view! { |
| 1425 | cx => |
| 1426 | <header class="topbar"> |
| 1427 | (topcoat::view::Child::new(crate::layout::family_mark(cx).await?)) |
| 1428 | (topcoat::view::Child::new(crate::layout::topbar_nav(cx, crate::layout::NavPage::Settings, user.role, lang).await?)) |
| 1429 | <div class="spacer"></div> |
| 1430 | (topcoat::view::Child::new(crate::layout::user_menu(cx, &me, lang).await?)) |
| 1431 | </header> |
| 1432 | |
| 1433 | <div class="settings-shell"> |
| 1434 | <nav class="settings-sections"> |
| 1435 | <a class=(rail_class(section, Section::Profile)) href="/settings?section=profile">(t(lang, Key::YourProfile))</a> |
| 1436 | if administers { |
| 1437 | <a class=(rail_class(section, Section::Outgoing)) href="/settings?section=outgoing">(t(lang, Key::OutgoingMail))</a> |
| 1438 | <a class=(rail_class(section, Section::Limits)) href="/settings?section=limits">(t(lang, Key::WorkspaceLimits))</a> |
| 1439 | <a class=(rail_class(section, Section::Server)) href="/settings?section=server">(t(lang, Key::Server))</a> |
| 1440 | <a class=(rail_class(section, Section::Message)) href="/settings?section=message">(t(lang, Key::Message))</a> |
| 1441 | } |
| 1442 | </nav> |
| 1443 | <main class="settings-stage"> |
| 1444 | if section == Section::Profile { |
| 1445 | <section class="panel" id="profile"> |
| 1446 | <div class="panel-head"> |
| 1447 | <h2 class="panel-title">(t(lang, Key::YourProfile))</h2> |
| 1448 | </div> |
| 1449 | <div class="panel-body"> |
| 1450 | <div class="identity-row"> |
| 1451 | (topcoat::view::Child::new(crate::layout::avatar(cx, &user.id, &user.display_name, user.photo_version, "avatar-lg").await?)) |
| 1452 | <div class="identity-who"> |
| 1453 | <div class="identity-name">(user.display_name.clone())</div> |
| 1454 | <div class="identity-address">(user.email.clone())</div> |
| 1455 | </div> |
| 1456 | </div> |
| 1457 | <p class="panel-lede">(t(lang, Key::IdentityFromIm))</p> |
| 1458 | </div> |
| 1459 | <form method="post" action="/api/save_profile" class="panel-body"> |
| 1460 | <label class="field"> |
| 1461 | <span class="field-label">(t(lang, Key::ThemeLabel))</span> |
| 1462 | <select class="field-input" name="theme"> |
| 1463 | <option value="light" selected=(user.theme == "light")>(t(lang, Key::LightOption))</option> |
| 1464 | <option value="dark" selected=(user.theme == "dark")>(t(lang, Key::DarkOption))</option> |
| 1465 | </select> |
| 1466 | </label> |
| 1467 | <label class="field"> |
| 1468 | <span class="field-label">(t(lang, Key::UiLabel))</span> |
| 1469 | <select class="field-input" name="ui"> |
| 1470 | <option value="instrument" selected=(user.ui == "instrument")>"Instrument"</option> |
| 1471 | <option value="ledger" selected=(user.ui == "ledger")>"Ledger"</option> |
| 1472 | </select> |
| 1473 | </label> |
| 1474 | <label class="field"> |
| 1475 | <span class="field-label">(t(lang, Key::LanguageLabel))</span> |
| 1476 | <select class="field-input" name="language"> |
| 1477 | <option value="en" selected=(user.language == "en")>"English"</option> |
| 1478 | <option value="tr" selected=(user.language == "tr")>"Türkçe"</option> |
| 1479 | </select> |
| 1480 | </label> |
| 1481 | <div class="panel-foot"> |
| 1482 | if let Some(refusal) = &profile_refusal { |
| 1483 | <span class="field-error">(refusal.message_in(lang))</span> |
| 1484 | } |
| 1485 | if profile_saved { |
| 1486 | <span class="field-note">(t(lang, Key::Saved))</span> |
| 1487 | } |
| 1488 | <button class="primary" type="submit">(t(lang, Key::Save))</button> |
| 1489 | </div> |
| 1490 | </form> |
| 1491 | </section> |
| 1492 | |
| 1493 | // The identity mirror's stand, for every signed-in member: |
| 1494 | // who iz trusts and as whom, and whether the live feed from |
| 1495 | // im is open right now. Read-only — the issuer and client id |
| 1496 | // are public by OIDC design, and the client secret is never |
| 1497 | // read into this page at all. |
| 1498 | <section class="panel" id="connection"> |
| 1499 | <div class="panel-head"> |
| 1500 | <h2 class="panel-title">(t(lang, Key::Connection))</h2> |
| 1501 | </div> |
| 1502 | <div class="panel-body"> |
| 1503 | (topcoat::view::Child::new(connection_card(cx, lang).await?)) |
| 1504 | </div> |
| 1505 | </section> |
| 1506 | } |
| 1507 | |
| 1508 | if section == Section::Outgoing && let Some(sender) = &sender { |
| 1509 | let standing = sender.standing(); |
| 1510 | let complete = sender.is_complete(); |
| 1511 | let password_set = sender.password_set; |
| 1512 | // One chip, four states. `Connected` carries the moment the |
| 1513 | // server said so, because the claim is about a moment: a |
| 1514 | // password rotated since is not something this can know. |
| 1515 | let (chip_class, chip_text) = match &standing { |
| 1516 | Standing::NotConfigured => { |
| 1517 | ("chip chip-off", t(lang, Key::NotConfiguredChip).to_string()) |
| 1518 | } |
| 1519 | Standing::Unchecked => { |
| 1520 | ("chip chip-off", t(lang, Key::Unchecked).to_string()) |
| 1521 | } |
| 1522 | Standing::Connected(at) => ( |
| 1523 | "chip chip-connected", |
| 1524 | format!("{} {}", t(lang, Key::Connected), at), |
| 1525 | ), |
| 1526 | Standing::Refused(_) => { |
| 1527 | ("chip chip-off", t(lang, Key::Refused).to_string()) |
| 1528 | } |
| 1529 | }; |
| 1530 | <section class="panel" id="outgoing"> |
| 1531 | <div class="panel-head"> |
| 1532 | <h2 class="panel-title">(t(lang, Key::OutgoingMail))</h2> |
| 1533 | <span class="chip chip-admin">(t(lang, Key::AdminOnly))</span> |
| 1534 | <span class=(chip_class)>(chip_text)</span> |
| 1535 | </div> |
| 1536 | <div class="panel-body"> |
| 1537 | // The server's own words when it refused us: |
| 1538 | // "535 authentication failed" is something an admin |
| 1539 | // can act on, and a bare chip is not. |
| 1540 | if let Standing::Refused(said) = &standing { |
| 1541 | <p class="panel-lede">(said)</p> |
| 1542 | } |
| 1543 | if !complete { |
| 1544 | <p class="panel-lede">(t(lang, Key::NotConnectedNote))</p> |
| 1545 | } |
| 1546 | <form method="post" action="/api/save_sender" id="sender-settings"> |
| 1547 | <div class="field-row"> |
| 1548 | <label class="field"> |
| 1549 | <span class="field-label">(t(lang, Key::SmtpHostLabel))</span> |
| 1550 | <input |
| 1551 | class="field-input" |
| 1552 | type="text" |
| 1553 | name="host" |
| 1554 | value=(sender.host.clone()) |
| 1555 | placeholder="smtp.fastmail.com" |
| 1556 | > |
| 1557 | </label> |
| 1558 | <label class="field field-narrow"> |
| 1559 | <span class="field-label">(t(lang, Key::PortLabel))</span> |
| 1560 | <input |
| 1561 | class="field-input" |
| 1562 | type="number" |
| 1563 | name="port" |
| 1564 | min="1" |
| 1565 | max="65535" |
| 1566 | value=(sender.port.to_string()) |
| 1567 | > |
| 1568 | </label> |
| 1569 | </div> |
| 1570 | <label class="field"> |
| 1571 | <span class="field-label">(t(lang, Key::UsernameLabel))</span> |
| 1572 | <input class="field-input" type="text" name="username" value=(sender.username.clone())> |
| 1573 | </label> |
| 1574 | <label class="field"> |
| 1575 | <span class="field-label">(t(lang, Key::PasswordLabel))</span> |
| 1576 | <input |
| 1577 | class="field-input" |
| 1578 | type="password" |
| 1579 | name="password" |
| 1580 | autocomplete="off" |
| 1581 | placeholder=(if password_set { t(lang, Key::PasswordSetPlaceholder) } else { t(lang, Key::PasswordNeededPlaceholder) }) |
| 1582 | > |
| 1583 | </label> |
| 1584 | <div class="field-row"> |
| 1585 | <label class="field"> |
| 1586 | <span class="field-label">(t(lang, Key::FromNameLabel))</span> |
| 1587 | <input |
| 1588 | class="field-input" |
| 1589 | type="text" |
| 1590 | name="from_name" |
| 1591 | value=(sender.from_name.clone()) |
| 1592 | placeholder="iz" |
| 1593 | > |
| 1594 | </label> |
| 1595 | <label class="field"> |
| 1596 | <span class="field-label">(t(lang, Key::FromAddressLabel))</span> |
| 1597 | <input |
| 1598 | class="field-input" |
| 1599 | type="text" |
| 1600 | name="from_address" |
| 1601 | value=(sender.from_address.clone()) |
| 1602 | placeholder="board@iz.sh" |
| 1603 | > |
| 1604 | </label> |
| 1605 | </div> |
| 1606 | <label class="field"> |
| 1607 | <span class="field-label">(t(lang, Key::LinkAddressLabel))</span> |
| 1608 | <input |
| 1609 | class="field-input" |
| 1610 | type="text" |
| 1611 | name="public_url" |
| 1612 | value=(sender.public_url.clone()) |
| 1613 | placeholder=(configured_url.clone()) |
| 1614 | > |
| 1615 | </label> |
| 1616 | </form> |
| 1617 | <div class="panel-foot panel-foot-split"> |
| 1618 | <div class="foot-side"> |
| 1619 | <form method="post" action="/api/check_sender"> |
| 1620 | <button class="quiet" type="submit" disabled=(!complete)> |
| 1621 | (t(lang, Key::CheckConnection)) |
| 1622 | </button> |
| 1623 | </form> |
| 1624 | <form method="post" action="/api/send_test_mail"> |
| 1625 | <button class="quiet" type="submit">(t(lang, Key::SendTestMail))</button> |
| 1626 | </form> |
| 1627 | match (&test_refusal, &sender.test) { |
| 1628 | (Some(refusal), _) => <span class="field-error">(refusal.message_in(lang))</span>, |
| 1629 | (None, Some(result)) => match (&result.error, &result.took) { |
| 1630 | (Some(problem), _) => <span class="field-error">(crate::i18n::not_delivered_label(lang, &result.moment, problem))</span>, |
| 1631 | (None, Some(took)) => <span class="field-note">(crate::i18n::delivered_in_label(lang, took, &result.moment))</span>, |
| 1632 | (None, None) => "", |
| 1633 | }, |
| 1634 | (None, None) => "", |
| 1635 | } |
| 1636 | </div> |
| 1637 | <div class="foot-side"> |
| 1638 | if let Some(refusal) = &sender_refusal { |
| 1639 | <span class="field-error">(refusal.message_in(lang))</span> |
| 1640 | } |
| 1641 | if sender_saved { |
| 1642 | <span class="field-note">(t(lang, Key::Saved))</span> |
| 1643 | } |
| 1644 | <button class="primary" type="submit" form="sender-settings">(t(lang, Key::Save))</button> |
| 1645 | </div> |
| 1646 | </div> |
| 1647 | </div> |
| 1648 | </section> |
| 1649 | } |
| 1650 | |
| 1651 | if section == Section::Limits |
| 1652 | && let Some((attachment_limit_mb, mail_batch_minutes, reminder_minutes)) = limits |
| 1653 | { |
| 1654 | <section class="panel" id="limits"> |
| 1655 | <div class="panel-head"> |
| 1656 | <h2 class="panel-title">(t(lang, Key::WorkspaceLimits))</h2> |
| 1657 | <span class="chip chip-admin">(t(lang, Key::AdminOnly))</span> |
| 1658 | </div> |
| 1659 | <form method="post" action="/api/save_limits" class="panel-body"> |
| 1660 | <div class="field-row"> |
| 1661 | <label class="field"> |
| 1662 | <span class="field-label">(t(lang, Key::AttachmentLimitLabel))</span> |
| 1663 | <input |
| 1664 | class="field-input" |
| 1665 | type="number" |
| 1666 | name="attachment_limit_mb" |
| 1667 | min="1" |
| 1668 | max=(WIDEST_ATTACHMENT_MB.to_string()) |
| 1669 | value=(attachment_limit_mb.to_string()) |
| 1670 | required="" |
| 1671 | > |
| 1672 | </label> |
| 1673 | </div> |
| 1674 | <div class="field-row"> |
| 1675 | <label class="field"> |
| 1676 | <span class="field-label">(t(lang, Key::MailBatchLabel))</span> |
| 1677 | <input |
| 1678 | class="field-input" |
| 1679 | type="number" |
| 1680 | name="mail_batch_minutes" |
| 1681 | min="0" |
| 1682 | max=(LONGEST_BATCH_MINUTES.to_string()) |
| 1683 | value=(mail_batch_minutes.to_string()) |
| 1684 | required="" |
| 1685 | > |
| 1686 | </label> |
| 1687 | <label class="field"> |
| 1688 | <span class="field-label">(t(lang, Key::ReminderMinutesLabel))</span> |
| 1689 | <input |
| 1690 | class="field-input" |
| 1691 | type="number" |
| 1692 | name="reminder_minutes" |
| 1693 | min="0" |
| 1694 | value=(reminder_minutes.to_string()) |
| 1695 | > |
| 1696 | </label> |
| 1697 | </div> |
| 1698 | <label class="field"> |
| 1699 | <span class="field-label">(t(lang, Key::AllowedFileTypesLabel))</span> |
| 1700 | <input |
| 1701 | class="field-input" |
| 1702 | type="text" |
| 1703 | name="allowed_file_types" |
| 1704 | value=(allowed_types.join(", ")) |
| 1705 | placeholder="png, jpg, pdf, zip" |
| 1706 | > |
| 1707 | </label> |
| 1708 | <div class="panel-foot"> |
| 1709 | if let Some(refusal) = &limits_refusal { |
| 1710 | <span class="field-error">(refusal.message_in(lang))</span> |
| 1711 | } |
| 1712 | if limits_saved { |
| 1713 | <span class="field-note">(t(lang, Key::Saved))</span> |
| 1714 | } |
| 1715 | <button class="primary" type="submit">(t(lang, Key::Save))</button> |
| 1716 | </div> |
| 1717 | </form> |
| 1718 | </section> |
| 1719 | } |
| 1720 | |
| 1721 | // The storage toggle and its card, beside the limits it |
| 1722 | // answers to: which surface keeps the bytes, and how the |
| 1723 | // Files service stands this beat. The `in` option only |
| 1724 | // exists when `[storage.in]` named a key — a deployment |
| 1725 | // without one is offered no road it cannot take. |
| 1726 | if section == Section::Limits && let Some((backend, listed, snap)) = storage { |
| 1727 | let local = backend == StorageBackend::Local; |
| 1728 | <section class="panel" id="storage" data-tick=""> |
| 1729 | <div class="panel-head"> |
| 1730 | <h2 class="panel-title">(t(lang, Key::StorageTitle))</h2> |
| 1731 | <span class="chip chip-admin">(t(lang, Key::AdminOnly))</span> |
| 1732 | </div> |
| 1733 | <form method="post" action="/api/save_storage" class="panel-body"> |
| 1734 | <label class="field"> |
| 1735 | <span class="field-label">(t(lang, Key::StorageMode))</span> |
| 1736 | <select class="field-input" name="backend"> |
| 1737 | <option value="local" selected=(local)>(t(lang, Key::StorageModeLocal))</option> |
| 1738 | if config(cx).storage_in.is_some() { |
| 1739 | <option value="in" selected=(!local)>(t(lang, Key::StorageModeIn))</option> |
| 1740 | } |
| 1741 | </select> |
| 1742 | </label> |
| 1743 | <div class="panel-foot"> |
| 1744 | if let Some(refusal) = &storage_refusal { |
| 1745 | <span class="field-error">(refusal.message_in(lang))</span> |
| 1746 | } |
| 1747 | if storage_saved { |
| 1748 | <span class="field-note">(t(lang, Key::Saved))</span> |
| 1749 | } |
| 1750 | <button class="primary" type="submit">(t(lang, Key::Save))</button> |
| 1751 | </div> |
| 1752 | </form> |
| 1753 | <div class="panel-body"> |
| 1754 | (topcoat::view::Child::new(storage_card(cx, lang, listed, &snap, backend).await?)) |
| 1755 | </div> |
| 1756 | </section> |
| 1757 | } |
| 1758 | |
| 1759 | if section == Section::Server { |
| 1760 | <section class="panel" id="server"> |
| 1761 | <div class="panel-head"> |
| 1762 | <h2 class="panel-title">(t(lang, Key::Server))</h2> |
| 1763 | <span class="chip chip-admin">(t(lang, Key::AdminOnly))</span> |
| 1764 | </div> |
| 1765 | <form method="post" action="/api/save_public_url" class="panel-body" id="public-address"> |
| 1766 | <label class="field"> |
| 1767 | <span class="field-label">(t(lang, Key::PublicAddressLabel))</span> |
| 1768 | <input |
| 1769 | class="field-input" |
| 1770 | type="text" |
| 1771 | name="public_url" |
| 1772 | value=(stored_public_url.clone().unwrap_or_default()) |
| 1773 | placeholder=(configured_url.clone()) |
| 1774 | > |
| 1775 | </label> |
| 1776 | <p class="panel-lede">(t(lang, Key::PublicAddressNote))</p> |
| 1777 | </form> |
| 1778 | <div class="panel-foot panel-foot-split"> |
| 1779 | <div class="foot-side"></div> |
| 1780 | <div class="foot-side"> |
| 1781 | if let Some(refusal) = &server_refusal { |
| 1782 | <span class="field-error">(refusal.message_in(lang))</span> |
| 1783 | } |
| 1784 | if server_saved { |
| 1785 | <span class="field-note">(t(lang, Key::Saved))</span> |
| 1786 | } |
| 1787 | <button class="primary" type="submit" form="public-address">(t(lang, Key::Save))</button> |
| 1788 | </div> |
| 1789 | </div> |
| 1790 | </section> |
| 1791 | } |
| 1792 | |
| 1793 | if section == Section::Members && let Some(members) = &members { |
| 1794 | <section class="panel" id="members"> |
| 1795 | <div class="panel-head"> |
| 1796 | <h2 class="panel-title">(t(lang, Key::Members))</h2> |
| 1797 | <span class="chip chip-admin">(t(lang, Key::AdminOnly))</span> |
| 1798 | </div> |
| 1799 | <div class="panel-body"> |
| 1800 | // The register keeps its named columns at every width; on a |
| 1801 | // narrow screen this box pans it sideways, silently, instead |
| 1802 | // of letting the columns crush the role select out. |
| 1803 | <div class="table-pan"> |
| 1804 | <table class="member-table"> |
| 1805 | <thead> |
| 1806 | <tr> |
| 1807 | <th class="member-col-name">(t(lang, Key::NameCol))</th> |
| 1808 | <th class="member-col-address">(t(lang, Key::AddressCol))</th> |
| 1809 | <th class="member-col-role">(t(lang, Key::RoleCol))</th> |
| 1810 | <th class="member-col-account">(t(lang, Key::AccountCol))</th> |
| 1811 | </tr> |
| 1812 | </thead> |
| 1813 | <tbody> |
| 1814 | for member in members { |
| 1815 | let account = if member.is_owner { |
| 1816 | t(lang, Key::OwnerStatus).to_string() |
| 1817 | } else if member.disabled { |
| 1818 | t(lang, Key::DisabledBadge).to_string() |
| 1819 | } else if let Some(day) = member.last_signed_in.clone() { |
| 1820 | crate::i18n::last_seen_label(lang, &day) |
| 1821 | } else { |
| 1822 | t(lang, Key::ActiveStatus).to_string() |
| 1823 | }; |
| 1824 | <tr class="member-row"> |
| 1825 | <td class="member-col-name member-name"> |
| 1826 | <span class="member-name-row"> |
| 1827 | (topcoat::view::Child::new(crate::layout::avatar(cx, &member.id, &member.display_name, member.photo_version, "avatar-sm").await?)) |
| 1828 | <a href=(format!("/people/{}", member.id))>(member.display_name.clone())</a> |
| 1829 | if member.is_you { |
| 1830 | <span class="member-you">(t(lang, Key::You))</span> |
| 1831 | } |
| 1832 | if member.disabled { |
| 1833 | <span class="chip chip-off">(t(lang, Key::DisabledBadge))</span> |
| 1834 | } |
| 1835 | </span> |
| 1836 | </td> |
| 1837 | <td class="member-col-address member-address">(member.email.clone())</td> |
| 1838 | <td class="member-col-role"> |
| 1839 | // The admin role is im's to grant: an im-admin row |
| 1840 | // wears its badge and is never written here. The |
| 1841 | // owner's row and one's own stay read-only the way |
| 1842 | // they always were. |
| 1843 | if member.is_owner || member.is_you || member.role == iz_core::Role::Admin { |
| 1844 | <span class="chip chip-role">(t(lang, match member.role { |
| 1845 | iz_core::Role::Admin => Key::RoleAdminOption, |
| 1846 | iz_core::Role::Member => Key::RoleMemberOption, |
| 1847 | iz_core::Role::Viewer => Key::RoleViewerOption, |
| 1848 | }))</span> |
| 1849 | } else { |
| 1850 | <form method="post" action="/api/set_role" class="member-role status-form"> |
| 1851 | <input type="hidden" name="user_id" value=(member.id.clone())> |
| 1852 | <select class="status-select" name="role" data-autosubmit=""> |
| 1853 | <option value="member" selected=(member.role == iz_core::Role::Member)>(t(lang, Key::RoleMemberOption))</option> |
| 1854 | <option value="viewer" selected=(member.role == iz_core::Role::Viewer)>(t(lang, Key::RoleViewerOption))</option> |
| 1855 | </select> |
| 1856 | (topcoat::view::Child::new(crate::detail::glyph::chevron(cx).await?)) |
| 1857 | </form> |
| 1858 | } |
| 1859 | </td> |
| 1860 | <td class="member-col-account member-account"> |
| 1861 | <span class="member-account-row"> |
| 1862 | <span class="member-status">(account)</span> |
| 1863 | // Not for yourself: your own sign-in is yours to |
| 1864 | // keep, and the guards would read you as signed |
| 1865 | // out from under your own hand. |
| 1866 | if !member.is_you { |
| 1867 | <form method="post" action="/api/set_disabled" class="member-resend"> |
| 1868 | <input type="hidden" name="user_id" value=(member.id.clone())> |
| 1869 | if member.disabled { |
| 1870 | <input type="hidden" name="disabled" value="0"> |
| 1871 | <button class="quiet" type="submit">(t(lang, Key::EnableUser))</button> |
| 1872 | } else { |
| 1873 | <input type="hidden" name="disabled" value="1"> |
| 1874 | <button class="quiet quiet-danger" type="submit">(t(lang, Key::DisableUser))</button> |
| 1875 | } |
| 1876 | </form> |
| 1877 | } |
| 1878 | </span> |
| 1879 | </td> |
| 1880 | </tr> |
| 1881 | } |
| 1882 | </tbody> |
| 1883 | </table> |
| 1884 | </div> |
| 1885 | |
| 1886 | if let Some(refusal) = &member_refusal { |
| 1887 | <p class="field-error">(refusal.message_in(lang))</p> |
| 1888 | } |
| 1889 | <form method="post" action="/api/add_member"> |
| 1890 | <div class="field-row"> |
| 1891 | <label class="field"> |
| 1892 | <span class="field-label">(t(lang, Key::NameLabel))</span> |
| 1893 | <input class="field-input" type="text" name="display_name" maxlength="200"> |
| 1894 | </label> |
| 1895 | <label class="field"> |
| 1896 | <span class="field-label">(t(lang, Key::EmailLabel))</span> |
| 1897 | <input class="field-input" type="text" name="email" maxlength="320"> |
| 1898 | </label> |
| 1899 | <label class="field field-narrow"> |
| 1900 | <span class="field-label">(t(lang, Key::RoleCol))</span> |
| 1901 | <select class="field-input" name="role"> |
| 1902 | <option value="member">(t(lang, Key::RoleMemberOption))</option> |
| 1903 | <option value="viewer">(t(lang, Key::RoleViewerOption))</option> |
| 1904 | </select> |
| 1905 | </label> |
| 1906 | </div> |
| 1907 | <div class="panel-foot"> |
| 1908 | if add_saved { |
| 1909 | <span class="field-note">(t(lang, Key::Saved))</span> |
| 1910 | } |
| 1911 | <button class="primary" type="submit">(t(lang, Key::AddMember))</button> |
| 1912 | </div> |
| 1913 | </form> |
| 1914 | </div> |
| 1915 | </section> |
| 1916 | } |
| 1917 | |
| 1918 | if section == Section::Message && let Some(members) = &members { |
| 1919 | <section class="panel" id="message"> |
| 1920 | <div class="panel-head"> |
| 1921 | <h2 class="panel-title">(t(lang, Key::Message))</h2> |
| 1922 | <span class="chip chip-admin">(t(lang, Key::AdminOnly))</span> |
| 1923 | </div> |
| 1924 | <form method="post" action="/api/send_message" class="panel-body"> |
| 1925 | <label class="field"> |
| 1926 | <span class="field-label">(t(lang, Key::Recipient))</span> |
| 1927 | <select class="field-input" name="to" data-search=""> |
| 1928 | <option value="everyone">(t(lang, Key::Everyone))</option> |
| 1929 | for member in members { |
| 1930 | <option value=(member.id.clone())>(member.display_name.clone())</option> |
| 1931 | } |
| 1932 | </select> |
| 1933 | </label> |
| 1934 | <label class="field"> |
| 1935 | <span class="field-label">(t(lang, Key::Subject))</span> |
| 1936 | <input class="field-input" type="text" name="subject" maxlength="200"> |
| 1937 | </label> |
| 1938 | <label class="field"> |
| 1939 | <span class="field-label">(t(lang, Key::Body))</span> |
| 1940 | <textarea class="detail-textarea" name="body" rows="6"></textarea> |
| 1941 | </label> |
| 1942 | <div class="panel-foot"> |
| 1943 | if let Some(refusal) = &message_refusal { |
| 1944 | <span class="field-error">(refusal.message_in(lang))</span> |
| 1945 | } |
| 1946 | if message_saved { |
| 1947 | <span class="field-note">(t(lang, Key::Saved))</span> |
| 1948 | } |
| 1949 | <button class="primary" type="submit">(t(lang, Key::Send))</button> |
| 1950 | </div> |
| 1951 | </form> |
| 1952 | </section> |
| 1953 | } |
| 1954 | </main> |
| 1955 | </div> |
| 1956 | (topcoat::view::Child::new(crate::dropdown::dropdown_script(cx).await?)) |
| 1957 | (topcoat::view::Child::new(crate::layout::escape_script(cx).await?)) |
| 1958 | (topcoat::view::Child::new(crate::detail::escape_closes(cx).await?)) |
| 1959 | }.boxed()) |
| 1960 | } |