| 1 | use std::path::PathBuf; |
| 2 | use std::sync::Arc; |
| 3 | |
| 4 | use chrono::{DateTime, Duration, Local, TimeZone, Utc}; |
| 5 | use gpui_kit::Image; |
| 6 | use store::{ |
| 7 | ChannelLayoutRecord, ChannelRecord, ChatRecord, MemberRecord, MessageRecord, Store, |
| 8 | TeamLayoutRecord, TeamRecord, |
| 9 | }; |
| 10 | use teams_core::{MentionCandidate, PersonCandidate, PersonSource}; |
| 11 | |
| 12 | use crate::app_state::{AppState, Selection}; |
| 13 | use crate::data::{FolderInfo, FolderKind, Person, PresenceKind}; |
| 14 | |
| 15 | pub const DEMO_USER_ID: &str = "demo-me"; |
| 16 | const DEMO_USER_NAME: &str = "Dana Demo"; |
| 17 | const PHOTO_SIZE: usize = 48; |
| 18 | const STAGING_IMAGE_KEY: &str = "demo://staging-dashboard"; |
| 19 | const PENDING_IMAGE_KEY: &str = "demo://pending-screenshot"; |
| 20 | const STAGING_SIZE: (usize, usize) = (960, 540); |
| 21 | const PRIORITY_CHAT: &str = "demo-chat-priya"; |
| 22 | const PEOPLE_DIRECTORY: [(&str, &str, &str, &str); 5] = [ |
| 23 | ( |
| 24 | MARA_ID, |
| 25 | "Mara Lindqvist", |
| 26 | "Product Owner", |
| 27 | "mara.lindqvist@example.com", |
| 28 | ), |
| 29 | ( |
| 30 | JONAS_ID, |
| 31 | "Jonas Ortega", |
| 32 | "Backend Engineer", |
| 33 | "jonas.ortega@example.com", |
| 34 | ), |
| 35 | ( |
| 36 | PRIYA_ID, |
| 37 | "Priya Nair", |
| 38 | "Engineering Manager", |
| 39 | "priya.nair@example.com", |
| 40 | ), |
| 41 | ( |
| 42 | LEA_ID, |
| 43 | "Lea Schneider", |
| 44 | "Customer Success", |
| 45 | "lea.schneider@example.com", |
| 46 | ), |
| 47 | ( |
| 48 | TOBIAS_ID, |
| 49 | "Tobias Klein", |
| 50 | "Support Engineer", |
| 51 | "tobias.klein@example.com", |
| 52 | ), |
| 53 | ]; |
| 54 | |
| 55 | struct Palette { |
| 56 | background: [u8; 3], |
| 57 | skin: [u8; 3], |
| 58 | hair: [u8; 3], |
| 59 | shirt: [u8; 3], |
| 60 | } |
| 61 | |
| 62 | const MARA: Palette = Palette { |
| 63 | background: [0x3f, 0x5f, 0x7a], |
| 64 | skin: [0xe0, 0xb2, 0x93], |
| 65 | hair: [0xd9, 0xa4, 0x41], |
| 66 | shirt: [0x1e, 0x29, 0x3b], |
| 67 | }; |
| 68 | const PRIYA: Palette = Palette { |
| 69 | background: [0x4b, 0x3b, 0x5c], |
| 70 | skin: [0x9a, 0x6a, 0x4a], |
| 71 | hair: [0x1c, 0x19, 0x17], |
| 72 | shirt: [0xbe, 0x12, 0x3c], |
| 73 | }; |
| 74 | const TOBIAS: Palette = Palette { |
| 75 | background: [0x3b, 0x4a, 0x3f], |
| 76 | skin: [0xf1, 0xc7, 0xa5], |
| 77 | hair: [0x57, 0x53, 0x4e], |
| 78 | shirt: [0x0f, 0x76, 0x6e], |
| 79 | }; |
| 80 | |
| 81 | const MARA_ID: &str = "demo-mara"; |
| 82 | const JONAS_ID: &str = "demo-jonas"; |
| 83 | const PRIYA_ID: &str = "demo-priya"; |
| 84 | const LEA_ID: &str = "demo-lea"; |
| 85 | const TOBIAS_ID: &str = "demo-tobias"; |
| 86 | |
| 87 | const DEMO_TEAMS: [(&str, &str, [&str; 3]); 4] = [ |
| 88 | ( |
| 89 | "demo-team-1", |
| 90 | "Platform", |
| 91 | ["General", "Incidents", "Releases"], |
| 92 | ), |
| 93 | ("demo-team-2", "Product", ["General", "Roadmap", "Feedback"]), |
| 94 | ( |
| 95 | "demo-team-3", |
| 96 | "Operations", |
| 97 | ["General", "On call", "Runbooks"], |
| 98 | ), |
| 99 | ( |
| 100 | "demo-team-4", |
| 101 | "Archive 2025", |
| 102 | ["General", "Trade fair", "Office move"], |
| 103 | ), |
| 104 | ]; |
| 105 | const DEMO_TEAM_ORDER: [&str; 4] = ["demo-team-2", "demo-team-1", "demo-team-3", "demo-team-4"]; |
| 106 | const DEMO_HIDDEN_TEAM: &str = "demo-team-4"; |
| 107 | const DEMO_HIDDEN_CHANNEL: &str = "demo-channel-3-3"; |
| 108 | |
| 109 | const UNREAD_CHAT: &str = "demo-chat-atlas"; |
| 110 | const RELEASE_CHAT: &str = "demo-chat-release"; |
| 111 | const FAVORITES_ID: &str = "demo-folder-favorites"; |
| 112 | const CUSTOMERS_FOLDER: &str = "demo-folder-customers"; |
| 113 | const ATLAS_FOLDER: &str = "demo-folder-atlas"; |
| 114 | const TODO_FOLDER: &str = "demo-folder-todo"; |
| 115 | |
| 116 | fn member(user_id: &str, name: &str) -> MemberRecord { |
| 117 | MemberRecord { |
| 118 | user_id: Some(user_id.to_owned()), |
| 119 | display_name: name.to_owned(), |
| 120 | } |
| 121 | } |
| 122 | |
| 123 | fn people(extra: &[(&str, &str)]) -> Vec<MemberRecord> { |
| 124 | std::iter::once(member(DEMO_USER_ID, DEMO_USER_NAME)) |
| 125 | .chain(extra.iter().map(|(id, name)| member(id, name))) |
| 126 | .collect() |
| 127 | } |
| 128 | |
| 129 | pub fn at(days_ago: i64, hour: u32, minute: u32) -> DateTime<Utc> { |
| 130 | let day = Local::now().date_naive() - Duration::days(days_ago); |
| 131 | let local = day.and_hms_opt(hour, minute, 0).expect("valid time"); |
| 132 | Local |
| 133 | .from_local_datetime(&local) |
| 134 | .earliest() |
| 135 | .map_or_else(Utc::now, |time| time.with_timezone(&Utc)) |
| 136 | } |
| 137 | |
| 138 | struct DemoChat { |
| 139 | id: &'static str, |
| 140 | kind: &'static str, |
| 141 | title: &'static str, |
| 142 | members: Vec<MemberRecord>, |
| 143 | time: DateTime<Utc>, |
| 144 | unread: bool, |
| 145 | preview: Option<(&'static str, &'static str, &'static str)>, |
| 146 | deleted: bool, |
| 147 | } |
| 148 | |
| 149 | fn demo_chats() -> Vec<DemoChat> { |
| 150 | let group = |extra: &[(&str, &str)]| people(extra); |
| 151 | vec![ |
| 152 | DemoChat { |
| 153 | id: "demo-chat-mara", |
| 154 | kind: "oneOnOne", |
| 155 | title: "Mara Lindqvist", |
| 156 | members: people(&[(MARA_ID, "Mara Lindqvist")]), |
| 157 | time: at(0, 13, 40), |
| 158 | unread: false, |
| 159 | preview: Some((MARA_ID, "Mara Lindqvist", "Sure, 2 pm works")), |
| 160 | deleted: false, |
| 161 | }, |
| 162 | DemoChat { |
| 163 | id: RELEASE_CHAT, |
| 164 | kind: "group", |
| 165 | title: "Release planning", |
| 166 | members: group(&[ |
| 167 | (MARA_ID, "Mara Lindqvist"), |
| 168 | (JONAS_ID, "Jonas Ortega"), |
| 169 | (PRIYA_ID, "Priya Nair"), |
| 170 | (LEA_ID, "Lea Schneider"), |
| 171 | ]), |
| 172 | time: at(0, 13, 35), |
| 173 | unread: false, |
| 174 | preview: Some(( |
| 175 | DEMO_USER_ID, |
| 176 | DEMO_USER_NAME, |
| 177 | "Build 42 is green, see pipeline", |
| 178 | )), |
| 179 | deleted: false, |
| 180 | }, |
| 181 | DemoChat { |
| 182 | id: "demo-chat-customer-1", |
| 183 | kind: "group", |
| 184 | title: "Customer North - Operations", |
| 185 | members: group(&[(LEA_ID, "Lea Schneider"), (TOBIAS_ID, "Tobias Klein")]), |
| 186 | time: at(0, 12, 50), |
| 187 | unread: true, |
| 188 | preview: Some((LEA_ID, "Lea Schneider", "Access is enabled now")), |
| 189 | deleted: false, |
| 190 | }, |
| 191 | DemoChat { |
| 192 | id: "demo-chat-customer-2", |
| 193 | kind: "group", |
| 194 | title: "Customer South - Rollout", |
| 195 | members: group(&[(JONAS_ID, "Jonas Ortega"), (PRIYA_ID, "Priya Nair")]), |
| 196 | time: at(0, 9, 5), |
| 197 | unread: true, |
| 198 | preview: Some((JONAS_ID, "Jonas Ortega", "Meeting is set for Friday")), |
| 199 | deleted: false, |
| 200 | }, |
| 201 | DemoChat { |
| 202 | id: "demo-chat-customer-3", |
| 203 | kind: "group", |
| 204 | title: "Customer West - Support", |
| 205 | members: group(&[(TOBIAS_ID, "Tobias Klein"), (LEA_ID, "Lea Schneider")]), |
| 206 | time: at(1, 16, 0), |
| 207 | unread: false, |
| 208 | preview: Some((TOBIAS_ID, "Tobias Klein", "Ticket is closed")), |
| 209 | deleted: false, |
| 210 | }, |
| 211 | DemoChat { |
| 212 | id: "demo-chat-customer-4", |
| 213 | kind: "group", |
| 214 | title: "Customer East - Pilot", |
| 215 | members: group(&[(MARA_ID, "Mara Lindqvist"), (PRIYA_ID, "Priya Nair")]), |
| 216 | time: at(3, 10, 0), |
| 217 | unread: false, |
| 218 | preview: Some((DEMO_USER_ID, DEMO_USER_NAME, "I'll send the documents")), |
| 219 | deleted: false, |
| 220 | }, |
| 221 | DemoChat { |
| 222 | id: "demo-chat-atlas", |
| 223 | kind: "group", |
| 224 | title: "Atlas core team", |
| 225 | members: group(&[ |
| 226 | (PRIYA_ID, "Priya Nair"), |
| 227 | (LEA_ID, "Lea Schneider"), |
| 228 | (MARA_ID, "Mara Lindqvist"), |
| 229 | ]), |
| 230 | time: at(0, 13, 12), |
| 231 | unread: true, |
| 232 | preview: Some((PRIYA_ID, "Priya Nair", "Who is taking the review?")), |
| 233 | deleted: false, |
| 234 | }, |
| 235 | DemoChat { |
| 236 | id: "demo-chat-jonas", |
| 237 | kind: "oneOnOne", |
| 238 | title: "Jonas Ortega", |
| 239 | members: people(&[(JONAS_ID, "Jonas Ortega")]), |
| 240 | time: at(0, 11, 2), |
| 241 | unread: false, |
| 242 | preview: Some((DEMO_USER_ID, DEMO_USER_NAME, "Thanks, it's merged")), |
| 243 | deleted: false, |
| 244 | }, |
| 245 | DemoChat { |
| 246 | id: "demo-chat-priya", |
| 247 | kind: "oneOnOne", |
| 248 | title: "Priya Nair", |
| 249 | members: people(&[(PRIYA_ID, "Priya Nair")]), |
| 250 | time: at(0, 10, 47), |
| 251 | unread: false, |
| 252 | preview: Some((PRIYA_ID, "Priya Nair", "Image")), |
| 253 | deleted: false, |
| 254 | }, |
| 255 | DemoChat { |
| 256 | id: "demo-chat-tobias", |
| 257 | kind: "oneOnOne", |
| 258 | title: "Tobias Klein", |
| 259 | members: people(&[(TOBIAS_ID, "Tobias Klein")]), |
| 260 | time: at(2, 9, 30), |
| 261 | unread: false, |
| 262 | preview: Some((TOBIAS_ID, "Tobias Klein", "ok")), |
| 263 | deleted: false, |
| 264 | }, |
| 265 | DemoChat { |
| 266 | id: "demo-chat-allhands", |
| 267 | kind: "group", |
| 268 | title: "All-hands announcements", |
| 269 | members: group(&[(LEA_ID, "Lea Schneider"), (MARA_ID, "Mara Lindqvist")]), |
| 270 | time: at(0, 9, 0), |
| 271 | unread: true, |
| 272 | preview: Some((LEA_ID, "Lea Schneider", "New vacation policy from November")), |
| 273 | deleted: false, |
| 274 | }, |
| 275 | DemoChat { |
| 276 | id: "demo-chat-design", |
| 277 | kind: "group", |
| 278 | title: "Design review", |
| 279 | members: group(&[(MARA_ID, "Mara Lindqvist"), (TOBIAS_ID, "Tobias Klein")]), |
| 280 | time: at(2, 15, 0), |
| 281 | unread: false, |
| 282 | preview: Some((MARA_ID, "Mara Lindqvist", "alt")), |
| 283 | deleted: true, |
| 284 | }, |
| 285 | DemoChat { |
| 286 | id: "demo-chat-quarterly", |
| 287 | kind: "group", |
| 288 | title: "Quarterly numbers, finance review and planning 2027", |
| 289 | members: group(&[(LEA_ID, "Lea Schneider"), (JONAS_ID, "Jonas Ortega")]), |
| 290 | time: at(4, 11, 0), |
| 291 | unread: false, |
| 292 | preview: Some((LEA_ID, "Lea Schneider", "Draft attached")), |
| 293 | deleted: false, |
| 294 | }, |
| 295 | ] |
| 296 | } |
| 297 | |
| 298 | pub fn mention_candidates(conversation_id: &str, query: &str) -> Vec<MentionCandidate> { |
| 299 | let needle = query.trim().to_lowercase(); |
| 300 | let matches = |name: &str| needle.is_empty() || name.to_lowercase().contains(&needle); |
| 301 | let people = PEOPLE_DIRECTORY |
| 302 | .iter() |
| 303 | .filter(|(_, name, _, _)| matches(name)) |
| 304 | .map(|(user_id, name, job_title, mail)| { |
| 305 | MentionCandidate::Person(PersonCandidate { |
| 306 | user_id: (*user_id).to_owned(), |
| 307 | display_name: (*name).to_owned(), |
| 308 | mail: Some((*mail).to_owned()), |
| 309 | job_title: Some((*job_title).to_owned()), |
| 310 | source: PersonSource::Member, |
| 311 | }) |
| 312 | }); |
| 313 | let scope: Vec<MentionCandidate> = if conversation_id.starts_with("demo-channel") { |
| 314 | [ |
| 315 | MentionCandidate::Channel { |
| 316 | channel_id: conversation_id.to_owned(), |
| 317 | name: "General".to_owned(), |
| 318 | }, |
| 319 | MentionCandidate::Team { |
| 320 | team_id: "demo-team-1".to_owned(), |
| 321 | name: "Platform".to_owned(), |
| 322 | }, |
| 323 | ] |
| 324 | .into_iter() |
| 325 | .filter(|candidate| matches(candidate.display_name())) |
| 326 | .collect() |
| 327 | } else { |
| 328 | Vec::new() |
| 329 | }; |
| 330 | people.chain(scope).collect() |
| 331 | } |
| 332 | |
| 333 | fn image_directory() -> PathBuf { |
| 334 | std::env::temp_dir().join("rusty-teams-demo") |
| 335 | } |
| 336 | |
| 337 | pub fn first_unread(conversation_id: &str) -> Option<String> { |
| 338 | (conversation_id == UNREAD_CHAT).then(|| "u3".to_owned()) |
| 339 | } |
| 340 | |
| 341 | pub fn read_message_ids(records: &[MessageRecord]) -> std::collections::HashSet<String> { |
| 342 | let newest = records.iter().max_by_key(|record| record.created_at); |
| 343 | records |
| 344 | .iter() |
| 345 | .filter(|record| { |
| 346 | Some(record.message_id.as_str()) != newest.map(|newest| newest.message_id.as_str()) |
| 347 | }) |
| 348 | .map(|record| record.message_id.clone()) |
| 349 | .collect() |
| 350 | } |
| 351 | |
| 352 | pub fn first_selection() -> Selection { |
| 353 | Selection::Chat(RELEASE_CHAT.to_owned()) |
| 354 | } |
| 355 | |
| 356 | pub fn seed(store: &Store) { |
| 357 | let chats: Vec<ChatRecord> = demo_chats() |
| 358 | .into_iter() |
| 359 | .map(|demo| ChatRecord { |
| 360 | id: demo.id.to_owned(), |
| 361 | kind: demo.kind.to_owned(), |
| 362 | title: if demo.kind == "oneOnOne" { |
| 363 | String::new() |
| 364 | } else { |
| 365 | demo.title.to_owned() |
| 366 | }, |
| 367 | member_summary: demo.title.to_owned(), |
| 368 | last_message_at: Some(demo.time), |
| 369 | unread: demo.unread, |
| 370 | members: demo.members, |
| 371 | last_message_preview: demo.preview.map(|(_, _, text)| text.to_owned()), |
| 372 | last_message_sender_id: demo.preview.map(|(id, _, _)| id.to_owned()), |
| 373 | last_message_sender_name: demo.preview.map(|(_, name, _)| name.to_owned()), |
| 374 | last_message_deleted: demo.deleted, |
| 375 | ..Default::default() |
| 376 | }) |
| 377 | .collect(); |
| 378 | let _ = store.upsert_chats(&chats); |
| 379 | let teams: Vec<TeamRecord> = DEMO_TEAMS |
| 380 | .iter() |
| 381 | .map(|(id, name, _)| TeamRecord { |
| 382 | id: (*id).to_owned(), |
| 383 | name: (*name).to_owned(), |
| 384 | }) |
| 385 | .collect(); |
| 386 | let _ = store.upsert_teams(&teams); |
| 387 | let mut channels = Vec::new(); |
| 388 | for (team_index, (team_id, _, names)) in DEMO_TEAMS.iter().enumerate() { |
| 389 | for (channel_index, name) in names.iter().enumerate() { |
| 390 | channels.push(ChannelRecord { |
| 391 | id: format!("demo-channel-{}-{}", team_index + 1, channel_index + 1), |
| 392 | team_id: (*team_id).to_owned(), |
| 393 | name: (*name).to_owned(), |
| 394 | membership_type: None, |
| 395 | last_message_at: Some(at(channel_index as i64, 12, 10)), |
| 396 | unread: (team_index == 0 && channel_index == 1) |
| 397 | || (team_index == 1 && channel_index == 0), |
| 398 | }); |
| 399 | } |
| 400 | } |
| 401 | let _ = store.upsert_channels(&channels); |
| 402 | let _ = store.replace_team_layout(&demo_team_layout(&channels)); |
| 403 | let _ = store.set_meta("me_user_id", DEMO_USER_ID); |
| 404 | let _ = store.set_meta("me_display_name", DEMO_USER_NAME); |
| 405 | let _ = store.upsert_messages(&release_messages()); |
| 406 | let _ = store.upsert_messages(&unread_messages()); |
| 407 | let _ = store.upsert_messages(&channel_messages()); |
| 408 | let _ = store.upsert_messages(&priority_messages()); |
| 409 | } |
| 410 | |
| 411 | fn photo(palette: &Palette) -> Arc<Image> { |
| 412 | crate::data::avatar_image_from("image/png", encode_png(palette)) |
| 413 | } |
| 414 | |
| 415 | pub fn seed_directory(state: &mut AppState) { |
| 416 | let directory = &mut state.directory; |
| 417 | directory.me = Some(Person { |
| 418 | user_id: DEMO_USER_ID.to_owned(), |
| 419 | display_name: DEMO_USER_NAME.to_owned(), |
| 420 | }); |
| 421 | let ids = |list: &[&str]| list.iter().map(|id| (*id).to_owned()).collect::<Vec<_>>(); |
| 422 | directory.folders = vec![ |
| 423 | FolderInfo { |
| 424 | id: FAVORITES_ID.to_owned(), |
| 425 | name: String::new(), |
| 426 | kind: FolderKind::Favorites, |
| 427 | conversation_ids: ids(&["demo-chat-mara", RELEASE_CHAT]), |
| 428 | }, |
| 429 | FolderInfo { |
| 430 | id: CUSTOMERS_FOLDER.to_owned(), |
| 431 | name: "Customers".to_owned(), |
| 432 | kind: FolderKind::UserCreated, |
| 433 | conversation_ids: ids(&[ |
| 434 | "demo-chat-customer-1", |
| 435 | "demo-chat-customer-2", |
| 436 | "demo-chat-customer-3", |
| 437 | "demo-chat-customer-4", |
| 438 | ]), |
| 439 | }, |
| 440 | FolderInfo { |
| 441 | id: ATLAS_FOLDER.to_owned(), |
| 442 | name: "Project Atlas".to_owned(), |
| 443 | kind: FolderKind::UserCreated, |
| 444 | conversation_ids: ids(&["demo-chat-atlas", "demo-chat-jonas"]), |
| 445 | }, |
| 446 | FolderInfo { |
| 447 | id: TODO_FOLDER.to_owned(), |
| 448 | name: "To do".to_owned(), |
| 449 | kind: FolderKind::UserCreated, |
| 450 | conversation_ids: Vec::new(), |
| 451 | }, |
| 452 | ]; |
| 453 | directory.pinned_channels = ids(&["demo-channel-1-1", "demo-channel-3-2"]); |
| 454 | directory |
| 455 | .unread_counts |
| 456 | .insert("demo-chat-atlas".to_owned(), 5); |
| 457 | directory |
| 458 | .unread_counts |
| 459 | .insert("demo-chat-customer-1".to_owned(), 2); |
| 460 | directory |
| 461 | .unread_counts |
| 462 | .insert("demo-chat-customer-2".to_owned(), 1); |
| 463 | directory |
| 464 | .unread_counts |
| 465 | .insert("demo-chat-allhands".to_owned(), 120); |
| 466 | directory.set_avatar(MARA_ID, Some(photo(&MARA))); |
| 467 | directory.set_avatar(PRIYA_ID, Some(photo(&PRIYA))); |
| 468 | directory.set_avatar(TOBIAS_ID, Some(photo(&TOBIAS))); |
| 469 | for (user_id, kind) in [ |
| 470 | (MARA_ID, PresenceKind::Available), |
| 471 | (JONAS_ID, PresenceKind::DoNotDisturb), |
| 472 | (PRIYA_ID, PresenceKind::Away), |
| 473 | (TOBIAS_ID, PresenceKind::Offline), |
| 474 | (LEA_ID, PresenceKind::DoNotDisturb), |
| 475 | (DEMO_USER_ID, PresenceKind::Available), |
| 476 | ] { |
| 477 | directory.set_presence(user_id, kind); |
| 478 | } |
| 479 | let directory_path = image_directory(); |
| 480 | if std::fs::create_dir_all(&directory_path).is_ok() { |
| 481 | let path = directory_path.join("staging-dashboard.png"); |
| 482 | let png = encode_rgb(STAGING_SIZE.0, STAGING_SIZE.1, staging_pixel); |
| 483 | if std::fs::write(&path, png).is_ok() { |
| 484 | state.directory.set_image(STAGING_IMAGE_KEY, &path); |
| 485 | } |
| 486 | } |
| 487 | state.collapsed.insert(CUSTOMERS_FOLDER.to_owned()); |
| 488 | state.last_sync = Some(Utc::now()); |
| 489 | } |
| 490 | |
| 491 | #[allow(clippy::too_many_arguments)] |
| 492 | fn message( |
| 493 | conversation: &str, |
| 494 | id: &str, |
| 495 | reply_to: Option<&str>, |
| 496 | sender: (&str, &str), |
| 497 | time: DateTime<Utc>, |
| 498 | html: &str, |
| 499 | reactions_json: &str, |
| 500 | edited: bool, |
| 501 | ) -> MessageRecord { |
| 502 | MessageRecord { |
| 503 | conversation_id: conversation.to_owned(), |
| 504 | message_id: id.to_owned(), |
| 505 | reply_to_id: reply_to.map(str::to_owned), |
| 506 | sender_id: Some(sender.0.to_owned()), |
| 507 | sender_name: Some(sender.1.to_owned()), |
| 508 | created_at: time, |
| 509 | edited_at: edited.then_some(time + Duration::minutes(2)), |
| 510 | deleted: false, |
| 511 | body_html: html.to_owned(), |
| 512 | attachments_json: "[]".to_owned(), |
| 513 | reactions_json: reactions_json.to_owned(), |
| 514 | mentions_json: "[]".to_owned(), |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | fn with_attachments(mut record: MessageRecord, attachments_json: &str) -> MessageRecord { |
| 519 | record.attachments_json = attachments_json.to_owned(); |
| 520 | record |
| 521 | } |
| 522 | |
| 523 | fn priority_messages() -> Vec<MessageRecord> { |
| 524 | let priya = (PRIYA_ID, "Priya Nair"); |
| 525 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 526 | vec![ |
| 527 | message( |
| 528 | PRIORITY_CHAT, |
| 529 | "p1", |
| 530 | None, |
| 531 | me, |
| 532 | at(0, 10, 40), |
| 533 | "<p>Have you seen the error on staging yet?</p>", |
| 534 | "[]", |
| 535 | false, |
| 536 | ), |
| 537 | message( |
| 538 | PRIORITY_CHAT, |
| 539 | "p2", |
| 540 | None, |
| 541 | priya, |
| 542 | at(0, 10, 47), |
| 543 | &format!( |
| 544 | "<p>Yes, here is the screenshot:</p><img src=\"{PENDING_IMAGE_KEY}\" width=\"1280\" height=\"720\">" |
| 545 | ), |
| 546 | "[]", |
| 547 | false, |
| 548 | ), |
| 549 | ] |
| 550 | } |
| 551 | |
| 552 | fn reaction(kind: &str, users: &[&str]) -> String { |
| 553 | users |
| 554 | .iter() |
| 555 | .map(|user| format!(r#"{{"reaction_type":"{kind}","user_id":"{user}","user_name":null}}"#)) |
| 556 | .collect::<Vec<_>>() |
| 557 | .join(",") |
| 558 | } |
| 559 | |
| 560 | fn release_messages() -> Vec<MessageRecord> { |
| 561 | let chat = RELEASE_CHAT; |
| 562 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 563 | let jonas = (JONAS_ID, "Jonas Ortega"); |
| 564 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 565 | let likes = format!("[{}]", reaction("like", &[JONAS_ID, PRIYA_ID, LEA_ID])); |
| 566 | let hearts = format!("[{}]", reaction("heart", &[DEMO_USER_ID, JONAS_ID])); |
| 567 | vec![ |
| 568 | message( |
| 569 | chat, |
| 570 | "m1", |
| 571 | None, |
| 572 | mara, |
| 573 | at(1, 16, 20), |
| 574 | "<p>Release candidate is on staging. Can someone run the regression suite?</p>", |
| 575 | "[]", |
| 576 | false, |
| 577 | ), |
| 578 | message( |
| 579 | chat, |
| 580 | "m2", |
| 581 | None, |
| 582 | mara, |
| 583 | at(1, 16, 22), |
| 584 | "<p>Checklist lives in <a href=\"https://example.com/checklist\">release checklist</a>.</p>", |
| 585 | &likes, |
| 586 | false, |
| 587 | ), |
| 588 | message( |
| 589 | chat, |
| 590 | "m2b", |
| 591 | None, |
| 592 | mara, |
| 593 | at(1, 16, 40), |
| 594 | &format!( |
| 595 | "<p>Staging dashboard right now:</p><img src=\"{STAGING_IMAGE_KEY}\" width=\"{}\" height=\"{}\">", |
| 596 | STAGING_SIZE.0, STAGING_SIZE.1 |
| 597 | ), |
| 598 | "[]", |
| 599 | false, |
| 600 | ), |
| 601 | with_attachments( |
| 602 | message( |
| 603 | chat, |
| 604 | "m2c", |
| 605 | None, |
| 606 | mara, |
| 607 | at(1, 16, 41), |
| 608 | "<p>Checklist and rollout plan attached.</p>", |
| 609 | "[]", |
| 610 | false, |
| 611 | ), |
| 612 | r#"[{"content_type":"reference","name":"Release-Checklist-Q4.xlsx","url":"https://example.com/files/checklist.xlsx","text":null,"size":48213},{"content_type":"reference","name":"Rollout-Plan.pdf","url":"https://example.com/files/plan.pdf","text":null,"size":2306867}]"#, |
| 613 | ), |
| 614 | message( |
| 615 | chat, |
| 616 | "m3", |
| 617 | None, |
| 618 | me, |
| 619 | at(0, 9, 2), |
| 620 | "<p>I'll take the changelog.</p>", |
| 621 | "[]", |
| 622 | false, |
| 623 | ), |
| 624 | message( |
| 625 | chat, |
| 626 | "m4", |
| 627 | None, |
| 628 | jonas, |
| 629 | at(0, 10, 14), |
| 630 | "<p><at id=\"0\">Mara Lindqvist</at> can you take a quick look at this?</p><pre><code>fn freeze(branch: &str) -> bool {\n branch.starts_with(\"release/\")\n}</code></pre>", |
| 631 | "[]", |
| 632 | false, |
| 633 | ), |
| 634 | message( |
| 635 | chat, |
| 636 | "m5", |
| 637 | None, |
| 638 | mara, |
| 639 | at(0, 10, 31), |
| 640 | "<blockquote><b>Jonas Ortega</b><br>can you take a quick look at this?</blockquote><p>Looks good. Small thing: the name could say what it checks.</p>", |
| 641 | &hearts, |
| 642 | true, |
| 643 | ), |
| 644 | message( |
| 645 | chat, |
| 646 | "m6", |
| 647 | None, |
| 648 | me, |
| 649 | at(0, 13, 35), |
| 650 | "<p>Thanks, renamed. Tagging now.</p>", |
| 651 | "[]", |
| 652 | false, |
| 653 | ), |
| 654 | message( |
| 655 | chat, |
| 656 | "m7", |
| 657 | None, |
| 658 | me, |
| 659 | at(0, 13, 36), |
| 660 | "<p>Build 42 is green, see <a href=\"https://example.com/build/42\">pipeline</a>.</p>", |
| 661 | "[]", |
| 662 | false, |
| 663 | ), |
| 664 | ] |
| 665 | } |
| 666 | |
| 667 | fn unread_messages() -> Vec<MessageRecord> { |
| 668 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 669 | let lea = (LEA_ID, "Lea Schneider"); |
| 670 | let tobias = (TOBIAS_ID, "Tobias Klein"); |
| 671 | let rows = [ |
| 672 | ( |
| 673 | "u1", |
| 674 | me, |
| 675 | at(1, 17, 5), |
| 676 | "<p>Can you enable access for North?</p>", |
| 677 | ), |
| 678 | ("u2", tobias, at(1, 17, 20), "<p>I'll check with IT.</p>"), |
| 679 | ( |
| 680 | "u3", |
| 681 | lea, |
| 682 | at(0, 12, 40), |
| 683 | "<p>Good morning, access is ready.</p>", |
| 684 | ), |
| 685 | ( |
| 686 | "u4", |
| 687 | lea, |
| 688 | at(0, 12, 41), |
| 689 | "<p>Sign in with your company ID, no password needed.</p>", |
| 690 | ), |
| 691 | ("u5", lea, at(0, 12, 50), "<p>Access is enabled now</p>"), |
| 692 | ]; |
| 693 | rows.into_iter() |
| 694 | .map(|(id, sender, time, html)| { |
| 695 | message(UNREAD_CHAT, id, None, sender, time, html, "[]", false) |
| 696 | }) |
| 697 | .collect() |
| 698 | } |
| 699 | |
| 700 | fn demo_team_layout(channels: &[ChannelRecord]) -> Vec<TeamLayoutRecord> { |
| 701 | DEMO_TEAM_ORDER |
| 702 | .iter() |
| 703 | .map(|team_id| TeamLayoutRecord { |
| 704 | team_id: (*team_id).to_owned(), |
| 705 | hidden: *team_id == DEMO_HIDDEN_TEAM, |
| 706 | channels: channels |
| 707 | .iter() |
| 708 | .filter(|channel| channel.team_id == *team_id) |
| 709 | .map(|channel| ChannelLayoutRecord { |
| 710 | channel_id: channel.id.clone(), |
| 711 | general: channel.name == "General", |
| 712 | hidden: channel.id == DEMO_HIDDEN_CHANNEL, |
| 713 | }) |
| 714 | .collect(), |
| 715 | }) |
| 716 | .collect() |
| 717 | } |
| 718 | |
| 719 | fn channel_messages() -> Vec<MessageRecord> { |
| 720 | let channel = "demo-channel-1-1"; |
| 721 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 722 | let tobias = (TOBIAS_ID, "Tobias Klein"); |
| 723 | vec![ |
| 724 | message( |
| 725 | channel, |
| 726 | "t1", |
| 727 | None, |
| 728 | mara, |
| 729 | at(0, 8, 0), |
| 730 | "<p>Weekly sync notes are up.</p>", |
| 731 | "[]", |
| 732 | false, |
| 733 | ), |
| 734 | message( |
| 735 | channel, |
| 736 | "t1r1", |
| 737 | Some("t1"), |
| 738 | tobias, |
| 739 | at(0, 8, 20), |
| 740 | "<p>Thanks, adding my items.</p>", |
| 741 | "[]", |
| 742 | false, |
| 743 | ), |
| 744 | message( |
| 745 | channel, |
| 746 | "t3", |
| 747 | None, |
| 748 | tobias, |
| 749 | at(0, 11, 30), |
| 750 | "<p>Release build 42 is green, deploying to staging now.</p>", |
| 751 | "[]", |
| 752 | false, |
| 753 | ), |
| 754 | message( |
| 755 | channel, |
| 756 | "t2", |
| 757 | None, |
| 758 | tobias, |
| 759 | at(0, 11, 0), |
| 760 | "<p>Who owns the on-call handover?</p>", |
| 761 | "[]", |
| 762 | false, |
| 763 | ), |
| 764 | message( |
| 765 | channel, |
| 766 | "t4", |
| 767 | None, |
| 768 | mara, |
| 769 | at(3, 8, 42), |
| 770 | "<p>Sprint Demos.</p><p>I added you both to the sprint. <at id=\"0\">Tobias Klein</at> did you present everything about the migration tool? The spec is at <a href=\"https://wiki.example.com/display/TEAM/Settings+types+and+password+policies\">https://wiki.example.com/display/TEAM/Settings+types+and+password+policies</a>Can you please review it?</p>", |
| 771 | "[]", |
| 772 | false, |
| 773 | ), |
| 774 | message( |
| 775 | channel, |
| 776 | "t4r1", |
| 777 | Some("t4"), |
| 778 | tobias, |
| 779 | at(0, 9, 15), |
| 780 | "<p>Yes, all done.</p>", |
| 781 | "[]", |
| 782 | false, |
| 783 | ), |
| 784 | ] |
| 785 | } |
| 786 | |
| 787 | fn crc32(bytes: &[u8]) -> u32 { |
| 788 | let mut crc = 0xffff_ffffu32; |
| 789 | for byte in bytes { |
| 790 | crc ^= u32::from(*byte); |
| 791 | for _ in 0..8 { |
| 792 | crc = if crc & 1 == 1 { |
| 793 | (crc >> 1) ^ 0xedb8_8320 |
| 794 | } else { |
| 795 | crc >> 1 |
| 796 | }; |
| 797 | } |
| 798 | } |
| 799 | !crc |
| 800 | } |
| 801 | |
| 802 | fn adler32(bytes: &[u8]) -> u32 { |
| 803 | let (mut low, mut high) = (1u32, 0u32); |
| 804 | for byte in bytes { |
| 805 | low = (low + u32::from(*byte)) % 65521; |
| 806 | high = (high + low) % 65521; |
| 807 | } |
| 808 | (high << 16) | low |
| 809 | } |
| 810 | |
| 811 | fn png_chunk(output: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) { |
| 812 | output.extend_from_slice(&(data.len() as u32).to_be_bytes()); |
| 813 | let mut body = kind.to_vec(); |
| 814 | body.extend_from_slice(data); |
| 815 | output.extend_from_slice(&body); |
| 816 | output.extend_from_slice(&crc32(&body).to_be_bytes()); |
| 817 | } |
| 818 | |
| 819 | fn inside(x: f32, y: f32, center: (f32, f32), radius: (f32, f32)) -> bool { |
| 820 | ((x - center.0) / radius.0).powi(2) + ((y - center.1) / radius.1).powi(2) <= 1. |
| 821 | } |
| 822 | |
| 823 | fn staging_pixel(column: usize, row: usize) -> [u8; 3] { |
| 824 | let (width, height) = (STAGING_SIZE.0 as f32, STAGING_SIZE.1 as f32); |
| 825 | let (x, y) = (column as f32, row as f32); |
| 826 | let shade = y / height; |
| 827 | let background = [ |
| 828 | (30. - 14. * shade) as u8, |
| 829 | (41. - 16. * shade) as u8, |
| 830 | (59. - 14. * shade) as u8, |
| 831 | ]; |
| 832 | let bar_width = width / 14.; |
| 833 | let slot = (x / bar_width) as usize; |
| 834 | let in_bar = x - slot as f32 * bar_width > bar_width * 0.18; |
| 835 | let bar_height = height * (0.25 + 0.5 * (((slot * 7 + 3) % 11) as f32 / 11.)); |
| 836 | let on_bars = in_bar && y > height * 0.86 - bar_height && y < height * 0.86 && slot < 14; |
| 837 | let header = y < height * 0.1; |
| 838 | if header { |
| 839 | [22, 30, 45] |
| 840 | } else if on_bars { |
| 841 | if slot % 4 == 3 { |
| 842 | [226, 87, 47] |
| 843 | } else { |
| 844 | [88, 133, 196] |
| 845 | } |
| 846 | } else if (y - height * 0.86).abs() < 1.5 { |
| 847 | [71, 85, 105] |
| 848 | } else { |
| 849 | background |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | fn encode_png(palette: &Palette) -> Vec<u8> { |
| 854 | encode_rgb(PHOTO_SIZE, PHOTO_SIZE, |column, row| { |
| 855 | let (x, y) = (column as f32 + 0.5, row as f32 + 0.5); |
| 856 | if inside(x, y, (24., 21.), (8.5, 9.5)) { |
| 857 | palette.skin |
| 858 | } else if inside(x, y, (24., 19.), (10.5, 11.)) && y < 22. { |
| 859 | palette.hair |
| 860 | } else if inside(x, y, (24., 50.), (22., 19.)) { |
| 861 | palette.shirt |
| 862 | } else { |
| 863 | palette.background |
| 864 | } |
| 865 | }) |
| 866 | } |
| 867 | |
| 868 | fn encode_rgb(width: usize, height: usize, pixel: impl Fn(usize, usize) -> [u8; 3]) -> Vec<u8> { |
| 869 | let mut raw = Vec::with_capacity(height * (width * 3 + 1)); |
| 870 | for row in 0..height { |
| 871 | raw.push(0); |
| 872 | for column in 0..width { |
| 873 | raw.extend_from_slice(&pixel(column, row)); |
| 874 | } |
| 875 | } |
| 876 | let mut zlib = vec![0x78, 0x01]; |
| 877 | let mut blocks = raw.chunks(65535).peekable(); |
| 878 | while let Some(block) = blocks.next() { |
| 879 | zlib.push(u8::from(blocks.peek().is_none())); |
| 880 | zlib.extend_from_slice(&(block.len() as u16).to_le_bytes()); |
| 881 | zlib.extend_from_slice(&(!(block.len() as u16)).to_le_bytes()); |
| 882 | zlib.extend_from_slice(block); |
| 883 | } |
| 884 | zlib.extend_from_slice(&adler32(&raw).to_be_bytes()); |
| 885 | let mut header = Vec::new(); |
| 886 | header.extend_from_slice(&(width as u32).to_be_bytes()); |
| 887 | header.extend_from_slice(&(height as u32).to_be_bytes()); |
| 888 | header.extend_from_slice(&[8, 2, 0, 0, 0]); |
| 889 | let mut png = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]; |
| 890 | png_chunk(&mut png, b"IHDR", &header); |
| 891 | png_chunk(&mut png, b"IDAT", &zlib); |
| 892 | png_chunk(&mut png, b"IEND", &[]); |
| 893 | png |
| 894 | } |
| 895 | |
| 896 | #[cfg(test)] |
| 897 | mod tests { |
| 898 | use super::*; |
| 899 | |
| 900 | #[test] |
| 901 | fn png_has_signature_and_ends_with_iend() { |
| 902 | let png = encode_png(&MARA); |
| 903 | assert_eq!(&png[..8], &[0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]); |
| 904 | assert_eq!(&png[png.len() - 8..png.len() - 4], b"IEND"); |
| 905 | } |
| 906 | |
| 907 | #[test] |
| 908 | fn crc_and_adler_match_known_values() { |
| 909 | assert_eq!(crc32(b"IEND"), 0xae42_6082); |
| 910 | assert_eq!(adler32(b"Wikipedia"), 0x11e6_0398); |
| 911 | } |
| 912 | |
| 913 | #[test] |
| 914 | fn mention_candidates_filter_by_name_and_add_scope_in_channels() { |
| 915 | let chat = mention_candidates("demo-chat-release", "ma"); |
| 916 | assert_eq!(chat.len(), 1); |
| 917 | assert_eq!(chat[0].display_name(), "Mara Lindqvist"); |
| 918 | let channel = mention_candidates("demo-channel-1-1", "gen"); |
| 919 | assert!(matches!(channel[0], MentionCandidate::Channel { .. })); |
| 920 | assert_eq!(mention_candidates("demo-chat-release", "").len(), 5); |
| 921 | } |
| 922 | |
| 923 | #[test] |
| 924 | fn demo_covers_the_sidebar_edge_cases() { |
| 925 | let chats = demo_chats(); |
| 926 | assert!(chats.iter().any(|chat| chat.deleted)); |
| 927 | assert!(chats.iter().any(|chat| chat.title.len() > 40)); |
| 928 | assert!(chats.iter().filter(|chat| chat.unread).count() >= 3); |
| 929 | } |
| 930 | } |