| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | |
| 10 | |
| 11 | |
| 12 | |
| 13 | |
| 14 | |
| 15 | |
| 16 | use crate::i18n::{Key, lang, t}; |
| 17 | use crate::layout::{NavPage, topbar}; |
| 18 | use crate::server::{ |
| 19 | Refusal, app, back_to, require_admin, require_user, share_link_url, share_origin, |
| 20 | }; |
| 21 | use crate::service::mintable_services; |
| 22 | use crate::share::{refusal_banner, refusal_of, share_copy_script}; |
| 23 | use in_core::store::{ShareKind, User}; |
| 24 | use serde::Deserialize; |
| 25 | use topcoat::Result; |
| 26 | use topcoat::context::Cx; |
| 27 | use topcoat::router::content::Form; |
| 28 | use topcoat::router::request::uri; |
| 29 | use topcoat::router::{HeaderName, StatusCode, header, page, route}; |
| 30 | use topcoat::view::{View, ViewExt, view}; |
| 31 | |
| 32 | |
| 33 | |
| 34 | pub fn human_bytes(bytes: u64) -> String { |
| 35 | const UNITS: [&str; 5] = ["B", "KiB", "MiB", "GiB", "TiB"]; |
| 36 | let mut value = bytes as f64; |
| 37 | let mut unit = 0; |
| 38 | while value >= 1024.0 && unit + 1 < UNITS.len() { |
| 39 | value /= 1024.0; |
| 40 | unit += 1; |
| 41 | } |
| 42 | if unit == 0 { |
| 43 | format!("{bytes} {}", UNITS[unit]) |
| 44 | } else { |
| 45 | format!("{value:.1} {}", UNITS[unit]) |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | |
| 50 | |
| 51 | |
| 52 | fn age_text(age: Option<i64>) -> String { |
| 53 | match age { |
| 54 | Some(secs) if secs >= 3600 => format!("{}h", secs / 3600), |
| 55 | Some(secs) if secs >= 60 => format!("{}m", secs / 60), |
| 56 | Some(secs) => format!("{secs}s"), |
| 57 | None => String::new(), |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | |
| 62 | fn quota_percent(user: &User) -> u8 { |
| 63 | if user.quota_bytes == 0 { |
| 64 | return 100; |
| 65 | } |
| 66 | (user.used_bytes.saturating_mul(100) / user.quota_bytes).min(100) as u8 |
| 67 | } |
| 68 | |
| 69 | |
| 70 | |
| 71 | |
| 72 | const MIB_BYTES: f64 = 1_048_576.0; |
| 73 | const GIB_BYTES: f64 = 1_073_741_824.0; |
| 74 | |
| 75 | |
| 76 | |
| 77 | |
| 78 | fn bytes_as_unit(bytes: u64) -> (String, &'static str) { |
| 79 | if bytes >= GIB_BYTES as u64 { |
| 80 | (trim_amount(bytes as f64 / GIB_BYTES), "GiB") |
| 81 | } else { |
| 82 | (trim_amount(bytes as f64 / MIB_BYTES), "MiB") |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | fn trim_amount(value: f64) -> String { |
| 90 | let rounded = (value * 100.0).round() / 100.0; |
| 91 | let text = format!("{rounded:.2}"); |
| 92 | text.trim_end_matches('0').trim_end_matches('.').to_string() |
| 93 | } |
| 94 | |
| 95 | |
| 96 | |
| 97 | |
| 98 | fn unit_bytes(amount: &str, unit: &str) -> Option<u64> { |
| 99 | let per = match unit { |
| 100 | "MiB" => MIB_BYTES, |
| 101 | "GiB" => GIB_BYTES, |
| 102 | _ => return None, |
| 103 | }; |
| 104 | let value: f64 = amount.trim().parse().ok()?; |
| 105 | if !value.is_finite() || value < 0.0 { |
| 106 | return None; |
| 107 | } |
| 108 | Some((value * per).round() as u64) |
| 109 | } |
| 110 | |
| 111 | type Redirect = Result<(StatusCode, [(HeaderName, String); 1])>; |
| 112 | |
| 113 | |
| 114 | |
| 115 | |
| 116 | |
| 117 | |
| 118 | fn redirect_back(cx: &Cx, nowhere: &str, call: &str, refusal: Option<Refusal>) -> Redirect { |
| 119 | let back = back_to(cx, nowhere); |
| 120 | let separator = if back.contains('?') { '&' } else { '?' }; |
| 121 | let location = match refusal { |
| 122 | Some(refusal) => format!("{back}{separator}refusal={}&on={call}", refusal.code()), |
| 123 | None => format!("{back}{separator}saved={call}"), |
| 124 | }; |
| 125 | Ok((StatusCode::SEE_OTHER, [(header::LOCATION, location)])) |
| 126 | } |
| 127 | |
| 128 | #[derive(Deserialize)] |
| 129 | struct QuotaForm { |
| 130 | user_id: String, |
| 131 | #[serde(default)] |
| 132 | quota: String, |
| 133 | #[serde(default)] |
| 134 | quota_unit: String, |
| 135 | } |
| 136 | |
| 137 | |
| 138 | |
| 139 | |
| 140 | |
| 141 | #[route(POST "/api/settings/quota")] |
| 142 | async fn set_quota(cx: &Cx, Form(input): Form<QuotaForm>) -> Redirect { |
| 143 | if let Err(refusal) = require_admin(cx).await { |
| 144 | return redirect_back(cx, "/settings?section=everyone", "quota", Some(refusal)); |
| 145 | } |
| 146 | |
| 147 | |
| 148 | |
| 149 | |
| 150 | let service_target = app(cx) |
| 151 | .store |
| 152 | .list_service_accounts() |
| 153 | .await |
| 154 | .map(|accounts| { |
| 155 | accounts |
| 156 | .iter() |
| 157 | .any(|account| account.user_id == input.user_id) |
| 158 | }) |
| 159 | .unwrap_or(false); |
| 160 | if service_target { |
| 161 | return redirect_back( |
| 162 | cx, |
| 163 | "/settings?section=everyone", |
| 164 | "quota", |
| 165 | Some(Refusal::ServiceQuota), |
| 166 | ); |
| 167 | } |
| 168 | match unit_bytes(&input.quota, &input.quota_unit) { |
| 169 | Some(quota_bytes) => match app(cx) |
| 170 | .store |
| 171 | .set_user_quota(&input.user_id, quota_bytes) |
| 172 | .await |
| 173 | { |
| 174 | Ok(()) => redirect_back(cx, "/settings?section=everyone", "quota", None), |
| 175 | Err(error) => redirect_back( |
| 176 | cx, |
| 177 | "/settings?section=everyone", |
| 178 | "quota", |
| 179 | Some(refusal_of(error)), |
| 180 | ), |
| 181 | }, |
| 182 | None => redirect_back( |
| 183 | cx, |
| 184 | "/settings?section=everyone", |
| 185 | "quota", |
| 186 | Some(Refusal::BadLimit), |
| 187 | ), |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | #[derive(Deserialize)] |
| 192 | struct DisableForm { |
| 193 | user_id: String, |
| 194 | #[serde(default)] |
| 195 | disabled: Option<String>, |
| 196 | } |
| 197 | |
| 198 | |
| 199 | |
| 200 | #[route(POST "/api/settings/disable")] |
| 201 | async fn set_disabled(cx: &Cx, Form(input): Form<DisableForm>) -> Redirect { |
| 202 | let admin = match require_admin(cx).await { |
| 203 | Ok(admin) => admin, |
| 204 | Err(refusal) => { |
| 205 | return redirect_back(cx, "/settings?section=everyone", "disable", Some(refusal)); |
| 206 | } |
| 207 | }; |
| 208 | if input.user_id == admin.id { |
| 209 | return redirect_back( |
| 210 | cx, |
| 211 | "/settings?section=everyone", |
| 212 | "disable", |
| 213 | Some(Refusal::Forbidden), |
| 214 | ); |
| 215 | } |
| 216 | let disabled = match input.disabled.as_deref() { |
| 217 | None => true, |
| 218 | Some(value) => !matches!( |
| 219 | value.trim().to_lowercase().as_str(), |
| 220 | "" | "0" | "false" | "no" | "off" |
| 221 | ), |
| 222 | }; |
| 223 | match app(cx) |
| 224 | .store |
| 225 | .set_user_disabled(&input.user_id, disabled) |
| 226 | .await |
| 227 | { |
| 228 | Ok(()) => redirect_back(cx, "/settings?section=everyone", "disable", None), |
| 229 | Err(error) => redirect_back( |
| 230 | cx, |
| 231 | "/settings?section=everyone", |
| 232 | "disable", |
| 233 | Some(refusal_of(error)), |
| 234 | ), |
| 235 | } |
| 236 | } |
| 237 | |
| 238 | #[derive(Deserialize)] |
| 239 | struct PreferencesForm { |
| 240 | ui: String, |
| 241 | theme: String, |
| 242 | language: String, |
| 243 | } |
| 244 | |
| 245 | |
| 246 | const UI_OPTIONS: [&str; 2] = ["instrument", "ledger"]; |
| 247 | |
| 248 | |
| 249 | const THEME_OPTIONS: [&str; 2] = ["light", "dark"]; |
| 250 | |
| 251 | |
| 252 | const LANGUAGE_OPTIONS: [&str; 2] = ["en", "tr"]; |
| 253 | |
| 254 | |
| 255 | |
| 256 | |
| 257 | |
| 258 | #[route(POST "/api/settings/preferences")] |
| 259 | async fn set_preferences(cx: &Cx, Form(input): Form<PreferencesForm>) -> Redirect { |
| 260 | let user = match require_user(cx).await { |
| 261 | Ok(user) => user, |
| 262 | Err(refusal) => { |
| 263 | return redirect_back( |
| 264 | cx, |
| 265 | "/settings?section=profile", |
| 266 | "preferences", |
| 267 | Some(refusal), |
| 268 | ); |
| 269 | } |
| 270 | }; |
| 271 | if !UI_OPTIONS.contains(&input.ui.as_str()) { |
| 272 | return redirect_back( |
| 273 | cx, |
| 274 | "/settings?section=profile", |
| 275 | "preferences", |
| 276 | Some(Refusal::BadUi), |
| 277 | ); |
| 278 | } |
| 279 | if !THEME_OPTIONS.contains(&input.theme.as_str()) { |
| 280 | return redirect_back( |
| 281 | cx, |
| 282 | "/settings?section=profile", |
| 283 | "preferences", |
| 284 | Some(Refusal::BadTheme), |
| 285 | ); |
| 286 | } |
| 287 | if !LANGUAGE_OPTIONS.contains(&input.language.as_str()) { |
| 288 | return redirect_back( |
| 289 | cx, |
| 290 | "/settings?section=profile", |
| 291 | "preferences", |
| 292 | Some(Refusal::BadLanguage), |
| 293 | ); |
| 294 | } |
| 295 | match app(cx) |
| 296 | .store |
| 297 | .set_preferences(&user.id, &input.theme, &input.language, &input.ui) |
| 298 | .await |
| 299 | { |
| 300 | Ok(()) => redirect_back(cx, "/settings?section=profile", "preferences", None), |
| 301 | Err(error) => redirect_back( |
| 302 | cx, |
| 303 | "/settings?section=profile", |
| 304 | "preferences", |
| 305 | Some(refusal_of(error)), |
| 306 | ), |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | #[derive(Deserialize)] |
| 311 | struct BaseUrlForm { |
| 312 | #[serde(default)] |
| 313 | base_url: String, |
| 314 | } |
| 315 | |
| 316 | |
| 317 | |
| 318 | |
| 319 | |
| 320 | |
| 321 | |
| 322 | |
| 323 | #[route(POST "/api/settings/base_url")] |
| 324 | async fn set_base_url(cx: &Cx, Form(input): Form<BaseUrlForm>) -> Redirect { |
| 325 | if let Err(refusal) = require_admin(cx).await { |
| 326 | return redirect_back(cx, "/settings?section=server", "base_url", Some(refusal)); |
| 327 | } |
| 328 | let trimmed = input.base_url.trim().to_string(); |
| 329 | if !trimmed.is_empty() && in_core::Config::validate_base_url(&trimmed).is_err() { |
| 330 | return redirect_back( |
| 331 | cx, |
| 332 | "/settings?section=server", |
| 333 | "base_url", |
| 334 | Some(Refusal::BadBaseUrl), |
| 335 | ); |
| 336 | } |
| 337 | match app(cx).store.set_setting("base_url", &trimmed).await { |
| 338 | Ok(()) => redirect_back(cx, "/settings?section=server", "base_url", None), |
| 339 | Err(error) => redirect_back( |
| 340 | cx, |
| 341 | "/settings?section=server", |
| 342 | "base_url", |
| 343 | Some(refusal_of(error)), |
| 344 | ), |
| 345 | } |
| 346 | } |
| 347 | |
| 348 | #[derive(Deserialize)] |
| 349 | struct ServiceAddForm { |
| 350 | service: String, |
| 351 | name: String, |
| 352 | } |
| 353 | |
| 354 | #[derive(Deserialize)] |
| 355 | struct ServiceAction { |
| 356 | service: String, |
| 357 | } |
| 358 | |
| 359 | |
| 360 | |
| 361 | fn redirect_shown(cx: &Cx, ticket: &str) -> Redirect { |
| 362 | let back = back_to(cx, "/settings?section=service"); |
| 363 | let separator = if back.contains('?') { '&' } else { '?' }; |
| 364 | Ok(( |
| 365 | StatusCode::SEE_OTHER, |
| 366 | [(header::LOCATION, format!("{back}{separator}shown={ticket}"))], |
| 367 | )) |
| 368 | } |
| 369 | |
| 370 | |
| 371 | |
| 372 | |
| 373 | #[route(POST "/api/settings/service_add")] |
| 374 | async fn service_add(cx: &Cx, Form(input): Form<ServiceAddForm>) -> Redirect { |
| 375 | if let Err(refusal) = require_admin(cx).await { |
| 376 | return redirect_back( |
| 377 | cx, |
| 378 | "/settings?section=service", |
| 379 | "service_add", |
| 380 | Some(refusal), |
| 381 | ); |
| 382 | } |
| 383 | let service = input.service.trim(); |
| 384 | let name = input.name.trim(); |
| 385 | if name.is_empty() { |
| 386 | return redirect_back( |
| 387 | cx, |
| 388 | "/settings?section=service", |
| 389 | "service_add", |
| 390 | Some(Refusal::BadServiceKey), |
| 391 | ); |
| 392 | } |
| 393 | let mintable = match mintable_services(cx).await { |
| 394 | Ok(mintable) => mintable, |
| 395 | Err(refusal) => { |
| 396 | return redirect_back( |
| 397 | cx, |
| 398 | "/settings?section=service", |
| 399 | "service_add", |
| 400 | Some(refusal), |
| 401 | ); |
| 402 | } |
| 403 | }; |
| 404 | if !mintable.iter().any(|row| row.key == service) { |
| 405 | return redirect_back( |
| 406 | cx, |
| 407 | "/settings?section=service", |
| 408 | "service_add", |
| 409 | Some(Refusal::BadServiceKey), |
| 410 | ); |
| 411 | } |
| 412 | let default_quota_bytes = app(cx).config.default_quota_bytes; |
| 413 | match app(cx) |
| 414 | .store |
| 415 | .create_service_account(service, name, default_quota_bytes) |
| 416 | .await |
| 417 | { |
| 418 | Ok(token) => redirect_shown(cx, &crate::server::stash_shown_secret(token)), |
| 419 | Err(_) => redirect_back( |
| 420 | cx, |
| 421 | "/settings?section=service", |
| 422 | "service_add", |
| 423 | Some(Refusal::Unavailable), |
| 424 | ), |
| 425 | } |
| 426 | } |
| 427 | |
| 428 | |
| 429 | |
| 430 | #[route(POST "/api/settings/service_rotate")] |
| 431 | async fn service_rotate(cx: &Cx, Form(input): Form<ServiceAction>) -> Redirect { |
| 432 | if let Err(refusal) = require_admin(cx).await { |
| 433 | return redirect_back( |
| 434 | cx, |
| 435 | "/settings?section=service", |
| 436 | "service_rotate", |
| 437 | Some(refusal), |
| 438 | ); |
| 439 | } |
| 440 | match app(cx).store.rotate_service_key(&input.service).await { |
| 441 | Ok(Some(token)) => redirect_shown(cx, &crate::server::stash_shown_secret(token)), |
| 442 | Ok(None) => redirect_back( |
| 443 | cx, |
| 444 | "/settings?section=service", |
| 445 | "service_rotate", |
| 446 | Some(Refusal::BadServiceKey), |
| 447 | ), |
| 448 | Err(_) => redirect_back( |
| 449 | cx, |
| 450 | "/settings?section=service", |
| 451 | "service_rotate", |
| 452 | Some(Refusal::Unavailable), |
| 453 | ), |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | |
| 458 | |
| 459 | #[route(POST "/api/settings/service_revoke")] |
| 460 | async fn service_revoke(cx: &Cx, Form(input): Form<ServiceAction>) -> Redirect { |
| 461 | if let Err(refusal) = require_admin(cx).await { |
| 462 | return redirect_back( |
| 463 | cx, |
| 464 | "/settings?section=service", |
| 465 | "service_revoke", |
| 466 | Some(refusal), |
| 467 | ); |
| 468 | } |
| 469 | match app(cx).store.revoke_service_key(&input.service).await { |
| 470 | Ok(true) => redirect_back(cx, "/settings?section=service", "service_revoke", None), |
| 471 | Ok(false) => redirect_back( |
| 472 | cx, |
| 473 | "/settings?section=service", |
| 474 | "service_revoke", |
| 475 | Some(Refusal::BadServiceKey), |
| 476 | ), |
| 477 | Err(_) => redirect_back( |
| 478 | cx, |
| 479 | "/settings?section=service", |
| 480 | "service_revoke", |
| 481 | Some(Refusal::Unavailable), |
| 482 | ), |
| 483 | } |
| 484 | } |
| 485 | |
| 486 | |
| 487 | |
| 488 | |
| 489 | #[derive(Clone, Copy, PartialEq, Eq)] |
| 490 | enum Section { |
| 491 | Profile, |
| 492 | Links, |
| 493 | Everyone, |
| 494 | Server, |
| 495 | Service, |
| 496 | } |
| 497 | |
| 498 | |
| 499 | fn rail_class(current: Section, target: Section) -> &'static str { |
| 500 | if current == target { |
| 501 | "settings-section-link active" |
| 502 | } else { |
| 503 | "settings-section-link" |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | |
| 508 | fn query_value(query: &str, key: &str) -> Option<String> { |
| 509 | query.split('&').find_map(|pair| { |
| 510 | let (name, value) = pair.split_once('=')?; |
| 511 | (name == key).then(|| value.to_string()) |
| 512 | }) |
| 513 | } |
| 514 | |
| 515 | |
| 516 | |
| 517 | |
| 518 | |
| 519 | |
| 520 | #[page("/settings")] |
| 521 | async fn settings(cx: &Cx) -> Result<impl View> { |
| 522 | let user = match require_user(cx).await { |
| 523 | Ok(user) => user, |
| 524 | Err(refusal) => { |
| 525 | let language = lang(cx).await; |
| 526 | return Ok(view! { |
| 527 | cx => |
| 528 | <main class="scaffold-note"> |
| 529 | <p>(refusal.message_in(language))</p> |
| 530 | <p><a href="/">(t(language, Key::BackToDrive))</a></p> |
| 531 | </main> |
| 532 | } |
| 533 | .boxed()); |
| 534 | } |
| 535 | }; |
| 536 | let language = lang(cx).await; |
| 537 | let app = app(cx); |
| 538 | let store = app.store; |
| 539 | let connected = app.health.stream_connected(); |
| 540 | let last_event = app.health.last_event_age_secs(); |
| 541 | let last_pass = app.health.last_pass_age_secs(); |
| 542 | // Both ages share one sentence shape: the humanized age and `ago`, |
| 543 | // or the `never` wording before the first occurrence — a card that |
| 544 | // went silent and a card that never spoke read differently. |
| 545 | let age_line = |age: Option<i64>| { |
| 546 | let text = age_text(age); |
| 547 | if text.is_empty() { |
| 548 | t(language, Key::Never).to_string() |
| 549 | } else { |
| 550 | format!("{text} {}", t(language, Key::AgoSuffix)) |
| 551 | } |
| 552 | }; |
| 553 | let event_line = age_line(last_event); |
| 554 | let pass_line = age_line(last_pass); |
| 555 | // The Connection card reads only what the config already carries and |
| 556 | // the sync task's own atomics — the client secret is never touched on |
| 557 | // any path that reaches this render. |
| 558 | // Re-read the row so the quota bar never shows a stale number. |
| 559 | let fresh = store.user(&user.id).await?.unwrap_or_else(|| user.clone()); |
| 560 | let administers = fresh.admin; |
| 561 | let query = uri(cx).query().unwrap_or(""); |
| 562 | let section = match query_value(query, "section").as_deref() { |
| 563 | Some("links") => Section::Links, |
| 564 | Some("everyone") if administers => Section::Everyone, |
| 565 | Some("server") if administers => Section::Server, |
| 566 | Some("service") if administers => Section::Service, |
| 567 | _ => Section::Profile, |
| 568 | }; |
| 569 | // Only the section on show pays for its rows: the links panel names |
| 570 | // each live link's target, Everyone lists the accounts, and the server |
| 571 | // panel needs the origin — nothing else reads them. |
| 572 | let mut link_names: Vec<(in_core::store::ShareLink, String, Option<String>)> = Vec::new(); |
| 573 | let links = if section == Section::Links { |
| 574 | store.share_links(&user.id).await? |
| 575 | } else { |
| 576 | Vec::new() |
| 577 | }; |
| 578 | if section == Section::Links { |
| 579 | // The display name per live link's target: the row carries only |
| 580 | // the id. A missing or trashed target keeps its id — the grant row |
| 581 | // outlives the trash, and the panel still revokes it. Enrichment |
| 582 | // never fails the page: an unreadable target reads as its id, the |
| 583 | // way the shared list degrades per row. |
| 584 | for link in links.iter().filter(|link| link.revoked_at.is_none()) { |
| 585 | let name = match link.kind { |
| 586 | ShareKind::File => store |
| 587 | .file(&link.target_id) |
| 588 | .await |
| 589 | .ok() |
| 590 | .flatten() |
| 591 | .map(|file| file.name), |
| 592 | ShareKind::Folder => store |
| 593 | .folder(&link.target_id) |
| 594 | .await |
| 595 | .ok() |
| 596 | .flatten() |
| 597 | .map(|folder| folder.name), |
| 598 | } |
| 599 | .unwrap_or_else(|| link.target_id.clone()); |
| 600 | // The full address, re-derived from the token the creation |
| 601 | // sealed away; `None` keeps the row masked with the note. |
| 602 | let url = share_link_url(cx, link).await; |
| 603 | link_names.push((link.clone(), name, url)); |
| 604 | } |
| 605 | } |
| 606 | let users = if administers && section == Section::Everyone { |
| 607 | store.users().await? |
| 608 | } else { |
| 609 | Vec::new() |
| 610 | }; |
| 611 | // The origin the copy-once banner and the server panel show: the |
| 612 | // stored setting when an admin set one, the config chain otherwise. |
| 613 | let origin = if matches!(section, Section::Links | Section::Server) { |
| 614 | share_origin(cx).await |
| 615 | } else { |
| 616 | String::new() |
| 617 | }; |
| 618 | // What the server form's field holds: the stored setting alone. An |
| 619 | // empty field is not an invented value — the effective line above it |
| 620 | // shows what an empty save falls back to. |
| 621 | let stored_base_url = if section == Section::Server { |
| 622 | store |
| 623 | .get_setting("base_url") |
| 624 | .await? |
| 625 | .filter(|value| !value.trim().is_empty()) |
| 626 | .unwrap_or_default() |
| 627 | } else { |
| 628 | String::new() |
| 629 | }; |
| 630 | // The Service keys panel's two reads: the keyed accounts, and the |
| 631 | // family rows no key covers yet — the mint dropdown's options. Both are |
| 632 | // admin-only reads on an admin-only section. |
| 633 | let (accounts, mintable) = if administers && section == Section::Service { |
| 634 | let accounts = store.list_service_accounts().await?; |
| 635 | let mintable = mintable_services(cx).await.unwrap_or_default(); |
| 636 | (accounts, mintable) |
| 637 | } else { |
| 638 | (Vec::new(), Vec::new()) |
| 639 | }; |
| 640 | // The one read that takes a stashed key off the shelf — and only on the |
| 641 | // section that shows it, so a lost section never eats a live ticket. |
| 642 | let created = created_token(query); |
| 643 | let shown = if section == Section::Service { |
| 644 | query_value(query, "shown") |
| 645 | .as_deref() |
| 646 | .and_then(crate::server::take_shown_secret) |
| 647 | } else { |
| 648 | None |
| 649 | }; |
| 650 | Ok(view! { |
| 651 | cx => |
| 652 | (topcoat::view::Child::new(topbar(cx, NavPage::Settings, &fresh, language).await?)) |
| 653 | <div class="settings-shell"> |
| 654 | <nav class="settings-sections"> |
| 655 | <a class=(rail_class(section, Section::Profile)) href="/settings?section=profile">(t(language, Key::Profile))</a> |
| 656 | <a class=(rail_class(section, Section::Links)) href="/settings?section=links">(t(language, Key::ManageLinks))</a> |
| 657 | if administers { |
| 658 | <a class=(rail_class(section, Section::Everyone)) href="/settings?section=everyone">(t(language, Key::AdminPanel))</a> |
| 659 | <a class=(rail_class(section, Section::Server)) href="/settings?section=server">(t(language, Key::ServerAddress))</a> |
| 660 | <a class=(rail_class(section, Section::Service)) href="/settings?section=service">(t(language, Key::ServiceKeysTitle))</a> |
| 661 | } |
| 662 | </nav> |
| 663 | <main class="settings-stage stage-wide"> |
| 664 | <h1 class="settings-title">(t(language, Key::Settings))</h1> |
| 665 | (topcoat::view::Child::new(refusal_banner(cx, language, &["create", "revoke", "add", "remove", "quota", "disable", "preferences", "base_url", "service_add", "service_rotate", "service_revoke"]).await?)) |
| 666 | if section == Section::Profile { |
| 667 | <section class="panel"> |
| 668 | <div class="panel-head"> |
| 669 | <h2 class="panel-title">(t(language, Key::Profile))</h2> |
| 670 | </div> |
| 671 | <div class="panel-body"> |
| 672 | <label class="field"> |
| 673 | <span class="field-label">(t(language, Key::DisplayName))</span> |
| 674 | <span class="field-static">(fresh.display_name.clone())</span> |
| 675 | </label> |
| 676 | <label class="field"> |
| 677 | <span class="field-label">(t(language, Key::EmailAddress))</span> |
| 678 | <span class="field-static">(fresh.email.clone())</span> |
| 679 | </label> |
| 680 | <div class="field"> |
| 681 | <span class="field-label">(t(language, Key::QuotaUsage))</span> |
| 682 | <span class="field-static">(format!("{} {} {}", human_bytes(fresh.used_bytes), t(language, Key::QuotaOf), human_bytes(fresh.quota_bytes)))</span> |
| 683 | <div class="quota-bar" role="progressbar" aria-valuenow=(quota_percent(&fresh).to_string()) aria-valuemin="0" aria-valuemax="100"> |
| 684 | <div class="quota-fill" style=(format!("width: {}%", quota_percent(&fresh)))></div> |
| 685 | </div> |
| 686 | </div> |
| 687 | <form class="field" method="post" action="/api/settings/preferences"> |
| 688 | <label class="field"> |
| 689 | <span class="field-label">(t(language, Key::UiLabel))</span> |
| 690 | <select class="field-input" name="ui"> |
| 691 | <option value="instrument" selected=(fresh.ui == "instrument")>(t(language, Key::InstrumentOption))</option> |
| 692 | <option value="ledger" selected=(fresh.ui == "ledger")>(t(language, Key::LedgerOption))</option> |
| 693 | </select> |
| 694 | </label> |
| 695 | <label class="field"> |
| 696 | <span class="field-label">(t(language, Key::ThemeLabel))</span> |
| 697 | <select class="field-input" name="theme"> |
| 698 | <option value="light" selected=(fresh.theme == "light")>(t(language, Key::LightOption))</option> |
| 699 | <option value="dark" selected=(fresh.theme == "dark")>(t(language, Key::DarkOption))</option> |
| 700 | </select> |
| 701 | </label> |
| 702 | <label class="field"> |
| 703 | <span class="field-label">(t(language, Key::LanguageLabel))</span> |
| 704 | <select class="field-input" name="language"> |
| 705 | <option value="en" selected=(fresh.language == "en")>"English"</option> |
| 706 | <option value="tr" selected=(fresh.language == "tr")>"Türkçe"</option> |
| 707 | </select> |
| 708 | </label> |
| 709 | <div class="panel-foot"> |
| 710 | <button class="primary" type="submit">(t(language, Key::Save))</button> |
| 711 | </div> |
| 712 | </form> |
| 713 | </div> |
| 714 | </section> |
| 715 | // The Connection card: who this app talks to, and whether |
| 716 | // the live feed is up right now. Read-only, every |
| 717 | // signed-in reader — nothing here is per-user data. |
| 718 | <section class="panel"> |
| 719 | <div class="panel-head"> |
| 720 | <h2 class="panel-title">(t(language, Key::Connection))</h2> |
| 721 | </div> |
| 722 | <div class="panel-body"> |
| 723 | <dl class="connection-facts"> |
| 724 | <dt>(t(language, Key::ConnectionIssuer))</dt> |
| 725 | <dd>(app.config.oidc.issuer.clone())</dd> |
| 726 | <dt>(t(language, Key::ConnectionClientId))</dt> |
| 727 | <dd>(app.config.oidc.client_id.clone())</dd> |
| 728 | <dt>(t(language, Key::ConnectionStream))</dt> |
| 729 | <dd> |
| 730 | <span class=(if connected { "connection-dot connection-on" } else { "connection-dot connection-wait" })></span> |
| 731 | if connected { |
| 732 | (t(language, Key::ConnectionConnected)) |
| 733 | } else { |
| 734 | (t(language, Key::ConnectionReconnecting)) |
| 735 | } |
| 736 | </dd> |
| 737 | <dt>(t(language, Key::ConnectionLastEvent))</dt> |
| 738 | <dd>(event_line)</dd> |
| 739 | <dt>(t(language, Key::ConnectionLastPass))</dt> |
| 740 | <dd>(pass_line)</dd> |
| 741 | </dl> |
| 742 | </div> |
| 743 | </section> |
| 744 | } |
| 745 | if section == Section::Links { |
| 746 | if let Some(token) = created { |
| 747 | <section class="panel"> |
| 748 | <div class="panel-head"> |
| 749 | <h2 class="panel-title">(t(language, Key::LinkCreated))</h2> |
| 750 | </div> |
| 751 | <div class="panel-body"> |
| 752 | <p class="field-note">(t(language, Key::CopyLinkOnce))</p> |
| 753 | <p class="member-link-value">(format!("{origin}/s/{token}"))</p> |
| 754 | </div> |
| 755 | </section> |
| 756 | } |
| 757 | <section class="panel"> |
| 758 | <div class="panel-head"> |
| 759 | <h2 class="panel-title">(t(language, Key::ManageLinks))</h2> |
| 760 | </div> |
| 761 | <div class="panel-body"> |
| 762 | if link_names.is_empty() { |
| 763 | <p class="field-note">(t(language, Key::NoLinks))</p> |
| 764 | } |
| 765 | if link_names.iter().any(|(link, _, _)| !link.can_download) { |
| 766 | <p class="field-note">(t(language, Key::ViewOnlyNote))</p> |
| 767 | } |
| 768 | for (link, target_name, url) in &link_names { |
| 769 | <div class="member-row"> |
| 770 | <span class="member-name">(format!("{} · {}", t(language, if link.kind == ShareKind::Folder { Key::KindFolder } else { Key::KindFile }), target_name.clone()))</span> |
| 771 | <span class="field-note">(expiry_line(language, link.expires_at))</span> |
| 772 | <div class="spacer"></div> |
| 773 | <span class="field-note">(if link.can_download { t(language, Key::CanDownload) } else { t(language, Key::ViewOnly) })</span> |
| 774 | <form class="pop-row-form" method="post" action="/api/share/link/revoke"> |
| 775 | <input type="hidden" name="id" value=(link.id.clone())> |
| 776 | <button class="quiet quiet-danger" type="submit">(t(language, Key::RevokeLink))</button> |
| 777 | </form> |
| 778 | </div> |
| 779 | // The same affordance the modal's live row |
| 780 | // carries: the full address with its copy |
| 781 | // button, or the one-click mint plus the |
| 782 | // carry-forward note for a link from before |
| 783 | // the sealing. |
| 784 | <div class="share-link-row"> |
| 785 | if let Some(url) = url { |
| 786 | <p class="member-link-value share-link-url" aria-label=(t(language, Key::ShareLink))>(url.clone())</p> |
| 787 | <button class="quiet share-copy" type="button" data-copied-label=(t(language, Key::Copied))>(t(language, Key::CopyLink))</button> |
| 788 | } else { |
| 789 | <form class="pop-row-form" method="post" action="/api/share/link/remint"> |
| 790 | <input type="hidden" name="id" value=(link.id.clone())> |
| 791 | <button class="quiet" type="submit">(t(language, Key::RemintLink))</button> |
| 792 | </form> |
| 793 | } |
| 794 | </div> |
| 795 | if url.is_none() { |
| 796 | <p class="share-link-note">(t(language, Key::LegacyLinkNote))</p> |
| 797 | } |
| 798 | } |
| 799 | </div> |
| 800 | </section> |
| 801 | // One delegated listener serves every row's copy button |
| 802 | // above; the script is idempotent across re-renders. |
| 803 | (topcoat::view::Child::new(share_copy_script(cx).await?)) |
| 804 | } |
| 805 | if section == Section::Everyone { |
| 806 | <section class="panel"> |
| 807 | <div class="panel-head"> |
| 808 | <h2 class="panel-title">(t(language, Key::AdminPanel))</h2> |
| 809 | </div> |
| 810 | <div class="panel-body"> |
| 811 | <div class="table-pan"> |
| 812 | <table class="member-table"> |
| 813 | <thead> |
| 814 | <tr> |
| 815 | <th class="member-col-name" scope="col">(t(language, Key::NameColumn))</th> |
| 816 | <th class="member-col-address" scope="col">(t(language, Key::EmailAddress))</th> |
| 817 | <th class="member-col-account" scope="col">(t(language, Key::QuotaUsage))</th> |
| 818 | <th class="member-col-quota" scope="col">(t(language, Key::QuotaBytes))</th> |
| 819 | </tr> |
| 820 | </thead> |
| 821 | <tbody> |
| 822 | for listed in &users { |
| 823 | <tr class="member-row"> |
| 824 | <td class="member-col-name member-name"> |
| 825 | <span class="member-name-row"> |
| 826 | (listed.display_name.clone()) |
| 827 | if listed.admin { |
| 828 | <span class="chip chip-admin">(t(language, Key::AdminBadge))</span> |
| 829 | } |
| 830 | if listed.disabled { |
| 831 | <span class="chip chip-off">(t(language, Key::DisabledBadge))</span> |
| 832 | } |
| 833 | </span> |
| 834 | </td> |
| 835 | <td class="member-col-address member-address">(listed.email.clone())</td> |
| 836 | <td class="member-col-account member-account">(format!("{} {} {}", human_bytes(listed.used_bytes), t(language, Key::QuotaOf), human_bytes(listed.quota_bytes)))</td> |
| 837 | <td class="member-col-quota"> |
| 838 | <form class="pop-row-form member-quota" method="post" action="/api/settings/quota"> |
| 839 | <input type="hidden" name="user_id" value=(listed.id.clone())> |
| 840 | <input class="field-input" type="number" name="quota" min="0" step="any" value=(bytes_as_unit(listed.quota_bytes).0) aria-label=(t(language, Key::QuotaBytes))> |
| 841 | <select class="field-input" name="quota_unit" aria-label=(t(language, Key::QuotaBytes))> |
| 842 | <option value="MiB" selected=(bytes_as_unit(listed.quota_bytes).1 == "MiB")>"MiB"</option> |
| 843 | <option value="GiB" selected=(bytes_as_unit(listed.quota_bytes).1 == "GiB")>"GiB"</option> |
| 844 | </select> |
| 845 | <button class="quiet" type="submit">(t(language, Key::SetQuota))</button> |
| 846 | </form> |
| 847 | </td> |
| 848 | <td class="member-col-role"> |
| 849 | if listed.id != fresh.id { |
| 850 | <form class="pop-row-form" method="post" action="/api/settings/disable"> |
| 851 | <input type="hidden" name="user_id" value=(listed.id.clone())> |
| 852 | if listed.disabled { |
| 853 | <input type="hidden" name="disabled" value="0"> |
| 854 | <button class="quiet" type="submit">(t(language, Key::EnableUser))</button> |
| 855 | } else { |
| 856 | <input type="hidden" name="disabled" value="1"> |
| 857 | <button class="quiet quiet-danger" type="submit">(t(language, Key::DisableUser))</button> |
| 858 | } |
| 859 | </form> |
| 860 | } |
| 861 | </td> |
| 862 | </tr> |
| 863 | } |
| 864 | </tbody> |
| 865 | </table> |
| 866 | </div> |
| 867 | </div> |
| 868 | </section> |
| 869 | } |
| 870 | if section == Section::Server { |
| 871 | <section class="panel"> |
| 872 | <div class="panel-head"> |
| 873 | <h2 class="panel-title">(t(language, Key::ServerAddress))</h2> |
| 874 | </div> |
| 875 | <div class="panel-body"> |
| 876 | <label class="field"> |
| 877 | <span class="field-label">(t(language, Key::EffectiveOrigin))</span> |
| 878 | <span class="field-static">(origin.clone())</span> |
| 879 | </label> |
| 880 | <form class="field" method="post" action="/api/settings/base_url"> |
| 881 | <label class="field"> |
| 882 | <span class="field-label">(t(language, Key::BaseUrlLabel))</span> |
| 883 | <input class="field-input" type="url" name="base_url" value=(stored_base_url) placeholder="https: |
| 884 | </label> |
| 885 | <p class="field-note">(t(language, Key::BaseUrlNote))</p> |
| 886 | <div class="panel-foot"> |
| 887 | <button class="primary" type="submit">(t(language, Key::Save))</button> |
| 888 | </div> |
| 889 | </form> |
| 890 | </div> |
| 891 | </section> |
| 892 | } |
| 893 | if section == Section::Service { |
| 894 | if let Some(key) = shown { |
| 895 | |
| 896 | |
| 897 | |
| 898 | |
| 899 | <section class="panel"> |
| 900 | <div class="panel-head"> |
| 901 | <h2 class="panel-title">(t(language, Key::ServiceKeysTitle))</h2> |
| 902 | </div> |
| 903 | <div class="panel-body"> |
| 904 | <p class="field-note">(t(language, Key::ServiceKeyShown))</p> |
| 905 | <div class="share-link-row"> |
| 906 | <p class="member-link-value share-link-url service-secret" aria-label=(t(language, Key::ServiceKeyName))>(key)</p> |
| 907 | <button class="quiet share-copy" type="button" data-copied-label=(t(language, Key::Copied))>(t(language, Key::ServiceKeyCopy))</button> |
| 908 | </div> |
| 909 | </div> |
| 910 | </section> |
| 911 | } |
| 912 | <section class="panel"> |
| 913 | <div class="panel-head"> |
| 914 | <h2 class="panel-title">(t(language, Key::ServiceKeysTitle))</h2> |
| 915 | </div> |
| 916 | <div class="panel-body"> |
| 917 | <form class="field" method="post" action="/api/settings/service_add"> |
| 918 | <label class="field"> |
| 919 | <span class="field-label">(t(language, Key::ServiceKeyService))</span> |
| 920 | <select class="field-input" name="service"> |
| 921 | for row in &mintable { |
| 922 | <option value=(row.key.clone())>(format!("{} · {}", row.name, row.key))</option> |
| 923 | } |
| 924 | </select> |
| 925 | </label> |
| 926 | <label class="field"> |
| 927 | <span class="field-label">(t(language, Key::ServiceKeyName))</span> |
| 928 | <input class="field-input" type="text" name="name" required="required"> |
| 929 | </label> |
| 930 | <div class="panel-foot"> |
| 931 | <button class="primary" type="submit">(t(language, Key::ServiceKeyMint))</button> |
| 932 | </div> |
| 933 | </form> |
| 934 | <div class="table-pan"> |
| 935 | <table class="member-table"> |
| 936 | <thead> |
| 937 | <tr> |
| 938 | <th class="member-col-name" scope="col">(t(language, Key::ServiceKeyName))</th> |
| 939 | <th class="member-col-address" scope="col">(t(language, Key::ServiceKeyService))</th> |
| 940 | <th class="member-col-account" scope="col">(t(language, Key::QuotaUsage))</th> |
| 941 | <th class="member-col-quota" scope="col">(t(language, Key::ServiceKeyCreated))</th> |
| 942 | </tr> |
| 943 | </thead> |
| 944 | <tbody> |
| 945 | for account in &accounts { |
| 946 | <tr class="member-row"> |
| 947 | <td class="member-col-name member-name">(account.name.clone())</td> |
| 948 | <td class="member-col-address member-address">(account.service.clone())</td> |
| 949 | <td class="member-col-account member-account">(human_bytes(account.used_bytes))</td> |
| 950 | <td class="member-col-quota"> |
| 951 | <div class="member-quota"> |
| 952 | <span class="field-note">(account.created_at.date().to_string())</span> |
| 953 | </div> |
| 954 | <details class="confirm-details"> |
| 955 | <summary class="quiet">(t(language, Key::ServiceKeyRotate))</summary> |
| 956 | <div class="confirm"> |
| 957 | <div class="confirm-title">(t(language, Key::ServiceKeyRotateTitle))</div> |
| 958 | <p class="field-note">(t(language, Key::ServiceKeyRotateCost))</p> |
| 959 | <form method="post" action="/api/settings/service_rotate"> |
| 960 | <input type="hidden" name="service" value=(account.service.clone())> |
| 961 | <button class="primary" type="submit">(t(language, Key::ServiceKeyRotate))</button> |
| 962 | </form> |
| 963 | </div> |
| 964 | </details> |
| 965 | <details class="confirm-details"> |
| 966 | <summary class="quiet quiet-danger">(t(language, Key::ServiceKeyRevoke))</summary> |
| 967 | <div class="confirm"> |
| 968 | <div class="confirm-title">(t(language, Key::ServiceKeyRevokeTitle))</div> |
| 969 | <p class="field-note">(t(language, Key::ServiceKeyRevokeCost))</p> |
| 970 | <form method="post" action="/api/settings/service_revoke"> |
| 971 | <input type="hidden" name="service" value=(account.service.clone())> |
| 972 | <button class="quiet quiet-danger" type="submit">(t(language, Key::ServiceKeyRevoke))</button> |
| 973 | </form> |
| 974 | </div> |
| 975 | </details> |
| 976 | </td> |
| 977 | </tr> |
| 978 | } |
| 979 | </tbody> |
| 980 | </table> |
| 981 | </div> |
| 982 | </div> |
| 983 | </section> |
| 984 | |
| 985 | |
| 986 | (topcoat::view::Child::new(share_copy_script(cx).await?)) |
| 987 | } |
| 988 | </main> |
| 989 | </div> |
| 990 | (topcoat::view::Child::new(crate::dropdown::dropdown_script(cx).await?)) |
| 991 | }.boxed()) |
| 992 | } |
| 993 | |
| 994 | |
| 995 | fn created_token(query: &str) -> Option<String> { |
| 996 | query_value(query, "created").filter(|value| !value.is_empty()) |
| 997 | } |
| 998 | |
| 999 | |
| 1000 | fn expiry_line(language: crate::i18n::Lang, expires_at: Option<time::OffsetDateTime>) -> String { |
| 1001 | match expires_at { |
| 1002 | Some(at) => format!( |
| 1003 | "{} {}", |
| 1004 | t(language, Key::ExpiresLabel), |
| 1005 | at.format(&time::format_description::well_known::Rfc3339) |
| 1006 | .unwrap_or_else(|_| "?".to_string()) |
| 1007 | ), |
| 1008 | None => t(language, Key::NeverExpires).to_string(), |
| 1009 | } |
| 1010 | } |