| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
use makeover_layout::Tone; |
| 89 |
use quasi_declare::declare; |
| 90 |
use quasi_router::screen::{Act, Choice, Tag}; |
| 91 |
use quasi_router::{Action, RouteError}; |
| 92 |
use synckit_client::InvitationState; |
| 93 |
use synckit_client::store::directory; |
| 94 |
|
| 95 |
use crate::commands::group::normalize_invite_token; |
| 96 |
use crate::state::AppState; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
fn conn( |
| 104 |
app: &AppState, |
| 105 |
) -> Result<impl std::ops::DerefMut<Target = rusqlite::Connection>, RouteError> { |
| 106 |
app.db |
| 107 |
.conn() |
| 108 |
.map_err(|error| RouteError::internal(error.to_string())) |
| 109 |
} |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
fn state_word(state: InvitationState) -> &'static str { |
| 119 |
match state { |
| 120 |
InvitationState::Pending => "Waiting to be used", |
| 121 |
InvitationState::Accepted => "Waiting on you", |
| 122 |
InvitationState::Redeemed => "Used", |
| 123 |
InvitationState::Revoked => "Cancelled", |
| 124 |
InvitationState::Expired => "Expired", |
| 125 |
_ => "In a state this version does not know", |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
struct Member { |
| 131 |
|
| 132 |
email: String, |
| 133 |
|
| 134 |
is_admin: bool, |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
struct Invitation { |
| 147 |
|
| 148 |
group: String, |
| 149 |
|
| 150 |
|
| 151 |
id: String, |
| 152 |
|
| 153 |
state: &'static str, |
| 154 |
|
| 155 |
|
| 156 |
usable: bool, |
| 157 |
|
| 158 |
issued: String, |
| 159 |
|
| 160 |
expires: String, |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
token: Option<String>, |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
struct Group { |
| 170 |
|
| 171 |
name: String, |
| 172 |
|
| 173 |
|
| 174 |
is_admin: bool, |
| 175 |
|
| 176 |
|
| 177 |
members: Vec<Member>, |
| 178 |
|
| 179 |
invitations: Vec<Invitation>, |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
struct Confirmation { |
| 184 |
|
| 185 |
group: String, |
| 186 |
|
| 187 |
id: String, |
| 188 |
|
| 189 |
who: String, |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
fingerprint: Option<String>, |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
struct Queued { |
| 200 |
|
| 201 |
id: String, |
| 202 |
|
| 203 |
|
| 204 |
doing: String, |
| 205 |
|
| 206 |
done: bool, |
| 207 |
|
| 208 |
|
| 209 |
failed: Option<String>, |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
enum Identity { |
| 214 |
|
| 215 |
Unconfigured, |
| 216 |
|
| 217 |
Locked, |
| 218 |
|
| 219 |
Held { |
| 220 |
|
| 221 |
|
| 222 |
pubkey: String, |
| 223 |
|
| 224 |
fingerprint: String, |
| 225 |
}, |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
struct Held { |
| 230 |
|
| 231 |
group: String, |
| 232 |
|
| 233 |
inviter: String, |
| 234 |
|
| 235 |
expires: String, |
| 236 |
|
| 237 |
usable: bool, |
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
refused: String, |
| 243 |
} |
| 244 |
|
| 245 |
|
| 246 |
pub(super) struct Sharing { |
| 247 |
|
| 248 |
groups: Vec<Group>, |
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
refreshed: Option<String>, |
| 253 |
|
| 254 |
confirmations: Vec<Confirmation>, |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
confirmations_refreshed: Option<String>, |
| 264 |
|
| 265 |
queued: Vec<Queued>, |
| 266 |
|
| 267 |
configured: bool, |
| 268 |
|
| 269 |
administered: Vec<Choice>, |
| 270 |
|
| 271 |
held: Option<Held>, |
| 272 |
|
| 273 |
identity: Identity, |
| 274 |
} |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
pub(super) fn read(app: &AppState) -> Result<Sharing, RouteError> { |
| 282 |
let conn = conn(app)?; |
| 283 |
let known = |
| 284 |
directory::groups(&conn).map_err(|error| RouteError::internal(error.to_string()))?; |
| 285 |
let refreshed = |
| 286 |
directory::refreshed_at(&conn).map_err(|error| RouteError::internal(error.to_string()))?; |
| 287 |
let waiting = directory::pending_confirmations(&conn) |
| 288 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 289 |
let confirmations_refreshed = directory::pending_confirmations_refreshed_at(&conn) |
| 290 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 291 |
let preview = |
| 292 |
directory::preview(&conn).map_err(|error| RouteError::internal(error.to_string()))?; |
| 293 |
|
| 294 |
let mut groups = Vec::with_capacity(known.len()); |
| 295 |
for group in &known { |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
let (members, invitations) = if group.is_admin { |
| 302 |
let members = directory::members(&conn, group.id) |
| 303 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 304 |
let invitations = directory::invitations(&conn, group.id) |
| 305 |
.map_err(|error| RouteError::internal(error.to_string()))?; |
| 306 |
(members, invitations) |
| 307 |
} else { |
| 308 |
(Vec::new(), Vec::new()) |
| 309 |
}; |
| 310 |
|
| 311 |
groups.push(Group { |
| 312 |
name: group.name.clone(), |
| 313 |
is_admin: group.is_admin, |
| 314 |
members: members |
| 315 |
.into_iter() |
| 316 |
.map(|member| Member { |
| 317 |
is_admin: member.role == "admin", |
| 318 |
email: member.email, |
| 319 |
}) |
| 320 |
.collect(), |
| 321 |
invitations: invitations |
| 322 |
.into_iter() |
| 323 |
.filter_map(|invitation| { |
| 324 |
let state = invitation.effective_state(); |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
if state == InvitationState::Accepted { |
| 330 |
return None; |
| 331 |
} |
| 332 |
let usable = state == InvitationState::Pending; |
| 333 |
Some(Invitation { |
| 334 |
group: group.id.to_string(), |
| 335 |
id: invitation.id.to_string(), |
| 336 |
state: state_word(state), |
| 337 |
usable, |
| 338 |
issued: invitation.created_at, |
| 339 |
expires: invitation.expires_at, |
| 340 |
token: usable.then_some(invitation.token).flatten(), |
| 341 |
}) |
| 342 |
}) |
| 343 |
.collect(), |
| 344 |
}); |
| 345 |
} |
| 346 |
drop(conn); |
| 347 |
|
| 348 |
let queued = crate::group_queue::pending(app).map_err(RouteError::internal)?; |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
let fingerprint_of = |invitation: Option<&str>| -> Option<String> { |
| 353 |
let wanted = invitation?; |
| 354 |
waiting |
| 355 |
.iter() |
| 356 |
.find(|invitation| invitation.id.to_string() == wanted)? |
| 357 |
.invitee_fingerprint |
| 358 |
.clone() |
| 359 |
}; |
| 360 |
|
| 361 |
Ok(Sharing { |
| 362 |
confirmations: waiting |
| 363 |
.iter() |
| 364 |
.map(|invitation| Confirmation { |
| 365 |
group: invitation.group_id.to_string(), |
| 366 |
id: invitation.id.to_string(), |
| 367 |
who: invitation |
| 368 |
.invitee_email |
| 369 |
.clone() |
| 370 |
.unwrap_or_else(|| "Somebody".to_owned()), |
| 371 |
fingerprint: invitation.invitee_fingerprint.clone(), |
| 372 |
}) |
| 373 |
.collect(), |
| 374 |
confirmations_refreshed, |
| 375 |
queued: queued |
| 376 |
.into_iter() |
| 377 |
.map(|op| Queued { |
| 378 |
doing: op |
| 379 |
.describe_confirming(fingerprint_of(op.invitation_id.as_deref()).as_deref()), |
| 380 |
done: op.done_at.is_some(), |
| 381 |
failed: op |
| 382 |
.last_error |
| 383 |
.map(|error| format!("{error} Tried {} times so far.", op.attempts)), |
| 384 |
id: op.id, |
| 385 |
}) |
| 386 |
.collect(), |
| 387 |
configured: app.read_recovering().is_some(), |
| 388 |
administered: known |
| 389 |
.iter() |
| 390 |
.filter(|group| group.is_admin) |
| 391 |
.map(|group| Choice::new(group.id.to_string(), &group.name)) |
| 392 |
.collect(), |
| 393 |
held: preview.map(|preview| Held { |
| 394 |
refused: format!( |
| 395 |
"That code leads to {} and cannot be used: {}.", |
| 396 |
preview.group_name, |
| 397 |
state_word(preview.state).to_lowercase() |
| 398 |
), |
| 399 |
group: preview.group_name, |
| 400 |
inviter: preview.inviter_email, |
| 401 |
expires: preview.expires_at, |
| 402 |
usable: preview.redeemable, |
| 403 |
}), |
| 404 |
identity: identity(app), |
| 405 |
groups, |
| 406 |
refreshed, |
| 407 |
}) |
| 408 |
} |
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
fn identity(app: &AppState) -> Identity { |
| 416 |
let Some(client) = app.read_recovering() else { |
| 417 |
return Identity::Unconfigured; |
| 418 |
}; |
| 419 |
let Ok(pubkey) = client.my_identity_public_key() else { |
| 420 |
return Identity::Locked; |
| 421 |
}; |
| 422 |
Identity::Held { |
| 423 |
fingerprint: synckit_client::identity::IdentityPublicKey::fingerprint_of_base64(&pubkey) |
| 424 |
.unwrap_or_else(|_| "unavailable".to_owned()), |
| 425 |
pubkey, |
| 426 |
} |
| 427 |
} |
| 428 |
|
| 429 |
declare! { |
| 430 |
|
| 431 |
shape member_row(member: &Member) -> Row; |
| 432 |
|
| 433 |
row &member.email { |
| 434 |
token Tag::badge("Admin") when member.is_admin; |
| 435 |
} |
| 436 |
} |
| 437 |
|
| 438 |
declare! { |
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
|
| 445 |
|
| 446 |
|
| 447 |
|
| 448 |
|
| 449 |
|
| 450 |
shape invitation_row(invitation: &Invitation) -> Row; |
| 451 |
|
| 452 |
row "Invite code" { |
| 453 |
token Tag::badge(invitation.state); |
| 454 |
meta "Issued {invitation.issued}"; |
| 455 |
|
| 456 |
secondary "Stops working {invitation.expires}." when invitation.usable; |
| 457 |
|
| 458 |
act "Cancel it" |
| 459 |
to post "/settings/sharing/invites/{invitation.group}/{invitation.id}/revoke" |
| 460 |
when invitation.usable { |
| 461 |
tone Danger; |
| 462 |
} |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
declare! { |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
shape invitation_token(invitation: &Invitation, token: &str) -> Row; |
| 475 |
|
| 476 |
row "The code to hand over" { |
| 477 |
beside Secondary include token_field(invitation, token); |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
declare! { |
| 482 |
|
| 483 |
shape token_field(invitation: &Invitation, token: &str) -> Field; |
| 484 |
|
| 485 |
field Text "invite_token_{invitation.id}" "Invite code" { |
| 486 |
value token; |
| 487 |
hint "One use. Whoever redeems it still lands in your confirmations."; |
| 488 |
} |
| 489 |
} |
| 490 |
|
| 491 |
declare! { |
| 492 |
|
| 493 |
shape group_rows(group: &Group) -> Vec<Node>; |
| 494 |
|
| 495 |
list { |
| 496 |
row &group.name { |
| 497 |
token Tag::badge("You administer this") when group.is_admin; |
| 498 |
} |
| 499 |
|
| 500 |
row "No members recorded yet" when group.is_admin and group.members.is_empty() { |
| 501 |
secondary "The member list is fetched on a sync; this device has not had one back."; |
| 502 |
} |
| 503 |
|
| 504 |
for member in group.members.iter() { |
| 505 |
include member_row(member); |
| 506 |
} |
| 507 |
|
| 508 |
for invitation in group.invitations.iter() { |
| 509 |
include invitation_row(invitation); |
| 510 |
|
| 511 |
for token in invitation.token.iter() { |
| 512 |
include invitation_token(invitation, token); |
| 513 |
} |
| 514 |
} |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
declare! { |
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
shape confirmation_row(confirmation: &Confirmation) -> Row; |
| 524 |
|
| 525 |
row &confirmation.who { |
| 526 |
token Tag::badge("Cannot be checked").tone(Tone::Danger) |
| 527 |
unless confirmation.fingerprint is_not None; |
| 528 |
|
| 529 |
secondary "The key on this invitation does not read as a key, so there is no \ |
| 530 |
fingerprint to compare. Cancel it from the group and issue another." |
| 531 |
unless confirmation.fingerprint is_not None; |
| 532 |
|
| 533 |
for fingerprint in confirmation.fingerprint.iter() { |
| 534 |
secondary "Fingerprint {fingerprint}. Ask them to read it out over a channel \ |
| 535 |
that is not this one, and admit them only if it matches."; |
| 536 |
|
| 537 |
act "It matches, admit them" |
| 538 |
to post "/settings/sharing/invites/{confirmation.group}/{confirmation.id}/confirm"; |
| 539 |
|
| 540 |
act "Reject" |
| 541 |
to post "/settings/sharing/invites/{confirmation.group}/{confirmation.id}/revoke" { |
| 542 |
tone Danger; |
| 543 |
} |
| 544 |
} |
| 545 |
} |
| 546 |
} |
| 547 |
|
| 548 |
declare! { |
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
shape confirmations(sharing: &Sharing) -> Vec<Node>; |
| 559 |
|
| 560 |
section "Waiting on you to admit them" unless sharing.confirmations.is_empty(); |
| 561 |
|
| 562 |
list { |
| 563 |
for confirmation in sharing.confirmations.iter() { |
| 564 |
include confirmation_row(confirmation); |
| 565 |
} |
| 566 |
} unless sharing.confirmations.is_empty(); |
| 567 |
|
| 568 |
for at in sharing.confirmations_refreshed.iter() { |
| 569 |
text "This list was last refreshed {at}. Somebody who accepted since then is not \ |
| 570 |
here yet." |
| 571 |
unless sharing.confirmations.is_empty(); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
declare! { |
| 576 |
|
| 577 |
shape queued_row(op: &Queued) -> Row; |
| 578 |
|
| 579 |
row &op.doing { |
| 580 |
token Tag::badge("Done").tone(Tone::Success) when op.done; |
| 581 |
token Tag::badge("Waiting") when not op.done and op.failed is None; |
| 582 |
|
| 583 |
for failed in op.failed.iter() { |
| 584 |
token Tag::badge("Failed").tone(Tone::Danger) unless op.done; |
| 585 |
secondary failed unless op.done; |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
menu [Act::new("Cancel", Action::post("/settings/sharing/queue/{op.id}/cancel")) |
| 591 |
.tone(Tone::Danger)] |
| 592 |
unless op.done; |
| 593 |
} |
| 594 |
} |
| 595 |
|
| 596 |
declare! { |
| 597 |
|
| 598 |
|
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
shape queue(sharing: &Sharing) -> Vec<Node>; |
| 604 |
|
| 605 |
section "Waiting to reach the server" unless sharing.queued.is_empty(); |
| 606 |
|
| 607 |
list { |
| 608 |
for op in sharing.queued.iter() { |
| 609 |
include queued_row(op); |
| 610 |
} |
| 611 |
} unless sharing.queued.is_empty(); |
| 612 |
} |
| 613 |
|
| 614 |
declare! { |
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
shape admin_acts(sharing: &Sharing) -> Vec<Node>; |
| 622 |
|
| 623 |
section "Make a group" when sharing.configured; |
| 624 |
|
| 625 |
form post "/settings/sharing/groups" when sharing.configured { |
| 626 |
submit "Queue it"; |
| 627 |
|
| 628 |
field Text "name" "Group name" { |
| 629 |
required; |
| 630 |
hint "It is created within a minute, and waits if you are offline."; |
| 631 |
} |
| 632 |
} |
| 633 |
|
| 634 |
section "Admit somebody" when sharing.configured and not sharing.administered.is_empty(); |
| 635 |
|
| 636 |
text "Ask them for the public key their own Sharing section shows, and check its \ |
| 637 |
fingerprint with them out of band. The key admits them to nothing until you \ |
| 638 |
seal the group key to it, which is what this does." |
| 639 |
when sharing.configured and not sharing.administered.is_empty(); |
| 640 |
|
| 641 |
form post "/settings/sharing/members" |
| 642 |
when sharing.configured and not sharing.administered.is_empty() { |
| 643 |
submit "Queue it"; |
| 644 |
|
| 645 |
field Select "group_id" "Group" { |
| 646 |
options sharing.administered.clone(); |
| 647 |
required; |
| 648 |
} |
| 649 |
|
| 650 |
field Email "email" "Their address" { |
| 651 |
required; |
| 652 |
} |
| 653 |
|
| 654 |
field Text "pubkey" "Their public key" { |
| 655 |
required; |
| 656 |
hint "The long value from their Sharing section, pasted whole."; |
| 657 |
} |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
section "Or hand out an invite code" |
| 666 |
when sharing.configured and not sharing.administered.is_empty(); |
| 667 |
|
| 668 |
text "They paste the code into their own Sharing section. It admits them to nothing \ |
| 669 |
on its own: it puts them in front of you, with a fingerprint to check." |
| 670 |
when sharing.configured and not sharing.administered.is_empty(); |
| 671 |
|
| 672 |
form post "/settings/sharing/invites" |
| 673 |
when sharing.configured and not sharing.administered.is_empty() { |
| 674 |
submit "Queue it"; |
| 675 |
|
| 676 |
field Select "group_id" "Group" { |
| 677 |
options sharing.administered.clone(); |
| 678 |
required; |
| 679 |
} |
| 680 |
|
| 681 |
field Number "expires_in_hours" "Hours it stays usable" { |
| 682 |
at_least "1"; |
| 683 |
hint "Left blank, the server's own default stands."; |
| 684 |
} |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
declare! { |
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
shape join(sharing: &Sharing) -> Vec<Node>; |
| 699 |
|
| 700 |
section "Joining a group you were invited to" when sharing.configured; |
| 701 |
|
| 702 |
form post "/settings/sharing/invites/preview" |
| 703 |
when sharing.configured and sharing.held.is_none() { |
| 704 |
submit "Read it"; |
| 705 |
|
| 706 |
field Text "token" "Invite code" { |
| 707 |
required; |
| 708 |
hint "Reading the code and accepting it are two queued writes, so it can be \ |
| 709 |
two minutes before you are asked to accept. Nothing is sent until you do."; |
| 710 |
} |
| 711 |
} |
| 712 |
|
| 713 |
for held in sharing.held.iter() { |
| 714 |
extend held_invite(held) when sharing.configured; |
| 715 |
} |
| 716 |
} |
| 717 |
|
| 718 |
declare! { |
| 719 |
|
| 720 |
shape held_invite(held: &Held) -> Vec<Node>; |
| 721 |
|
| 722 |
list { |
| 723 |
row &held.group when held.usable { |
| 724 |
secondary "Invited by {held.inviter}."; |
| 725 |
meta "Usable until {held.expires}"; |
| 726 |
} |
| 727 |
} when held.usable; |
| 728 |
|
| 729 |
text "Accepting sends this device's public key. You are not in the group yet at that \ |
| 730 |
point: it appears once the group's admin has checked your fingerprint against \ |
| 731 |
what you read out to them." |
| 732 |
when held.usable; |
| 733 |
|
| 734 |
empty &held.refused unless held.usable; |
| 735 |
|
| 736 |
list { |
| 737 |
row "Accept it" when held.usable { |
| 738 |
act "Accept" to post "/settings/sharing/invites/accept"; |
| 739 |
act "Dismiss" to post "/settings/sharing/invites/dismiss" { |
| 740 |
tone Danger; |
| 741 |
} |
| 742 |
} |
| 743 |
|
| 744 |
row "Nothing to accept" unless held.usable { |
| 745 |
act "Dismiss" to post "/settings/sharing/invites/dismiss"; |
| 746 |
} |
| 747 |
} |
| 748 |
} |
| 749 |
|
| 750 |
declare! { |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
|
| 758 |
shape identity_section(identity: &Identity) -> Vec<Node>; |
| 759 |
|
| 760 |
section "Your identity key"; |
| 761 |
|
| 762 |
given identity { |
| 763 |
Identity::Unconfigured -> empty "Sync is not set up on this device, so there is \ |
| 764 |
no identity key yet. A key is derived from your \ |
| 765 |
encryption master key."; |
| 766 |
Identity::Locked -> empty "Encryption is not set up yet, so there is no identity \ |
| 767 |
key to show."; |
| 768 |
otherwise -> text "Give this to a group's admin and they can add you. It is a \ |
| 769 |
public key: it admits you to nothing on its own."; |
| 770 |
} |
| 771 |
|
| 772 |
for pubkey in identity.pubkey().into_iter() { |
| 773 |
include pubkey_field(pubkey); |
| 774 |
} |
| 775 |
|
| 776 |
for fingerprint in identity.fingerprint().into_iter() { |
| 777 |
include fingerprint_field(fingerprint); |
| 778 |
} |
| 779 |
} |
| 780 |
|
| 781 |
declare! { |
| 782 |
|
| 783 |
shape pubkey_field(pubkey: &str) -> Field; |
| 784 |
|
| 785 |
field Text "identity_pubkey" "Your public key" { |
| 786 |
value pubkey; |
| 787 |
hint "Read the fingerprint below out loud to check it arrived intact."; |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
declare! { |
| 792 |
|
| 793 |
shape fingerprint_field(fingerprint: &str) -> Field; |
| 794 |
|
| 795 |
field Text "identity_fingerprint" "Fingerprint" { |
| 796 |
value fingerprint; |
| 797 |
} |
| 798 |
} |
| 799 |
|
| 800 |
impl Identity { |
| 801 |
|
| 802 |
|
| 803 |
fn pubkey(&self) -> Option<&str> { |
| 804 |
match self { |
| 805 |
Self::Held { pubkey, .. } => Some(pubkey), |
| 806 |
_ => None, |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
fn fingerprint(&self) -> Option<&str> { |
| 812 |
match self { |
| 813 |
Self::Held { fingerprint, .. } => Some(fingerprint), |
| 814 |
_ => None, |
| 815 |
} |
| 816 |
} |
| 817 |
} |
| 818 |
|
| 819 |
declare! { |
| 820 |
|
| 821 |
pub(super) shape sharing_pane(sharing: &Sharing) -> Vec<Node>; |
| 822 |
|
| 823 |
section "Sharing"; |
| 824 |
|
| 825 |
|
| 826 |
extend confirmations(sharing); |
| 827 |
|
| 828 |
empty "This device knows of no groups. A group list arrives with a sync, so a device \ |
| 829 |
that has not synced since you joined one will not show it yet." |
| 830 |
when sharing.groups.is_empty(); |
| 831 |
|
| 832 |
for group in sharing.groups.iter() { |
| 833 |
extend group_rows(group); |
| 834 |
} |
| 835 |
|
| 836 |
for at in sharing.refreshed.iter() { |
| 837 |
text "Group list last updated {at}." unless sharing.groups.is_empty(); |
| 838 |
} |
| 839 |
|
| 840 |
extend queue(sharing); |
| 841 |
extend admin_acts(sharing); |
| 842 |
extend join(sharing); |
| 843 |
extend identity_section(&sharing.identity); |
| 844 |
} |
| 845 |
|
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
pub(super) fn create_group( |
| 852 |
app: &AppState, |
| 853 |
request: &quasi_router::Request, |
| 854 |
) -> Result<&'static str, RouteError> { |
| 855 |
let name = request |
| 856 |
.payload |
| 857 |
.get("name") |
| 858 |
.unwrap_or_default() |
| 859 |
.trim() |
| 860 |
.to_owned(); |
| 861 |
if name.is_empty() { |
| 862 |
return Err(RouteError::conflict("A group needs a name.")); |
| 863 |
} |
| 864 |
|
| 865 |
crate::group_queue::enqueue( |
| 866 |
app, |
| 867 |
&crate::group_queue::QueuedOp { |
| 868 |
id: uuid::Uuid::new_v4().to_string(), |
| 869 |
kind: "create_group".to_owned(), |
| 870 |
group_id: None, |
| 871 |
name: Some(name), |
| 872 |
email: None, |
| 873 |
pubkey: None, |
| 874 |
member_user_id: None, |
| 875 |
..Default::default() |
| 876 |
}, |
| 877 |
) |
| 878 |
.map_err(RouteError::internal)?; |
| 879 |
|
| 880 |
Ok("Queued. The group is created within a minute, or when you are next online.") |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
pub(super) fn add_member( |
| 889 |
app: &AppState, |
| 890 |
request: &quasi_router::Request, |
| 891 |
) -> Result<&'static str, RouteError> { |
| 892 |
let field = |name: &str| { |
| 893 |
request |
| 894 |
.payload |
| 895 |
.get(name) |
| 896 |
.unwrap_or_default() |
| 897 |
.trim() |
| 898 |
.to_owned() |
| 899 |
}; |
| 900 |
let (group_id, email, pubkey) = (field("group_id"), field("email"), field("pubkey")); |
| 901 |
|
| 902 |
if group_id.is_empty() || email.is_empty() || pubkey.is_empty() { |
| 903 |
return Err(RouteError::conflict( |
| 904 |
"A group, an address and a public key are all needed.", |
| 905 |
)); |
| 906 |
} |
| 907 |
|
| 908 |
|
| 909 |
if synckit_client::identity::IdentityPublicKey::fingerprint_of_base64(&pubkey).is_err() { |
| 910 |
return Err(RouteError::conflict( |
| 911 |
"That does not look like a public key.", |
| 912 |
)); |
| 913 |
} |
| 914 |
|
| 915 |
crate::group_queue::enqueue( |
| 916 |
app, |
| 917 |
&crate::group_queue::QueuedOp { |
| 918 |
id: uuid::Uuid::new_v4().to_string(), |
| 919 |
kind: "add_member".to_owned(), |
| 920 |
group_id: Some(group_id), |
| 921 |
name: None, |
| 922 |
email: Some(email), |
| 923 |
pubkey: Some(pubkey), |
| 924 |
member_user_id: None, |
| 925 |
..Default::default() |
| 926 |
}, |
| 927 |
) |
| 928 |
.map_err(RouteError::internal)?; |
| 929 |
|
| 930 |
Ok("Queued. They are admitted within a minute, once your encryption key is loaded.") |
| 931 |
} |
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
pub(super) fn create_invite( |
| 939 |
app: &AppState, |
| 940 |
request: &quasi_router::Request, |
| 941 |
) -> Result<&'static str, RouteError> { |
| 942 |
let group_id = request |
| 943 |
.payload |
| 944 |
.get("group_id") |
| 945 |
.unwrap_or_default() |
| 946 |
.trim() |
| 947 |
.to_owned(); |
| 948 |
if group_id.is_empty() { |
| 949 |
return Err(RouteError::conflict("An invite needs a group.")); |
| 950 |
} |
| 951 |
|
| 952 |
let raw = request |
| 953 |
.payload |
| 954 |
.get("expires_in_hours") |
| 955 |
.unwrap_or_default() |
| 956 |
.trim() |
| 957 |
.to_owned(); |
| 958 |
let expires_in_hours = if raw.is_empty() { |
| 959 |
None |
| 960 |
} else { |
| 961 |
Some( |
| 962 |
raw.parse::<i64>() |
| 963 |
.ok() |
| 964 |
.filter(|hours| *hours > 0) |
| 965 |
.ok_or_else(|| { |
| 966 |
RouteError::conflict("Hours has to be a whole number above zero, or blank.") |
| 967 |
})?, |
| 968 |
) |
| 969 |
}; |
| 970 |
|
| 971 |
crate::group_queue::enqueue( |
| 972 |
app, |
| 973 |
&crate::group_queue::QueuedOp { |
| 974 |
id: uuid::Uuid::new_v4().to_string(), |
| 975 |
kind: "create_invite".to_owned(), |
| 976 |
group_id: Some(group_id), |
| 977 |
expires_in_hours, |
| 978 |
..Default::default() |
| 979 |
}, |
| 980 |
) |
| 981 |
.map_err(RouteError::internal)?; |
| 982 |
|
| 983 |
Ok("Queued. The code appears under the group within a minute.") |
| 984 |
} |
| 985 |
|
| 986 |
|
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
pub(super) fn revoke_invite( |
| 992 |
app: &AppState, |
| 993 |
request: &quasi_router::Request, |
| 994 |
) -> Result<&'static str, RouteError> { |
| 995 |
let (group_id, invitation_id) = addressed(request)?; |
| 996 |
crate::group_queue::enqueue( |
| 997 |
app, |
| 998 |
&crate::group_queue::QueuedOp { |
| 999 |
id: uuid::Uuid::new_v4().to_string(), |
| 1000 |
kind: "revoke_invite".to_owned(), |
| 1001 |
group_id: Some(group_id), |
| 1002 |
invitation_id: Some(invitation_id), |
| 1003 |
..Default::default() |
| 1004 |
}, |
| 1005 |
) |
| 1006 |
.map_err(RouteError::internal)?; |
| 1007 |
|
| 1008 |
Ok("Queued. The invitation is cancelled within a minute.") |
| 1009 |
} |
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
pub(super) fn confirm_invite( |
| 1018 |
app: &AppState, |
| 1019 |
request: &quasi_router::Request, |
| 1020 |
) -> Result<&'static str, RouteError> { |
| 1021 |
let (group_id, invitation_id) = addressed(request)?; |
| 1022 |
crate::group_queue::enqueue( |
| 1023 |
app, |
| 1024 |
&crate::group_queue::QueuedOp { |
| 1025 |
id: uuid::Uuid::new_v4().to_string(), |
| 1026 |
kind: "confirm_invite".to_owned(), |
| 1027 |
group_id: Some(group_id), |
| 1028 |
invitation_id: Some(invitation_id), |
| 1029 |
..Default::default() |
| 1030 |
}, |
| 1031 |
) |
| 1032 |
.map_err(RouteError::internal)?; |
| 1033 |
|
| 1034 |
Ok("Queued. They are admitted within a minute, once your encryption key is loaded.") |
| 1035 |
} |
| 1036 |
|
| 1037 |
|
| 1038 |
fn addressed(request: &quasi_router::Request) -> Result<(String, String), RouteError> { |
| 1039 |
let capture = |name: &str| { |
| 1040 |
request |
| 1041 |
.captures |
| 1042 |
.get(name) |
| 1043 |
.filter(|value| !value.is_empty()) |
| 1044 |
.map(ToOwned::to_owned) |
| 1045 |
.ok_or_else(|| RouteError::not_found("no such invitation")) |
| 1046 |
}; |
| 1047 |
Ok((capture("group_id")?, capture("invitation_id")?)) |
| 1048 |
} |
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
pub(super) fn preview_invite( |
| 1056 |
app: &AppState, |
| 1057 |
request: &quasi_router::Request, |
| 1058 |
) -> Result<&'static str, RouteError> { |
| 1059 |
let token = normalize_invite_token(request.payload.get("token").unwrap_or_default()); |
| 1060 |
if token.is_empty() { |
| 1061 |
return Err(RouteError::conflict("Paste the code you were given.")); |
| 1062 |
} |
| 1063 |
|
| 1064 |
crate::group_queue::enqueue( |
| 1065 |
app, |
| 1066 |
&crate::group_queue::QueuedOp { |
| 1067 |
id: uuid::Uuid::new_v4().to_string(), |
| 1068 |
kind: "preview_invite".to_owned(), |
| 1069 |
invite_token: Some(token), |
| 1070 |
..Default::default() |
| 1071 |
}, |
| 1072 |
) |
| 1073 |
.map_err(RouteError::internal)?; |
| 1074 |
|
| 1075 |
Ok("Queued. What the code leads to appears here within a minute, before anything is sent.") |
| 1076 |
} |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
pub(super) fn accept_invite(app: &AppState) -> Result<&'static str, RouteError> { |
| 1084 |
let conn = conn(app)?; |
| 1085 |
let held = |
| 1086 |
directory::preview(&conn).map_err(|error| RouteError::internal(error.to_string()))?; |
| 1087 |
drop(conn); |
| 1088 |
|
| 1089 |
let preview = held.ok_or_else(|| { |
| 1090 |
RouteError::not_found("There is no invite code waiting to be accepted here.") |
| 1091 |
})?; |
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
if !preview.redeemable { |
| 1096 |
return Err(RouteError::conflict( |
| 1097 |
"That code cannot be used any more. Dismiss it and ask for another.", |
| 1098 |
)); |
| 1099 |
} |
| 1100 |
|
| 1101 |
crate::group_queue::enqueue( |
| 1102 |
app, |
| 1103 |
&crate::group_queue::QueuedOp { |
| 1104 |
id: uuid::Uuid::new_v4().to_string(), |
| 1105 |
kind: "accept_invite".to_owned(), |
| 1106 |
invite_token: Some(preview.token), |
| 1107 |
..Default::default() |
| 1108 |
}, |
| 1109 |
) |
| 1110 |
.map_err(RouteError::internal)?; |
| 1111 |
|
| 1112 |
Ok("Queued. Your key is sent within a minute; the group appears once its admin confirms it.") |
| 1113 |
} |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
pub(super) fn dismiss_preview(app: &AppState) -> Result<&'static str, RouteError> { |
| 1121 |
let conn = conn(app)?; |
| 1122 |
directory::clear_preview(&conn).map_err(|error| RouteError::internal(error.to_string()))?; |
| 1123 |
Ok("Forgotten.") |
| 1124 |
} |
| 1125 |
|
| 1126 |
|
| 1127 |
pub(super) fn cancel( |
| 1128 |
app: &AppState, |
| 1129 |
request: &quasi_router::Request, |
| 1130 |
) -> Result<&'static str, RouteError> { |
| 1131 |
let id = request |
| 1132 |
.captures |
| 1133 |
.get("id") |
| 1134 |
.ok_or_else(|| RouteError::not_found("no queued action"))?; |
| 1135 |
if crate::group_queue::cancel(app, id).map_err(RouteError::internal)? { |
| 1136 |
Ok("Taken back out of the queue.") |
| 1137 |
} else { |
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
Err(RouteError::not_found( |
| 1142 |
"That action is not in the queue any more.", |
| 1143 |
)) |
| 1144 |
} |
| 1145 |
} |
| 1146 |
|