Reset a forgotten password by mail; a personal feed; board search; multi-file upload
Browse files at this commit5bbedf2babff41865d3907b6789b18f35aa852d0Parent: a3f338421 changed files +1851 -123
- crates/izlek-core/migrations/0001_init.sql +6 -2
- crates/izlek-core/src/accounts.rs +150 -3
- crates/izlek-core/src/board.rs +27 -0
- crates/izlek-core/src/mail.rs +38 -0
- crates/izlek-core/src/store/mod.rs +53 -3
- crates/izlek-core/src/store/reconcile.rs +39 -18
- crates/izlek-core/src/store/turso_store.rs +157 -7
- crates/izlek-core/tests/store.rs +449 -4
- crates/izlek-web/src/auth.rs +105 -0
- crates/izlek-web/src/board.rs +51 -19
- crates/izlek-web/src/detail.rs +2 -9
- crates/izlek-web/src/feed.rs +111 -0
- crates/izlek-web/src/files.rs +104 -40
- crates/izlek-web/src/i18n.rs +40 -0
- crates/izlek-web/src/layout.rs +34 -9
- crates/izlek-web/src/lib.rs +1 -0
- crates/izlek-web/src/logs.rs +4 -1
- crates/izlek-web/src/pages.rs +122 -0
- crates/izlek-web/src/people.rs +6 -0
- crates/izlek-web/tests/browser/moment.mjs +23 -8
- crates/izlek-web/tests/http.rs +329 -0
crates/izlek-core/migrations/0001_init.sqlmodified+6 -2
| @@ -106,7 +106,10 @@ CREATE TABLE user ( | ||
| 106 | 106 | photo BLOB, |
| 107 | 107 | photo_mime TEXT, |
| 108 | 108 | created_at TEXT NOT NULL, |
| 109 | - last_signed_in_at TEXT | |
| 109 | + last_signed_in_at TEXT, | |
| 110 | + -- Where the person's "what changed for me" feed has been read to. NULL | |
| 111 | + -- until their first visit, which reads every line as news. | |
| 112 | + feed_seen_at TEXT | |
| 110 | 113 | ); |
| 111 | 114 | CREATE UNIQUE INDEX user_email_unique ON user(workspace_id, email); |
| 112 | 115 | |
| @@ -126,7 +129,8 @@ CREATE TABLE signin_link ( | ||
| 126 | 129 | token_hash TEXT NOT NULL, |
| 127 | 130 | created_at TEXT NOT NULL, |
| 128 | 131 | expires_at TEXT NOT NULL, |
| 129 | - used_at TEXT | |
| 132 | + used_at TEXT, | |
| 133 | + kind TEXT NOT NULL DEFAULT 'join' | |
| 130 | 134 | ); |
| 131 | 135 | CREATE UNIQUE INDEX signin_link_token ON signin_link(token_hash); |
| 132 | 136 |
crates/izlek-core/src/accounts.rsmodified+150 -3
| @@ -17,7 +17,9 @@ use time::{Duration, OffsetDateTime}; | ||
| 17 | 17 | |
| 18 | 18 | use crate::Role; |
| 19 | 19 | use crate::auth::{self, AuthError, PasswordProblem, Token, hash_password, verify_password}; |
| 20 | -use crate::store::{NewUser, Session, Store, StoreError, User, Workspace}; | |
| 20 | +use crate::store::{ | |
| 21 | + LinkKind, NewUser, Session, Store, StoreError, User, Workspace, | |
| 22 | +}; | |
| 21 | 23 | |
| 22 | 24 | /// How long a first-sign-in link is good for. The mockups say seven days, and |
| 23 | 25 | /// an expired link is not a dead account: resending opens the same one. |
| @@ -230,7 +232,7 @@ impl Accounts { | ||
| 230 | 232 | let token = Token::mint(); |
| 231 | 233 | let expires_at = OffsetDateTime::now_utc() + SIGNIN_LINK_LIFETIME; |
| 232 | 234 | self.store |
| 233 | - .create_signin_link(&user.id, &token.hash(), expires_at) | |
| 235 | + .create_signin_link(&user.id, &token.hash(), expires_at, LinkKind::Join) | |
| 234 | 236 | .await?; |
| 235 | 237 | let subject = "Your İzlek sign-in link"; |
| 236 | 238 | let body = format!( |
| @@ -294,6 +296,13 @@ impl Accounts { | ||
| 294 | 296 | return Err(AccountError::Rejected); |
| 295 | 297 | } |
| 296 | 298 | }; |
| 299 | + // A reset link is not an invitation: the flow that spends it checks | |
| 300 | + // the password rules itself and signs the other browsers out. Same | |
| 301 | + // answer, same work. | |
| 302 | + if link.kind != LinkKind::Join { | |
| 303 | + let _ = hash_password(password); | |
| 304 | + return Err(AccountError::Rejected); | |
| 305 | + } | |
| 297 | 306 | |
| 298 | 307 | // The rules are checked before the link is spent, so a rejected |
| 299 | 308 | // password does not burn the invitation. |
| @@ -319,7 +328,121 @@ impl Accounts { | ||
| 319 | 328 | self.start_session(user).await |
| 320 | 329 | } |
| 321 | 330 | |
| 322 | - // -- signing in -------------------------------------------------------- | |
| 331 | + // -- forgot password --------------------------------------------------- | |
| 332 | + | |
| 333 | + /// Mails a password-reset link to the address, if the address has an | |
| 334 | + /// account. The answer is the same whether it does or not: the miss path | |
| 335 | + /// pays for an Argon2 hash the way sign-in's pays for a verify, and only | |
| 336 | + /// the queued mail knows the difference. The token travels in the mail | |
| 337 | + /// and nowhere else — this method hands it to nobody. | |
| 338 | + /// | |
| 339 | + /// The client bucket refuses outright, like sign-in's. The address bucket | |
| 340 | + /// counts but stays silent: refusing by name would hand anyone who knows | |
| 341 | + /// a colleague's address a lock-out, so past the limit the request simply | |
| 342 | + /// mints nothing and answers the same success. | |
| 343 | + pub async fn request_password_reset(&self, email: &str, client: &str) -> Result<()> { | |
| 344 | + let now = OffsetDateTime::now_utc(); | |
| 345 | + let client_bucket = format!("client:{client}"); | |
| 346 | + self.check_rate(&client_bucket).await?; | |
| 347 | + self.store.record_auth_attempt(&client_bucket, now).await?; | |
| 348 | + | |
| 349 | + let address_bucket = format!("address:{}", email.trim().to_lowercase()); | |
| 350 | + let address_over_limit = self.over_limit(&address_bucket).await?; | |
| 351 | + self.store.record_auth_attempt(&address_bucket, now).await?; | |
| 352 | + | |
| 353 | + let user = match self.store.workspace().await? { | |
| 354 | + Some(workspace) => self.store.user_by_email(&workspace.id, email).await?, | |
| 355 | + None => None, | |
| 356 | + }; | |
| 357 | + let Some(user) = user else { | |
| 358 | + // Same work as the minting path, minus the effect. | |
| 359 | + let _ = hash_password(email); | |
| 360 | + return Ok(()); | |
| 361 | + }; | |
| 362 | + if address_over_limit { | |
| 363 | + // The request counts, the mail does not go: the answer above is | |
| 364 | + // still the answer a working reset gets. | |
| 365 | + return Ok(()); | |
| 366 | + } | |
| 367 | + | |
| 368 | + let token = Token::mint(); | |
| 369 | + let expires_at = now + SIGNIN_LINK_LIFETIME; | |
| 370 | + self.store | |
| 371 | + .create_signin_link(&user.id, &token.hash(), expires_at, LinkKind::Reset) | |
| 372 | + .await?; | |
| 373 | + let (subject, body) = reset_mail(&user, self.base().await?, &token.expose()); | |
| 374 | + self.store | |
| 375 | + .queue_notice(&user.email, &subject, &body, now) | |
| 376 | + .await?; | |
| 377 | + Ok(()) | |
| 378 | + } | |
| 379 | + | |
| 380 | + /// Redeems a reset link and sets the password the person chose, signing | |
| 381 | + /// every other browser out. Everything the invitation redeem refuses, | |
| 382 | + /// this refuses the same way — an unknown, expired, spent or wrong-kind | |
| 383 | + /// link is one [`AccountError::Rejected`], and every miss pays for a | |
| 384 | + /// hash. The limit is bucketed on the client, exactly as there. | |
| 385 | + pub async fn redeem_reset_link( | |
| 386 | + &self, | |
| 387 | + presented: &str, | |
| 388 | + password: &str, | |
| 389 | + client: &str, | |
| 390 | + ) -> Result<SignedIn> { | |
| 391 | + let now = OffsetDateTime::now_utc(); | |
| 392 | + let client_bucket = format!("client:{client}"); | |
| 393 | + self.check_rate(&client_bucket).await?; | |
| 394 | + self.store.record_auth_attempt(&client_bucket, now).await?; | |
| 395 | + let digest = auth::hash_token(presented); | |
| 396 | + | |
| 397 | + let link = match self.store.signin_link_by_hash(&digest).await? { | |
| 398 | + Some(link) => link, | |
| 399 | + None => { | |
| 400 | + let _ = hash_password(password); | |
| 401 | + return Err(AccountError::Rejected); | |
| 402 | + } | |
| 403 | + }; | |
| 404 | + if !link.is_usable(now) { | |
| 405 | + let _ = hash_password(password); | |
| 406 | + return Err(AccountError::Rejected); | |
| 407 | + } | |
| 408 | + let user = match self.store.user(&link.user_id).await? { | |
| 409 | + Some(user) => user, | |
| 410 | + None => { | |
| 411 | + let _ = hash_password(password); | |
| 412 | + return Err(AccountError::Rejected); | |
| 413 | + } | |
| 414 | + }; | |
| 415 | + // An invitation is not a reset: it carries no expiry of the other | |
| 416 | + // browsers, so it must not be spendable on this screen. | |
| 417 | + if link.kind != LinkKind::Reset { | |
| 418 | + let _ = hash_password(password); | |
| 419 | + return Err(AccountError::Rejected); | |
| 420 | + } | |
| 421 | + | |
| 422 | + // The rules are checked before the link is spent, so a rejected | |
| 423 | + // password does not burn it. | |
| 424 | + auth::check_password(password, &user.email, &user.display_name)?; | |
| 425 | + let hash = hash_password(password)?; | |
| 426 | + | |
| 427 | + if !self.store.consume_signin_link(&link.id, now).await? { | |
| 428 | + return Err(AccountError::Rejected); | |
| 429 | + } | |
| 430 | + | |
| 431 | + self.store.set_password_hash(&user.id, &hash).await?; | |
| 432 | + // The charter the password change lives by: the new password signs | |
| 433 | + // the other browsers out, all of them. | |
| 434 | + self.store | |
| 435 | + .revoke_sessions_for_user(&user.id, now) | |
| 436 | + .await?; | |
| 437 | + self.store.mark_signed_in(&user.id, now).await?; | |
| 438 | + self.store.clear_auth_attempts(&client_bucket).await?; | |
| 439 | + let user = self | |
| 440 | + .store | |
| 441 | + .user(&user.id) | |
| 442 | + .await? | |
| 443 | + .ok_or(StoreError::NotFound)?; | |
| 444 | + self.start_session(user).await | |
| 445 | + } | |
| 323 | 446 | |
| 324 | 447 | /// Signs a person in. `client` is whatever identifies the caller's machine |
| 325 | 448 | /// — it only ever becomes a rate-limit bucket. |
| @@ -506,3 +629,27 @@ impl Accounts { | ||
| 506 | 629 | Ok(self.store.count_auth_attempts(bucket, since).await? >= RATE_LIMIT) |
| 507 | 630 | } |
| 508 | 631 | } |
| 632 | + | |
| 633 | +/// The reset mail, in the account's own language. Terse like the invitation's: | |
| 634 | +/// what happened, the link, the once-only fact. | |
| 635 | +fn reset_mail(user: &User, base: String, token: &str) -> (String, String) { | |
| 636 | + if user.language == "tr" { | |
| 637 | + ( | |
| 638 | + "İzlek parolanı sıfırla".to_string(), | |
| 639 | + format!( | |
| 640 | + "Bu adres için bir İzlek parola sıfırlaması istendi.\n\n\ | |
| 641 | + {base}/reset/{token}\n\n\ | |
| 642 | + Bağlantı bir kez çalışır ve 7 gün içinde geçersiz olur.\n" | |
| 643 | + ), | |
| 644 | + ) | |
| 645 | + } else { | |
| 646 | + ( | |
| 647 | + "Reset your İzlek password".to_string(), | |
| 648 | + format!( | |
| 649 | + "A reset was asked for the İzlek password on this address.\n\n\ | |
| 650 | + {base}/reset/{token}\n\n\ | |
| 651 | + The link works once and expires in 7 days.\n" | |
| 652 | + ), | |
| 653 | + ) | |
| 654 | + } | |
| 655 | +} |
crates/izlek-core/src/board.rsmodified+27 -0
| @@ -274,6 +274,33 @@ impl BoardView { | ||
| 274 | 274 | pub fn blocked_count(&self) -> usize { |
| 275 | 275 | self.cards().filter(|card| card.is_blocked()).count() |
| 276 | 276 | } |
| 277 | + | |
| 278 | + /// Keeps only the cards whose key or title answers `query`: a literal, | |
| 279 | + /// case-insensitive substring of either. The human key typed in any | |
| 280 | + /// case matches; a fragment of a title matches; nothing is | |
| 281 | + /// pattern-matched, so there are no wildcards to escape. | |
| 282 | + pub fn searching(&mut self, query: &str) { | |
| 283 | + let needle = query.to_lowercase(); | |
| 284 | + for column in &mut self.columns { | |
| 285 | + column.cards.retain(|card| { | |
| 286 | + card.task_key.to_lowercase().contains(&needle) | |
| 287 | + || card.title.to_lowercase().contains(&needle) | |
| 288 | + }); | |
| 289 | + } | |
| 290 | + } | |
| 291 | + | |
| 292 | + /// Keeps only the cards wearing `tag`, when one is named — the Project | |
| 293 | + /// filter, riding the same narrowing pass as [`BoardView::searching`]. | |
| 294 | + pub fn tagged(&mut self, tag_id: Option<&str>) { | |
| 295 | + let Some(tag_id) = tag_id else { | |
| 296 | + return; | |
| 297 | + }; | |
| 298 | + for column in &mut self.columns { | |
| 299 | + column | |
| 300 | + .cards | |
| 301 | + .retain(|card| card.tag.as_ref().is_some_and(|tag| tag.id == tag_id)); | |
| 302 | + } | |
| 303 | + } | |
| 277 | 304 | } |
| 278 | 305 | |
| 279 | 306 | /// `Sep 12` — the card chips are day-and-month, never a year. |
crates/izlek-core/src/mail.rsmodified+38 -0
| @@ -759,6 +759,12 @@ impl Engine { | ||
| 759 | 759 | for one in sends { |
| 760 | 760 | self.store.record_send_accepted(&one.id, now).await?; |
| 761 | 761 | } |
| 762 | + // A reminder that went out is a fact of the meeting's card, | |
| 763 | + // and a fact of the person's feed: the system did the | |
| 764 | + // reminding, the recipient is the line's subject. | |
| 765 | + for one in sends.iter().filter(|one| one.kind == SendKind::Reminder) { | |
| 766 | + self.record_reminded(one, now).await?; | |
| 767 | + } | |
| 762 | 768 | report.sent += 1; |
| 763 | 769 | } |
| 764 | 770 | Err(problem) if !problem.attempted => { |
| @@ -792,6 +798,38 @@ impl Engine { | ||
| 792 | 798 | } |
| 793 | 799 | Ok(()) |
| 794 | 800 | } |
| 801 | + /// The trail line a delivered reminder writes: on the meeting's card, with | |
| 802 | + /// the system as the actor and the recipient as the subject, so the feed | |
| 803 | + /// a person reads carries the reminder they were sent. A recipient whose | |
| 804 | + /// account has since gone still gets the line — about nobody. | |
| 805 | + async fn record_reminded( | |
| 806 | + &self, | |
| 807 | + send: &ClaimedSend, | |
| 808 | + at: OffsetDateTime, | |
| 809 | + ) -> crate::store::Result<()> { | |
| 810 | + let Some(task_id) = send.task_id.as_deref() else { | |
| 811 | + return Ok(()); | |
| 812 | + }; | |
| 813 | + let subject = match self.store.workspace().await? { | |
| 814 | + Some(workspace) => self | |
| 815 | + .store | |
| 816 | + .user_by_email(&workspace.id, &send.recipient) | |
| 817 | + .await? | |
| 818 | + .map(|user| user.id), | |
| 819 | + None => None, | |
| 820 | + }; | |
| 821 | + self.store | |
| 822 | + .record_activity( | |
| 823 | + task_id, | |
| 824 | + None, | |
| 825 | + subject.as_deref(), | |
| 826 | + &ActivityKind::Other("reminded".to_string()), | |
| 827 | + &send.recipient, | |
| 828 | + at, | |
| 829 | + ) | |
| 830 | + .await?; | |
| 831 | + Ok(()) | |
| 832 | + } | |
| 795 | 833 | |
| 796 | 834 | /// The mail itself, built from the facts as they were committed. |
| 797 | 835 | async fn compose( |
crates/izlek-core/src/store/mod.rsmodified+53 -3
| @@ -232,8 +232,36 @@ pub struct UserStats { | ||
| 232 | 232 | pub comments: u32, |
| 233 | 233 | } |
| 234 | 234 | |
| 235 | -/// A first-sign-in link. Only the hash of the token is ever stored; the | |
| 236 | -/// plaintext is shown once, when the link is created or resent. | |
| 235 | +/// What a link is for. The same table and the same token machinery carry | |
| 236 | +/// both; the kind decides which redeeming flow may spend it. | |
| 237 | +#[derive(Debug, Clone, Copy, PartialEq, Eq)] | |
| 238 | +pub enum LinkKind { | |
| 239 | + /// An invited person's first sign-in. | |
| 240 | + Join, | |
| 241 | + /// A self-serve password reset, mailed to the account's own address. | |
| 242 | + Reset, | |
| 243 | +} | |
| 244 | + | |
| 245 | +impl LinkKind { | |
| 246 | + pub fn as_str(&self) -> &'static str { | |
| 247 | + match self { | |
| 248 | + LinkKind::Join => "join", | |
| 249 | + LinkKind::Reset => "reset", | |
| 250 | + } | |
| 251 | + } | |
| 252 | + | |
| 253 | + pub fn from_str(raw: &str) -> Option<LinkKind> { | |
| 254 | + match raw { | |
| 255 | + "join" => Some(LinkKind::Join), | |
| 256 | + "reset" => Some(LinkKind::Reset), | |
| 257 | + _ => None, | |
| 258 | + } | |
| 259 | + } | |
| 260 | +} | |
| 261 | + | |
| 262 | +/// A mailed link — an invitation or a password reset. Only the hash of the | |
| 263 | +/// token is ever stored; the plaintext is shown once, when the link is | |
| 264 | +/// created or mailed. | |
| 237 | 265 | #[derive(Debug, Clone, PartialEq)] |
| 238 | 266 | pub struct SigninLink { |
| 239 | 267 | pub id: String, |
| @@ -241,6 +269,7 @@ pub struct SigninLink { | ||
| 241 | 269 | pub created_at: OffsetDateTime, |
| 242 | 270 | pub expires_at: OffsetDateTime, |
| 243 | 271 | pub used_at: Option<OffsetDateTime>, |
| 272 | + pub kind: LinkKind, | |
| 244 | 273 | } |
| 245 | 274 | |
| 246 | 275 | impl SigninLink { |
| @@ -824,12 +853,13 @@ pub trait Store: BoardReads + DetailReads + 'static { | ||
| 824 | 853 | // -- sign-in links ----------------------------------------------------- |
| 825 | 854 | |
| 826 | 855 | /// Stores the hash of a freshly minted link. The caller keeps the plaintext |
| 827 | - /// and shows it once. | |
| 856 | + /// and shows it once. The kind decides which redeeming flow may spend it. | |
| 828 | 857 | async fn create_signin_link( |
| 829 | 858 | &self, |
| 830 | 859 | user_id: &str, |
| 831 | 860 | token_hash: &str, |
| 832 | 861 | expires_at: OffsetDateTime, |
| 862 | + kind: LinkKind, | |
| 833 | 863 | ) -> Result<SigninLink>; |
| 834 | 864 | |
| 835 | 865 | /// Looks a link up by the hash of the presented token. Returns the link |
| @@ -1335,6 +1365,26 @@ pub trait Store: BoardReads + DetailReads + 'static { | ||
| 1335 | 1365 | cursor: Option<&FeedCursor>, |
| 1336 | 1366 | ) -> Result<u64>; |
| 1337 | 1367 | |
| 1368 | + /// Puts `user_id` on a task's watchers. Idempotent: watching twice is | |
| 1369 | + /// watching once. | |
| 1370 | + async fn watch_task(&self, task_id: &str, user_id: &str) -> Result<()>; | |
| 1371 | + | |
| 1372 | + /// Takes `user_id` off a task's watchers. Absent watch, absent row: no | |
| 1373 | + /// error. | |
| 1374 | + async fn unwatch_task(&self, task_id: &str, user_id: &str) -> Result<()>; | |
| 1375 | + | |
| 1376 | + /// The "what changed for me" feed: activity events on the tasks the user | |
| 1377 | + /// watches, plus events that name them (`subject_id` — an assignment | |
| 1378 | + /// outlives the watch it created), minus the user's own actions. Newest | |
| 1379 | + /// first, capped at `limit`. | |
| 1380 | + async fn feed_for_user(&self, user_id: &str, limit: u32) -> Result<Vec<ActivityLine>>; | |
| 1381 | + | |
| 1382 | + /// How many feed lines have landed since the user last read the feed. | |
| 1383 | + async fn count_feed_unseen(&self, user_id: &str) -> Result<u64>; | |
| 1384 | + | |
| 1385 | + /// Reads the feed to `at`: the unseen count resets, the history stays. | |
| 1386 | + async fn mark_feed_seen(&self, user_id: &str, at: OffsetDateTime) -> Result<()>; | |
| 1387 | + | |
| 1338 | 1388 | // -- who gets mailed --------------------------------------------------- |
| 1339 | 1389 | |
| 1340 | 1390 | /// The people a task points at. Viewers cannot be assigned, so none appear. |
crates/izlek-core/src/store/reconcile.rsmodified+39 -18
| @@ -294,23 +294,30 @@ fn build_maps( | ||
| 294 | 294 | |
| 295 | 295 | maps.push(TableMap { |
| 296 | 296 | name: "user", |
| 297 | - columns: old_cols(&[ | |
| 298 | - "id", | |
| 299 | - "workspace_id", | |
| 300 | - "email", | |
| 301 | - "display_name", | |
| 302 | - "role", | |
| 303 | - "password_hash", | |
| 304 | - "invited_by", | |
| 305 | - "timezone", | |
| 306 | - "theme", | |
| 307 | - "language", | |
| 308 | - "ui", | |
| 309 | - "photo", | |
| 310 | - "photo_mime", | |
| 311 | - "created_at", | |
| 312 | - "last_signed_in_at", | |
| 313 | - ]), | |
| 297 | + columns: { | |
| 298 | + let mut columns = old_cols(&[ | |
| 299 | + "id", | |
| 300 | + "workspace_id", | |
| 301 | + "email", | |
| 302 | + "display_name", | |
| 303 | + "role", | |
| 304 | + "password_hash", | |
| 305 | + "invited_by", | |
| 306 | + "timezone", | |
| 307 | + "theme", | |
| 308 | + "language", | |
| 309 | + "ui", | |
| 310 | + "photo", | |
| 311 | + "photo_mime", | |
| 312 | + "created_at", | |
| 313 | + "last_signed_in_at", | |
| 314 | + ]); | |
| 315 | + // The feed's read marker is new with this schema: no database old | |
| 316 | + // enough to be reconciled ever carried it, so every copied row | |
| 317 | + // starts unseen rather than pretending to have been read. | |
| 318 | + columns.push(("feed_seen_at", "NULL".into())); | |
| 319 | + columns | |
| 320 | + } | |
| 314 | 321 | }); |
| 315 | 322 | maps.push(TableMap { |
| 316 | 323 | name: "workspace_owner", |
| @@ -318,7 +325,21 @@ fn build_maps( | ||
| 318 | 325 | }); |
| 319 | 326 | maps.push(TableMap { |
| 320 | 327 | name: "signin_link", |
| 321 | - columns: old_cols(&["id", "user_id", "token_hash", "created_at", "expires_at", "used_at"]), | |
| 328 | + columns: { | |
| 329 | + let mut columns = old_cols(&[ | |
| 330 | + "id", | |
| 331 | + "user_id", | |
| 332 | + "token_hash", | |
| 333 | + "created_at", | |
| 334 | + "expires_at", | |
| 335 | + "used_at", | |
| 336 | + ]); | |
| 337 | + // The link's kind is new with this schema: every link a database | |
| 338 | + // old enough to be reconciled could carry is an invitation — | |
| 339 | + // resets did not exist to be mailed yet. | |
| 340 | + columns.push(("kind", "'join'".into())); | |
| 341 | + columns | |
| 342 | + } | |
| 322 | 343 | }); |
| 323 | 344 | maps.push(TableMap { |
| 324 | 345 | name: "session", |
crates/izlek-core/src/store/turso_store.rsmodified+157 -7
| @@ -19,7 +19,7 @@ use super::{ | ||
| 19 | 19 | Deletion, Dir, Event, FeedCursor, FeedPage, Freeing, MailDecision, MailOutcome, MailRule, |
| 20 | 20 | MailSend, NewAttachment, NewSender, NewTask, NewUser, Recipient, Result, SendKind, SendState, |
| 21 | 21 | SenderCheck, SenderTest, Session, SigninLink, Store, StoreError, Tag, TaskCreated, Trigger, |
| 22 | - User, UserStats, Workspace, | |
| 22 | + User, UserStats, Workspace, LinkKind, | |
| 23 | 23 | }; |
| 24 | 24 | use super::{reconcile, ReconcileOptions, schema}; |
| 25 | 25 | use crate::Role; |
| @@ -863,6 +863,8 @@ fn signin_link_from(row: &Row) -> Result<SigninLink> { | ||
| 863 | 863 | created_at: parse_stamp(&text(row, 2)?)?, |
| 864 | 864 | expires_at: parse_stamp(&text(row, 3)?)?, |
| 865 | 865 | used_at: opt_stamp(row, 4)?, |
| 866 | + kind: LinkKind::from_str(&text(row, 5)?) | |
| 867 | + .ok_or_else(|| StoreError::Corrupt("signin_link kind".into()))?, | |
| 866 | 868 | }) |
| 867 | 869 | } |
| 868 | 870 | |
| @@ -1482,20 +1484,22 @@ impl Store for TursoStore { | ||
| 1482 | 1484 | user_id: &str, |
| 1483 | 1485 | token_hash: &str, |
| 1484 | 1486 | expires_at: OffsetDateTime, |
| 1487 | + kind: LinkKind, | |
| 1485 | 1488 | ) -> Result<SigninLink> { |
| 1486 | 1489 | let id = Ulid::new().to_string(); |
| 1487 | 1490 | self.conn |
| 1488 | 1491 | .lock() |
| 1489 | 1492 | .await |
| 1490 | 1493 | .execute( |
| 1491 | - "INSERT INTO signin_link (id, user_id, token_hash, created_at, expires_at) \ | |
| 1492 | - VALUES (?1, ?2, ?3, ?4, ?5)", | |
| 1494 | + "INSERT INTO signin_link (id, user_id, token_hash, created_at, expires_at, kind) \ | |
| 1495 | + VALUES (?1, ?2, ?3, ?4, ?5, ?6)", | |
| 1493 | 1496 | params![ |
| 1494 | 1497 | id.clone(), |
| 1495 | 1498 | user_id, |
| 1496 | 1499 | token_hash, |
| 1497 | 1500 | now_text()?, |
| 1498 | - stamp(expires_at)? | |
| 1501 | + stamp(expires_at)?, | |
| 1502 | + kind.as_str(), | |
| 1499 | 1503 | ], |
| 1500 | 1504 | ) |
| 1501 | 1505 | .await |
| @@ -1503,7 +1507,8 @@ impl Store for TursoStore { | ||
| 1503 | 1507 | self.announce([Topic::Members]); |
| 1504 | 1508 | match self |
| 1505 | 1509 | .one_row( |
| 1506 | - "SELECT id, user_id, created_at, expires_at, used_at FROM signin_link WHERE id = ?1", | |
| 1510 | + "SELECT id, user_id, created_at, expires_at, used_at, kind \ | |
| 1511 | + FROM signin_link WHERE id = ?1", | |
| 1507 | 1512 | params![id], |
| 1508 | 1513 | ) |
| 1509 | 1514 | .await? |
| @@ -1516,8 +1521,8 @@ impl Store for TursoStore { | ||
| 1516 | 1521 | async fn signin_link_by_hash(&self, token_hash: &str) -> Result<Option<SigninLink>> { |
| 1517 | 1522 | match self |
| 1518 | 1523 | .one_row( |
| 1519 | - "SELECT id, user_id, created_at, expires_at, used_at FROM signin_link \ | |
| 1520 | - WHERE token_hash = ?1", | |
| 1524 | + "SELECT id, user_id, created_at, expires_at, used_at, kind \ | |
| 1525 | + FROM signin_link WHERE token_hash = ?1", | |
| 1521 | 1526 | params![token_hash], |
| 1522 | 1527 | ) |
| 1523 | 1528 | .await? |
| @@ -1857,6 +1862,13 @@ impl Store for TursoStore { | ||
| 1857 | 1862 | ], |
| 1858 | 1863 | ) |
| 1859 | 1864 | .await?; |
| 1865 | + // The maker watches what they made: its changes concern them | |
| 1866 | + // until they say otherwise. | |
| 1867 | + tx.execute( | |
| 1868 | + "INSERT OR IGNORE INTO task_watcher (task_id, user_id) VALUES (?1, ?2)", | |
| 1869 | + params![id.clone(), new.created_by], | |
| 1870 | + ) | |
| 1871 | + .await?; | |
| 1860 | 1872 | Ok::<_, turso::Error>(Ok((task_key, position))) |
| 1861 | 1873 | } |
| 1862 | 1874 | .await; |
| @@ -2052,6 +2064,15 @@ impl Store for TursoStore { | ||
| 2052 | 2064 | ) |
| 2053 | 2065 | .await |
| 2054 | 2066 | .map_err(backend)?; |
| 2067 | + // Being assigned is being concerned: the assignee watches the task | |
| 2068 | + // until an unassign takes the watch back off. Idempotent, so a | |
| 2069 | + // no-change assign leaves the watch exactly as it is. | |
| 2070 | + tx.execute( | |
| 2071 | + "INSERT OR IGNORE INTO task_watcher (task_id, user_id) VALUES (?1, ?2)", | |
| 2072 | + params![task_id, user_id], | |
| 2073 | + ) | |
| 2074 | + .await | |
| 2075 | + .map_err(backend)?; | |
| 2055 | 2076 | // A no-change assign changes no facts, so the reminders stand as |
| 2056 | 2077 | // they are. |
| 2057 | 2078 | let reminded = if n > 0 { |
| @@ -2091,6 +2112,14 @@ impl Store for TursoStore { | ||
| 2091 | 2112 | ) |
| 2092 | 2113 | .await |
| 2093 | 2114 | .map_err(backend)?; |
| 2115 | + // Leaving the task takes the watch back off: its changes stop | |
| 2116 | + // concerning the person who left. | |
| 2117 | + tx.execute( | |
| 2118 | + "DELETE FROM task_watcher WHERE task_id = ?1 AND user_id = ?2", | |
| 2119 | + params![task_id, user_id], | |
| 2120 | + ) | |
| 2121 | + .await | |
| 2122 | + .map_err(backend)?; | |
| 2094 | 2123 | let reminded = if n > 0 { |
| 2095 | 2124 | match self.sync_task_reminders(&tx, task_id, OffsetDateTime::now_utc()).await { |
| 2096 | 2125 | Ok(reminded) => reminded, |
| @@ -2294,6 +2323,11 @@ impl Store for TursoStore { | ||
| 2294 | 2323 | ], |
| 2295 | 2324 | ) |
| 2296 | 2325 | .await?; |
| 2326 | + tx.execute( | |
| 2327 | + "INSERT OR IGNORE INTO task_watcher (task_id, user_id) VALUES (?1, ?2)", | |
| 2328 | + params![task_id, author_id], | |
| 2329 | + ) | |
| 2330 | + .await?; | |
| 2297 | 2331 | Ok::<_, turso::Error>(()) |
| 2298 | 2332 | } |
| 2299 | 2333 | .await; |
| @@ -3944,6 +3978,122 @@ impl Store for TursoStore { | ||
| 3944 | 3978 | row.as_ref().map(count_of).unwrap_or(Ok(0)) |
| 3945 | 3979 | } |
| 3946 | 3980 | |
| 3981 | + async fn watch_task(&self, task_id: &str, user_id: &str) -> Result<()> { | |
| 3982 | + let conn = self.conn.lock().await; | |
| 3983 | + conn.execute( | |
| 3984 | + "INSERT OR IGNORE INTO task_watcher (task_id, user_id) VALUES (?1, ?2)", | |
| 3985 | + params![task_id, user_id], | |
| 3986 | + ) | |
| 3987 | + .await | |
| 3988 | + .map_err(backend)?; | |
| 3989 | + drop(conn); | |
| 3990 | + self.announce([Topic::Activity]); | |
| 3991 | + Ok(()) | |
| 3992 | + } | |
| 3993 | + | |
| 3994 | + async fn unwatch_task(&self, task_id: &str, user_id: &str) -> Result<()> { | |
| 3995 | + let conn = self.conn.lock().await; | |
| 3996 | + conn.execute( | |
| 3997 | + "DELETE FROM task_watcher WHERE task_id = ?1 AND user_id = ?2", | |
| 3998 | + params![task_id, user_id], | |
| 3999 | + ) | |
| 4000 | + .await | |
| 4001 | + .map_err(backend)?; | |
| 4002 | + drop(conn); | |
| 4003 | + self.announce([Topic::Activity]); | |
| 4004 | + Ok(()) | |
| 4005 | + } | |
| 4006 | + | |
| 4007 | + async fn feed_for_user(&self, user_id: &str, limit: u32) -> Result<Vec<ActivityLine>> { | |
| 4008 | + const SELECT: &str = "SELECT a.id, a.task_id, t.title, u.display_name, a.kind, a.detail, \ | |
| 4009 | + a.created_at, t.task_key FROM activity a \ | |
| 4010 | + JOIN task t ON t.id = a.task_id \ | |
| 4011 | + LEFT JOIN user u ON u.id = a.actor_id"; | |
| 4012 | + // Two ways an event can concern somebody, unioned: it happened on a | |
| 4013 | + // task they watch, or it names them. An event arriving both ways is | |
| 4014 | + // one row — UNION, not UNION ALL — and a person's own actions are | |
| 4015 | + // nobody's news, not even their own (`IS NOT`, so the system's rows | |
| 4016 | + // with no actor still pass). | |
| 4017 | + let conn = self.conn.lock().await; | |
| 4018 | + let sql = format!( | |
| 4019 | + "{SELECT} JOIN task_watcher w ON w.task_id = a.task_id AND w.user_id = ?1 \ | |
| 4020 | + WHERE a.actor_id IS NOT ?2 \ | |
| 4021 | + UNION {SELECT} WHERE a.subject_id = ?3 AND a.actor_id IS NOT ?4 \ | |
| 4022 | + ORDER BY created_at DESC, id DESC LIMIT ?5" | |
| 4023 | + ); | |
| 4024 | + let mut rows = conn | |
| 4025 | + .query( | |
| 4026 | + &sql, | |
| 4027 | + params![user_id, user_id, user_id, user_id, i64::from(limit)], | |
| 4028 | + ) | |
| 4029 | + .await | |
| 4030 | + .map_err(backend)?; | |
| 4031 | + let mut out = Vec::new(); | |
| 4032 | + while let Some(row) = rows.next().await.map_err(backend)? { | |
| 4033 | + out.push(ActivityLine { | |
| 4034 | + id: text(&row, 0)?, | |
| 4035 | + task_id: opt_text(&row, 1)?, | |
| 4036 | + title: opt_text(&row, 2)?, | |
| 4037 | + actor_name: opt_text(&row, 3)?, | |
| 4038 | + kind: ActivityKind::parse(&text(&row, 4)?), | |
| 4039 | + detail: text(&row, 5)?, | |
| 4040 | + at: parse_stamp(&text(&row, 6)?)?, | |
| 4041 | + task_key: opt_text(&row, 7)?, | |
| 4042 | + }); | |
| 4043 | + } | |
| 4044 | + Ok(out) | |
| 4045 | + } | |
| 4046 | + | |
| 4047 | + async fn count_feed_unseen(&self, user_id: &str) -> Result<u64> { | |
| 4048 | + const SELECT: &str = "SELECT a.id FROM activity a \ | |
| 4049 | + JOIN task t ON t.id = a.task_id \ | |
| 4050 | + LEFT JOIN user u ON u.id = a.actor_id"; | |
| 4051 | + // Unseen is the same union as the feed, narrowed to rows that landed | |
| 4052 | + // after the reader's marker. A reader who has never visited has no | |
| 4053 | + // marker, and COALESCE with the empty string — which sorts before | |
| 4054 | + // every stamp — reads the whole feed as news. | |
| 4055 | + let conn = self.conn.lock().await; | |
| 4056 | + let sql = format!( | |
| 4057 | + "SELECT COUNT(*) FROM (\ | |
| 4058 | + {SELECT} JOIN task_watcher w ON w.task_id = a.task_id AND w.user_id = ?1 \ | |
| 4059 | + WHERE a.actor_id IS NOT ?2 \ | |
| 4060 | + AND a.created_at > COALESCE((SELECT feed_seen_at FROM user WHERE id = ?3), '') \ | |
| 4061 | + UNION \ | |
| 4062 | + {SELECT} WHERE a.subject_id = ?4 AND a.actor_id IS NOT ?5 \ | |
| 4063 | + AND a.created_at > COALESCE((SELECT feed_seen_at FROM user WHERE id = ?6), ''))" | |
| 4064 | + ); | |
| 4065 | + let mut rows = conn | |
| 4066 | + .query( | |
| 4067 | + &sql, | |
| 4068 | + params![ | |
| 4069 | + user_id, user_id, user_id, user_id, user_id, user_id, | |
| 4070 | + ], | |
| 4071 | + ) | |
| 4072 | + .await | |
| 4073 | + .map_err(backend)?; | |
| 4074 | + let row = rows.next().await.map_err(backend)?; | |
| 4075 | + match row { | |
| 4076 | + Some(row) => { | |
| 4077 | + let n: i64 = row.get(0).map_err(backend)?; | |
| 4078 | + Ok(usize::try_from(n).unwrap_or(0) as u64) | |
| 4079 | + } | |
| 4080 | + None => Ok(0), | |
| 4081 | + } | |
| 4082 | + } | |
| 4083 | + | |
| 4084 | + async fn mark_feed_seen(&self, user_id: &str, at: OffsetDateTime) -> Result<()> { | |
| 4085 | + let conn = self.conn.lock().await; | |
| 4086 | + conn.execute( | |
| 4087 | + "UPDATE user SET feed_seen_at = ?1 WHERE id = ?2", | |
| 4088 | + params![stamp(at)?, user_id], | |
| 4089 | + ) | |
| 4090 | + .await | |
| 4091 | + .map_err(backend)?; | |
| 4092 | + drop(conn); | |
| 4093 | + self.announce([Topic::Activity]); | |
| 4094 | + Ok(()) | |
| 4095 | + } | |
| 4096 | + | |
| 3947 | 4097 | async fn count_activity_preceding( |
| 3948 | 4098 | &self, |
| 3949 | 4099 | filter: &ActivityFilter, |
crates/izlek-core/tests/store.rsmodified+449 -4
| @@ -1333,7 +1333,7 @@ async fn a_signin_link_stores_only_the_hash_and_is_used_once() { | ||
| 1333 | 1333 | let now = OffsetDateTime::now_utc(); |
| 1334 | 1334 | let link = scratch |
| 1335 | 1335 | .store |
| 1336 | - .create_signin_link(&user.id, "hash-of-the-token", now + Duration::days(7)) | |
| 1336 | + .create_signin_link(&user.id, "hash-of-the-token", now + Duration::days(7), izlek_core::store::LinkKind::Join) | |
| 1337 | 1337 | .await |
| 1338 | 1338 | .unwrap(); |
| 1339 | 1339 | assert!(link.is_usable(now)); |
| @@ -1398,7 +1398,7 @@ async fn an_expired_link_is_still_a_live_account() { | ||
| 1398 | 1398 | let now = OffsetDateTime::now_utc(); |
| 1399 | 1399 | let stale = scratch |
| 1400 | 1400 | .store |
| 1401 | - .create_signin_link(&user.id, "stale-hash", now - Duration::hours(1)) | |
| 1401 | + .create_signin_link(&user.id, "stale-hash", now - Duration::hours(1), izlek_core::store::LinkKind::Join) | |
| 1402 | 1402 | .await |
| 1403 | 1403 | .unwrap(); |
| 1404 | 1404 | assert!(!stale.is_usable(now)); |
| @@ -1406,7 +1406,7 @@ async fn an_expired_link_is_still_a_live_account() { | ||
| 1406 | 1406 | // Resending opens the same account rather than making a new one. |
| 1407 | 1407 | let fresh = scratch |
| 1408 | 1408 | .store |
| 1409 | - .create_signin_link(&user.id, "fresh-hash", now + Duration::days(7)) | |
| 1409 | + .create_signin_link(&user.id, "fresh-hash", now + Duration::days(7), izlek_core::store::LinkKind::Join) | |
| 1410 | 1410 | .await |
| 1411 | 1411 | .unwrap(); |
| 1412 | 1412 | assert_eq!(fresh.user_id, stale.user_id); |
| @@ -1437,7 +1437,7 @@ async fn a_prefetched_link_is_consumed_exactly_once() { | ||
| 1437 | 1437 | let token = Token::mint(); |
| 1438 | 1438 | let now = OffsetDateTime::now_utc(); |
| 1439 | 1439 | let link = store |
| 1440 | - .create_signin_link(&user_id, &token.hash(), now + Duration::days(7)) | |
| 1440 | + .create_signin_link(&user_id, &token.hash(), now + Duration::days(7), izlek_core::store::LinkKind::Join) | |
| 1441 | 1441 | .await |
| 1442 | 1442 | .unwrap(); |
| 1443 | 1443 | |
| @@ -1996,6 +1996,7 @@ async fn an_expired_link_is_refused_and_resending_opens_the_same_account() { | ||
| 1996 | 1996 | &invitation.user.id, |
| 1997 | 1997 | &stale.hash(), |
| 1998 | 1998 | OffsetDateTime::now_utc() - Duration::minutes(1), |
| 1999 | + izlek_core::store::LinkKind::Join, | |
| 1999 | 2000 | ) |
| 2000 | 2001 | .await |
| 2001 | 2002 | .unwrap(); |
| @@ -8543,3 +8544,447 @@ async fn a_change_back_to_the_same_password_is_refused_and_signs_nobody_out() { | ||
| 8543 | 8544 | .await |
| 8544 | 8545 | .unwrap(); |
| 8545 | 8546 | } |
| 8547 | + | |
| 8548 | +/// The board search keeps the cards whose key or title answers the query — | |
| 8549 | +/// case-insensitively, each in the column it already sits in — and drops | |
| 8550 | +/// the rest. | |
| 8551 | +#[tokio::test] | |
| 8552 | +async fn board_search_matches_keys_and_titles_in_place() { | |
| 8553 | + let (scratch, workspace, admin) = workspace_with_admin().await; | |
| 8554 | + let panel = add_task(&scratch.store, &workspace, "Backlog", "Panel polish", None, &admin).await; | |
| 8555 | + let wire = add_task(&scratch.store, &workspace, "In Progress", "Wire the exporter", None, &admin).await; | |
| 8556 | + let panel_key = scratch | |
| 8557 | + .store | |
| 8558 | + .task(&panel) | |
| 8559 | + .await | |
| 8560 | + .unwrap() | |
| 8561 | + .expect("panel task gone") | |
| 8562 | + .row | |
| 8563 | + .task_key; | |
| 8564 | + | |
| 8565 | + // The human key, typed in any case, finds its card — and only its card. | |
| 8566 | + let mut board = izlek_core::board::load(&scratch.store, &workspace) | |
| 8567 | + .await | |
| 8568 | + .unwrap() | |
| 8569 | + .unwrap(); | |
| 8570 | + board.searching(&panel_key.to_lowercase()); | |
| 8571 | + assert_eq!(board.task_count(), 1); | |
| 8572 | + let backlog = board | |
| 8573 | + .columns | |
| 8574 | + .iter() | |
| 8575 | + .find(|column| column.column.name == "Backlog") | |
| 8576 | + .expect("no Backlog column"); | |
| 8577 | + assert_eq!(backlog.cards.len(), 1); | |
| 8578 | + assert_eq!(backlog.cards[0].id, panel); | |
| 8579 | + assert!( | |
| 8580 | + board | |
| 8581 | + .columns | |
| 8582 | + .iter() | |
| 8583 | + .find(|column| column.column.name == "In Progress") | |
| 8584 | + .expect("no In Progress column") | |
| 8585 | + .cards | |
| 8586 | + .is_empty() | |
| 8587 | + ); | |
| 8588 | + | |
| 8589 | + // A title fragment, any case, the same story in the other column. | |
| 8590 | + let mut board = izlek_core::board::load(&scratch.store, &workspace) | |
| 8591 | + .await | |
| 8592 | + .unwrap() | |
| 8593 | + .unwrap(); | |
| 8594 | + board.searching("EXPORT"); | |
| 8595 | + assert_eq!(board.task_count(), 1); | |
| 8596 | + let progress = board | |
| 8597 | + .columns | |
| 8598 | + .iter() | |
| 8599 | + .find(|column| column.column.name == "In Progress") | |
| 8600 | + .expect("no In Progress column"); | |
| 8601 | + assert_eq!(progress.cards[0].id, wire); | |
| 8602 | + | |
| 8603 | + // A query nothing answers empties the board. | |
| 8604 | + let mut board = izlek_core::board::load(&scratch.store, &workspace) | |
| 8605 | + .await | |
| 8606 | + .unwrap() | |
| 8607 | + .unwrap(); | |
| 8608 | + board.searching("no-such-card-anywhere"); | |
| 8609 | + assert!(board.is_empty()); | |
| 8610 | +} | |
| 8611 | +// --------------------------------------------------------------------------- | |
| 8612 | +// Self-serve password reset: a "Forgot it?" ask mails a reset link, the link | |
| 8613 | +// redeems on its own screen only, and the new password signs every other | |
| 8614 | +// browser out. The address's existence stays the requester's guess. | |
| 8615 | +// --------------------------------------------------------------------------- | |
| 8616 | + | |
| 8617 | +/// The reset token out of the newest notice queued for the address — the mail | |
| 8618 | +/// is the token's only carrier, so the test reads it like a recipient would. | |
| 8619 | +async fn queued_reset_token( | |
| 8620 | + store: &std::sync::Arc<dyn Store>, | |
| 8621 | + email: &str, | |
| 8622 | +) -> Option<String> { | |
| 8623 | + let sends = store | |
| 8624 | + .mail_queue(50, izlek_core::store::FeedPage::Newest) | |
| 8625 | + .await | |
| 8626 | + .unwrap(); | |
| 8627 | + let body = sends | |
| 8628 | + .into_iter() | |
| 8629 | + .rev() | |
| 8630 | + .find(|send| { | |
| 8631 | + send.kind == izlek_core::store::SendKind::Notice && send.recipient == email | |
| 8632 | + }) | |
| 8633 | + .and_then(|send| send.body)?; | |
| 8634 | + body.rsplit_once("/reset/") | |
| 8635 | + .and_then(|(_, rest)| rest.split_whitespace().next()) | |
| 8636 | + .map(str::to_string) | |
| 8637 | +} | |
| 8638 | + | |
| 8639 | +#[tokio::test] | |
| 8640 | +async fn a_reset_link_is_mailed_only_to_a_known_address() { | |
| 8641 | + let (dir, store, workspace, _admin) = shared().await; | |
| 8642 | + let _grace = member(&store, &workspace, "grace@izlek.sh", "Grace").await; | |
| 8643 | + let store = store as std::sync::Arc<dyn Store>; | |
| 8644 | + let accounts = Accounts::new(store.clone(), "https://izlek.sh"); | |
| 8645 | + | |
| 8646 | + // One ask for an address with an account, one without: the caller sees | |
| 8647 | + // two identical answers. | |
| 8648 | + accounts | |
| 8649 | + .request_password_reset("grace@izlek.sh", "203.0.113.9") | |
| 8650 | + .await | |
| 8651 | + .unwrap(); | |
| 8652 | + accounts | |
| 8653 | + .request_password_reset("nobody@izlek.sh", "203.0.113.9") | |
| 8654 | + .await | |
| 8655 | + .unwrap(); | |
| 8656 | + | |
| 8657 | + let link = scalar( | |
| 8658 | + &dir.join("izlek.db").to_string_lossy(), | |
| 8659 | + "SELECT COUNT(*) FROM signin_link WHERE kind = 'reset'", | |
| 8660 | + ) | |
| 8661 | + .await; | |
| 8662 | + assert_eq!(link, 1, "only the known address gets a link"); | |
| 8663 | + assert!( | |
| 8664 | + queued_reset_token(&store, "grace@izlek.sh").await.is_some(), | |
| 8665 | + "the known address was mailed a reset link" | |
| 8666 | + ); | |
| 8667 | + assert!( | |
| 8668 | + queued_reset_token(&store, "nobody@izlek.sh").await.is_none(), | |
| 8669 | + "the unknown address was mailed nothing" | |
| 8670 | + ); | |
| 8671 | + let _ = std::fs::remove_dir_all(&dir); | |
| 8672 | +} | |
| 8673 | + | |
| 8674 | +#[tokio::test] | |
| 8675 | +async fn a_reset_redeems_and_signs_the_other_browsers_out() { | |
| 8676 | + let (_scratch, accounts, _admin) = claimed().await; | |
| 8677 | + // A second browser, signed in before the reset. | |
| 8678 | + let before = accounts | |
| 8679 | + .sign_in("ada@izlek.sh", "tide-tables-1892", "198.51.100.7") | |
| 8680 | + .await | |
| 8681 | + .unwrap(); | |
| 8682 | + | |
| 8683 | + accounts | |
| 8684 | + .request_password_reset("ada@izlek.sh", "203.0.113.9") | |
| 8685 | + .await | |
| 8686 | + .unwrap(); | |
| 8687 | + let token = queued_reset_token(accounts.store(), "ada@izlek.sh") | |
| 8688 | + .await | |
| 8689 | + .expect("no reset mail queued"); | |
| 8690 | + | |
| 8691 | + let after = accounts | |
| 8692 | + .redeem_reset_link(&token, "chronometer-1761", "203.0.113.10") | |
| 8693 | + .await | |
| 8694 | + .unwrap(); | |
| 8695 | + | |
| 8696 | + // The old password is gone, the new one signs in. | |
| 8697 | + assert!(matches!( | |
| 8698 | + accounts | |
| 8699 | + .sign_in("ada@izlek.sh", "tide-tables-1892", "198.51.100.11") | |
| 8700 | + .await, | |
| 8701 | + Err(AccountError::Rejected) | |
| 8702 | + )); | |
| 8703 | + accounts | |
| 8704 | + .sign_in("ada@izlek.sh", "chronometer-1761", "198.51.100.11") | |
| 8705 | + .await | |
| 8706 | + .unwrap(); | |
| 8707 | + // The browser that was signed in before the reset is out; the redeeming | |
| 8708 | + // browser's own session is alive. | |
| 8709 | + assert!(matches!( | |
| 8710 | + accounts.authenticate(&before.session_token.expose()).await.unwrap(), | |
| 8711 | + None | |
| 8712 | + )); | |
| 8713 | + assert!(matches!( | |
| 8714 | + accounts.authenticate(&after.session_token.expose()).await.unwrap(), | |
| 8715 | + Some(_) | |
| 8716 | + )); | |
| 8717 | +} | |
| 8718 | + | |
| 8719 | +#[tokio::test] | |
| 8720 | +async fn a_reset_link_only_redeems_once() { | |
| 8721 | + let (_scratch, accounts, _admin) = claimed().await; | |
| 8722 | + accounts | |
| 8723 | + .request_password_reset("ada@izlek.sh", "203.0.113.9") | |
| 8724 | + .await | |
| 8725 | + .unwrap(); | |
| 8726 | + let token = queued_reset_token(accounts.store(), "ada@izlek.sh") | |
| 8727 | + .await | |
| 8728 | + .expect("no reset mail queued"); | |
| 8729 | + | |
| 8730 | + accounts | |
| 8731 | + .redeem_reset_link(&token, "chronometer-1761", "203.0.113.10") | |
| 8732 | + .await | |
| 8733 | + .unwrap(); | |
| 8734 | + assert!(matches!( | |
| 8735 | + accounts | |
| 8736 | + .redeem_reset_link(&token, "chronometer-1761", "203.0.113.10") | |
| 8737 | + .await, | |
| 8738 | + Err(AccountError::Rejected) | |
| 8739 | + )); | |
| 8740 | +} | |
| 8741 | + | |
| 8742 | +#[tokio::test] | |
| 8743 | +async fn an_expired_reset_link_is_refused() { | |
| 8744 | + let (scratch, accounts, admin) = claimed().await; | |
| 8745 | + let token = izlek_core::auth::Token::mint(); | |
| 8746 | + scratch | |
| 8747 | + .store | |
| 8748 | + .create_signin_link( | |
| 8749 | + &admin.id, | |
| 8750 | + &token.hash(), | |
| 8751 | + time::OffsetDateTime::now_utc() - time::Duration::seconds(1), | |
| 8752 | + izlek_core::store::LinkKind::Reset, | |
| 8753 | + ) | |
| 8754 | + .await | |
| 8755 | + .unwrap(); | |
| 8756 | + assert!(matches!( | |
| 8757 | + accounts | |
| 8758 | + .redeem_reset_link(token.expose(), "chronometer-1761", "203.0.113.9") | |
| 8759 | + .await, | |
| 8760 | + Err(AccountError::Rejected) | |
| 8761 | + )); | |
| 8762 | +} | |
| 8763 | + | |
| 8764 | +#[tokio::test] | |
| 8765 | +async fn each_link_redeems_only_on_its_own_screen() { | |
| 8766 | + let (scratch, accounts, admin) = claimed().await; | |
| 8767 | + // An invitation is not a reset: it must not spend itself where the other | |
| 8768 | + // browsers get signed out. | |
| 8769 | + let join = izlek_core::auth::Token::mint(); | |
| 8770 | + scratch | |
| 8771 | + .store | |
| 8772 | + .create_signin_link( | |
| 8773 | + &admin.id, | |
| 8774 | + &join.hash(), | |
| 8775 | + time::OffsetDateTime::now_utc() + SIGNIN_LINK_LIFETIME, | |
| 8776 | + izlek_core::store::LinkKind::Join, | |
| 8777 | + ) | |
| 8778 | + .await | |
| 8779 | + .unwrap(); | |
| 8780 | + assert!(matches!( | |
| 8781 | + accounts | |
| 8782 | + .redeem_reset_link(&join.expose(), "chronometer-1761", "203.0.113.9") | |
| 8783 | + .await, | |
| 8784 | + Err(AccountError::Rejected) | |
| 8785 | + )); | |
| 8786 | + // And a reset link is not an invitation. | |
| 8787 | + accounts | |
| 8788 | + .request_password_reset("ada@izlek.sh", "203.0.113.9") | |
| 8789 | + .await | |
| 8790 | + .unwrap(); | |
| 8791 | + let reset = queued_reset_token(accounts.store(), "ada@izlek.sh") | |
| 8792 | + .await | |
| 8793 | + .expect("no reset mail queued"); | |
| 8794 | + assert!(matches!( | |
| 8795 | + accounts | |
| 8796 | + .redeem_signin_link(&reset, "chronometer-1761", "203.0.113.9") | |
| 8797 | + .await, | |
| 8798 | + Err(AccountError::Rejected) | |
| 8799 | + )); | |
| 8800 | +} | |
| 8801 | + | |
| 8802 | +#[tokio::test] | |
| 8803 | +async fn reset_asks_are_rate_limited_per_client_and_silenced_per_address() { | |
| 8804 | + let (dir, store, workspace, _admin) = shared().await; | |
| 8805 | + let _grace = member(&store, &workspace, "grace@izlek.sh", "Grace").await; | |
| 8806 | + let store = store as std::sync::Arc<dyn Store>; | |
| 8807 | + let accounts = Accounts::new(store.clone(), "https://izlek.sh"); | |
| 8808 | + | |
| 8809 | + // One machine asks RATE_LIMIT times: every ask answers the same, and the | |
| 8810 | + // eleventh from that machine is refused by name. | |
| 8811 | + for _ in 0..RATE_LIMIT { | |
| 8812 | + accounts | |
| 8813 | + .request_password_reset("grace@izlek.sh", "203.0.113.9") | |
| 8814 | + .await | |
| 8815 | + .unwrap(); | |
| 8816 | + } | |
| 8817 | + assert!(matches!( | |
| 8818 | + accounts | |
| 8819 | + .request_password_reset("grace@izlek.sh", "203.0.113.9") | |
| 8820 | + .await, | |
| 8821 | + Err(AccountError::RateLimited) | |
| 8822 | + )); | |
| 8823 | + // A fresh client hammering one address gets the same answer as ever, and | |
| 8824 | + // the mailbox stays quiet: the address bucket caps the mail, not the | |
| 8825 | + // answer. | |
| 8826 | + accounts | |
| 8827 | + .request_password_reset("grace@izlek.sh", "203.0.113.201") | |
| 8828 | + .await | |
| 8829 | + .unwrap(); | |
| 8830 | + let queued = scalar( | |
| 8831 | + &dir.join("izlek.db").to_string_lossy(), | |
| 8832 | + "SELECT COUNT(*) FROM mail_send WHERE kind = 'notice'", | |
| 8833 | + ) | |
| 8834 | + .await; | |
| 8835 | + assert_eq!(queued, RATE_LIMIT as i64, "the flood minted no mail"); | |
| 8836 | + let _ = std::fs::remove_dir_all(&dir); | |
| 8837 | +} | |
| 8838 | + | |
| 8839 | +/// Watches and the personal feed: creating, being assigned and commenting | |
| 8840 | +/// each start a watch; an unassign ends it. The feed carries events on | |
| 8841 | +/// watched tasks and events naming the reader, never the reader's own | |
| 8842 | +/// actions; the unseen marker counts what landed since the last read. | |
| 8843 | +#[tokio::test] | |
| 8844 | +async fn the_personal_feed_follows_watches_names_and_the_marker() { | |
| 8845 | + let (dir, store, workspace, admin) = shared().await; | |
| 8846 | + let grace = member(&store, &workspace, "grace@izlek.sh", "Grace").await; | |
| 8847 | + let now = OffsetDateTime::now_utc(); | |
| 8848 | + let board = store.board(&workspace).await.unwrap().unwrap(); | |
| 8849 | + let column_id = column_named(&store, &workspace, "Backlog").await; | |
| 8850 | + let task = store | |
| 8851 | + .create_task(NewTask { | |
| 8852 | + board_id: &board.id, | |
| 8853 | + column_id: &column_id, | |
| 8854 | + parent_id: None, | |
| 8855 | + title: "Feed me", | |
| 8856 | + description: "", | |
| 8857 | + deadline: None, | |
| 8858 | + clock_at: None, | |
| 8859 | + created_by: &admin, | |
| 8860 | + }) | |
| 8861 | + .await | |
| 8862 | + .unwrap() | |
| 8863 | + .row | |
| 8864 | + .id; | |
| 8865 | + | |
| 8866 | + store.assign_task(&task, &grace).await.unwrap(); | |
| 8867 | + | |
| 8868 | + // Creating started Ada's watch; the assignment starts Grace's. Nobody's | |
| 8869 | + // own actions are their own news: Ada's created line is Grace's news, | |
| 8870 | + // and Grace's feed shows it while Ada's stays empty. | |
| 8871 | + let grace_feed = store.feed_for_user(&grace, 50).await.unwrap(); | |
| 8872 | + assert_eq!(grace_feed.len(), 1); | |
| 8873 | + assert_eq!(grace_feed[0].kind, ActivityKind::Created); | |
| 8874 | + assert_eq!(grace_feed[0].actor_name.as_deref(), Some("Ada")); | |
| 8875 | + assert!(store.feed_for_user(&admin, 50).await.unwrap().is_empty()); | |
| 8876 | + assert_eq!(store.count_feed_unseen(&grace).await.unwrap(), 1); | |
| 8877 | + | |
| 8878 | + // Grace's own comment is her own action: it is Ada's news, not hers. | |
| 8879 | + store | |
| 8880 | + .add_comment(&task, &grace, "Looking at it now", now) | |
| 8881 | + .await | |
| 8882 | + .unwrap(); | |
| 8883 | + // Her feed keeps the created line; her own comment never comes back. | |
| 8884 | + let grace_feed = store.feed_for_user(&grace, 50).await.unwrap(); | |
| 8885 | + assert_eq!(grace_feed.len(), 1); | |
| 8886 | + assert_eq!(grace_feed[0].kind, ActivityKind::Created); | |
| 8887 | + let admin_feed = store.feed_for_user(&admin, 50).await.unwrap(); | |
| 8888 | + assert_eq!(admin_feed.len(), 1); | |
| 8889 | + assert_eq!(admin_feed[0].kind, ActivityKind::Commented); | |
| 8890 | + assert_eq!(admin_feed[0].actor_name.as_deref(), Some("Grace")); | |
| 8891 | + assert_eq!(store.count_feed_unseen(&admin).await.unwrap(), 1); | |
| 8892 | + | |
| 8893 | + // Reading the feed is reading it: the marker resets, the history stays. | |
| 8894 | + store | |
| 8895 | + .mark_feed_seen(&admin, now + Duration::seconds(5)) | |
| 8896 | + .await | |
| 8897 | + .unwrap(); | |
| 8898 | + assert_eq!(store.count_feed_unseen(&admin).await.unwrap(), 0); | |
| 8899 | + assert_eq!(store.feed_for_user(&admin, 50).await.unwrap().len(), 1); | |
| 8900 | + | |
| 8901 | + // Grace reads the feed too: her marker takes the created line off her | |
| 8902 | + // unseen count, history included. | |
| 8903 | + store | |
| 8904 | + .mark_feed_seen(&grace, now + Duration::seconds(3)) | |
| 8905 | + .await | |
| 8906 | + .unwrap(); | |
| 8907 | + assert_eq!(store.count_feed_unseen(&grace).await.unwrap(), 0); | |
| 8908 | + assert_eq!(store.feed_for_user(&grace, 50).await.unwrap().len(), 1); | |
| 8909 | + | |
| 8910 | + // Ada's comment lands on a task Grace watches, after her marker: news. | |
| 8911 | + store | |
| 8912 | + .add_comment(&task, &admin, "Thanks — ship it", now + Duration::seconds(6)) | |
| 8913 | + .await | |
| 8914 | + .unwrap(); | |
| 8915 | + assert_eq!(store.count_feed_unseen(&grace).await.unwrap(), 1); | |
| 8916 | + let grace_feed = store.feed_for_user(&grace, 50).await.unwrap(); | |
| 8917 | + assert_eq!(grace_feed.len(), 2, "{grace_feed:?}"); | |
| 8918 | + assert_eq!(grace_feed[0].kind, ActivityKind::Commented); | |
| 8919 | + assert_eq!(grace_feed[0].actor_name.as_deref(), Some("Ada")); | |
| 8920 | + | |
| 8921 | + // An unassign takes the watch back off: history on the task stops | |
| 8922 | + // concerning the person who left. | |
| 8923 | + store.unassign_task(&task, &grace).await.unwrap(); | |
| 8924 | + assert!(store.feed_for_user(&grace, 50).await.unwrap().is_empty()); | |
| 8925 | + assert_eq!(store.count_feed_unseen(&grace).await.unwrap(), 0); | |
| 8926 | + | |
| 8927 | + // An event that NAMES her concerns her with no watch at all — the | |
| 8928 | + // second way into the feed. | |
| 8929 | + store | |
| 8930 | + .record_activity( | |
| 8931 | + &task, | |
| 8932 | + Some(&admin), | |
| 8933 | + Some(&grace), | |
| 8934 | + &ActivityKind::Assigned, | |
| 8935 | + "Grace", | |
| 8936 | + now + Duration::seconds(7), | |
| 8937 | + ) | |
| 8938 | + .await | |
| 8939 | + .unwrap(); | |
| 8940 | + let grace_feed = store.feed_for_user(&grace, 50).await.unwrap(); | |
| 8941 | + assert_eq!(grace_feed.len(), 1); | |
| 8942 | + assert_eq!(grace_feed[0].kind, ActivityKind::Assigned); | |
| 8943 | + | |
| 8944 | + let _ = std::fs::remove_dir_all(&dir); | |
| 8945 | +} | |
| 8946 | +#[tokio::test] | |
| 8947 | +async fn a_delivered_reminder_writes_the_reminded_event() { | |
| 8948 | + let (dir, store, workspace, admin) = shared().await; | |
| 8949 | + let grace = member(&store, &workspace, "grace@izlek.sh", "Grace").await; | |
| 8950 | + let clock = OffsetDateTime::now_utc() + Duration::hours(2); | |
| 8951 | + let task = clocked_task(&store, &workspace, &admin, Some(clock)).await; | |
| 8952 | + store.assign_task(&task, &grace).await.unwrap(); | |
| 8953 | + | |
| 8954 | + let engine = Engine::new( | |
| 8955 | + store.clone() as std::sync::Arc<dyn Store>, | |
| 8956 | + Remembering::taking_everything(), | |
| 8957 | + "https://izlek.sh", | |
| 8958 | + ); | |
| 8959 | + let later = clock - Duration::minutes(14); | |
| 8960 | + assert_eq!(engine.deliver_owed(later, 10).await.unwrap().sent, 1); | |
| 8961 | + | |
| 8962 | + // The delivery wrote the trail line: on the meeting's card, about the | |
| 8963 | + // person the reminder landed on, with the system as the actor. | |
| 8964 | + let path = dir.join("izlek.db").to_string_lossy().into_owned(); | |
| 8965 | + let events = scalar( | |
| 8966 | + &path, | |
| 8967 | + "SELECT COUNT(*) FROM activity WHERE kind = 'reminded' AND actor_id IS NULL", | |
| 8968 | + ) | |
| 8969 | + .await; | |
| 8970 | + assert_eq!(events, 1, "one delivery, one line"); | |
| 8971 | + let about = scalar( | |
| 8972 | + &path, | |
| 8973 | + &format!( | |
| 8974 | + "SELECT COUNT(*) FROM activity WHERE kind = 'reminded' \ | |
| 8975 | + AND subject_id = '{grace}' AND task_id = '{task}'" | |
| 8976 | + ), | |
| 8977 | + ) | |
| 8978 | + .await; | |
| 8979 | + assert_eq!(about, 1, "the line is about the reminded person"); | |
| 8980 | + | |
| 8981 | + // Sweeping again re-sends nothing and writes nothing. | |
| 8982 | + engine.deliver_owed(later + Duration::minutes(1), 10).await.unwrap(); | |
| 8983 | + let events = scalar( | |
| 8984 | + &path, | |
| 8985 | + "SELECT COUNT(*) FROM activity WHERE kind = 'reminded'", | |
| 8986 | + ) | |
| 8987 | + .await; | |
| 8988 | + assert_eq!(events, 1); | |
| 8989 | + let _ = std::fs::remove_dir_all(&dir); | |
| 8990 | +} |
crates/izlek-web/src/auth.rsmodified+105 -0
| @@ -339,3 +339,108 @@ struct InvitationForm { | ||
| 339 | 339 | async fn invitation(cx: &Cx, Form(input): Form<InvitationForm>) -> Result<Json<Option<Invited>>> { |
| 340 | 340 | Ok(Json(invited_by_token(cx, &input.token).await?)) |
| 341 | 341 | } |
| 342 | + | |
| 343 | +/// Who a reset link was mailed to, looked up by the link's own token — the | |
| 344 | +/// reset page's "signing in as" line. Only a link of the reset kind resolves | |
| 345 | +/// here, and only while it is still usable: a spent or expired link shows | |
| 346 | +/// the same dead card an unknown token does. | |
| 347 | +pub async fn reset_by_token(cx: &Cx, token: &str) -> Result<Option<Invited>> { | |
| 348 | + use izlek_core::auth::hash_token; | |
| 349 | + use time::OffsetDateTime; | |
| 350 | + | |
| 351 | + let store = accounts(cx).store().clone(); | |
| 352 | + let digest = hash_token(token); | |
| 353 | + let Some(link) = store.signin_link_by_hash(&digest).await? else { | |
| 354 | + return Ok(None); | |
| 355 | + }; | |
| 356 | + if link.kind != izlek_core::store::LinkKind::Reset | |
| 357 | + || !link.is_usable(OffsetDateTime::now_utc()) | |
| 358 | + { | |
| 359 | + return Ok(None); | |
| 360 | + } | |
| 361 | + let Some(user) = store.user(&link.user_id).await? else { | |
| 362 | + return Ok(None); | |
| 363 | + }; | |
| 364 | + Ok(Some(Invited { | |
| 365 | + display_name: user.display_name, | |
| 366 | + email: user.email, | |
| 367 | + invited_by: None, | |
| 368 | + })) | |
| 369 | +} | |
| 370 | + | |
| 371 | +#[derive(Deserialize)] | |
| 372 | +struct ForgotPasswordForm { | |
| 373 | + email: String, | |
| 374 | +} | |
| 375 | + | |
| 376 | +/// Asks for a password-reset mail. The answer is the same whether the | |
| 377 | +/// address has an account or not — the difference is not the browser's | |
| 378 | +/// business. The request is the trail's fact either way; a rate-limited | |
| 379 | +/// client is refused by name. | |
| 380 | +#[route(POST "/api/forgot_password")] | |
| 381 | +async fn forgot_password(cx: &Cx, Form(input): Form<ForgotPasswordForm>) -> Redirect { | |
| 382 | + match accounts(cx) | |
| 383 | + .request_password_reset(&input.email, &client_label(cx)) | |
| 384 | + .await | |
| 385 | + { | |
| 386 | + Ok(()) => { | |
| 387 | + let _ = accounts(cx) | |
| 388 | + .store() | |
| 389 | + .record_event( | |
| 390 | + None, | |
| 391 | + &ActivityKind::Other("password_reset_requested".to_string()), | |
| 392 | + &input.email, | |
| 393 | + time::OffsetDateTime::now_utc(), | |
| 394 | + ) | |
| 395 | + .await; | |
| 396 | + // The sent note rides the redirect, the way `saved=` does: one | |
| 397 | + // sentence for every address. | |
| 398 | + let back = back_to(cx, "/forgot"); | |
| 399 | + let sep = if back.contains('?') { "&" } else { "?" }; | |
| 400 | + Ok(( | |
| 401 | + StatusCode::SEE_OTHER, | |
| 402 | + [(header::LOCATION, format!("{back}{sep}sent=forgot_password"))], | |
| 403 | + Json(None), | |
| 404 | + )) | |
| 405 | + } | |
| 406 | + Err(error) => redirect(cx, Some(error.into())), | |
| 407 | + } | |
| 408 | +} | |
| 409 | + | |
| 410 | +#[derive(Deserialize)] | |
| 411 | +struct RedeemResetForm { | |
| 412 | + token: String, | |
| 413 | + password: String, | |
| 414 | +} | |
| 415 | + | |
| 416 | +/// The reset link spends itself on the new password, and every other | |
| 417 | +/// browser is signed out — the promise the change-password pane makes. | |
| 418 | +#[route(POST "/api/redeem_reset")] | |
| 419 | +async fn redeem_reset(cx: &Cx, Form(input): Form<RedeemResetForm>) -> Redirect { | |
| 420 | + match accounts(cx) | |
| 421 | + .redeem_reset_link(&input.token, &input.password, &client_label(cx)) | |
| 422 | + .await | |
| 423 | + { | |
| 424 | + Ok(signed_in) => { | |
| 425 | + set_session_cookie(cx, signed_in.session_token.expose(), SESSION_LIFETIME); | |
| 426 | + let _ = accounts(cx) | |
| 427 | + .store() | |
| 428 | + .record_event( | |
| 429 | + Some(&signed_in.user.id), | |
| 430 | + &ActivityKind::Other("password_reset_completed".to_string()), | |
| 431 | + &signed_in.user.email, | |
| 432 | + time::OffsetDateTime::now_utc(), | |
| 433 | + ) | |
| 434 | + .await; | |
| 435 | + // The referring `/reset/{token}` names a spent link now; a | |
| 436 | + // browser that just signed in belongs on the board, the way the | |
| 437 | + // invitation redeem lands. | |
| 438 | + Ok(( | |
| 439 | + StatusCode::SEE_OTHER, | |
| 440 | + [(header::LOCATION, "/".to_string())], | |
| 441 | + Json(None), | |
| 442 | + )) | |
| 443 | + } | |
| 444 | + Err(error) => redirect(cx, Some(error.into())), | |
| 445 | + } | |
| 446 | +} |
crates/izlek-web/src/board.rsmodified+51 -19
| @@ -235,6 +235,15 @@ fn valid_tag(tag: Option<&str>, tags: &[izlek_core::store::Tag]) -> Option<Strin | ||
| 235 | 235 | tag.and_then(|id| tags.iter().find(|t| t.id == id).map(|t| t.id.clone())) |
| 236 | 236 | } |
| 237 | 237 | |
| 238 | +/// The `?q=` search: the box's text trimmed, and an empty box is no search | |
| 239 | +/// at all — clearing it restores the whole board. | |
| 240 | +fn searched(query: Option<&str>) -> Option<String> { | |
| 241 | + query | |
| 242 | + .map(str::trim) | |
| 243 | + .filter(|query| !query.is_empty()) | |
| 244 | + .map(str::to_string) | |
| 245 | +} | |
| 246 | + | |
| 238 | 247 | /// `deadline` is the order [`izlek_core::board::load`] already hands back |
| 239 | 248 | /// (soonest first); the other two re-order in place here so core stays free |
| 240 | 249 | /// of a UI sort preference. `created` reads the task id, a ULID, which sorts |
| @@ -252,6 +261,7 @@ async fn board_columns( | ||
| 252 | 261 | cx: &Cx, |
| 253 | 262 | sort: String, |
| 254 | 263 | tag_filter: Option<String>, |
| 264 | + search: Option<String>, | |
| 255 | 265 | default_tag_id: Option<&str>, |
| 256 | 266 | ) -> Result { |
| 257 | 267 | let user = match require_user(cx).await { |
| @@ -274,14 +284,13 @@ async fn board_columns( | ||
| 274 | 284 | <div class="scaffold-note"><p>(t(lang, Key::SomethingWentWrong))</p></div> |
| 275 | 285 | }; |
| 276 | 286 | }; |
| 287 | + if let Some(needle) = &search { | |
| 288 | + view_data.searching(needle); | |
| 289 | + } | |
| 290 | + view_data.tagged(tag_filter.as_deref()); | |
| 277 | 291 | for column in &mut view_data.columns { |
| 278 | 292 | column.column.name = crate::i18n::column_name(lang, &column.column.name); |
| 279 | 293 | sort_column_cards(&mut column.cards, &sort); |
| 280 | - if let Some(tag_id) = &tag_filter { | |
| 281 | - column | |
| 282 | - .cards | |
| 283 | - .retain(|card| card.tag.as_ref().is_some_and(|tag| tag.id == *tag_id)); | |
| 284 | - } | |
| 285 | 294 | } |
| 286 | 295 | let today = OffsetDateTime::now_utc().date(); |
| 287 | 296 | let may_write = user.role.can_write_tasks(); |
| @@ -569,6 +578,7 @@ struct BoardQuery { | ||
| 569 | 578 | confirm: Option<String>, |
| 570 | 579 | new: Option<String>, |
| 571 | 580 | tag: Option<String>, |
| 581 | + q: Option<String>, | |
| 572 | 582 | } |
| 573 | 583 | |
| 574 | 584 | /// The noun a sort key shows in the `<select>` — terse, no explainer. |
| @@ -601,10 +611,20 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 601 | 611 | for column in &mut view_data.columns { |
| 602 | 612 | column.column.name = crate::i18n::column_name(lang, &column.column.name); |
| 603 | 613 | } |
| 614 | + let query = query_params::<BoardQuery>(cx)?; | |
| 604 | 615 | let tags = { |
| 605 | 616 | let store = accounts(cx).store().clone(); |
| 606 | 617 | store.tags(&view_data.board.id).await? |
| 607 | 618 | }; |
| 619 | + // The whole filter pipeline narrows the board before anything counts or | |
| 620 | + // renders it, so the chips, the columns and the empty state all describe | |
| 621 | + // the same set. | |
| 622 | + let search = searched(query.q.as_deref()); | |
| 623 | + let tag_filter = valid_tag(query.tag.as_deref(), &tags); | |
| 624 | + if let Some(needle) = &search { | |
| 625 | + view_data.searching(needle); | |
| 626 | + } | |
| 627 | + view_data.tagged(tag_filter.as_deref()); | |
| 608 | 628 | let today = OffsetDateTime::now_utc().date(); |
| 609 | 629 | let overdue = view_data.overdue_count(today); |
| 610 | 630 | let blocked = view_data.blocked_count(); |
| @@ -614,7 +634,6 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 614 | 634 | .iter() |
| 615 | 635 | .map(|column| (column.column.id.clone(), column.column.name.clone())) |
| 616 | 636 | .collect(); |
| 617 | - let query = query_params::<BoardQuery>(cx)?; | |
| 618 | 637 | let open_task = query.task.clone(); |
| 619 | 638 | let open_tab = crate::detail::Tab::from_query(query.tab.as_deref()); |
| 620 | 639 | // Which sheet of an open workbook is drawn. Anything that is not a number |
| @@ -637,7 +656,6 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 637 | 656 | // an open task wins and `new` is ignored. |
| 638 | 657 | let open_new = may_write && query.new.is_some() && open_task.is_none(); |
| 639 | 658 | let sort = valid_sort(query.sort.as_deref()).to_string(); |
| 640 | - let tag_filter = valid_tag(query.tag.as_deref(), &tags); | |
| 641 | 659 | let default_tag_id = tags.iter().find(|tag| tag.is_default).map(|tag| tag.id.as_str()); |
| 642 | 660 | let refusal = match (query.on.as_deref(), query.refusal.as_deref()) { |
| 643 | 661 | (Some("create_task") | Some("move_card"), Some(code)) => Refusal::from_code(code), |
| @@ -658,10 +676,13 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 658 | 676 | <a class="primary new-task-open" href="/?new=1">(t(lang, Key::NewTask))</a> |
| 659 | 677 | } |
| 660 | 678 | <form class="field-box field-box-sort" method="get" action="/"> |
| 661 | - <span class="field-text">(t(lang, Key::Sort))</span> | |
| 662 | - <select class="status-select" name="sort" data-autosubmit=""> | |
| 663 | - for key in SORT_KEYS { | |
| 664 | - <option value=(key) selected=(key == sort)>(sort_label(key, lang))</option> | |
| 679 | + <input type="hidden" name="sort" value=(sort.clone())> | |
| 680 | + <input type="hidden" name="q" value=(search.as_deref().unwrap_or(""))> | |
| 681 | + <span class="field-text">(t(lang, Key::Project))</span> | |
| 682 | + <select class="status-select" name="tag" data-autosubmit="" data-search=""> | |
| 683 | + <option value="" selected=(tag_filter.is_none())>(t(lang, Key::AllTags))</option> | |
| 684 | + for tag in &tags { | |
| 685 | + <option value=(tag.id.clone()) selected=(tag_filter.as_deref() == Some(tag.id.as_str()))>(tag.name.clone())</option> | |
| 665 | 686 | } |
| 666 | 687 | </select> |
| 667 | 688 | <svg class="glyph" width="14" height="14" viewBox="0 0 16 16" fill="none" |
| @@ -671,11 +692,12 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 671 | 692 | </svg> |
| 672 | 693 | </form> |
| 673 | 694 | <form class="field-box field-box-sort" method="get" action="/"> |
| 674 | - <span class="field-text">(t(lang, Key::Project))</span> | |
| 675 | - <select class="status-select" name="tag" data-autosubmit="" data-search=""> | |
| 676 | - <option value="" selected=(tag_filter.is_none())>(t(lang, Key::AllTags))</option> | |
| 677 | - for tag in &tags { | |
| 678 | - <option value=(tag.id.clone()) selected=(tag_filter.as_deref() == Some(tag.id.as_str()))>(tag.name.clone())</option> | |
| 695 | + <input type="hidden" name="tag" value=(tag_filter.clone().unwrap_or_default())> | |
| 696 | + <input type="hidden" name="q" value=(search.as_deref().unwrap_or(""))> | |
| 697 | + <span class="field-text">(t(lang, Key::Sort))</span> | |
| 698 | + <select class="status-select" name="sort" data-autosubmit=""> | |
| 699 | + for key in SORT_KEYS { | |
| 700 | + <option value=(key) selected=(key == sort)>(sort_label(key, lang))</option> | |
| 679 | 701 | } |
| 680 | 702 | </select> |
| 681 | 703 | <svg class="glyph" width="14" height="14" viewBox="0 0 16 16" fill="none" |
| @@ -684,6 +706,12 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 684 | 706 | <path d="M4 6l4 4 4-4"></path> |
| 685 | 707 | </svg> |
| 686 | 708 | </form> |
| 709 | + <form class="field-box field-box-search" method="get" action="/"> | |
| 710 | + <input type="hidden" name="sort" value=(sort.clone())> | |
| 711 | + <input type="hidden" name="tag" value=(tag_filter.clone().unwrap_or_default())> | |
| 712 | + <span class="field-text">(t(lang, Key::Search))</span> | |
| 713 | + <input class="field-input" type="search" name="q" value=(search.as_deref().unwrap_or(""))> | |
| 714 | + </form> | |
| 687 | 715 | <div class="spacer"></div> |
| 688 | 716 | if overdue > 0 { <span class="chip chip-overdue">(format!("{overdue} {}", t(lang, Key::Overdue)))</span> } |
| 689 | 717 | if blocked > 0 { <span class="chip chip-blocked">(format!("{blocked} {}", t(lang, Key::Blocked)))</span> } |
| @@ -692,9 +720,13 @@ pub async fn board_page(cx: &Cx, user: &User) -> Result { | ||
| 692 | 720 | <p class="field-error">(refusal.message_in(lang))</p> |
| 693 | 721 | } |
| 694 | 722 | <main class="board-stage"> |
| 695 | - <div class="board-columns"> | |
| 696 | - (board_columns(cx, sort.clone(), tag_filter.clone(), default_tag_id).await?) | |
| 697 | - </div> | |
| 723 | + if search.is_some() && view_data.is_empty() { | |
| 724 | + <div class="scaffold-note"><p>(t(lang, Key::NoMatches))</p></div> | |
| 725 | + } else { | |
| 726 | + <div class="board-columns"> | |
| 727 | + (board_columns(cx, sort.clone(), tag_filter.clone(), search.clone(), default_tag_id).await?) | |
| 728 | + </div> | |
| 729 | + } | |
| 698 | 730 | </main> |
| 699 | 731 | if let Some(task_id) = &open_task { |
| 700 | 732 | (crate::detail::task_modal(cx, task_id, query.confirm.as_deref() == Some("delete"), open_tab).await?) |
crates/izlek-web/src/detail.rsmodified+2 -9
| @@ -1956,14 +1956,7 @@ pub async fn task_modal(cx: &Cx, task_id: &str, confirm_delete: bool, tab: Tab) | ||
| 1956 | 1956 | }; |
| 1957 | 1957 | let has_deps = !detail.blocked_by.is_empty() || !detail.blocks.is_empty(); |
| 1958 | 1958 | let accept = (!allowed_file_types.is_empty()) |
| 1959 | - .then(|| { | |
| 1960 | - allowed_file_types | |
| 1961 | - .iter() | |
| 1962 | - .map(|kind| format!(".{kind}")) | |
| 1963 | - .collect::<Vec<_>>() | |
| 1964 | - .join(",") | |
| 1965 | - }) | |
| 1966 | - .unwrap_or_default(); | |
| 1959 | + .then(|| crate::files::accept_attribute(&allowed_file_types)); | |
| 1967 | 1960 | |
| 1968 | 1961 | // The delete confirmation is computed eagerly rather than fetched on |
| 1969 | 1962 | // demand: there is no script here to hold the intermediate "did they |
| @@ -2193,7 +2186,7 @@ pub async fn task_modal(cx: &Cx, task_id: &str, confirm_delete: bool, tab: Tab) | ||
| 2193 | 2186 | <label class="field-box file-upload-box"> |
| 2194 | 2187 | (glyph::plus(cx).await?) |
| 2195 | 2188 | <span class="field-text file-upload-name">(t(lang, Key::File))</span> |
| 2196 | - <input class="file-upload-input" type="file" name="file" accept=(accept) required=""> | |
| 2189 | + <input class="file-upload-input" type="file" name="file" accept=(accept) multiple="" required=""> | |
| 2197 | 2190 | </label> |
| 2198 | 2191 | </form> |
| 2199 | 2192 | (refused(cx, "upload_file", lang).await?) |
crates/izlek-web/src/feed.rsadded+111 -0
| @@ -0,0 +1,111 @@ | ||
| 1 | +//! "What changed for me": the events a signed-in person has a stake in. | |
| 2 | +//! | |
| 3 | +//! Every role gets it — a Viewer's feed is simply emptier, not hidden. Two | |
| 4 | +//! things put a line on someone's feed: it happened on a task they watch | |
| 5 | +//! (making a task, being assigned, commenting each start a watch; an | |
| 6 | +//! unassign ends it), or it names them. Nobody's own actions come back to | |
| 7 | +//! them — the store's feed query leaves those off. The watch and marker | |
| 8 | +//! mechanics live in the store (`feed_for_user`, `count_feed_unseen`, | |
| 9 | +//! `mark_feed_seen`); this page only reads and renders. | |
| 10 | +//! | |
| 11 | +//! Visiting is reading: the page marks the feed seen after fetching its | |
| 12 | +//! rows, so the badge on the next page anywhere — and on this one, which | |
| 13 | +//! renders after the mark — reads zero. New events announce on the live | |
| 14 | +//! channel like every other write, and the shell's refresh morph brings | |
| 15 | +//! both this page and the topbar badge back up to date. | |
| 16 | + | |
| 17 | +use time::OffsetDateTime; | |
| 18 | +use topcoat::Result; | |
| 19 | +use topcoat::context::Cx; | |
| 20 | +use topcoat::router::{error::not_found, page}; | |
| 21 | +use topcoat::view::view; | |
| 22 | + | |
| 23 | + | |
| 24 | +use crate::i18n::{Key, Lang, t}; | |
| 25 | +use crate::server::{accounts, require_user}; | |
| 26 | + | |
| 27 | +/// How many lines one page draws, the same cap the logs page reads by: a | |
| 28 | +/// read a person can finish, panned inside the shell, never a document | |
| 29 | +/// growing past the viewport. | |
| 30 | +const LIMIT: u32 = 50; | |
| 31 | + | |
| 32 | +/// One rendered feed line: the strings the view stamps in, decided here so | |
| 33 | +/// the template stays a list of facts rather than a chain of lookups. | |
| 34 | +struct FeedRow { | |
| 35 | + task_label: String, | |
| 36 | + href: String, | |
| 37 | + actor: String, | |
| 38 | + sentence: String, | |
| 39 | + when: String, | |
| 40 | +} | |
| 41 | + | |
| 42 | +/// The person's feed. `/feed`. | |
| 43 | +#[page("/feed")] | |
| 44 | +async fn feed_page(cx: &Cx) -> Result { | |
| 45 | + let user = match require_user(cx).await { | |
| 46 | + Ok(user) => user, | |
| 47 | + Err(_) => return Err(not_found().into()), | |
| 48 | + }; | |
| 49 | + let lang = Lang::from_code(&user.language); | |
| 50 | + let store = accounts(cx).store().clone(); | |
| 51 | + let lines = store.feed_for_user(&user.id, LIMIT).await?; | |
| 52 | + // The visit is the marker, taken after the rows are read: the badge on | |
| 53 | + // the next page anywhere reads zero, and this page's own topbar — | |
| 54 | + // rendered after the mark — agrees with it. | |
| 55 | + store | |
| 56 | + .mark_feed_seen(&user.id, OffsetDateTime::now_utc()) | |
| 57 | + .await?; | |
| 58 | + let zone = izlek_core::detail::parse_zone(&user.timezone); | |
| 59 | + | |
| 60 | + let rows: Vec<FeedRow> = lines | |
| 61 | + .iter() | |
| 62 | + .map(|line| FeedRow { | |
| 63 | + task_label: match (&line.task_key, &line.title) { | |
| 64 | + (Some(key), Some(title)) => format!("{key} · {title}"), | |
| 65 | + (Some(key), None) => key.clone(), | |
| 66 | + _ => line.title.clone().unwrap_or_default(), | |
| 67 | + }, | |
| 68 | + href: format!("/?task={}", line.task_id.as_deref().unwrap_or_default()), | |
| 69 | + actor: line | |
| 70 | + .actor_name | |
| 71 | + .clone() | |
| 72 | + .unwrap_or_else(|| t(lang, Key::TheSystem).to_string()), | |
| 73 | + sentence: crate::logs::activity_sentence(&line.kind, &line.detail, lang), | |
| 74 | + when: izlek_core::detail::moment_label_in(line.at, zone), | |
| 75 | + }) | |
| 76 | + .collect(); | |
| 77 | + | |
| 78 | + view! { | |
| 79 | + cx => | |
| 80 | + <header class="topbar"> | |
| 81 | + (crate::layout::mark(cx).await?) | |
| 82 | + (crate::layout::topbar_nav(cx, crate::layout::NavPage::Feed, user.role, lang).await?) | |
| 83 | + <div class="spacer"></div> | |
| 84 | + (crate::layout::user_menu(cx, &crate::detail::Me::from(&user), lang).await?) | |
| 85 | + </header> | |
| 86 | + | |
| 87 | + <main class="feed-shell"> | |
| 88 | + <section class="panel"> | |
| 89 | + <div class="panel-head"> | |
| 90 | + <h2 class="panel-title">(t(lang, Key::NavFeed))</h2> | |
| 91 | + </div> | |
| 92 | + if rows.is_empty() { | |
| 93 | + <p class="feed-empty">(t(lang, Key::FeedEmpty))</p> | |
| 94 | + } else { | |
| 95 | + <ol class="feed-list"> | |
| 96 | + for row in rows { | |
| 97 | + <li class="feed-row"> | |
| 98 | + <a class="feed-task" href=(row.href)>(row.task_label)</a> | |
| 99 | + <span class="feed-line"> | |
| 100 | + <span class="feed-actor">(row.actor)</span> | |
| 101 | + <span class="feed-sentence">(row.sentence)</span> | |
| 102 | + </span> | |
| 103 | + <span class="feed-when">(row.when)</span> | |
| 104 | + </li> | |
| 105 | + } | |
| 106 | + </ol> | |
| 107 | + } | |
| 108 | + </section> | |
| 109 | + </main> | |
| 110 | + } | |
| 111 | +} |
crates/izlek-web/src/files.rsmodified+104 -40
| @@ -69,6 +69,51 @@ fn label_of(file_name: &str) -> String { | ||
| 69 | 69 | base.chars().filter(|c| !c.is_control()).collect() |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | +/// The `accept` attribute the task modal's upload input wears, built from | |
| 73 | +/// the workspace's allowed extensions. Known extensions travel as MIME | |
| 74 | +/// types — a mobile picker filters on MIME and opens on the documents | |
| 75 | +/// shelf, with the camera at most one option among many and never the | |
| 76 | +/// forced door; an unknown extension keeps its `.ext` form; an empty list | |
| 77 | +/// is no attribute at all, and the picker opens on the whole filesystem. | |
| 78 | +/// The word `capture` appears nowhere in this crate: it is the attribute | |
| 79 | +/// that turns a file input into a camera. | |
| 80 | +pub(crate) fn accept_attribute(allowed: &[String]) -> String { | |
| 81 | + allowed | |
| 82 | + .iter() | |
| 83 | + .map(|ext| { | |
| 84 | + let ext = ext.trim_start_matches('.').to_lowercase(); | |
| 85 | + match mime_of_extension(&ext) { | |
| 86 | + Some(mime) => mime.to_string(), | |
| 87 | + None => format!(".{ext}"), | |
| 88 | + } | |
| 89 | + }) | |
| 90 | + .collect::<Vec<_>>() | |
| 91 | + .join(",") | |
| 92 | +} | |
| 93 | + | |
| 94 | +/// The MIME types a mobile picker understands, for the extensions a | |
| 95 | +/// workspace is likely to allow; anything else stays an extension hint. | |
| 96 | +fn mime_of_extension(ext: &str) -> Option<&'static str> { | |
| 97 | + match ext { | |
| 98 | + "png" => Some("image/png"), | |
| 99 | + "jpg" | "jpeg" => Some("image/jpeg"), | |
| 100 | + "gif" => Some("image/gif"), | |
| 101 | + "webp" => Some("image/webp"), | |
| 102 | + "svg" => Some("image/svg+xml"), | |
| 103 | + "pdf" => Some("application/pdf"), | |
| 104 | + "txt" | "md" => Some("text/plain"), | |
| 105 | + "csv" => Some("text/csv"), | |
| 106 | + "json" => Some("application/json"), | |
| 107 | + "zip" => Some("application/zip"), | |
| 108 | + "mp4" => Some("video/mp4"), | |
| 109 | + "webm" => Some("video/webm"), | |
| 110 | + "mp3" => Some("audio/mpeg"), | |
| 111 | + "wav" => Some("audio/wav"), | |
| 112 | + "ogg" => Some("audio/ogg"), | |
| 113 | + _ => None, | |
| 114 | + } | |
| 115 | +} | |
| 116 | + | |
| 72 | 117 | /// What the bytes are, decided from the bytes themselves — never from the |
| 73 | 118 | /// part's `content_type()`, which is whatever the browser felt like sending. |
| 74 | 119 | pub(crate) fn sniff(bytes: &[u8]) -> &'static str { |
| @@ -291,14 +336,18 @@ struct DownloadQuery { | ||
| 291 | 336 | dl: Option<String>, |
| 292 | 337 | } |
| 293 | 338 | |
| 294 | -/// Takes one file onto a task. The route this hangs off already caps the | |
| 295 | -/// whole request body at the widest limit any workspace could set | |
| 296 | -/// ([`crate::settings::WIDEST_ATTACHMENT_MB`], registered in `main.rs`); this handler enforces | |
| 297 | -/// the workspace's own, usually narrower, limit while the bytes are still | |
| 298 | -/// arriving rather than after they have all landed. | |
| 339 | +/// Takes the files a task's upload form carries onto that task — one per | |
| 340 | +/// `file` part, and a `multiple` picker posts several. The route this hangs | |
| 341 | +/// off already caps the whole request body at the widest limit any | |
| 342 | +/// workspace could set ([`crate::settings::WIDEST_ATTACHMENT_MB`], | |
| 343 | +/// registered in `main.rs`); this handler enforces the workspace's own, | |
| 344 | +/// usually narrower, limit per file while the bytes are still arriving | |
| 345 | +/// rather than after they have all landed, and a part that breaks it | |
| 346 | +/// refuses the whole post before anything is inserted. | |
| 299 | 347 | /// |
| 300 | -/// Fields arrive in request order: `task_id` before `file`, so the task and | |
| 301 | -/// the workspace's limits are known before a byte of the file is kept. | |
| 348 | +/// Fields arrive in request order: `task_id` before the `file` parts, so | |
| 349 | +/// the task and the workspace's limits are known before a byte of any file | |
| 350 | +/// is kept. | |
| 302 | 351 | #[route(POST "/files")] |
| 303 | 352 | async fn upload( |
| 304 | 353 | cx: &Cx, |
| @@ -314,8 +363,7 @@ async fn upload( | ||
| 314 | 363 | |
| 315 | 364 | let mut task_id: Option<String> = None; |
| 316 | 365 | let mut comment_id: Option<String> = None; |
| 317 | - let mut file_name: Option<String> = None; | |
| 318 | - let mut bytes: Option<Vec<u8>> = None; | |
| 366 | + let mut files: Vec<(String, Vec<u8>)> = Vec::new(); | |
| 319 | 367 | |
| 320 | 368 | let store = accounts(cx).store().clone(); |
| 321 | 369 | |
| @@ -374,8 +422,7 @@ async fn upload( | ||
| 374 | 422 | } |
| 375 | 423 | } |
| 376 | 424 | |
| 377 | - file_name = Some(name); | |
| 378 | - bytes = Some(collected); | |
| 425 | + files.push((name, collected)); | |
| 379 | 426 | } |
| 380 | 427 | _ => {} |
| 381 | 428 | } |
| @@ -384,10 +431,9 @@ async fn upload( | ||
| 384 | 431 | let Some(task_id) = task_id else { |
| 385 | 432 | return Ok(home(Refusal::NotFound)); |
| 386 | 433 | }; |
| 387 | - let Some(file_name) = file_name else { | |
| 434 | + if files.is_empty() { | |
| 388 | 435 | return Ok(back_to(&task_id, Some(Refusal::NoFile))); |
| 389 | - }; | |
| 390 | - let bytes = bytes.unwrap_or_default(); | |
| 436 | + } | |
| 391 | 437 | |
| 392 | 438 | // A `comment_id` only travels if it names a comment that is actually on |
| 393 | 439 | // this task; anything else is dropped rather than refused, since the |
| @@ -404,32 +450,36 @@ async fn upload( | ||
| 404 | 450 | None => None, |
| 405 | 451 | }; |
| 406 | 452 | |
| 407 | - let label = label_of(&file_name); | |
| 408 | - let mime_type = sniff(&bytes); | |
| 409 | - | |
| 410 | - let added = store | |
| 411 | - .add_attachment(NewAttachment { | |
| 412 | - task_id: &task_id, | |
| 413 | - comment_id: comment_id.as_deref(), | |
| 414 | - file_name: &label, | |
| 415 | - mime_type, | |
| 416 | - bytes, | |
| 417 | - uploaded_by: &user.id, | |
| 418 | - at: OffsetDateTime::now_utc(), | |
| 419 | - }) | |
| 420 | - .await; | |
| 421 | - | |
| 422 | - if added.is_ok() && comment_id.is_none() { | |
| 423 | - let _ = store | |
| 424 | - .record_activity( | |
| 425 | - &task_id, | |
| 426 | - Some(&user.id), | |
| 427 | - None, | |
| 428 | - &izlek_core::detail::ActivityKind::FileAdded, | |
| 429 | - &label, | |
| 430 | - OffsetDateTime::now_utc(), | |
| 431 | - ) | |
| 432 | - .await; | |
| 453 | + let mut added: Result<(), izlek_core::store::StoreError> = Ok(()); | |
| 454 | + for (file_name, bytes) in files { | |
| 455 | + let label = label_of(&file_name); | |
| 456 | + let mime_type = sniff(&bytes); | |
| 457 | + | |
| 458 | + added = store | |
| 459 | + .add_attachment(NewAttachment { | |
| 460 | + task_id: &task_id, | |
| 461 | + comment_id: comment_id.as_deref(), | |
| 462 | + file_name: &label, | |
| 463 | + mime_type, | |
| 464 | + bytes, | |
| 465 | + uploaded_by: &user.id, | |
| 466 | + at: OffsetDateTime::now_utc(), | |
| 467 | + }) | |
| 468 | + .await | |
| 469 | + .map(|_| ()); | |
| 470 | + | |
| 471 | + if added.is_ok() && comment_id.is_none() { | |
| 472 | + let _ = store | |
| 473 | + .record_activity( | |
| 474 | + &task_id, | |
| 475 | + Some(&user.id), | |
| 476 | + None, | |
| 477 | + &izlek_core::detail::ActivityKind::FileAdded, | |
| 478 | + &label, | |
| 479 | + OffsetDateTime::now_utc(), | |
| 480 | + ) | |
| 481 | + .await; | |
| 482 | + } | |
| 433 | 483 | } |
| 434 | 484 | |
| 435 | 485 | Ok(match added { |
| @@ -662,4 +712,18 @@ mod tests { | ||
| 662 | 712 | "an empty file has no range to satisfy" |
| 663 | 713 | ); |
| 664 | 714 | } |
| 715 | + | |
| 716 | + #[test] | |
| 717 | + fn accept_attribute_speaks_mime_for_known_extensions_and_dots_for_the_rest() { | |
| 718 | + let allowed: Vec<String> = ["png", "PDF", ".txt", "weird1"] | |
| 719 | + .iter() | |
| 720 | + .map(|s| s.to_string()) | |
| 721 | + .collect(); | |
| 722 | + assert_eq!( | |
| 723 | + accept_attribute(&allowed), | |
| 724 | + "image/png,application/pdf,text/plain,.weird1", | |
| 725 | + "MIME types filter a mobile picker; the camera is never the only door" | |
| 726 | + ); | |
| 727 | + assert_eq!(accept_attribute(&[]), "", "no list, no attribute"); | |
| 728 | + } | |
| 665 | 729 | } |
crates/izlek-web/src/i18n.rsmodified+40 -0
| @@ -365,6 +365,19 @@ pub enum Key { | ||
| 365 | 365 | ReminderMinutesLabel, |
| 366 | 366 | ClockHour, |
| 367 | 367 | ClockMinute, |
| 368 | + Search, | |
| 369 | + NoMatches, | |
| 370 | + ForgotIt, | |
| 371 | + ForgotTitle, | |
| 372 | + ForgotSend, | |
| 373 | + ForgotSent, | |
| 374 | + ResetTitle, | |
| 375 | + ResetLinkDead, | |
| 376 | + ActResetRequested, | |
| 377 | + ActResetDone, | |
| 378 | + NavFeed, | |
| 379 | + ActReminded, | |
| 380 | + FeedEmpty, | |
| 368 | 381 | } |
| 369 | 382 | |
| 370 | 383 | /// The lone templated phrase — a name in the middle reads worse as two |
| @@ -1039,6 +1052,33 @@ pub fn t(lang: Lang, key: Key) -> &'static str { | ||
| 1039 | 1052 | (ClockHour, Tr) => "Saat", |
| 1040 | 1053 | (ClockMinute, En) => "Minute", |
| 1041 | 1054 | (ClockMinute, Tr) => "Dakika", |
| 1055 | + | |
| 1056 | + (Search, En) => "Search", | |
| 1057 | + (Search, Tr) => "Ara", | |
| 1058 | + (NoMatches, En) => "No matches", | |
| 1059 | + (NoMatches, Tr) => "Eşleşme yok", | |
| 1060 | + (ForgotIt, En) => "Forgot it?", | |
| 1061 | + (ForgotIt, Tr) => "Parolamı unuttum", | |
| 1062 | + (ForgotTitle, En) => "Reset the password", | |
| 1063 | + (ForgotTitle, Tr) => "Parolayı sıfırla", | |
| 1064 | + (ForgotSend, En) => "Send the link", | |
| 1065 | + (ForgotSend, Tr) => "Bağlantıyı gönder", | |
| 1066 | + (ForgotSent, En) => "If the address has an account, a reset link is on its way.", | |
| 1067 | + (ForgotSent, Tr) => "Adres bir hesaba aitse sıfırlama bağlantısı yolda.", | |
| 1068 | + (ResetTitle, En) => "Choose a new password", | |
| 1069 | + (ResetTitle, Tr) => "Yeni parola seç", | |
| 1070 | + (ResetLinkDead, En) => "This reset link no longer works", | |
| 1071 | + (ResetLinkDead, Tr) => "Bu sıfırlama bağlantısı artık çalışmıyor", | |
| 1072 | + (ActResetRequested, En) => "asked to reset the password", | |
| 1073 | + (ActResetRequested, Tr) => "parola sıfırlaması istedi", | |
| 1074 | + (ActResetDone, En) => "set a new password by reset link", | |
| 1075 | + (ActResetDone, Tr) => "sıfırlama bağlantısıyla yeni parola aldı", | |
| 1076 | + (ActReminded, En) => "sent a reminder mail", | |
| 1077 | + (ActReminded, Tr) => "hatırlatma gönderdi", | |
| 1078 | + (NavFeed, En) => "For me", | |
| 1079 | + (NavFeed, Tr) => "Benim için", | |
| 1080 | + (FeedEmpty, En) => "Nothing yet.", | |
| 1081 | + (FeedEmpty, Tr) => "Henüz bir şey yok.", | |
| 1042 | 1082 | } |
| 1043 | 1083 | } |
| 1044 | 1084 |
crates/izlek-web/src/layout.rsmodified+34 -9
| @@ -133,10 +133,11 @@ pub async fn user_menu(cx: &Cx, me: &crate::detail::Me, lang: Lang) -> Result { | ||
| 133 | 133 | } |
| 134 | 134 | } |
| 135 | 135 | |
| 136 | -/// The five signed-in pages, as the topbar nav links between them. | |
| 136 | +/// The signed-in pages, as the topbar nav links between them. | |
| 137 | 137 | #[derive(Clone, Copy, PartialEq)] |
| 138 | 138 | pub enum NavPage { |
| 139 | 139 | Board, |
| 140 | + Feed, | |
| 140 | 141 | Rules, |
| 141 | 142 | Logs, |
| 142 | 143 | Tags, |
| @@ -144,11 +145,19 @@ pub enum NavPage { | ||
| 144 | 145 | } |
| 145 | 146 | |
| 146 | 147 | impl NavPage { |
| 147 | - const ALL: [Self; 5] = [Self::Board, Self::Rules, Self::Logs, Self::Tags, Self::Settings]; | |
| 148 | + const ALL: [Self; 6] = [ | |
| 149 | + Self::Board, | |
| 150 | + Self::Feed, | |
| 151 | + Self::Rules, | |
| 152 | + Self::Logs, | |
| 153 | + Self::Tags, | |
| 154 | + Self::Settings, | |
| 155 | + ]; | |
| 148 | 156 | |
| 149 | 157 | fn href(self) -> &'static str { |
| 150 | 158 | match self { |
| 151 | 159 | Self::Board => "/", |
| 160 | + Self::Feed => "/feed", | |
| 152 | 161 | Self::Rules => "/rules", |
| 153 | 162 | Self::Logs => "/logs", |
| 154 | 163 | Self::Tags => "/tags", |
| @@ -159,6 +168,7 @@ impl NavPage { | ||
| 159 | 168 | fn label(self) -> Key { |
| 160 | 169 | match self { |
| 161 | 170 | Self::Board => Key::NavBoard, |
| 171 | + Self::Feed => Key::NavFeed, | |
| 162 | 172 | Self::Rules => Key::NavMailRules, |
| 163 | 173 | Self::Logs => Key::NavLogs, |
| 164 | 174 | Self::Tags => Key::NavTags, |
| @@ -167,11 +177,21 @@ impl NavPage { | ||
| 167 | 177 | } |
| 168 | 178 | } |
| 169 | 179 | |
| 170 | -/// The topbar's page nav, shared by every signed-in page: the five pages | |
| 171 | -/// with the current one marked, the admin-only ones (rules, logs, tags) shown | |
| 172 | -/// only to roles that can administer. Plain `<a>`s — the soft-nav forwarder | |
| 173 | -/// swaps them like any same-origin link, so `data-hard` stays off. | |
| 180 | +/// The topbar's page nav, shared by every signed-in page: the pages with the | |
| 181 | +/// current one marked, the admin-only ones (rules, logs, tags) shown only to | |
| 182 | +/// roles that can administer. The feed link carries the unread count — how | |
| 183 | +/// many lines have landed since the person last read their feed — as a chip | |
| 184 | +/// that disappears at zero. Plain `<a>`s — the soft-nav forwarder swaps them | |
| 185 | +/// like any same-origin link, so `data-hard` stays off. | |
| 174 | 186 | pub async fn topbar_nav(cx: &Cx, active: NavPage, role: izlek_core::Role, lang: Lang) -> Result { |
| 187 | + let unread = match current_user(cx).await { | |
| 188 | + Ok(Some(me)) => crate::server::accounts(cx) | |
| 189 | + .store() | |
| 190 | + .count_feed_unseen(&me.id) | |
| 191 | + .await | |
| 192 | + .unwrap_or(0), | |
| 193 | + _ => 0, | |
| 194 | + }; | |
| 175 | 195 | view! { |
| 176 | 196 | cx => |
| 177 | 197 | <nav class="topbar-nav-links"> |
| @@ -182,6 +202,9 @@ pub async fn topbar_nav(cx: &Cx, active: NavPage, role: izlek_core::Role, lang: | ||
| 182 | 202 | href=(page.href()) |
| 183 | 203 | > |
| 184 | 204 | (t(lang, page.label())) |
| 205 | + if page == NavPage::Feed && unread > 0 { | |
| 206 | + <span class="feed-badge">(unread)</span> | |
| 207 | + } | |
| 185 | 208 | </a> |
| 186 | 209 | } |
| 187 | 210 | } |
| @@ -494,7 +517,9 @@ pub async fn soft_nav_script(cx: &Cx) -> Result { | ||
| 494 | 517 | if (control.classList && control.classList.contains('file-upload-input')) { \ |
| 495 | 518 | var label = control.closest('label'); \ |
| 496 | 519 | var name = label ? label.querySelector('.file-upload-name') : null; \ |
| 497 | - if (name && control.files && control.files[0]) { name.textContent = control.files[0].name; } \ | |
| 520 | + if (name && control.files && control.files[0]) { \ | |
| 521 | + name.textContent = control.files[0].name + (control.files.length > 1 ? ' +' + (control.files.length - 1) : ''); \ | |
| 522 | + } \ | |
| 498 | 523 | control.form.requestSubmit(); \ |
| 499 | 524 | return; \ |
| 500 | 525 | } \ |
| @@ -676,8 +701,8 @@ pub async fn live_script(cx: &Cx) -> Result { | ||
| 676 | 701 | if (path === '/rules') { return topic === 'rules' || topic === 'members'; } \ |
| 677 | 702 | if (path === '/tags') { return topic === 'tags' || topic === 'members'; } \ |
| 678 | 703 | if (path === '/settings') { return topic === 'settings' || topic === 'members'; } \ |
| 679 | - if (path.indexOf('/people/') === 0) { return topic === 'members'; } \ | |
| 680 | - return topic === 'board' || topic === 'task' || topic === 'members'; \ | |
| 704 | + if (path === '/people/') { return topic === 'members'; } \ | |
| 705 | + if (path === '/feed') { return true; } \ | |
| 681 | 706 | } \ |
| 682 | 707 | try { \ |
| 683 | 708 | var src = new EventSource('/api/live'); \ |
crates/izlek-web/src/lib.rsmodified+1 -0
| @@ -7,6 +7,7 @@ pub mod auth; | ||
| 7 | 7 | pub mod board; |
| 8 | 8 | pub mod detail; |
| 9 | 9 | pub mod dropdown; |
| 10 | +pub mod feed; | |
| 10 | 11 | pub mod files; |
| 11 | 12 | pub mod i18n; |
| 12 | 13 | pub mod layout; |
crates/izlek-web/src/logs.rsmodified+4 -1
| @@ -152,7 +152,7 @@ pub(crate) fn outcome_word(outcome: izlek_core::store::MailOutcome, lang: Lang) | ||
| 152 | 152 | /// The sentence after a task's name, mirroring `ActivityEntry::sentence` — |
| 153 | 153 | /// the per-task strip on the detail screen — since the workspace feed carries |
| 154 | 154 | /// the same kind and detail but no `Person` to build an `ActivityEntry` from. |
| 155 | -fn activity_sentence(kind: &izlek_core::detail::ActivityKind, detail: &str, lang: Lang) -> String { | |
| 155 | +pub(crate) fn activity_sentence(kind: &izlek_core::detail::ActivityKind, detail: &str, lang: Lang) -> String { | |
| 156 | 156 | use izlek_core::detail::ActivityKind; |
| 157 | 157 | let detail = detail.trim(); |
| 158 | 158 | match kind { |
| @@ -201,6 +201,9 @@ fn activity_sentence(kind: &izlek_core::detail::ActivityKind, detail: &str, lang | ||
| 201 | 201 | "tag_moved" => crate::i18n::tag_moved_label(lang, detail), |
| 202 | 202 | "send_retried" => t(lang, Key::ActSendRetried).to_string(), |
| 203 | 203 | "photo_saved" => t(lang, Key::ActPhotoSaved).to_string(), |
| 204 | + "password_reset_requested" => t(lang, Key::ActResetRequested).to_string(), | |
| 205 | + "password_reset_completed" => t(lang, Key::ActResetDone).to_string(), | |
| 206 | + "reminded" => t(lang, Key::ActReminded).to_string(), | |
| 204 | 207 | "photo_removed" => t(lang, Key::ActPhotoRemoved).to_string(), |
| 205 | 208 | "sender_checked" => t(lang, Key::ActSenderChecked).to_string(), |
| 206 | 209 | _ => detail.to_string(), |
crates/izlek-web/src/pages.rsmodified+122 -0
| @@ -178,6 +178,7 @@ async fn sign_in_card(cx: &Cx) -> Result { | ||
| 178 | 178 | <span class="auth-key">"↵"</span> |
| 179 | 179 | </button> |
| 180 | 180 | </form> |
| 181 | + <a class="auth-alt" href="/forgot">(t(lang, Key::ForgotIt))</a> | |
| 181 | 182 | if let Some(refusal) = &refusal { |
| 182 | 183 | <div class="auth-problem">(refusal.message())</div> |
| 183 | 184 | } |
| @@ -244,3 +245,124 @@ async fn join_card(cx: &Cx, token: &str, person: Invited) -> Result { | ||
| 244 | 245 | async fn signed_in_shell(cx: &Cx, user: User) -> Result { |
| 245 | 246 | crate::board::board_page(cx, &user).await |
| 246 | 247 | } |
| 248 | + | |
| 249 | +/// "Forgot it?" — the self-serve reset ask, reached from the sign-in card. | |
| 250 | +/// It answers the same whether the address has an account: one terse note, | |
| 251 | +/// the same one for every address. | |
| 252 | +#[page("/forgot")] | |
| 253 | +async fn forgot(cx: &Cx) -> Result { | |
| 254 | + let lang = Lang::En; | |
| 255 | + let refusal = refusal_of(cx, "forgot_password"); | |
| 256 | + let sent = topcoat::router::request::uri(cx) | |
| 257 | + .query() | |
| 258 | + .and_then(|query| { | |
| 259 | + query.split('&').find_map(|pair| { | |
| 260 | + let (key, value) = pair.split_once('=')?; | |
| 261 | + (key == "sent" && value == "forgot_password").then_some(()) | |
| 262 | + }) | |
| 263 | + }) | |
| 264 | + .is_some(); | |
| 265 | + | |
| 266 | + view! { | |
| 267 | + cx => | |
| 268 | + (topbar(cx).await?) | |
| 269 | + <main class="auth-stage"> | |
| 270 | + <div class="auth-column"> | |
| 271 | + <div class="auth-card"> | |
| 272 | + <div class="auth-head"> | |
| 273 | + <div class="auth-title">(t(lang, Key::ForgotTitle))</div> | |
| 274 | + </div> | |
| 275 | + <form method="post" action="/api/forgot_password" data-hard=""> | |
| 276 | + <label class="auth-field"> | |
| 277 | + <span class="auth-label">(t(lang, Key::EmailLabel))</span> | |
| 278 | + <input | |
| 279 | + class="auth-input auth-input-mono" | |
| 280 | + type="email" | |
| 281 | + name="email" | |
| 282 | + autocomplete="email" | |
| 283 | + required="" | |
| 284 | + > | |
| 285 | + </label> | |
| 286 | + <button class="auth-submit" type="submit"> | |
| 287 | + <span class="auth-submit-text">(t(lang, Key::ForgotSend))</span> | |
| 288 | + <span class="auth-key">"↵"</span> | |
| 289 | + </button> | |
| 290 | + </form> | |
| 291 | + if sent { | |
| 292 | + <div class="auth-note">(t(lang, Key::ForgotSent))</div> | |
| 293 | + } | |
| 294 | + if let Some(refusal) = &refusal { | |
| 295 | + <div class="auth-problem">(refusal.message())</div> | |
| 296 | + } | |
| 297 | + </div> | |
| 298 | + </div> | |
| 299 | + </main> | |
| 300 | + } | |
| 301 | +} | |
| 302 | + | |
| 303 | +/// "Choose a new password" — reached from the mailed reset link. Same shape | |
| 304 | +/// as the invitation's pick-a-password card; the address is the account's | |
| 305 | +/// own and cannot be edited here. A spent, expired or unknown token shows | |
| 306 | +/// the dead card, and says which kind of link it was. | |
| 307 | +#[page("/reset/{token}")] | |
| 308 | +async fn reset(cx: &Cx) -> Result { | |
| 309 | + let token: &str = path_param::<Token>(cx); | |
| 310 | + match crate::auth::reset_by_token(cx, token).await? { | |
| 311 | + Some(person) => reset_card(cx, token, person).await, | |
| 312 | + None => view! { | |
| 313 | + cx => | |
| 314 | + <main class="auth-stage"> | |
| 315 | + <div class="auth-column"> | |
| 316 | + <div class="auth-card"> | |
| 317 | + <div class="auth-title">(t(Lang::En, Key::ResetLinkDead))</div> | |
| 318 | + </div> | |
| 319 | + </div> | |
| 320 | + </main> | |
| 321 | + }, | |
| 322 | + } | |
| 323 | +} | |
| 324 | + | |
| 325 | +/// The reset card itself, once the token resolved to a live reset link. | |
| 326 | +async fn reset_card(cx: &Cx, token: &str, person: Invited) -> Result { | |
| 327 | + let refusal = refusal_of(cx, "redeem_reset"); | |
| 328 | + let lang = Lang::En; | |
| 329 | + | |
| 330 | + view! { | |
| 331 | + cx => | |
| 332 | + <main class="auth-stage"> | |
| 333 | + <div class="auth-column"> | |
| 334 | + <div class="auth-card"> | |
| 335 | + <div class="auth-head"> | |
| 336 | + <div class="auth-title">(t(lang, Key::ResetTitle))</div> | |
| 337 | + </div> | |
| 338 | + <div class="auth-field"> | |
| 339 | + <span class="auth-label">(t(lang, Key::SigningInAsLabel))</span> | |
| 340 | + <div class="auth-locked"> | |
| 341 | + <span class="auth-locked-value">(person.email)</span> | |
| 342 | + </div> | |
| 343 | + </div> | |
| 344 | + <form method="post" action="/api/redeem_reset" data-hard=""> | |
| 345 | + <input type="hidden" name="token" value=(token)> | |
| 346 | + <label class="auth-field"> | |
| 347 | + <span class="auth-label">(t(lang, Key::NewPasswordLabel))</span> | |
| 348 | + <input | |
| 349 | + class="auth-input auth-input-mono" | |
| 350 | + type="password" | |
| 351 | + name="password" | |
| 352 | + autocomplete="new-password" | |
| 353 | + minlength=(izlek_core::auth::MIN_PASSWORD_CHARS.to_string()) | |
| 354 | + required="" | |
| 355 | + > | |
| 356 | + </label> | |
| 357 | + <button class="auth-submit" type="submit"> | |
| 358 | + <span class="auth-submit-text">(t(lang, Key::SetPasswordAndSignIn))</span> | |
| 359 | + </button> | |
| 360 | + </form> | |
| 361 | + if let Some(refusal) = &refusal { | |
| 362 | + <div class="auth-problem">(refusal.message())</div> | |
| 363 | + } | |
| 364 | + </div> | |
| 365 | + </div> | |
| 366 | + </main> | |
| 367 | + } | |
| 368 | +} |
crates/izlek-web/src/people.rsmodified+6 -0
| @@ -48,6 +48,12 @@ fn kind_word(lang: Lang, kind: &ActivityKind) -> String { | ||
| 48 | 48 | ActivityKind::LimitsSaved => t(lang, Key::ActLimitsSaved).into(), |
| 49 | 49 | ActivityKind::TestMailSent => t(lang, Key::ActTestMailSent).into(), |
| 50 | 50 | ActivityKind::MessageSent => t(lang, Key::ActMessageSent).into(), |
| 51 | + ActivityKind::Other(raw) if raw == "password_reset_requested" => { | |
| 52 | + t(lang, Key::ActResetRequested).into() | |
| 53 | + } | |
| 54 | + ActivityKind::Other(raw) if raw == "password_reset_completed" => { | |
| 55 | + t(lang, Key::ActResetDone).into() | |
| 56 | + } | |
| 51 | 57 | other => crate::i18n::activity_kind_word(lang, other.as_str()), |
| 52 | 58 | } |
| 53 | 59 | } |
crates/izlek-web/tests/browser/moment.mjsmodified+23 -8
| @@ -112,16 +112,31 @@ for (const [name, value] of [ | ||
| 112 | 112 | const trigger = page.locator( |
| 113 | 113 | `.modal-new-task select[name="${name}"] >> xpath=preceding-sibling::button[1]`, |
| 114 | 114 | ); |
| 115 | - await trigger.click({ timeout: 5000 }); | |
| 116 | - const menu = page.locator('.dd-panel.dd-open'); | |
| 117 | - await menu.locator('.dd-search').fill(value); | |
| 118 | - const rowOn = await menu.locator('.dd-search').evaluate((el, want) => { | |
| 119 | - const panel = el.closest('.dd-panel'); | |
| 115 | + // A pointer click makes Playwright scroll the clipped trigger into view | |
| 116 | + // first, and the modal's scroll event lands after the click has already | |
| 117 | + // opened the panel — the scroll-close then slams it. The scroll is the | |
| 118 | + // automation's, not a hand's: a hand opens what is already on screen. | |
| 119 | + // So the whole gesture — open, type to filter, press the row — runs | |
| 120 | + // through the page's own handlers in one go, with no synthetic scroll | |
| 121 | + // between open and commit. | |
| 122 | + const driven = await trigger.evaluate((el, want) => { | |
| 123 | + el.click(); | |
| 124 | + const panel = el.__ddPanel; | |
| 125 | + if (!panel || !panel.classList.contains('dd-open')) { | |
| 126 | + return { opened: false, rowOn: false }; | |
| 127 | + } | |
| 128 | + const search = panel.__ddSearch; | |
| 129 | + search.value = want; | |
| 130 | + search.dispatchEvent(new Event('input', { bubbles: true })); | |
| 120 | 131 | const row = panel.querySelector(`.dd-option[data-value="${want}"]`); |
| 121 | - return !!row && !row.classList.contains('dd-option-hidden'); | |
| 132 | + const rowOn = !!row && !row.classList.contains('dd-option-hidden'); | |
| 133 | + if (rowOn) { | |
| 134 | + row.click(); | |
| 135 | + } | |
| 136 | + return { opened: true, rowOn }; | |
| 122 | 137 | }, value); |
| 123 | - if (!rowOn) failures.push(`typing '${value}' in the ${name} menu hid its row`); | |
| 124 | - await menu.locator(`.dd-option[data-value="${value}"]`).evaluate((el) => el.click()); | |
| 138 | + if (!driven || !driven.opened) failures.push(`the ${name} menu never opened`); | |
| 139 | + else if (!driven.rowOn) failures.push(`typing '${value}' in the ${name} menu hid its row`); | |
| 125 | 140 | } |
| 126 | 141 | // Then the day press, whose pick() writes the hidden input in place — in |
| 127 | 142 | // the modal nothing autosubmits, so the value is read in the same tick as |
crates/izlek-web/tests/http.rsmodified+329 -0
| @@ -9574,3 +9574,332 @@ async fn a_card_lists_both_dependency_directions_as_keys() { | ||
| 9574 | 9574 | "the blocked card did not name its blocker: {html}" |
| 9575 | 9575 | ); |
| 9576 | 9576 | } |
| 9577 | + | |
| 9578 | +/// The board search filters server-side by key and title, composes with the | |
| 9579 | +/// project filter, and a query nothing answers says so in one line. | |
| 9580 | +#[tokio::test] | |
| 9581 | +async fn the_board_search_filters_and_composes_with_the_project_filter() { | |
| 9582 | + let app = App::open().await; | |
| 9583 | + let admin = admin(&app).await; | |
| 9584 | + let column = first_column(&app).await; | |
| 9585 | + let panel = a_task(&app, &admin, &column, "Panel polish").await; | |
| 9586 | + let _wire = a_task(&app, &admin, &column, "Wire the exporter").await; | |
| 9587 | + let aurora = a_tag(&app, &admin, "Aurora").await; | |
| 9588 | + let boreal = a_tag(&app, &admin, "Boreal").await; | |
| 9589 | + let answer = app | |
| 9590 | + .post( | |
| 9591 | + "/api/set_task_tag", | |
| 9592 | + Some(&admin), | |
| 9593 | + &[("task_id", &panel), ("tag_id", &aurora.id)], | |
| 9594 | + ) | |
| 9595 | + .await; | |
| 9596 | + assert_eq!(answer.body, "null", "the tag did not attach: {}", answer.body); | |
| 9597 | + let panel_key = app | |
| 9598 | + .store | |
| 9599 | + .task(&panel) | |
| 9600 | + .await | |
| 9601 | + .unwrap() | |
| 9602 | + .expect("panel task gone") | |
| 9603 | + .row | |
| 9604 | + .task_key; | |
| 9605 | + | |
| 9606 | + // By key, typed in any case: its card stays, the other goes. | |
| 9607 | + let page = app | |
| 9608 | + .get(&format!("/?q={}", panel_key.to_lowercase()), Some(&admin)) | |
| 9609 | + .await; | |
| 9610 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 9611 | + assert!(html.contains("Panel polish"), "{html}"); | |
| 9612 | + assert!(!html.contains("Wire the exporter"), "{html}"); | |
| 9613 | + | |
| 9614 | + // By title fragment, any case. | |
| 9615 | + let page = app.get("/?q=EXPORTER", Some(&admin)).await; | |
| 9616 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 9617 | + assert!(html.contains("Wire the exporter"), "{html}"); | |
| 9618 | + assert!(!html.contains("Panel polish"), "{html}"); | |
| 9619 | + | |
| 9620 | + // Search and project compose: both must hold for a card to show. | |
| 9621 | + let page = app | |
| 9622 | + .get(&format!("/?q=polish&tag={}", aurora.id), Some(&admin)) | |
| 9623 | + .await; | |
| 9624 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 9625 | + assert!(html.contains("Panel polish"), "{html}"); | |
| 9626 | + let page = app | |
| 9627 | + .get(&format!("/?q=polish&tag={}", boreal.id), Some(&admin)) | |
| 9628 | + .await; | |
| 9629 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 9630 | + assert!(html.contains("No matches"), "{html}"); | |
| 9631 | + assert!(!html.contains("Panel polish"), "{html}"); | |
| 9632 | + | |
| 9633 | + // Clearing the box restores the whole board. | |
| 9634 | + let page = app.get("/", Some(&admin)).await; | |
| 9635 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 9636 | + assert!( | |
| 9637 | + html.contains("Panel polish") && html.contains("Wire the exporter"), | |
| 9638 | + "{html}" | |
| 9639 | + ); | |
| 9640 | +} | |
| 9641 | +// --------------------------------------------------------------------------- | |
| 9642 | +// Self-serve password reset: the forgot form, the indistinguishable answer, | |
| 9643 | +// and the redeem flow over the mailed link. | |
| 9644 | +// --------------------------------------------------------------------------- | |
| 9645 | + | |
| 9646 | +/// The reset token out of the newest notice queued for the address — the mail | |
| 9647 | +/// is the token's only carrier, so the test reads it like a recipient would. | |
| 9648 | +async fn queued_reset_token(app: &App, email: &str) -> Option<String> { | |
| 9649 | + let sends = app | |
| 9650 | + .store | |
| 9651 | + .mail_queue(50, izlek_core::store::FeedPage::Newest) | |
| 9652 | + .await | |
| 9653 | + .unwrap(); | |
| 9654 | + let body = sends | |
| 9655 | + .into_iter() | |
| 9656 | + .rev() | |
| 9657 | + .find(|send| { | |
| 9658 | + send.kind == SendKind::Notice && send.recipient == email | |
| 9659 | + }) | |
| 9660 | + .and_then(|send| send.body)?; | |
| 9661 | + body.rsplit_once("/reset/") | |
| 9662 | + .and_then(|(_, rest)| rest.split_whitespace().next()) | |
| 9663 | + .map(str::to_string) | |
| 9664 | +} | |
| 9665 | + | |
| 9666 | +#[tokio::test] | |
| 9667 | +async fn forgot_password_answers_the_same_whatever_the_address() { | |
| 9668 | + let app = App::open().await; | |
| 9669 | + let _admin = admin(&app).await; | |
| 9670 | + | |
| 9671 | + let known = app | |
| 9672 | + .post("/api/forgot_password", None, &[("email", "ada@izlek.sh")]) | |
| 9673 | + .await; | |
| 9674 | + let unknown = app | |
| 9675 | + .post("/api/forgot_password", None, &[("email", "ghost@izlek.sh")]) | |
| 9676 | + .await; | |
| 9677 | + | |
| 9678 | + assert_eq!(known.status, StatusCode::SEE_OTHER, "{}", known.body); | |
| 9679 | + assert_eq!(unknown.status, StatusCode::SEE_OTHER, "{}", unknown.body); | |
| 9680 | + assert_eq!(known.body, "null", "{}", known.body); | |
| 9681 | + // The two answers are word for word the same, and both land back on the | |
| 9682 | + // forgot form, which says its one sentence either way. | |
| 9683 | + assert_eq!(known.body, unknown.body); | |
| 9684 | + assert_eq!( | |
| 9685 | + known.location.as_deref().map(str::to_string), | |
| 9686 | + Some("/forgot?sent=forgot_password".to_string()), | |
| 9687 | + "{:?}", | |
| 9688 | + known.location | |
| 9689 | + ); | |
| 9690 | + | |
| 9691 | + // The ledger keeps the difference the surface refuses to show: one reset | |
| 9692 | + // mail, for the address that has an account. | |
| 9693 | + let token = queued_reset_token(&app, "ada@izlek.sh") | |
| 9694 | + .await | |
| 9695 | + .expect("no reset mail queued for the admin"); | |
| 9696 | + assert!( | |
| 9697 | + queued_reset_token(&app, "ghost@izlek.sh").await.is_none(), | |
| 9698 | + "the unknown address was mailed a reset link" | |
| 9699 | + ); | |
| 9700 | + assert!(!token.is_empty()); | |
| 9701 | +} | |
| 9702 | + | |
| 9703 | +#[tokio::test] | |
| 9704 | +async fn a_reset_link_redeems_on_its_own_screen() { | |
| 9705 | + let app = App::open().await; | |
| 9706 | + let _admin = admin(&app).await; | |
| 9707 | + app.post("/api/forgot_password", None, &[("email", "ada@izlek.sh")]) | |
| 9708 | + .await; | |
| 9709 | + let token = queued_reset_token(&app, "ada@izlek.sh") | |
| 9710 | + .await | |
| 9711 | + .expect("no reset mail queued"); | |
| 9712 | + | |
| 9713 | + // The mailed link opens the choose-password card, naming the address it | |
| 9714 | + // was mailed to. | |
| 9715 | + let page = app.get(&format!("/reset/{token}"), None).await; | |
| 9716 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 9717 | + assert!(html.contains("Choose a new password"), "{html}"); | |
| 9718 | + assert!(html.contains("ada@izlek.sh"), "{html}"); | |
| 9719 | + | |
| 9720 | + // Redeeming signs this browser in and lands on the board. | |
| 9721 | + let answer = app | |
| 9722 | + .post( | |
| 9723 | + "/api/redeem_reset", | |
| 9724 | + None, | |
| 9725 | + &[("token", token.as_str()), ("password", "chronometer-1761")], | |
| 9726 | + ) | |
| 9727 | + .await; | |
| 9728 | + assert_eq!(answer.status, StatusCode::SEE_OTHER, "{}", answer.body); | |
| 9729 | + assert_eq!(answer.body, "null", "{}", answer.body); | |
| 9730 | + assert_eq!(answer.location.as_deref(), Some("/")); | |
| 9731 | + let cookie = answer.session.expect("redeeming set no session cookie"); | |
| 9732 | + | |
| 9733 | + // The old password is refused, the new one signs in. | |
| 9734 | + let old = app | |
| 9735 | + .post( | |
| 9736 | + "/api/sign_in", | |
| 9737 | + None, | |
| 9738 | + &[("email", "ada@izlek.sh"), ("password", "correct horse battery staple")], | |
| 9739 | + ) | |
| 9740 | + .await; | |
| 9741 | + assert_ne!(old.body, "null", "the old password still signed in"); | |
| 9742 | + assert!(old.session.is_none(), "the old password set a session"); | |
| 9743 | + let new = app | |
| 9744 | + .post( | |
| 9745 | + "/api/sign_in", | |
| 9746 | + None, | |
| 9747 | + &[("email", "ada@izlek.sh"), ("password", "chronometer-1761")], | |
| 9748 | + ) | |
| 9749 | + .await; | |
| 9750 | + assert_eq!(new.body, "null", "{}", new.body); | |
| 9751 | + assert!(new.session.is_some(), "the new password signed nobody in"); | |
| 9752 | + let _ = cookie; | |
| 9753 | + | |
| 9754 | + // The spent link shows the dead card, not the password form. | |
| 9755 | + let page = app.get(&format!("/reset/{token}"), None).await; | |
| 9756 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 9757 | + assert!(html.contains("no longer works"), "{html}"); | |
| 9758 | + assert!(!html.contains("Choose a new password"), "{html}"); | |
| 9759 | +} | |
| 9760 | + | |
| 9761 | +#[tokio::test] | |
| 9762 | +async fn a_used_or_wrong_token_shows_the_dead_card() { | |
| 9763 | + let app = App::open().await; | |
| 9764 | + let _admin = admin(&app).await; | |
| 9765 | + | |
| 9766 | + let page = app.get("/reset/not-a-token", None).await; | |
| 9767 | + let html = String::from_utf8_lossy(&page.bytes); | |
| 9768 | + assert!(html.contains("no longer works"), "{html}"); | |
| 9769 | + | |
| 9770 | + // Redeeming over a bogus token is refused, and nothing signs in. | |
| 9771 | + let answer = app | |
| 9772 | + .post( | |
| 9773 | + "/api/redeem_reset", | |
| 9774 | + None, | |
| 9775 | + &[("token", "not-a-token"), ("password", "chronometer-1761")], | |
| 9776 | + ) | |
| 9777 | + .await; | |
| 9778 | + assert_eq!(answer.status, StatusCode::SEE_OTHER); | |
| 9779 | + assert_ne!(answer.body, "null", "a bogus token redeemed"); | |
| 9780 | + assert!(answer.session.is_none()); | |
| 9781 | +} | |
| 9782 | + | |
| 9783 | +// A `multiple` picker posts several `file` parts in one form; every part | |
| 9784 | +// lands as its own attachment with its own bytes, under the same type and | |
| 9785 | +// size rules the single-file path always had. | |
| 9786 | +#[tokio::test] | |
| 9787 | +async fn a_multi_file_picker_lands_every_part_as_its_own_attachment() { | |
| 9788 | + let app = App::open().await; | |
| 9789 | + let admin_cookie = admin(&app).await; | |
| 9790 | + let column = first_column(&app).await; | |
| 9791 | + let task = a_task(&app, &admin_cookie, &column, "Two parts, one post") | |
| 9792 | + .await; | |
| 9793 | + | |
| 9794 | + const BOUNDARY: &str = "izlek-test-boundary"; | |
| 9795 | + let png = [0x89u8, 0x50, 0x4E, 0x47, 0x0D, 0x0A, 0x1A, 0x0A, 9, 9]; | |
| 9796 | + let text = b"hello, from the second part".as_slice(); | |
| 9797 | + let mut body = Vec::new(); | |
| 9798 | + body.extend_from_slice( | |
| 9799 | + format!( | |
| 9800 | + "--{BOUNDARY}\r\nContent-Disposition: form-data; name=\"task_id\"\r\n\r\n{task}\r\n" | |
| 9801 | + ) | |
| 9802 | + .as_bytes(), | |
| 9803 | + ); | |
| 9804 | + for (filename, content_type, bytes) in [ | |
| 9805 | + ("first.png", "image/png", &png[..]), | |
| 9806 | + ("notes.txt", "text/plain", text), | |
| 9807 | + ] { | |
| 9808 | + body.extend_from_slice( | |
| 9809 | + format!( | |
| 9810 | + "--{BOUNDARY}\r\nContent-Disposition: form-data; name=\"file\"; filename=\"{filename}\"\r\nContent-Type: {content_type}\r\n\r\n" | |
| 9811 | + ) | |
| 9812 | + .as_bytes(), | |
| 9813 | + ); | |
| 9814 | + body.extend_from_slice(bytes); | |
| 9815 | + body.extend_from_slice(b"\r\n"); | |
| 9816 | + } | |
| 9817 | + body.extend_from_slice(format!("--{BOUNDARY}--\r\n").as_bytes()); | |
| 9818 | + | |
| 9819 | + let request = Request::builder() | |
| 9820 | + .method("POST") | |
| 9821 | + .uri("/files") | |
| 9822 | + .header( | |
| 9823 | + header::CONTENT_TYPE, | |
| 9824 | + format!("multipart/form-data; boundary={BOUNDARY}"), | |
| 9825 | + ) | |
| 9826 | + .header( | |
| 9827 | + header::COOKIE, | |
| 9828 | + HeaderValue::from_str(&format!("{SESSION_COOKIE}={admin_cookie}")).unwrap(), | |
| 9829 | + ) | |
| 9830 | + .body(Body::from(body)) | |
| 9831 | + .unwrap(); | |
| 9832 | + let answer = Answer::from_response(app.router.handle(request).await).await; | |
| 9833 | + assert_eq!(answer.status, StatusCode::SEE_OTHER); | |
| 9834 | + assert_eq!( | |
| 9835 | + answer.location.as_deref(), | |
| 9836 | + Some(format!("/?task={task}&tab=files").as_str()) | |
| 9837 | + ); | |
| 9838 | + | |
| 9839 | + let snapshot = app | |
| 9840 | + .post("/api/fetch_task", Some(&admin_cookie), &[("task_id", &task)]) | |
| 9841 | + .await; | |
| 9842 | + let png_id = attachment_id_named(&snapshot.body, "first.png"); | |
| 9843 | + let txt_id = attachment_id_named(&snapshot.body, "notes.txt"); | |
| 9844 | + | |
| 9845 | + let png_back = app | |
| 9846 | + .get(&format!("/files/{png_id}"), Some(&admin_cookie)) | |
| 9847 | + .await; | |
| 9848 | + assert_eq!(png_back.bytes, png); | |
| 9849 | + let txt_back = app | |
| 9850 | + .get(&format!("/files/{txt_id}"), Some(&admin_cookie)) | |
| 9851 | + .await; | |
| 9852 | + assert_eq!(txt_back.bytes, text); | |
| 9853 | +} | |
| 9854 | + | |
| 9855 | +/// The feed page: a member sees the events that concern them (the task they | |
| 9856 | +/// were assigned, named with the actor), the topbar badge counts what is | |
| 9857 | +/// unread, and visiting the feed clears it. A viewer gets the page too — | |
| 9858 | +/// empty, not refused. | |
| 9859 | +#[tokio::test] | |
| 9860 | +async fn the_feed_shows_each_person_their_stake() { | |
| 9861 | + let app = App::open().await; | |
| 9862 | + let admin_cookie = admin(&app).await; | |
| 9863 | + let member = invited(&app, &admin_cookie, "deniz@izlek.sh", "Deniz", Role::Member).await; | |
| 9864 | + let columns = columns_of(&app).await; | |
| 9865 | + let task = a_task(&app, &admin_cookie, &columns[0], "Watch me").await; | |
| 9866 | + let deniz_id = person_id(&app, &admin_cookie, &task, "Deniz").await; | |
| 9867 | + | |
| 9868 | + // Ada assigns Deniz: the assignment names him and starts his watch. | |
| 9869 | + app | |
| 9870 | + .post( | |
| 9871 | + "/api/assign", | |
| 9872 | + Some(&admin_cookie), | |
| 9873 | + &[("task_id", &task), ("user_id", &deniz_id)], | |
| 9874 | + ) | |
| 9875 | + .await; | |
| 9876 | + | |
| 9877 | + // The badge rides every page's topbar while the news is unread... | |
| 9878 | + let board = app.get("/", Some(&member)).await; | |
| 9879 | + let board_html = String::from_utf8_lossy(&board.bytes); | |
| 9880 | + assert!(board_html.contains("feed-badge"), "{board_html}"); | |
| 9881 | + assert!(board_html.contains("href=\"/feed\""), "{board_html}"); | |
| 9882 | + | |
| 9883 | + // ...and the feed page names what happened, with the actor. | |
| 9884 | + let feed = app.get("/feed", Some(&member)).await; | |
| 9885 | + assert_eq!(feed.status, StatusCode::OK); | |
| 9886 | + let feed_html = String::from_utf8_lossy(&feed.bytes); | |
| 9887 | + assert!(feed_html.contains("Watch me"), "{feed_html}"); | |
| 9888 | + assert!(feed_html.contains("Ada Lovelace"), "{feed_html}"); | |
| 9889 | + assert!(feed_html.contains("assigned"), "{feed_html}"); | |
| 9890 | + | |
| 9891 | + // Visiting was reading: the badge is gone on the next page. | |
| 9892 | + let board = app.get("/", Some(&member)).await; | |
| 9893 | + let board_html = String::from_utf8_lossy(&board.bytes); | |
| 9894 | + assert!( | |
| 9895 | + !board_html.contains("feed-badge"), | |
| 9896 | + "badge survived the read: {board_html}" | |
| 9897 | + ); | |
| 9898 | + | |
| 9899 | + // A viewer's feed is empty, not hidden: every role gets the page. | |
| 9900 | + let viewer = invited(&app, &admin_cookie, "kim@izlek.sh", "Kim", Role::Viewer).await; | |
| 9901 | + let feed = app.get("/feed", Some(&viewer)).await; | |
| 9902 | + assert_eq!(feed.status, StatusCode::OK); | |
| 9903 | + let feed_html = String::from_utf8_lossy(&feed.bytes); | |
| 9904 | + assert!(feed_html.contains("Nothing yet."), "{feed_html}"); | |
| 9905 | +} |