| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
#![allow(clippy::needless_pass_by_value)] |
| 51 |
|
| 52 |
use chrono::{DateTime, Utc}; |
| 53 |
use goingson_core::{ |
| 54 |
BodyFormat, Email, EmailId, EmailSource, EmailThread, Validate as _, date_utils, email_compose, |
| 55 |
event_from_email, task_from_email, |
| 56 |
}; |
| 57 |
use makeover_layout::Tone; |
| 58 |
use quasi_declare::declare; |
| 59 |
use quasi_router::screen::{Choice, Consult, Figure, Rest, Tag}; |
| 60 |
use quasi_router::{Action, Response, RouteError, Router}; |
| 61 |
|
| 62 |
use crate::commands::get_snooze_options; |
| 63 |
use crate::state::{AppState, DESKTOP_USER_ID}; |
| 64 |
|
| 65 |
#[cfg(test)] |
| 66 |
mod tests; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
const PAGE: i64 = 200; |
| 73 |
|
| 74 |
|
| 75 |
const LIST: &str = "emails-list"; |
| 76 |
|
| 77 |
|
| 78 |
const THREAD: &str = "emails-thread"; |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
const SELECTION: &str = "chosen"; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
#[derive(Debug, Clone, Default)] |
| 99 |
struct View { |
| 100 |
|
| 101 |
folder: Option<String>, |
| 102 |
|
| 103 |
label: Option<String>, |
| 104 |
|
| 105 |
archived: bool, |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
ticked: bool, |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
shown: i64, |
| 131 |
} |
| 132 |
|
| 133 |
impl View { |
| 134 |
|
| 135 |
fn of(request: &quasi_router::Request) -> Self { |
| 136 |
Self { |
| 137 |
folder: text(&request.carried, "folder"), |
| 138 |
label: text(&request.carried, "label"), |
| 139 |
archived: matches!(request.carried.get("archived"), Some("1" | "true")), |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
ticked: matches!(request.carried.get("ticked"), Some("all")), |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
shown: request |
| 151 |
.carried |
| 152 |
.get("shown") |
| 153 |
.and_then(|raw| raw.parse::<i64>().ok()) |
| 154 |
.unwrap_or(PAGE) |
| 155 |
.clamp(PAGE, PAGE * 10), |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
fn carry(&self, action: Action) -> Action { |
| 192 |
let action = match &self.folder { |
| 193 |
Some(folder) => action.carrying("folder", folder.clone()), |
| 194 |
None => action, |
| 195 |
}; |
| 196 |
let action = match &self.label { |
| 197 |
Some(label) => action.carrying("label", label.clone()), |
| 198 |
None => action, |
| 199 |
}; |
| 200 |
let action = if self.archived { |
| 201 |
action.carrying("archived", "1") |
| 202 |
} else { |
| 203 |
action |
| 204 |
}; |
| 205 |
let action = if self.ticked { |
| 206 |
action.carrying("ticked", "all") |
| 207 |
} else { |
| 208 |
action |
| 209 |
}; |
| 210 |
if self.shown == PAGE { |
| 211 |
action |
| 212 |
} else { |
| 213 |
action.carrying("shown", self.shown.to_string()) |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
fn list(&self) -> Action { |
| 219 |
self.carry(Action::get("/emails/list")) |
| 220 |
} |
| 221 |
} |
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
fn text(params: &quasi_router::Params, name: &str) -> Option<String> { |
| 228 |
params |
| 229 |
.get(name) |
| 230 |
.map(str::trim) |
| 231 |
.filter(|value| !value.is_empty()) |
| 232 |
.map(ToOwned::to_owned) |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
fn email_id(request: &quasi_router::Request) -> Result<EmailId, RouteError> { |
| 237 |
let raw = request |
| 238 |
.captures |
| 239 |
.get("id") |
| 240 |
.ok_or_else(|| RouteError::not_found("no email id"))?; |
| 241 |
Ok(EmailId::from( |
| 242 |
uuid::Uuid::parse_str(raw).map_err(|_| RouteError::not_found("not an email id"))?, |
| 243 |
)) |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
fn load(state: &AppState, id: EmailId) -> Result<Email, RouteError> { |
| 248 |
state |
| 249 |
.emails |
| 250 |
.get_by_id(id, DESKTOP_USER_ID) |
| 251 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 252 |
.ok_or_else(|| RouteError::not_found("no such email")) |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
fn threads(state: &AppState, view: &View) -> Result<(Vec<EmailThread>, i64), RouteError> { |
| 257 |
state |
| 258 |
.emails |
| 259 |
.list_threaded( |
| 260 |
DESKTOP_USER_ID, |
| 261 |
view.archived, |
| 262 |
Some(0), |
| 263 |
Some(view.shown), |
| 264 |
view.folder.as_deref(), |
| 265 |
view.label.as_deref(), |
| 266 |
) |
| 267 |
.map_err(|error| RouteError::internal(error.to_string())) |
| 268 |
} |
| 269 |
|
| 270 |
declare! { |
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
shape row_for(thread: &EmailThread, view: &View, open: Option<EmailId>) -> Row; |
| 298 |
|
| 299 |
row &thread.most_recent_email.subject { |
| 300 |
secondary &thread.most_recent_email.from; |
| 301 |
meta thread.most_recent_email.received_formatted(); |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
token Tag::badge("Unread").tone(Tone::Info) when thread.has_unread; |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
token Tag::badge("{thread.thread_count} messages") when thread.thread_count over 1; |
| 313 |
|
| 314 |
for label in thread.most_recent_email.labels.iter() { |
| 315 |
token Tag::badge(label); |
| 316 |
} |
| 317 |
|
| 318 |
token Tag::badge(snooze_word(&thread.most_recent_email)).tone(Tone::Warning) |
| 319 |
when thread.most_recent_email.is_snoozed(); |
| 320 |
|
| 321 |
current is_open(thread, open); |
| 322 |
activate to doing view.carry(Action::get("/emails/{thread.most_recent_email.id}")); |
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
ticking thread.most_recent_email.id.to_string() view.ticked; |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
for act in row_acts(&thread.most_recent_email, view) { |
| 333 |
beside Actions include act; |
| 334 |
} |
| 335 |
} |
| 336 |
} |
| 337 |
|
| 338 |
|
| 339 |
fn is_open(thread: &EmailThread, open: Option<EmailId>) -> bool { |
| 340 |
open == Some(thread.most_recent_email.id) |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
fn snooze_word(email: &Email) -> String { |
| 345 |
snoozed_until(email).map_or_else( |
| 346 |
|| "Snoozed".to_owned(), |
| 347 |
|when| format!("Snoozed until {when}"), |
| 348 |
) |
| 349 |
} |
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
fn snoozed_until(email: &Email) -> Option<String> { |
| 357 |
email |
| 358 |
.snoozed_until |
| 359 |
.map(|until| date_utils::format_relative_future(until, Utc::now())) |
| 360 |
} |
| 361 |
|
| 362 |
declare! { |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
shape row_acts(email: &Email, view: &View) -> Vec<Node>; |
| 370 |
|
| 371 |
act "Mark unread" to doing view.carry(Action::post("/emails/{email.id}/read")) |
| 372 |
with "read" "false" |
| 373 |
when email.is_read; |
| 374 |
|
| 375 |
act "Mark read" to doing view.carry(Action::post("/emails/{email.id}/read")) |
| 376 |
with "read" "true" |
| 377 |
unless email.is_read; |
| 378 |
|
| 379 |
act "Unarchive" to doing view.carry(Action::post("/emails/{email.id}/archive")) |
| 380 |
with "archived" "false" |
| 381 |
when email.is_archived { |
| 382 |
key "a"; |
| 383 |
} |
| 384 |
|
| 385 |
act "Archive" to doing view.carry(Action::post("/emails/{email.id}/archive")) |
| 386 |
with "archived" "true" |
| 387 |
unless email.is_archived { |
| 388 |
key "a"; |
| 389 |
} |
| 390 |
|
| 391 |
act "Create task" to doing view.carry(Action::post("/emails/{email.id}/task")) { |
| 392 |
key "t"; |
| 393 |
} |
| 394 |
|
| 395 |
act "Create event" to doing view.carry(Action::post("/emails/{email.id}/event")) { |
| 396 |
key "e"; |
| 397 |
} |
| 398 |
|
| 399 |
act "Unsnooze" to doing view.carry(Action::post("/emails/{email.id}/snooze")) |
| 400 |
with "clear" "true" |
| 401 |
when email.is_snoozed(); |
| 402 |
|
| 403 |
act "Delete" to doing view.carry(Action::post("/emails/{email.id}/delete")) { |
| 404 |
tone Danger; |
| 405 |
confirm "Are you sure you want to delete this email? This cannot be undone."; |
| 406 |
} |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
struct Listing { |
| 411 |
|
| 412 |
view: View, |
| 413 |
|
| 414 |
open: Option<EmailId>, |
| 415 |
|
| 416 |
threads: Vec<EmailThread>, |
| 417 |
|
| 418 |
total: i64, |
| 419 |
|
| 420 |
filtered: bool, |
| 421 |
|
| 422 |
configured: bool, |
| 423 |
} |
| 424 |
|
| 425 |
|
| 426 |
fn listing(state: &AppState, view: &View, open: Option<EmailId>) -> Result<Listing, RouteError> { |
| 427 |
let (threads, total) = threads(state, view)?; |
| 428 |
Ok(Listing { |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
filtered: view.folder.is_some() || view.label.is_some(), |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
configured: !state |
| 439 |
.email_accounts |
| 440 |
.list_by_user(DESKTOP_USER_ID) |
| 441 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 442 |
.is_empty(), |
| 443 |
view: view.clone(), |
| 444 |
open, |
| 445 |
threads, |
| 446 |
total, |
| 447 |
}) |
| 448 |
} |
| 449 |
|
| 450 |
impl Listing { |
| 451 |
|
| 452 |
fn all_shown(&self) -> bool { |
| 453 |
self.total <= i64::try_from(self.threads.len()).unwrap_or(i64::MAX) |
| 454 |
} |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
fn rest(&self) -> Rest { |
| 462 |
Rest::more( |
| 463 |
self.threads.len(), |
| 464 |
View { |
| 465 |
shown: self.view.shown + PAGE, |
| 466 |
..self.view.clone() |
| 467 |
} |
| 468 |
.list(), |
| 469 |
) |
| 470 |
.of(usize::try_from(self.total).unwrap_or(usize::MAX)) |
| 471 |
} |
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
fn unfiltered(&self) -> Action { |
| 476 |
View { |
| 477 |
archived: self.view.archived, |
| 478 |
..View::default() |
| 479 |
} |
| 480 |
.list() |
| 481 |
} |
| 482 |
} |
| 483 |
|
| 484 |
declare! { |
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
shape list(listing: &Listing) -> Node; |
| 501 |
|
| 502 |
given listing.nothing() { |
| 503 |
Nothing::Filtered -> empty "No mail matching this filter." { |
| 504 |
offering "Clear filters" to doing listing.unfiltered(); |
| 505 |
} |
| 506 |
Nothing::NoAccount -> empty "Set up an email account to get started."; |
| 507 |
Nothing::NoMail -> empty "No emails yet."; |
| 508 |
otherwise -> list { |
| 509 |
for thread in listing.threads.iter() { |
| 510 |
include row_for(thread, &listing.view, listing.open); |
| 511 |
} |
| 512 |
|
| 513 |
more listing.rest() unless listing.all_shown(); |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
enum Nothing { |
| 520 |
|
| 521 |
Filtered, |
| 522 |
|
| 523 |
NoAccount, |
| 524 |
|
| 525 |
NoMail, |
| 526 |
|
| 527 |
Some, |
| 528 |
} |
| 529 |
|
| 530 |
impl Listing { |
| 531 |
|
| 532 |
const fn nothing(&self) -> Nothing { |
| 533 |
if !self.threads.is_empty() { |
| 534 |
Nothing::Some |
| 535 |
} else if self.filtered { |
| 536 |
Nothing::Filtered |
| 537 |
} else if self.configured { |
| 538 |
Nothing::NoMail |
| 539 |
} else { |
| 540 |
Nothing::NoAccount |
| 541 |
} |
| 542 |
} |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
struct Band { |
| 547 |
|
| 548 |
view: View, |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
base: View, |
| 558 |
|
| 559 |
|
| 560 |
folders: Vec<Choice>, |
| 561 |
|
| 562 |
labels: Vec<Choice>, |
| 563 |
|
| 564 |
unread: i64, |
| 565 |
} |
| 566 |
|
| 567 |
|
| 568 |
fn band(state: &AppState, view: &View) -> Result<Band, RouteError> { |
| 569 |
let folders = state |
| 570 |
.emails |
| 571 |
.list_folders(DESKTOP_USER_ID) |
| 572 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 573 |
let labels = state |
| 574 |
.emails |
| 575 |
.list_labels(DESKTOP_USER_ID) |
| 576 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 577 |
|
| 578 |
let offered = |all: &str, values: Vec<String>| -> Vec<Choice> { |
| 579 |
if values.is_empty() { |
| 580 |
return Vec::new(); |
| 581 |
} |
| 582 |
let mut options = vec![Choice::new("", all)]; |
| 583 |
options.extend(values.iter().map(|value| Choice::new(value, value))); |
| 584 |
options |
| 585 |
}; |
| 586 |
|
| 587 |
Ok(Band { |
| 588 |
folders: offered("All folders", folders), |
| 589 |
labels: offered("All labels", labels), |
| 590 |
unread: state |
| 591 |
.emails |
| 592 |
.count_unread(DESKTOP_USER_ID) |
| 593 |
.map_err(|error| RouteError::internal(error.to_string()))?, |
| 594 |
base: View { |
| 595 |
shown: PAGE, |
| 596 |
ticked: false, |
| 597 |
..view.clone() |
| 598 |
}, |
| 599 |
view: view.clone(), |
| 600 |
}) |
| 601 |
} |
| 602 |
|
| 603 |
impl Band { |
| 604 |
|
| 605 |
|
| 606 |
|
| 607 |
fn asking_folder(&self) -> Action { |
| 608 |
View { |
| 609 |
folder: None, |
| 610 |
..self.base.clone() |
| 611 |
} |
| 612 |
.list() |
| 613 |
} |
| 614 |
|
| 615 |
|
| 616 |
fn asking_label(&self) -> Action { |
| 617 |
View { |
| 618 |
label: None, |
| 619 |
..self.base.clone() |
| 620 |
} |
| 621 |
.list() |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
fn toggling_archived(&self) -> Action { |
| 626 |
View { |
| 627 |
archived: !self.view.archived, |
| 628 |
..self.base.clone() |
| 629 |
} |
| 630 |
.list() |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
declare! { |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
shape folder_field(band: &Band) -> Field; |
| 648 |
|
| 649 |
field Select "folder" "Folder" { |
| 650 |
options band.folders.clone(); |
| 651 |
consulting Consult::at_once(band.asking_folder()); |
| 652 |
|
| 653 |
for folder in band.view.folder.iter() { |
| 654 |
value folder; |
| 655 |
} |
| 656 |
} |
| 657 |
} |
| 658 |
|
| 659 |
declare! { |
| 660 |
|
| 661 |
shape label_field(band: &Band) -> Field; |
| 662 |
|
| 663 |
field Select "label" "Label" { |
| 664 |
options band.labels.clone(); |
| 665 |
consulting Consult::at_once(band.asking_label()); |
| 666 |
|
| 667 |
for label in band.view.label.iter() { |
| 668 |
value label; |
| 669 |
} |
| 670 |
} |
| 671 |
} |
| 672 |
|
| 673 |
declare! { |
| 674 |
|
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
shape band_region(band: &Band) -> Slot; |
| 684 |
|
| 685 |
region "emails-band" as Band { |
| 686 |
page "Emails"; |
| 687 |
|
| 688 |
stats [] when band.unread over 0 { |
| 689 |
figure Figure::new("{band.unread}", "Unread").tone(Tone::Info); |
| 690 |
} |
| 691 |
|
| 692 |
include folder_field(band) unless band.folders.is_empty(); |
| 693 |
include label_field(band) unless band.labels.is_empty(); |
| 694 |
|
| 695 |
chip "Include archived" to doing band.toggling_archived() { |
| 696 |
latched band.view.archived; |
| 697 |
} |
| 698 |
|
| 699 |
act "Mark all read" to doing band.view.carry(Action::post("/emails/read-all")); |
| 700 |
|
| 701 |
extend bulk(&band.view); |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
declare! { |
| 706 |
|
| 707 |
|
| 708 |
|
| 709 |
|
| 710 |
|
| 711 |
|
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
|
| 725 |
|
| 726 |
|
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
|
| 733 |
|
| 734 |
shape bulk(view: &View) -> Vec<Node>; |
| 735 |
|
| 736 |
act "Mark read" to doing view.carry(Action::post("/emails/list/read")) { |
| 737 |
over SELECTION; |
| 738 |
} |
| 739 |
|
| 740 |
act "Archive" to doing view.carry(Action::post("/emails/list/archive")) { |
| 741 |
over SELECTION; |
| 742 |
} |
| 743 |
|
| 744 |
act "Snooze" to doing view.carry(Action::post("/emails/list/snooze")) { |
| 745 |
over SELECTION; |
| 746 |
field Select "until" "Snooze until" { |
| 747 |
options snooze_choices(); |
| 748 |
} |
| 749 |
} |
| 750 |
|
| 751 |
act "Delete" to doing view.carry(Action::post("/emails/list/delete")) { |
| 752 |
tone Danger; |
| 753 |
over SELECTION; |
| 754 |
confirm "Delete every selected email? This cannot be undone."; |
| 755 |
} |
| 756 |
|
| 757 |
|
| 758 |
|
| 759 |
act "Select all" to doing ticking(view, true); |
| 760 |
act "Clear selection" to doing ticking(view, false) when view.ticked; |
| 761 |
} |
| 762 |
|
| 763 |
|
| 764 |
fn ticking(view: &View, ticked: bool) -> Action { |
| 765 |
View { |
| 766 |
ticked, |
| 767 |
..view.clone() |
| 768 |
} |
| 769 |
.list() |
| 770 |
} |
| 771 |
|
| 772 |
|
| 773 |
fn snooze_choices() -> Vec<Choice> { |
| 774 |
get_snooze_options() |
| 775 |
.options |
| 776 |
.into_iter() |
| 777 |
.map(|option| Choice::new(option.time.to_rfc3339(), option.label)) |
| 778 |
.collect() |
| 779 |
} |
| 780 |
|
| 781 |
declare! { |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
|
| 788 |
shape screen(band: &Band, listing: &Listing, thread: &Option<Thread>) -> Screen; |
| 789 |
|
| 790 |
screen list_detail "Emails" false { |
| 791 |
at_place super::shell::EMAILS; |
| 792 |
selecting SELECTION; |
| 793 |
|
| 794 |
include band_region(band); |
| 795 |
|
| 796 |
region LIST as Pane { |
| 797 |
include list(listing); |
| 798 |
} |
| 799 |
|
| 800 |
include thread_slot(thread); |
| 801 |
} |
| 802 |
} |
| 803 |
|
| 804 |
|
| 805 |
struct Message { |
| 806 |
|
| 807 |
heading: String, |
| 808 |
|
| 809 |
body: String, |
| 810 |
|
| 811 |
format: BodyFormat, |
| 812 |
|
| 813 |
truncated: bool, |
| 814 |
} |
| 815 |
|
| 816 |
|
| 817 |
struct Attachment { |
| 818 |
|
| 819 |
name: String, |
| 820 |
|
| 821 |
size: String, |
| 822 |
|
| 823 |
kind: String, |
| 824 |
} |
| 825 |
|
| 826 |
|
| 827 |
struct Sender { |
| 828 |
|
| 829 |
contact: String, |
| 830 |
|
| 831 |
name: String, |
| 832 |
|
| 833 |
company: Option<String>, |
| 834 |
} |
| 835 |
|
| 836 |
|
| 837 |
struct Thread { |
| 838 |
|
| 839 |
view: View, |
| 840 |
|
| 841 |
subject: String, |
| 842 |
|
| 843 |
from: String, |
| 844 |
|
| 845 |
received: String, |
| 846 |
|
| 847 |
folder: Option<String>, |
| 848 |
|
| 849 |
archived: bool, |
| 850 |
|
| 851 |
labels: Vec<String>, |
| 852 |
|
| 853 |
snoozed: Option<String>, |
| 854 |
|
| 855 |
sender: Option<Sender>, |
| 856 |
|
| 857 |
attachments: Vec<Attachment>, |
| 858 |
|
| 859 |
|
| 860 |
messages: Vec<Message>, |
| 861 |
|
| 862 |
threaded: bool, |
| 863 |
|
| 864 |
latest: Email, |
| 865 |
|
| 866 |
snooze_choices: Vec<Choice>, |
| 867 |
|
| 868 |
label_box: String, |
| 869 |
|
| 870 |
folder_box: String, |
| 871 |
} |
| 872 |
|
| 873 |
|
| 874 |
|
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
|
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 897 |
|
| 898 |
|
| 899 |
|
| 900 |
|
| 901 |
fn open_thread(state: &AppState, id: EmailId, view: &View) -> Result<Thread, RouteError> { |
| 902 |
let email = load(state, id)?; |
| 903 |
|
| 904 |
|
| 905 |
|
| 906 |
let messages = match &email.thread_id { |
| 907 |
Some(thread_id) => { |
| 908 |
let found = state |
| 909 |
.emails |
| 910 |
.list_by_thread(DESKTOP_USER_ID, thread_id) |
| 911 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 912 |
if found.len() > 1 { found } else { vec![email] } |
| 913 |
} |
| 914 |
None => vec![email], |
| 915 |
}; |
| 916 |
let opened = messages |
| 917 |
.iter() |
| 918 |
.find(|message| message.id == id) |
| 919 |
.unwrap_or_else(|| &messages[0]); |
| 920 |
let latest = messages.last().unwrap_or(opened).clone(); |
| 921 |
|
| 922 |
Ok(Thread { |
| 923 |
subject: opened.subject.clone(), |
| 924 |
from: opened.from.clone(), |
| 925 |
received: opened.received_formatted(), |
| 926 |
folder: opened.source_folder.clone(), |
| 927 |
archived: opened.is_archived, |
| 928 |
labels: opened.labels.clone(), |
| 929 |
snoozed: snoozed_until(&latest).filter(|_| latest.is_snoozed()), |
| 930 |
sender: sender(state, opened), |
| 931 |
attachments: attachments(&messages), |
| 932 |
threaded: messages.len() > 1, |
| 933 |
messages: messages |
| 934 |
.iter() |
| 935 |
.map(|message| Message { |
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
heading: format!( |
| 940 |
"{} {}", |
| 941 |
if message.is_outgoing { "To" } else { "From" }, |
| 942 |
message.from |
| 943 |
), |
| 944 |
body: message.body.clone(), |
| 945 |
format: message.body_format, |
| 946 |
truncated: message.body_truncated, |
| 947 |
}) |
| 948 |
.collect(), |
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
snooze_choices: if latest.is_snoozed() { |
| 954 |
Vec::new() |
| 955 |
} else { |
| 956 |
get_snooze_options() |
| 957 |
.options |
| 958 |
.into_iter() |
| 959 |
.map(|option| { |
| 960 |
Choice::new( |
| 961 |
option.time.to_rfc3339(), |
| 962 |
format!("{} \u{2014} {}", option.label, option.formatted), |
| 963 |
) |
| 964 |
}) |
| 965 |
.collect() |
| 966 |
}, |
| 967 |
label_box: latest.labels.join(", "), |
| 968 |
folder_box: latest.source_folder.clone().unwrap_or_default(), |
| 969 |
view: view.clone(), |
| 970 |
latest, |
| 971 |
}) |
| 972 |
} |
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
fn sender(state: &AppState, email: &Email) -> Option<Sender> { |
| 982 |
let address = email_compose::extract_email_address(&email.from); |
| 983 |
if address.is_empty() { |
| 984 |
return None; |
| 985 |
} |
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
let Ok(Some(contact)) = state.contacts.find_by_email(DESKTOP_USER_ID, address) else { |
| 990 |
return None; |
| 991 |
}; |
| 992 |
Some(Sender { |
| 993 |
contact: contact.id.to_string(), |
| 994 |
name: contact.display_name, |
| 995 |
company: contact.company, |
| 996 |
}) |
| 997 |
} |
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
fn attachments(messages: &[Email]) -> Vec<Attachment> { |
| 1006 |
messages |
| 1007 |
.iter() |
| 1008 |
.flat_map(|message| { |
| 1009 |
message |
| 1010 |
.attachment_meta |
| 1011 |
.as_deref() |
| 1012 |
.and_then(|json| { |
| 1013 |
serde_json::from_str::<Vec<goingson_core::AttachmentMeta>>(json).ok() |
| 1014 |
}) |
| 1015 |
.unwrap_or_default() |
| 1016 |
}) |
| 1017 |
.map(|meta| Attachment { |
| 1018 |
name: meta.filename, |
| 1019 |
size: goingson_core::format_file_size(i64::try_from(meta.size).unwrap_or(i64::MAX)), |
| 1020 |
kind: meta.mime_type, |
| 1021 |
}) |
| 1022 |
.collect() |
| 1023 |
} |
| 1024 |
|
| 1025 |
declare! { |
| 1026 |
|
| 1027 |
shape attachment_row(attachment: &Attachment) -> Row; |
| 1028 |
|
| 1029 |
row &attachment.name { |
| 1030 |
meta &attachment.size; |
| 1031 |
token Tag::badge(&attachment.kind); |
| 1032 |
} |
| 1033 |
} |
| 1034 |
|
| 1035 |
declare! { |
| 1036 |
|
| 1037 |
shape sender_row(sender: &Sender) -> Row; |
| 1038 |
|
| 1039 |
row &sender.name { |
| 1040 |
for company in sender.company.iter() { |
| 1041 |
secondary company; |
| 1042 |
} |
| 1043 |
activate to get "/contacts/{sender.contact}"; |
| 1044 |
} |
| 1045 |
} |
| 1046 |
|
| 1047 |
declare! { |
| 1048 |
|
| 1049 |
shape message_body(message: &Message, threaded: bool) -> Vec<Node>; |
| 1050 |
|
| 1051 |
subsection &message.heading when threaded; |
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
given message.format { |
| 1058 |
BodyFormat::Markdown -> rich &message.body; |
| 1059 |
otherwise -> text &message.body; |
| 1060 |
} |
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
banner Tone::Warning |
| 1066 |
"This message was truncated when it was synced. The rest of it is on the server." |
| 1067 |
when message.truncated; |
| 1068 |
} |
| 1069 |
|
| 1070 |
declare! { |
| 1071 |
|
| 1072 |
shape thread_slot(thread: &Option<Thread>) -> Slot; |
| 1073 |
|
| 1074 |
region THREAD as Pane { |
| 1075 |
empty "Nothing selected" when thread.is_none(); |
| 1076 |
|
| 1077 |
for thread in thread.iter() { |
| 1078 |
section &thread.subject; |
| 1079 |
|
| 1080 |
for sender in thread.sender.iter() { |
| 1081 |
list { |
| 1082 |
include sender_row(sender); |
| 1083 |
} |
| 1084 |
} |
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
list { |
| 1089 |
row &thread.from { |
| 1090 |
meta &thread.received; |
| 1091 |
|
| 1092 |
for folder in thread.folder.iter() { |
| 1093 |
token Tag::badge(folder); |
| 1094 |
} |
| 1095 |
|
| 1096 |
token Tag::badge("Archived") when thread.archived; |
| 1097 |
|
| 1098 |
for label in thread.labels.iter() { |
| 1099 |
token Tag::badge(label); |
| 1100 |
} |
| 1101 |
|
| 1102 |
for when in thread.snoozed.iter() { |
| 1103 |
token Tag::badge("Snoozed until {when}").tone(Tone::Warning); |
| 1104 |
} |
| 1105 |
} |
| 1106 |
} |
| 1107 |
|
| 1108 |
subsection "Attachments ({thread.attachments.len()})" |
| 1109 |
unless thread.attachments.is_empty(); |
| 1110 |
|
| 1111 |
list { |
| 1112 |
for attachment in thread.attachments.iter() { |
| 1113 |
include attachment_row(attachment); |
| 1114 |
} |
| 1115 |
} unless thread.attachments.is_empty(); |
| 1116 |
|
| 1117 |
for message in thread.messages.iter() { |
| 1118 |
extend message_body(message, thread.threaded); |
| 1119 |
} |
| 1120 |
|
| 1121 |
extend thread_acts(thread); |
| 1122 |
} |
| 1123 |
} |
| 1124 |
} |
| 1125 |
|
| 1126 |
declare! { |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
|
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
shape thread_acts(thread: &Thread) -> Vec<Node>; |
| 1149 |
|
| 1150 |
extend row_acts(&thread.latest, &thread.view); |
| 1151 |
|
| 1152 |
subsection "Snooze" unless thread.snooze_choices.is_empty(); |
| 1153 |
|
| 1154 |
for offered in thread.snooze_choices.iter() { |
| 1155 |
act offered.label.clone() |
| 1156 |
to doing thread.view.carry(Action::post("/emails/{thread.latest.id}/snooze")) |
| 1157 |
with "until" offered.value.clone(); |
| 1158 |
} |
| 1159 |
|
| 1160 |
subsection "Organise"; |
| 1161 |
|
| 1162 |
form doing thread.view.carry(Action::post("/emails/{thread.latest.id}/labels")) { |
| 1163 |
submit "Save labels"; |
| 1164 |
|
| 1165 |
field Text "labels" "Labels" { |
| 1166 |
value &thread.label_box; |
| 1167 |
hint "Comma-separated."; |
| 1168 |
} |
| 1169 |
} |
| 1170 |
|
| 1171 |
form doing thread.view.carry(Action::post("/emails/{thread.latest.id}/folder")) { |
| 1172 |
submit "Move"; |
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
field Text "folder" "Folder" { |
| 1178 |
value &thread.folder_box; |
| 1179 |
required; |
| 1180 |
} |
| 1181 |
} |
| 1182 |
} |
| 1183 |
|
| 1184 |
|
| 1185 |
fn index(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1186 |
wrote(state, &View::of(&request), None) |
| 1187 |
} |
| 1188 |
|
| 1189 |
|
| 1190 |
fn list_only(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1191 |
let view = View::of(&request); |
| 1192 |
let node = list(&listing(state, &view, None)?); |
| 1193 |
Ok(Response::fragment(LIST, node)) |
| 1194 |
} |
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
fn thread(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1208 |
let id = email_id(&request)?; |
| 1209 |
let view = View::of(&request); |
| 1210 |
|
| 1211 |
|
| 1212 |
state |
| 1213 |
.emails |
| 1214 |
.mark_read(id, DESKTOP_USER_ID) |
| 1215 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1216 |
wrote(state, &view, Some(id)) |
| 1217 |
} |
| 1218 |
|
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
|
| 1223 |
fn wrote(state: &AppState, view: &View, open: Option<EmailId>) -> Result<Response, RouteError> { |
| 1224 |
let thread = open.map(|id| open_thread(state, id, view)).transpose()?; |
| 1225 |
Ok(screen(&band(state, view)?, &listing(state, view, open)?, &thread).into()) |
| 1226 |
} |
| 1227 |
|
| 1228 |
|
| 1229 |
fn set_read(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1230 |
let id = email_id(&request)?; |
| 1231 |
let view = View::of(&request); |
| 1232 |
let read = request.payload.get("read") == Some("true"); |
| 1233 |
|
| 1234 |
let found = if read { |
| 1235 |
state.emails.mark_read(id, DESKTOP_USER_ID) |
| 1236 |
} else { |
| 1237 |
state.emails.mark_unread(id, DESKTOP_USER_ID) |
| 1238 |
} |
| 1239 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1240 |
if !found { |
| 1241 |
return Err(RouteError::not_found("no such email")); |
| 1242 |
} |
| 1243 |
|
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
wrote(state, &view, read.then_some(id)) |
| 1248 |
} |
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
fn read_all(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1260 |
let view = View::of(&request); |
| 1261 |
let marked = state |
| 1262 |
.emails |
| 1263 |
.mark_all_read(DESKTOP_USER_ID) |
| 1264 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1265 |
|
| 1266 |
|
| 1267 |
|
| 1268 |
|
| 1269 |
Ok(wrote(state, &view, None)?.toast( |
| 1270 |
makeover_layout::Tone::Success, |
| 1271 |
match marked { |
| 1272 |
0 => "Nothing was unread".to_owned(), |
| 1273 |
1 => "1 email marked read".to_owned(), |
| 1274 |
many => format!("{many} emails marked read"), |
| 1275 |
}, |
| 1276 |
)) |
| 1277 |
} |
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
|
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
fn chosen(request: &quasi_router::Request) -> Vec<EmailId> { |
| 1296 |
request |
| 1297 |
.payload |
| 1298 |
.get_all(quasi_router::Node::TICKED) |
| 1299 |
.filter_map(|raw| uuid::Uuid::parse_str(raw.trim()).ok()) |
| 1300 |
.map(EmailId::from) |
| 1301 |
.collect() |
| 1302 |
} |
| 1303 |
|
| 1304 |
|
| 1305 |
fn counted(n: usize) -> String { |
| 1306 |
if n == 1 { |
| 1307 |
"1 email".to_owned() |
| 1308 |
} else { |
| 1309 |
format!("{n} emails") |
| 1310 |
} |
| 1311 |
} |
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
fn bulk_wrote( |
| 1320 |
state: &AppState, |
| 1321 |
request: &quasi_router::Request, |
| 1322 |
message: String, |
| 1323 |
) -> Result<Response, RouteError> { |
| 1324 |
let view = View { |
| 1325 |
ticked: false, |
| 1326 |
..View::of(request) |
| 1327 |
}; |
| 1328 |
Ok(wrote(state, &view, None)?.toast(makeover_layout::Tone::Success, message)) |
| 1329 |
} |
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
fn read_chosen(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1338 |
let mut done = 0; |
| 1339 |
for id in chosen(&request) { |
| 1340 |
if state |
| 1341 |
.emails |
| 1342 |
.mark_read(id, DESKTOP_USER_ID) |
| 1343 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1344 |
{ |
| 1345 |
done += 1; |
| 1346 |
} |
| 1347 |
} |
| 1348 |
bulk_wrote(state, &request, format!("{} marked read.", counted(done))) |
| 1349 |
} |
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
fn archive_chosen( |
| 1356 |
state: &AppState, |
| 1357 |
request: quasi_router::Request, |
| 1358 |
) -> Result<Response, RouteError> { |
| 1359 |
let mut done = 0; |
| 1360 |
for id in chosen(&request) { |
| 1361 |
if state |
| 1362 |
.emails |
| 1363 |
.archive(id, DESKTOP_USER_ID) |
| 1364 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1365 |
{ |
| 1366 |
done += 1; |
| 1367 |
} |
| 1368 |
} |
| 1369 |
bulk_wrote(state, &request, format!("{} archived.", counted(done))) |
| 1370 |
} |
| 1371 |
|
| 1372 |
|
| 1373 |
|
| 1374 |
|
| 1375 |
|
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
fn snooze_chosen(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1380 |
let until = request |
| 1381 |
.payload |
| 1382 |
.get("until") |
| 1383 |
.and_then(|raw| DateTime::parse_from_rfc3339(raw).ok()) |
| 1384 |
.map(|when| when.with_timezone(&Utc)) |
| 1385 |
.filter(|when| *when > Utc::now()) |
| 1386 |
.ok_or_else(|| RouteError::not_found("not a time to snooze until"))?; |
| 1387 |
|
| 1388 |
let mut done = 0; |
| 1389 |
for id in chosen(&request) { |
| 1390 |
if state |
| 1391 |
.emails |
| 1392 |
.snooze(id, DESKTOP_USER_ID, until) |
| 1393 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1394 |
.is_some() |
| 1395 |
{ |
| 1396 |
done += 1; |
| 1397 |
} |
| 1398 |
} |
| 1399 |
bulk_wrote( |
| 1400 |
state, |
| 1401 |
&request, |
| 1402 |
format!( |
| 1403 |
"{} snoozed until {}.", |
| 1404 |
counted(done), |
| 1405 |
date_utils::format_relative_future(until, Utc::now()) |
| 1406 |
), |
| 1407 |
) |
| 1408 |
} |
| 1409 |
|
| 1410 |
|
| 1411 |
fn delete_chosen(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1412 |
let mut done = 0; |
| 1413 |
for id in chosen(&request) { |
| 1414 |
if state |
| 1415 |
.emails |
| 1416 |
.delete(id, DESKTOP_USER_ID) |
| 1417 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1418 |
{ |
| 1419 |
done += 1; |
| 1420 |
} |
| 1421 |
} |
| 1422 |
bulk_wrote(state, &request, format!("{} deleted.", counted(done))) |
| 1423 |
} |
| 1424 |
|
| 1425 |
|
| 1426 |
|
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
|
| 1443 |
|
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
fn set_archived(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1449 |
let id = email_id(&request)?; |
| 1450 |
let view = View::of(&request); |
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
let archived = request.payload.get("archived") == Some("true"); |
| 1455 |
|
| 1456 |
let found = if archived { |
| 1457 |
state.emails.archive(id, DESKTOP_USER_ID) |
| 1458 |
} else { |
| 1459 |
state.emails.unarchive(id, DESKTOP_USER_ID) |
| 1460 |
} |
| 1461 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1462 |
if !found { |
| 1463 |
return Err(RouteError::not_found("no such email")); |
| 1464 |
} |
| 1465 |
|
| 1466 |
|
| 1467 |
let still_here = view.archived; |
| 1468 |
Ok(wrote(state, &view, still_here.then_some(id))?.toast( |
| 1469 |
makeover_layout::Tone::Success, |
| 1470 |
if archived { |
| 1471 |
"Email archived" |
| 1472 |
} else { |
| 1473 |
"Email unarchived" |
| 1474 |
}, |
| 1475 |
)) |
| 1476 |
} |
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
fn remove(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1483 |
let id = email_id(&request)?; |
| 1484 |
let view = View::of(&request); |
| 1485 |
let deleted = state |
| 1486 |
.emails |
| 1487 |
.delete(id, DESKTOP_USER_ID) |
| 1488 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1489 |
if !deleted { |
| 1490 |
return Err(RouteError::not_found("no such email")); |
| 1491 |
} |
| 1492 |
Ok(wrote(state, &view, None)?.toast(makeover_layout::Tone::Success, "Email deleted")) |
| 1493 |
} |
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
fn set_labels(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1501 |
let id = email_id(&request)?; |
| 1502 |
let view = View::of(&request); |
| 1503 |
let labels: Vec<String> = request |
| 1504 |
.payload |
| 1505 |
.get("labels") |
| 1506 |
.unwrap_or_default() |
| 1507 |
.split(',') |
| 1508 |
.map(|label| label.trim().to_owned()) |
| 1509 |
.filter(|label| !label.is_empty()) |
| 1510 |
.collect(); |
| 1511 |
|
| 1512 |
state |
| 1513 |
.emails |
| 1514 |
.update_labels(id, DESKTOP_USER_ID, &labels) |
| 1515 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1516 |
.ok_or_else(|| RouteError::not_found("no such email"))?; |
| 1517 |
|
| 1518 |
Ok(wrote(state, &view, Some(id))?.toast(makeover_layout::Tone::Success, "Labels updated")) |
| 1519 |
} |
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
fn set_folder(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1525 |
let id = email_id(&request)?; |
| 1526 |
let view = View::of(&request); |
| 1527 |
let folder = request |
| 1528 |
.payload |
| 1529 |
.get("folder") |
| 1530 |
.unwrap_or_default() |
| 1531 |
.trim() |
| 1532 |
.to_owned(); |
| 1533 |
if folder.is_empty() { |
| 1534 |
return Err(RouteError::not_found("no folder")); |
| 1535 |
} |
| 1536 |
|
| 1537 |
let moved = state |
| 1538 |
.emails |
| 1539 |
.update_source_folder(id, DESKTOP_USER_ID, &folder) |
| 1540 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1541 |
if !moved { |
| 1542 |
return Err(RouteError::not_found("no such email")); |
| 1543 |
} |
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
let still_here = view.folder.as_deref() == Some(folder.as_str()) || view.folder.is_none(); |
| 1548 |
Ok(wrote(state, &view, still_here.then_some(id))? |
| 1549 |
.toast(makeover_layout::Tone::Success, format!("Moved to {folder}"))) |
| 1550 |
} |
| 1551 |
|
| 1552 |
|
| 1553 |
fn set_snooze(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1554 |
let id = email_id(&request)?; |
| 1555 |
let view = View::of(&request); |
| 1556 |
|
| 1557 |
if request.payload.get("clear") == Some("true") { |
| 1558 |
state |
| 1559 |
.emails |
| 1560 |
.unsnooze(id, DESKTOP_USER_ID) |
| 1561 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1562 |
.ok_or_else(|| RouteError::not_found("no such email"))?; |
| 1563 |
return Ok( |
| 1564 |
wrote(state, &view, Some(id))?.toast(makeover_layout::Tone::Success, "Snooze cleared") |
| 1565 |
); |
| 1566 |
} |
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
let until = request |
| 1573 |
.payload |
| 1574 |
.get("until") |
| 1575 |
.and_then(|raw| DateTime::parse_from_rfc3339(raw).ok()) |
| 1576 |
.map(|when| when.with_timezone(&Utc)) |
| 1577 |
.filter(|when| *when > Utc::now()) |
| 1578 |
.ok_or_else(|| RouteError::not_found("not a time to snooze until"))?; |
| 1579 |
|
| 1580 |
state |
| 1581 |
.emails |
| 1582 |
.snooze(id, DESKTOP_USER_ID, until) |
| 1583 |
.map_err(|error| RouteError::internal(error.to_string()))? |
| 1584 |
.ok_or_else(|| RouteError::not_found("no such email"))?; |
| 1585 |
|
| 1586 |
Ok(wrote(state, &view, Some(id))?.toast( |
| 1587 |
makeover_layout::Tone::Success, |
| 1588 |
format!( |
| 1589 |
"Snoozed until {}", |
| 1590 |
date_utils::format_relative_future(until, Utc::now()) |
| 1591 |
), |
| 1592 |
)) |
| 1593 |
} |
| 1594 |
|
| 1595 |
|
| 1596 |
|
| 1597 |
|
| 1598 |
|
| 1599 |
|
| 1600 |
fn sender_contact(state: &AppState, from: &str) -> Option<goingson_core::ContactId> { |
| 1601 |
let address = email_compose::extract_email_address(from); |
| 1602 |
if address.is_empty() { |
| 1603 |
return None; |
| 1604 |
} |
| 1605 |
state |
| 1606 |
.contacts |
| 1607 |
.find_by_email(DESKTOP_USER_ID, address) |
| 1608 |
.ok() |
| 1609 |
.flatten() |
| 1610 |
.map(|contact| contact.id) |
| 1611 |
} |
| 1612 |
|
| 1613 |
|
| 1614 |
|
| 1615 |
|
| 1616 |
|
| 1617 |
|
| 1618 |
|
| 1619 |
|
| 1620 |
fn to_task(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1621 |
let id = email_id(&request)?; |
| 1622 |
let view = View::of(&request); |
| 1623 |
let email = load(state, id)?; |
| 1624 |
|
| 1625 |
let contact_id = sender_contact(state, &email.from); |
| 1626 |
let new_task = task_from_email( |
| 1627 |
&EmailSource { |
| 1628 |
id: email.id, |
| 1629 |
subject: &email.subject, |
| 1630 |
from: &email.from, |
| 1631 |
body: &email.body, |
| 1632 |
project_id: email.project_id, |
| 1633 |
}, |
| 1634 |
contact_id, |
| 1635 |
Utc::now(), |
| 1636 |
); |
| 1637 |
new_task |
| 1638 |
.validate() |
| 1639 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1640 |
state |
| 1641 |
.tasks |
| 1642 |
.create(DESKTOP_USER_ID, new_task) |
| 1643 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
|
| 1651 |
Ok(wrote(state, &view, Some(id))? |
| 1652 |
.toast(makeover_layout::Tone::Success, "Task created from email")) |
| 1653 |
} |
| 1654 |
|
| 1655 |
|
| 1656 |
fn to_event(state: &AppState, request: quasi_router::Request) -> Result<Response, RouteError> { |
| 1657 |
let id = email_id(&request)?; |
| 1658 |
let view = View::of(&request); |
| 1659 |
let email = load(state, id)?; |
| 1660 |
|
| 1661 |
let contact_id = sender_contact(state, &email.from); |
| 1662 |
let mut new_event = event_from_email( |
| 1663 |
&EmailSource { |
| 1664 |
id: email.id, |
| 1665 |
subject: &email.subject, |
| 1666 |
from: &email.from, |
| 1667 |
body: &email.body, |
| 1668 |
project_id: email.project_id, |
| 1669 |
}, |
| 1670 |
contact_id, |
| 1671 |
Utc::now(), |
| 1672 |
); |
| 1673 |
new_event.user_id = Some(DESKTOP_USER_ID); |
| 1674 |
new_event |
| 1675 |
.validate() |
| 1676 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1677 |
state |
| 1678 |
.events |
| 1679 |
.create(DESKTOP_USER_ID, new_event) |
| 1680 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 1681 |
|
| 1682 |
Ok(wrote(state, &view, Some(id))? |
| 1683 |
.toast(makeover_layout::Tone::Success, "Event created from email")) |
| 1684 |
} |
| 1685 |
|
| 1686 |
|
| 1687 |
#[must_use] |
| 1688 |
pub fn routes(router: Router<AppState>) -> Router<AppState> { |
| 1689 |
router |
| 1690 |
|
| 1691 |
|
| 1692 |
|
| 1693 |
.get("/emails/list", list_only) |
| 1694 |
.get("/emails", index) |
| 1695 |
.post("/emails/read-all", read_all) |
| 1696 |
|
| 1697 |
|
| 1698 |
|
| 1699 |
|
| 1700 |
.post("/emails/list/read", read_chosen) |
| 1701 |
.post("/emails/list/archive", archive_chosen) |
| 1702 |
.post("/emails/list/snooze", snooze_chosen) |
| 1703 |
.post("/emails/list/delete", delete_chosen) |
| 1704 |
.get("/emails/{id}", thread) |
| 1705 |
.post("/emails/{id}/read", set_read) |
| 1706 |
.post("/emails/{id}/archive", set_archived) |
| 1707 |
.post("/emails/{id}/delete", remove) |
| 1708 |
.post("/emails/{id}/labels", set_labels) |
| 1709 |
.post("/emails/{id}/folder", set_folder) |
| 1710 |
.post("/emails/{id}/snooze", set_snooze) |
| 1711 |
.post("/emails/{id}/task", to_task) |
| 1712 |
.post("/emails/{id}/event", to_event) |
| 1713 |
} |
| 1714 |
|