| 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::{ |
| 11 | CardActionOutcome, ChatApp, MentionCandidate, PersonCandidate, PersonSource, TaskDialog, |
| 12 | TaskDialogKind, |
| 13 | }; |
| 14 | |
| 15 | use crate::activity::{Actor, Entry, Kind}; |
| 16 | use crate::app_state::{AppState, Selection}; |
| 17 | use crate::card_actions::{CardAnswer, CardTask}; |
| 18 | use crate::data::{FolderInfo, FolderKind, Person, PresenceKind}; |
| 19 | |
| 20 | pub const DEMO_USER_ID: &str = "demo-me"; |
| 21 | const DEMO_USER_NAME: &str = "Dana Demo"; |
| 22 | const PHOTO_SIZE: usize = 48; |
| 23 | const STAGING_IMAGE_KEY: &str = "demo://staging-dashboard"; |
| 24 | const PENDING_IMAGE_KEY: &str = "demo://pending-screenshot"; |
| 25 | const STAGING_SIZE: (usize, usize) = (960, 540); |
| 26 | const PRIORITY_CHAT: &str = "demo-chat-priya"; |
| 27 | const BOT_CHAT: &str = "demo-chat-wiki-bot"; |
| 28 | const BOT_NAME: &str = "Wiki Bot"; |
| 29 | const BOT_APP_NAME: &str = "Wiki Cloud"; |
| 30 | const BOT_ID: &str = "00000000-0000-0000-0000-00000000b07a"; |
| 31 | const CARD_ANSWER_DELAY: std::time::Duration = std::time::Duration::from_millis(700); |
| 32 | const CARD_PERSON_URL: &str = "https://avatars.githubusercontent.com/u/9919?s=64"; |
| 33 | const CARD_PAGE_URL: &str = "https://avatars.githubusercontent.com/u/9919?s=64"; |
| 34 | const NEW_CHAT_PREFIX: &str = "demo-chat-new-"; |
| 35 | const PEOPLE_DIRECTORY: [(&str, &str, &str, &str); 5] = [ |
| 36 | ( |
| 37 | MARA_ID, |
| 38 | "Mara Lindqvist", |
| 39 | "Product Owner", |
| 40 | "mara.lindqvist@example.com", |
| 41 | ), |
| 42 | ( |
| 43 | JONAS_ID, |
| 44 | "Jonas Ortega", |
| 45 | "Backend Engineer", |
| 46 | "jonas.ortega@example.com", |
| 47 | ), |
| 48 | ( |
| 49 | PRIYA_ID, |
| 50 | "Priya Nair", |
| 51 | "Engineering Manager", |
| 52 | "priya.nair@example.com", |
| 53 | ), |
| 54 | ( |
| 55 | LEA_ID, |
| 56 | "Lea Schneider", |
| 57 | "Customer Success", |
| 58 | "lea.schneider@example.com", |
| 59 | ), |
| 60 | ( |
| 61 | TOBIAS_ID, |
| 62 | "Tobias Klein", |
| 63 | "Support Engineer", |
| 64 | "tobias.klein@example.com", |
| 65 | ), |
| 66 | ]; |
| 67 | |
| 68 | struct Palette { |
| 69 | background: [u8; 3], |
| 70 | skin: [u8; 3], |
| 71 | hair: [u8; 3], |
| 72 | shirt: [u8; 3], |
| 73 | } |
| 74 | |
| 75 | const MARA: Palette = Palette { |
| 76 | background: [0x3f, 0x5f, 0x7a], |
| 77 | skin: [0xe0, 0xb2, 0x93], |
| 78 | hair: [0xd9, 0xa4, 0x41], |
| 79 | shirt: [0x1e, 0x29, 0x3b], |
| 80 | }; |
| 81 | const PRIYA: Palette = Palette { |
| 82 | background: [0x4b, 0x3b, 0x5c], |
| 83 | skin: [0x9a, 0x6a, 0x4a], |
| 84 | hair: [0x1c, 0x19, 0x17], |
| 85 | shirt: [0xbe, 0x12, 0x3c], |
| 86 | }; |
| 87 | const TOBIAS: Palette = Palette { |
| 88 | background: [0x3b, 0x4a, 0x3f], |
| 89 | skin: [0xf1, 0xc7, 0xa5], |
| 90 | hair: [0x57, 0x53, 0x4e], |
| 91 | shirt: [0x0f, 0x76, 0x6e], |
| 92 | }; |
| 93 | |
| 94 | const MARA_ID: &str = "demo-mara"; |
| 95 | const JONAS_ID: &str = "demo-jonas"; |
| 96 | const PRIYA_ID: &str = "demo-priya"; |
| 97 | const LEA_ID: &str = "demo-lea"; |
| 98 | const TOBIAS_ID: &str = "demo-tobias"; |
| 99 | |
| 100 | const DEMO_TEAMS: [(&str, &str, [&str; 3]); 4] = [ |
| 101 | ( |
| 102 | "demo-team-1", |
| 103 | "Platform", |
| 104 | ["General", "Incidents", "Releases"], |
| 105 | ), |
| 106 | ("demo-team-2", "Product", ["General", "Roadmap", "Feedback"]), |
| 107 | ( |
| 108 | "demo-team-3", |
| 109 | "Operations", |
| 110 | ["General", "On call", "Runbooks"], |
| 111 | ), |
| 112 | ( |
| 113 | "demo-team-4", |
| 114 | "Archive 2025", |
| 115 | ["General", "Trade fair", "Office move"], |
| 116 | ), |
| 117 | ]; |
| 118 | const DEMO_TEAM_ORDER: [&str; 4] = ["demo-team-2", "demo-team-1", "demo-team-3", "demo-team-4"]; |
| 119 | const DEMO_HIDDEN_TEAM: &str = "demo-team-4"; |
| 120 | const DEMO_HIDDEN_CHANNEL: &str = "demo-channel-3-3"; |
| 121 | |
| 122 | const UNREAD_CHAT: &str = "demo-chat-atlas"; |
| 123 | const RELEASE_CHAT: &str = "demo-chat-release"; |
| 124 | const FAVORITES_ID: &str = "demo-folder-favorites"; |
| 125 | const CUSTOMERS_FOLDER: &str = "demo-folder-customers"; |
| 126 | const ATLAS_FOLDER: &str = "demo-folder-atlas"; |
| 127 | const TODO_FOLDER: &str = "demo-folder-todo"; |
| 128 | |
| 129 | fn member(user_id: &str, name: &str) -> MemberRecord { |
| 130 | MemberRecord { |
| 131 | user_id: Some(user_id.to_owned()), |
| 132 | display_name: name.to_owned(), |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | fn people(extra: &[(&str, &str)]) -> Vec<MemberRecord> { |
| 137 | std::iter::once(member(DEMO_USER_ID, DEMO_USER_NAME)) |
| 138 | .chain(extra.iter().map(|(id, name)| member(id, name))) |
| 139 | .collect() |
| 140 | } |
| 141 | |
| 142 | pub fn at(days_ago: i64, hour: u32, minute: u32) -> DateTime<Utc> { |
| 143 | let day = Local::now().date_naive() - Duration::days(days_ago); |
| 144 | let local = day.and_hms_opt(hour, minute, 0).expect("valid time"); |
| 145 | Local |
| 146 | .from_local_datetime(&local) |
| 147 | .earliest() |
| 148 | .map_or_else(Utc::now, |time| time.with_timezone(&Utc)) |
| 149 | } |
| 150 | |
| 151 | struct DemoChat { |
| 152 | id: &'static str, |
| 153 | kind: &'static str, |
| 154 | title: &'static str, |
| 155 | members: Vec<MemberRecord>, |
| 156 | time: DateTime<Utc>, |
| 157 | unread: bool, |
| 158 | preview: Option<(&'static str, &'static str, &'static str)>, |
| 159 | deleted: bool, |
| 160 | } |
| 161 | |
| 162 | fn demo_chats() -> Vec<DemoChat> { |
| 163 | let group = |extra: &[(&str, &str)]| people(extra); |
| 164 | vec![ |
| 165 | DemoChat { |
| 166 | id: BOT_CHAT, |
| 167 | kind: "oneOnOne", |
| 168 | title: BOT_NAME, |
| 169 | members: people(&[]), |
| 170 | time: at(0, 13, 45), |
| 171 | unread: true, |
| 172 | preview: Some(("", BOT_NAME, "Mara Lindqvist edited your page")), |
| 173 | deleted: false, |
| 174 | }, |
| 175 | DemoChat { |
| 176 | id: "demo-chat-mara", |
| 177 | kind: "oneOnOne", |
| 178 | title: "Mara Lindqvist", |
| 179 | members: people(&[(MARA_ID, "Mara Lindqvist")]), |
| 180 | time: at(0, 13, 40), |
| 181 | unread: false, |
| 182 | preview: Some((MARA_ID, "Mara Lindqvist", "Sure, 2 pm works")), |
| 183 | deleted: false, |
| 184 | }, |
| 185 | DemoChat { |
| 186 | id: RELEASE_CHAT, |
| 187 | kind: "group", |
| 188 | title: "Release planning", |
| 189 | members: group(&[ |
| 190 | (MARA_ID, "Mara Lindqvist"), |
| 191 | (JONAS_ID, "Jonas Ortega"), |
| 192 | (PRIYA_ID, "Priya Nair"), |
| 193 | (LEA_ID, "Lea Schneider"), |
| 194 | ]), |
| 195 | time: at(0, 13, 35), |
| 196 | unread: false, |
| 197 | preview: Some(( |
| 198 | DEMO_USER_ID, |
| 199 | DEMO_USER_NAME, |
| 200 | "Build 42 is green, see pipeline", |
| 201 | )), |
| 202 | deleted: false, |
| 203 | }, |
| 204 | DemoChat { |
| 205 | id: "demo-chat-customer-1", |
| 206 | kind: "group", |
| 207 | title: "Customer North - Operations", |
| 208 | members: group(&[(LEA_ID, "Lea Schneider"), (TOBIAS_ID, "Tobias Klein")]), |
| 209 | time: at(0, 12, 50), |
| 210 | unread: true, |
| 211 | preview: Some((LEA_ID, "Lea Schneider", "Access is enabled now")), |
| 212 | deleted: false, |
| 213 | }, |
| 214 | DemoChat { |
| 215 | id: "demo-chat-customer-2", |
| 216 | kind: "group", |
| 217 | title: "Customer South - Rollout", |
| 218 | members: group(&[(JONAS_ID, "Jonas Ortega"), (PRIYA_ID, "Priya Nair")]), |
| 219 | time: at(0, 9, 5), |
| 220 | unread: true, |
| 221 | preview: Some((JONAS_ID, "Jonas Ortega", "Meeting is set for Friday")), |
| 222 | deleted: false, |
| 223 | }, |
| 224 | DemoChat { |
| 225 | id: "demo-chat-customer-3", |
| 226 | kind: "group", |
| 227 | title: "Customer West - Support", |
| 228 | members: group(&[(TOBIAS_ID, "Tobias Klein"), (LEA_ID, "Lea Schneider")]), |
| 229 | time: at(1, 16, 0), |
| 230 | unread: false, |
| 231 | preview: Some((TOBIAS_ID, "Tobias Klein", "Ticket is closed")), |
| 232 | deleted: false, |
| 233 | }, |
| 234 | DemoChat { |
| 235 | id: "demo-chat-customer-4", |
| 236 | kind: "group", |
| 237 | title: "Customer East - Pilot", |
| 238 | members: group(&[(MARA_ID, "Mara Lindqvist"), (PRIYA_ID, "Priya Nair")]), |
| 239 | time: at(3, 10, 0), |
| 240 | unread: false, |
| 241 | preview: Some((DEMO_USER_ID, DEMO_USER_NAME, "I'll send the documents")), |
| 242 | deleted: false, |
| 243 | }, |
| 244 | DemoChat { |
| 245 | id: "demo-chat-atlas", |
| 246 | kind: "group", |
| 247 | title: "Atlas core team", |
| 248 | members: group(&[ |
| 249 | (PRIYA_ID, "Priya Nair"), |
| 250 | (LEA_ID, "Lea Schneider"), |
| 251 | (MARA_ID, "Mara Lindqvist"), |
| 252 | ]), |
| 253 | time: at(0, 13, 12), |
| 254 | unread: true, |
| 255 | preview: Some((PRIYA_ID, "Priya Nair", "Who is taking the review?")), |
| 256 | deleted: false, |
| 257 | }, |
| 258 | DemoChat { |
| 259 | id: "demo-chat-jonas", |
| 260 | kind: "oneOnOne", |
| 261 | title: "Jonas Ortega", |
| 262 | members: people(&[(JONAS_ID, "Jonas Ortega")]), |
| 263 | time: at(0, 11, 2), |
| 264 | unread: false, |
| 265 | preview: Some((DEMO_USER_ID, DEMO_USER_NAME, "Thanks, it's merged")), |
| 266 | deleted: false, |
| 267 | }, |
| 268 | DemoChat { |
| 269 | id: "demo-chat-priya", |
| 270 | kind: "oneOnOne", |
| 271 | title: "Priya Nair", |
| 272 | members: people(&[(PRIYA_ID, "Priya Nair")]), |
| 273 | time: at(0, 10, 47), |
| 274 | unread: false, |
| 275 | preview: Some((PRIYA_ID, "Priya Nair", "Image")), |
| 276 | deleted: false, |
| 277 | }, |
| 278 | DemoChat { |
| 279 | id: "demo-chat-tobias", |
| 280 | kind: "oneOnOne", |
| 281 | title: "Tobias Klein", |
| 282 | members: people(&[(TOBIAS_ID, "Tobias Klein")]), |
| 283 | time: at(2, 9, 30), |
| 284 | unread: false, |
| 285 | preview: Some((TOBIAS_ID, "Tobias Klein", "ok")), |
| 286 | deleted: false, |
| 287 | }, |
| 288 | DemoChat { |
| 289 | id: "demo-chat-allhands", |
| 290 | kind: "group", |
| 291 | title: "All-hands announcements", |
| 292 | members: group(&[(LEA_ID, "Lea Schneider"), (MARA_ID, "Mara Lindqvist")]), |
| 293 | time: at(0, 9, 0), |
| 294 | unread: true, |
| 295 | preview: Some((LEA_ID, "Lea Schneider", "New vacation policy from November")), |
| 296 | deleted: false, |
| 297 | }, |
| 298 | DemoChat { |
| 299 | id: "demo-chat-design", |
| 300 | kind: "group", |
| 301 | title: "Design review", |
| 302 | members: group(&[(MARA_ID, "Mara Lindqvist"), (TOBIAS_ID, "Tobias Klein")]), |
| 303 | time: at(2, 15, 0), |
| 304 | unread: false, |
| 305 | preview: Some((MARA_ID, "Mara Lindqvist", "alt")), |
| 306 | deleted: true, |
| 307 | }, |
| 308 | DemoChat { |
| 309 | id: "demo-chat-quarterly", |
| 310 | kind: "group", |
| 311 | title: "Quarterly numbers, finance review and planning 2027", |
| 312 | members: group(&[(LEA_ID, "Lea Schneider"), (JONAS_ID, "Jonas Ortega")]), |
| 313 | time: at(4, 11, 0), |
| 314 | unread: false, |
| 315 | preview: Some((LEA_ID, "Lea Schneider", "Draft attached")), |
| 316 | deleted: false, |
| 317 | }, |
| 318 | ] |
| 319 | } |
| 320 | |
| 321 | pub fn mention_candidates(conversation_id: &str, query: &str) -> Vec<MentionCandidate> { |
| 322 | let needle = query.trim().to_lowercase(); |
| 323 | let matches = |name: &str| needle.is_empty() || name.to_lowercase().contains(&needle); |
| 324 | let people = PEOPLE_DIRECTORY |
| 325 | .iter() |
| 326 | .filter(|(_, name, _, _)| matches(name)) |
| 327 | .map(|(user_id, name, job_title, mail)| { |
| 328 | MentionCandidate::Person(PersonCandidate { |
| 329 | user_id: (*user_id).to_owned(), |
| 330 | display_name: (*name).to_owned(), |
| 331 | mail: Some((*mail).to_owned()), |
| 332 | job_title: Some((*job_title).to_owned()), |
| 333 | source: PersonSource::Member, |
| 334 | }) |
| 335 | }); |
| 336 | let scope: Vec<MentionCandidate> = if conversation_id.starts_with("demo-channel") { |
| 337 | [ |
| 338 | MentionCandidate::Channel { |
| 339 | channel_id: conversation_id.to_owned(), |
| 340 | name: "General".to_owned(), |
| 341 | }, |
| 342 | MentionCandidate::Team { |
| 343 | team_id: "demo-team-1".to_owned(), |
| 344 | name: "Platform".to_owned(), |
| 345 | }, |
| 346 | ] |
| 347 | .into_iter() |
| 348 | .filter(|candidate| matches(candidate.display_name())) |
| 349 | .collect() |
| 350 | } else { |
| 351 | Vec::new() |
| 352 | }; |
| 353 | people.chain(scope).collect() |
| 354 | } |
| 355 | |
| 356 | pub fn send_in_chat( |
| 357 | chat: &ChatRecord, |
| 358 | preview: &str, |
| 359 | html: &str, |
| 360 | now: DateTime<Utc>, |
| 361 | ) -> (ChatRecord, MessageRecord) { |
| 362 | let mut chat = chat.clone(); |
| 363 | chat.last_message_at = Some(now); |
| 364 | chat.last_message_preview = Some(preview.to_owned()); |
| 365 | chat.last_message_sender_id = Some(DEMO_USER_ID.to_owned()); |
| 366 | chat.last_message_sender_name = Some(DEMO_USER_NAME.to_owned()); |
| 367 | chat.last_message_deleted = false; |
| 368 | chat.unread = false; |
| 369 | let record = message( |
| 370 | &chat.id, |
| 371 | &format!("demo-sent-{}", now.timestamp_millis()), |
| 372 | None, |
| 373 | (DEMO_USER_ID, DEMO_USER_NAME), |
| 374 | now, |
| 375 | html, |
| 376 | "[]", |
| 377 | false, |
| 378 | ); |
| 379 | (chat, record) |
| 380 | } |
| 381 | |
| 382 | pub fn new_chat( |
| 383 | existing: &[ChatRecord], |
| 384 | people: &[(String, String)], |
| 385 | topic: Option<&str>, |
| 386 | preview: &str, |
| 387 | html: &str, |
| 388 | now: DateTime<Utc>, |
| 389 | ) -> (ChatRecord, MessageRecord) { |
| 390 | let number = existing |
| 391 | .iter() |
| 392 | .filter(|chat| chat.id.starts_with(NEW_CHAT_PREFIX)) |
| 393 | .count() |
| 394 | + 1; |
| 395 | let one_on_one = people.len() == 1; |
| 396 | let members = std::iter::once(member(DEMO_USER_ID, DEMO_USER_NAME)) |
| 397 | .chain(people.iter().map(|(user_id, name)| member(user_id, name))) |
| 398 | .collect(); |
| 399 | let chat = ChatRecord { |
| 400 | id: format!("{NEW_CHAT_PREFIX}{number}"), |
| 401 | kind: if one_on_one { "oneOnOne" } else { "group" }.to_owned(), |
| 402 | title: if one_on_one { |
| 403 | String::new() |
| 404 | } else { |
| 405 | topic.unwrap_or_default().to_owned() |
| 406 | }, |
| 407 | member_summary: people |
| 408 | .iter() |
| 409 | .map(|(_, name)| name.as_str()) |
| 410 | .collect::<Vec<_>>() |
| 411 | .join(", "), |
| 412 | members, |
| 413 | ..Default::default() |
| 414 | }; |
| 415 | send_in_chat(&chat, preview, html, now) |
| 416 | } |
| 417 | |
| 418 | fn image_directory() -> PathBuf { |
| 419 | std::env::temp_dir().join("rusty-teams-demo") |
| 420 | } |
| 421 | |
| 422 | pub fn first_unread(conversation_id: &str) -> Option<String> { |
| 423 | (conversation_id == UNREAD_CHAT).then(|| "u3".to_owned()) |
| 424 | } |
| 425 | |
| 426 | pub fn read_message_ids(records: &[MessageRecord]) -> std::collections::HashSet<String> { |
| 427 | let newest = records.iter().max_by_key(|record| record.created_at); |
| 428 | records |
| 429 | .iter() |
| 430 | .filter(|record| { |
| 431 | Some(record.message_id.as_str()) != newest.map(|newest| newest.message_id.as_str()) |
| 432 | }) |
| 433 | .map(|record| record.message_id.clone()) |
| 434 | .collect() |
| 435 | } |
| 436 | |
| 437 | pub fn first_selection() -> Selection { |
| 438 | Selection::Chat(RELEASE_CHAT.to_owned()) |
| 439 | } |
| 440 | |
| 441 | pub fn activity_entries() -> Vec<Entry> { |
| 442 | let actor = |user_id: &str, name: &str| Actor { |
| 443 | user_id: Some(user_id.to_owned()), |
| 444 | name: name.to_owned(), |
| 445 | }; |
| 446 | let entry = |conversation_id: &str, |
| 447 | kind: Kind, |
| 448 | message_id: &str, |
| 449 | actors: Vec<Actor>, |
| 450 | preview: &str, |
| 451 | count: u32, |
| 452 | updated_at: DateTime<Utc>, |
| 453 | read: bool| Entry { |
| 454 | id: 0, |
| 455 | conversation_id: conversation_id.to_owned(), |
| 456 | kind, |
| 457 | message_id: message_id.to_owned(), |
| 458 | actors, |
| 459 | preview: preview.to_owned(), |
| 460 | glyphs: Vec::new(), |
| 461 | count, |
| 462 | updated_at, |
| 463 | read, |
| 464 | }; |
| 465 | let reaction = Entry { |
| 466 | glyphs: vec!["\u{1F602}".to_owned(), "\u{1F44D}".to_owned()], |
| 467 | ..entry( |
| 468 | RELEASE_CHAT, |
| 469 | Kind::Reaction, |
| 470 | "m3", |
| 471 | vec![actor(PRIYA_ID, "Priya Nair"), actor(LEA_ID, "Lea Schneider")], |
| 472 | "I'll take the changelog.", |
| 473 | 2, |
| 474 | at(0, 13, 20), |
| 475 | false, |
| 476 | ) |
| 477 | }; |
| 478 | vec![ |
| 479 | entry( |
| 480 | UNREAD_CHAT, |
| 481 | Kind::Messages, |
| 482 | "u3", |
| 483 | vec![actor(LEA_ID, "Lea Schneider"), actor(PRIYA_ID, "Priya Nair")], |
| 484 | "Who is taking the review?", |
| 485 | 3, |
| 486 | at(0, 13, 12), |
| 487 | false, |
| 488 | ), |
| 489 | entry( |
| 490 | "demo-channel-1-1", |
| 491 | Kind::Mention, |
| 492 | "t1", |
| 493 | vec![actor(MARA_ID, "Mara Lindqvist")], |
| 494 | "@Dana can you approve the merge? The pipeline is waiting.", |
| 495 | 1, |
| 496 | at(0, 11, 40), |
| 497 | false, |
| 498 | ), |
| 499 | reaction, |
| 500 | entry( |
| 501 | "demo-chat-customer-3", |
| 502 | Kind::Messages, |
| 503 | "demo-customer-3-closed", |
| 504 | vec![actor(TOBIAS_ID, "Tobias Klein")], |
| 505 | "Ticket is closed", |
| 506 | 1, |
| 507 | at(1, 16, 0), |
| 508 | true, |
| 509 | ), |
| 510 | ] |
| 511 | } |
| 512 | |
| 513 | pub fn seed(store: &Store) { |
| 514 | let chats: Vec<ChatRecord> = demo_chats() |
| 515 | .into_iter() |
| 516 | .map(|demo| ChatRecord { |
| 517 | id: demo.id.to_owned(), |
| 518 | kind: demo.kind.to_owned(), |
| 519 | title: if demo.kind == "oneOnOne" { |
| 520 | String::new() |
| 521 | } else { |
| 522 | demo.title.to_owned() |
| 523 | }, |
| 524 | member_summary: demo.title.to_owned(), |
| 525 | last_message_at: Some(demo.time), |
| 526 | unread: demo.unread, |
| 527 | members: demo.members, |
| 528 | last_message_preview: demo.preview.map(|(_, _, text)| text.to_owned()), |
| 529 | last_message_sender_id: demo.preview.map(|(id, _, _)| id.to_owned()), |
| 530 | last_message_sender_name: demo.preview.map(|(_, name, _)| name.to_owned()), |
| 531 | last_message_deleted: demo.deleted, |
| 532 | ..Default::default() |
| 533 | }) |
| 534 | .collect(); |
| 535 | let _ = store.upsert_chats(&chats); |
| 536 | let teams: Vec<TeamRecord> = DEMO_TEAMS |
| 537 | .iter() |
| 538 | .map(|(id, name, _)| TeamRecord { |
| 539 | id: (*id).to_owned(), |
| 540 | name: (*name).to_owned(), |
| 541 | }) |
| 542 | .collect(); |
| 543 | let _ = store.upsert_teams(&teams); |
| 544 | let mut channels = Vec::new(); |
| 545 | for (team_index, (team_id, _, names)) in DEMO_TEAMS.iter().enumerate() { |
| 546 | for (channel_index, name) in names.iter().enumerate() { |
| 547 | channels.push(ChannelRecord { |
| 548 | id: format!("demo-channel-{}-{}", team_index + 1, channel_index + 1), |
| 549 | team_id: (*team_id).to_owned(), |
| 550 | name: (*name).to_owned(), |
| 551 | membership_type: None, |
| 552 | last_message_at: Some(at(channel_index as i64, 12, 10)), |
| 553 | unread: (team_index == 0 && channel_index == 1) |
| 554 | || (team_index == 1 && channel_index == 0), |
| 555 | }); |
| 556 | } |
| 557 | } |
| 558 | let _ = store.upsert_channels(&channels); |
| 559 | let _ = store.replace_team_layout(&demo_team_layout(&channels)); |
| 560 | let _ = store.set_meta("me_user_id", DEMO_USER_ID); |
| 561 | let _ = store.set_meta("me_display_name", DEMO_USER_NAME); |
| 562 | let _ = store.upsert_messages(&release_messages()); |
| 563 | let _ = store.upsert_messages(&unread_messages()); |
| 564 | let _ = store.upsert_messages(&channel_messages()); |
| 565 | let _ = store.upsert_messages(&priority_messages()); |
| 566 | let _ = store.upsert_messages(&bot_messages()); |
| 567 | } |
| 568 | |
| 569 | fn photo(palette: &Palette) -> Arc<Image> { |
| 570 | crate::data::avatar_image_from("image/png", encode_png(palette)) |
| 571 | } |
| 572 | |
| 573 | pub fn seed_directory(state: &mut AppState) { |
| 574 | let directory = &mut state.directory; |
| 575 | directory.me = Some(Person { |
| 576 | user_id: DEMO_USER_ID.to_owned(), |
| 577 | display_name: DEMO_USER_NAME.to_owned(), |
| 578 | }); |
| 579 | let ids = |list: &[&str]| list.iter().map(|id| (*id).to_owned()).collect::<Vec<_>>(); |
| 580 | directory.folders = vec![ |
| 581 | FolderInfo { |
| 582 | id: FAVORITES_ID.to_owned(), |
| 583 | name: String::new(), |
| 584 | kind: FolderKind::Favorites, |
| 585 | conversation_ids: ids(&["demo-chat-mara", RELEASE_CHAT]), |
| 586 | }, |
| 587 | FolderInfo { |
| 588 | id: CUSTOMERS_FOLDER.to_owned(), |
| 589 | name: "Customers".to_owned(), |
| 590 | kind: FolderKind::UserCreated, |
| 591 | conversation_ids: ids(&[ |
| 592 | "demo-chat-customer-1", |
| 593 | "demo-chat-customer-2", |
| 594 | "demo-chat-customer-3", |
| 595 | "demo-chat-customer-4", |
| 596 | ]), |
| 597 | }, |
| 598 | FolderInfo { |
| 599 | id: ATLAS_FOLDER.to_owned(), |
| 600 | name: "Project Atlas".to_owned(), |
| 601 | kind: FolderKind::UserCreated, |
| 602 | conversation_ids: ids(&["demo-chat-atlas", "demo-chat-jonas"]), |
| 603 | }, |
| 604 | FolderInfo { |
| 605 | id: TODO_FOLDER.to_owned(), |
| 606 | name: "To do".to_owned(), |
| 607 | kind: FolderKind::UserCreated, |
| 608 | conversation_ids: Vec::new(), |
| 609 | }, |
| 610 | ]; |
| 611 | state.bot_apps.insert(BOT_CHAT.to_owned(), vec![bot_app()]); |
| 612 | let directory = &mut state.directory; |
| 613 | directory.pinned_channels = ids(&["demo-channel-1-1", "demo-channel-3-2"]); |
| 614 | directory |
| 615 | .unread_counts |
| 616 | .insert("demo-chat-atlas".to_owned(), 5); |
| 617 | directory |
| 618 | .unread_counts |
| 619 | .insert("demo-chat-customer-1".to_owned(), 2); |
| 620 | directory |
| 621 | .unread_counts |
| 622 | .insert("demo-chat-customer-2".to_owned(), 1); |
| 623 | directory |
| 624 | .unread_counts |
| 625 | .insert("demo-chat-allhands".to_owned(), 120); |
| 626 | directory.set_avatar(MARA_ID, Some(photo(&MARA))); |
| 627 | directory.set_avatar(PRIYA_ID, Some(photo(&PRIYA))); |
| 628 | directory.set_avatar(TOBIAS_ID, Some(photo(&TOBIAS))); |
| 629 | for (user_id, kind) in [ |
| 630 | (MARA_ID, PresenceKind::Available), |
| 631 | (JONAS_ID, PresenceKind::DoNotDisturb), |
| 632 | (PRIYA_ID, PresenceKind::Away), |
| 633 | (TOBIAS_ID, PresenceKind::Offline), |
| 634 | (LEA_ID, PresenceKind::DoNotDisturb), |
| 635 | (DEMO_USER_ID, PresenceKind::Available), |
| 636 | ] { |
| 637 | directory.set_presence(user_id, kind); |
| 638 | } |
| 639 | let directory_path = image_directory(); |
| 640 | if std::fs::create_dir_all(&directory_path).is_ok() { |
| 641 | let path = directory_path.join("staging-dashboard.png"); |
| 642 | let png = encode_rgb(STAGING_SIZE.0, STAGING_SIZE.1, staging_pixel); |
| 643 | if std::fs::write(&path, png).is_ok() { |
| 644 | state.directory.set_image(STAGING_IMAGE_KEY, &path); |
| 645 | } |
| 646 | } |
| 647 | state.collapsed.insert(CUSTOMERS_FOLDER.to_owned()); |
| 648 | state.last_sync = Some(Utc::now()); |
| 649 | state.apply_bot_titles(); |
| 650 | } |
| 651 | |
| 652 | #[allow(clippy::too_many_arguments)] |
| 653 | fn message( |
| 654 | conversation: &str, |
| 655 | id: &str, |
| 656 | reply_to: Option<&str>, |
| 657 | sender: (&str, &str), |
| 658 | time: DateTime<Utc>, |
| 659 | html: &str, |
| 660 | reactions_json: &str, |
| 661 | edited: bool, |
| 662 | ) -> MessageRecord { |
| 663 | MessageRecord { |
| 664 | conversation_id: conversation.to_owned(), |
| 665 | message_id: id.to_owned(), |
| 666 | reply_to_id: reply_to.map(str::to_owned), |
| 667 | sender_id: Some(sender.0.to_owned()), |
| 668 | sender_name: Some(sender.1.to_owned()), |
| 669 | sender_application_id: None, |
| 670 | created_at: time, |
| 671 | edited_at: edited.then_some(time + Duration::minutes(2)), |
| 672 | deleted: false, |
| 673 | body_html: html.to_owned(), |
| 674 | attachments_json: "[]".to_owned(), |
| 675 | reactions_json: reactions_json.to_owned(), |
| 676 | mentions_json: "[]".to_owned(), |
| 677 | } |
| 678 | } |
| 679 | |
| 680 | fn with_attachments(mut record: MessageRecord, attachments_json: &str) -> MessageRecord { |
| 681 | record.attachments_json = attachments_json.to_owned(); |
| 682 | record |
| 683 | } |
| 684 | |
| 685 | fn bot_messages() -> Vec<MessageRecord> { |
| 686 | let card = serde_json::json!({ |
| 687 | "type": "AdaptiveCard", |
| 688 | "body": [ |
| 689 | {"type": "ColumnSet", "columns": [ |
| 690 | {"type": "Column", "width": "auto", "verticalContentAlignment": "center", "items": [ |
| 691 | {"type": "Image", "url": CARD_PERSON_URL, "size": "small", "style": "person", "height": "32px"} |
| 692 | ]}, |
| 693 | {"type": "Column", "width": "stretch", "verticalContentAlignment": "center", "items": [ |
| 694 | {"type": "TextBlock", "size": "medium", "weight": "bolder", "wrap": true, "text": "Mara Lindqvist edited your page"} |
| 695 | ]} |
| 696 | ]}, |
| 697 | {"type": "ColumnSet", "separator": true, "columns": [ |
| 698 | {"type": "Column", "width": "auto", "items": [ |
| 699 | {"type": "Image", "url": CARD_PAGE_URL, "width": "24px", "height": "24px"} |
| 700 | ]}, |
| 701 | {"type": "Column", "width": "stretch", "items": [ |
| 702 | {"type": "TextBlock", "weight": "bolder", "wrap": true, "text": "[**Release Checklist Q4**](https://example.com/wiki/release-checklist) in [**Platform**](https://example.com/wiki/platform)"}, |
| 703 | {"type": "TextBlock", "spacing": "small", "isSubtle": true, "wrap": true, "text": "Owned by: Dana Demo"} |
| 704 | ]} |
| 705 | ]}, |
| 706 | {"type": "TextBlock", "id": "owner-note", "isVisible": false, "isSubtle": true, "wrap": true, "text": "Owner notes are only shown on request."}, |
| 707 | {"type": "ActionSet", "actions": [ |
| 708 | {"type": "Action.OpenUrl", "title": "View page", "url": "https://example.com/wiki/release-checklist"}, |
| 709 | {"type": "Action.OpenUrl", "title": "View changes", "url": "https://example.com/wiki/release-checklist/changes"}, |
| 710 | {"type": "Action.Submit", "title": "Watch page", "data": {"action": "watch"}}, |
| 711 | {"type": "Action.Submit", "title": "Stop watching", "data": {"action": "unwatch"}}, |
| 712 | {"type": "Action.Submit", "title": "Settings", "data": {"msteams": {"type": "task/fetch"}}}, |
| 713 | {"type": "Action.ToggleVisibility", "title": "Owner notes", "targetElements": ["owner-note"]}, |
| 714 | {"type": "Action.ShowCard", "title": "Details", "card": {"body": [ |
| 715 | {"type": "TextBlock", "wrap": true, "text": "Last edited by **Mara Lindqvist** on Tuesday."} |
| 716 | ]}} |
| 717 | ]} |
| 718 | ] |
| 719 | }); |
| 720 | let attachments = serde_json::json!([{ |
| 721 | "content_type": "application/vnd.microsoft.card.adaptive", |
| 722 | "name": null, |
| 723 | "url": null, |
| 724 | "text": null, |
| 725 | "content": card.to_string() |
| 726 | }]); |
| 727 | let mut record = with_attachments( |
| 728 | message( |
| 729 | BOT_CHAT, |
| 730 | "b1", |
| 731 | None, |
| 732 | ("", BOT_NAME), |
| 733 | at(0, 13, 45), |
| 734 | "<attachment id=\"b1\"></attachment>", |
| 735 | "[]", |
| 736 | false, |
| 737 | ), |
| 738 | &attachments.to_string(), |
| 739 | ); |
| 740 | record.sender_id = None; |
| 741 | record.sender_application_id = Some(BOT_ID.to_owned()); |
| 742 | vec![record] |
| 743 | } |
| 744 | |
| 745 | fn bot_app() -> ChatApp { |
| 746 | ChatApp { |
| 747 | app_id: "demo-app-wiki".to_owned(), |
| 748 | name: BOT_APP_NAME.to_owned(), |
| 749 | bot_ids: vec![BOT_ID.to_owned()], |
| 750 | small_image_url: None, |
| 751 | accent_color: None, |
| 752 | web_application_resource: None, |
| 753 | } |
| 754 | } |
| 755 | |
| 756 | pub async fn card_answer(task: CardTask) -> CardAnswer { |
| 757 | tokio::time::sleep(CARD_ANSWER_DELAY).await; |
| 758 | let data = match task { |
| 759 | CardTask::Submit(_) => { |
| 760 | return Ok(( |
| 761 | CardActionOutcome::Message("Settings saved".into()), |
| 762 | Some((bot_app(), BOT_ID.to_owned())), |
| 763 | )); |
| 764 | } |
| 765 | CardTask::Action(action) => match action.invoke_payload() { |
| 766 | Some(payload) => payload.value, |
| 767 | None => return Err("Not supported".to_owned()), |
| 768 | }, |
| 769 | }; |
| 770 | let outcome = if data.pointer("/data/type").and_then(|kind| kind.as_str()) == Some("task/fetch") |
| 771 | { |
| 772 | CardActionOutcome::Dialog(TaskDialog { |
| 773 | title: None, |
| 774 | width: 520, |
| 775 | height: 300, |
| 776 | kind: TaskDialogKind::Card(settings_card().to_string()), |
| 777 | }) |
| 778 | } else if data.get("action").and_then(|action| action.as_str()) == Some("unwatch") { |
| 779 | CardActionOutcome::Failed("The app answered 500: demo failure".into()) |
| 780 | } else { |
| 781 | CardActionOutcome::Sent |
| 782 | }; |
| 783 | Ok((outcome, Some((bot_app(), BOT_ID.to_owned())))) |
| 784 | } |
| 785 | |
| 786 | fn settings_card() -> serde_json::Value { |
| 787 | serde_json::json!({ |
| 788 | "type": "AdaptiveCard", |
| 789 | "body": [ |
| 790 | {"type": "TextBlock", "weight": "bolder", "text": "Notification settings"}, |
| 791 | {"type": "TextBlock", "wrap": true, "isSubtle": true, "text": "Choose how the bot tells you about page changes."} |
| 792 | ], |
| 793 | "actions": [ |
| 794 | {"type": "Action.Submit", "title": "Save", "data": {"save": true}} |
| 795 | ] |
| 796 | }) |
| 797 | } |
| 798 | |
| 799 | fn priority_messages() -> Vec<MessageRecord> { |
| 800 | let priya = (PRIYA_ID, "Priya Nair"); |
| 801 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 802 | vec![ |
| 803 | message( |
| 804 | PRIORITY_CHAT, |
| 805 | "p1", |
| 806 | None, |
| 807 | me, |
| 808 | at(0, 10, 40), |
| 809 | "<p>Have you seen the error on staging yet?</p>", |
| 810 | "[]", |
| 811 | false, |
| 812 | ), |
| 813 | message( |
| 814 | PRIORITY_CHAT, |
| 815 | "p2", |
| 816 | None, |
| 817 | priya, |
| 818 | at(0, 10, 47), |
| 819 | &format!( |
| 820 | "<p>Yes, here is the screenshot:</p><img src=\"{PENDING_IMAGE_KEY}\" width=\"1280\" height=\"720\">" |
| 821 | ), |
| 822 | "[]", |
| 823 | false, |
| 824 | ), |
| 825 | ] |
| 826 | } |
| 827 | |
| 828 | fn reaction(kind: &str, users: &[(&str, &str, DateTime<Utc>)]) -> Vec<serde_json::Value> { |
| 829 | users |
| 830 | .iter() |
| 831 | .map(|(user_id, user_name, created_at)| { |
| 832 | serde_json::json!({ |
| 833 | "reaction_type": kind, |
| 834 | "user_id": user_id, |
| 835 | "user_name": user_name, |
| 836 | "created_at": created_at, |
| 837 | }) |
| 838 | }) |
| 839 | .collect() |
| 840 | } |
| 841 | |
| 842 | fn reactions_json(groups: Vec<Vec<serde_json::Value>>) -> String { |
| 843 | serde_json::Value::Array(groups.into_iter().flatten().collect()).to_string() |
| 844 | } |
| 845 | |
| 846 | fn release_messages() -> Vec<MessageRecord> { |
| 847 | let chat = RELEASE_CHAT; |
| 848 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 849 | let jonas = (JONAS_ID, "Jonas Ortega"); |
| 850 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 851 | let jonas_name = "Jonas Ortega"; |
| 852 | let priya_name = "Priya Nair"; |
| 853 | let lea_name = "Lea Schneider"; |
| 854 | let mara_name = "Mara Lindqvist"; |
| 855 | let tobias_name = "Tobias Klein"; |
| 856 | let likes = reactions_json(vec![reaction( |
| 857 | "like", |
| 858 | &[ |
| 859 | (JONAS_ID, jonas_name, at(1, 16, 30)), |
| 860 | (PRIYA_ID, priya_name, at(1, 17, 2)), |
| 861 | (LEA_ID, lea_name, at(0, 8, 15)), |
| 862 | ], |
| 863 | )]); |
| 864 | let hearts = reactions_json(vec![reaction( |
| 865 | "heart", |
| 866 | &[ |
| 867 | (DEMO_USER_ID, DEMO_USER_NAME, at(0, 10, 35)), |
| 868 | (JONAS_ID, jonas_name, at(0, 10, 33)), |
| 869 | ], |
| 870 | )]); |
| 871 | let single = reactions_json(vec![reaction( |
| 872 | "laugh", |
| 873 | &[(PRIYA_ID, priya_name, at(0, 9, 10))], |
| 874 | )]); |
| 875 | let many = reactions_json(vec![ |
| 876 | reaction( |
| 877 | "like", |
| 878 | &[ |
| 879 | (MARA_ID, mara_name, at(0, 13, 37)), |
| 880 | (JONAS_ID, jonas_name, at(0, 13, 38)), |
| 881 | (PRIYA_ID, priya_name, at(0, 13, 40)), |
| 882 | (LEA_ID, lea_name, at(0, 13, 45)), |
| 883 | (TOBIAS_ID, tobias_name, at(0, 13, 50)), |
| 884 | ], |
| 885 | ), |
| 886 | reaction( |
| 887 | "heart", |
| 888 | &[ |
| 889 | (LEA_ID, lea_name, at(0, 13, 46)), |
| 890 | (TOBIAS_ID, tobias_name, at(0, 13, 51)), |
| 891 | ], |
| 892 | ), |
| 893 | reaction("\u{1F389}", &[(MARA_ID, mara_name, at(0, 13, 39))]), |
| 894 | ]); |
| 895 | vec![ |
| 896 | message( |
| 897 | chat, |
| 898 | "m1", |
| 899 | None, |
| 900 | mara, |
| 901 | at(1, 16, 20), |
| 902 | "<p>Release candidate is on staging. Can someone run the regression suite?</p>", |
| 903 | "[]", |
| 904 | false, |
| 905 | ), |
| 906 | message( |
| 907 | chat, |
| 908 | "m2", |
| 909 | None, |
| 910 | mara, |
| 911 | at(1, 16, 22), |
| 912 | "<p>Checklist lives in <a href=\"https://example.com/checklist\">release checklist</a>.</p>", |
| 913 | &likes, |
| 914 | false, |
| 915 | ), |
| 916 | message( |
| 917 | chat, |
| 918 | "m2b", |
| 919 | None, |
| 920 | mara, |
| 921 | at(1, 16, 40), |
| 922 | &format!( |
| 923 | "<p>Staging dashboard right now:</p><img src=\"{STAGING_IMAGE_KEY}\" width=\"{}\" height=\"{}\">", |
| 924 | STAGING_SIZE.0, STAGING_SIZE.1 |
| 925 | ), |
| 926 | "[]", |
| 927 | false, |
| 928 | ), |
| 929 | with_attachments( |
| 930 | message( |
| 931 | chat, |
| 932 | "m2c", |
| 933 | None, |
| 934 | mara, |
| 935 | at(1, 16, 41), |
| 936 | "<p>Checklist and rollout plan attached.</p>", |
| 937 | "[]", |
| 938 | false, |
| 939 | ), |
| 940 | 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}]"#, |
| 941 | ), |
| 942 | message( |
| 943 | chat, |
| 944 | "m3", |
| 945 | None, |
| 946 | me, |
| 947 | at(0, 9, 2), |
| 948 | "<p>I'll take the changelog.</p>", |
| 949 | &single, |
| 950 | false, |
| 951 | ), |
| 952 | message( |
| 953 | chat, |
| 954 | "m4", |
| 955 | None, |
| 956 | jonas, |
| 957 | at(0, 10, 14), |
| 958 | "<p><at id=\"0\">Mara Lindqvist</at> can you take a quick look at this? It runs before <code>cargo test</code>.</p><pre><code class=\"language-rust\">// only release branches\nfn freeze(branch: &str) -> bool {\n branch.starts_with(\"release/\")\n}</code></pre>", |
| 959 | "[]", |
| 960 | false, |
| 961 | ), |
| 962 | message( |
| 963 | chat, |
| 964 | "m5", |
| 965 | None, |
| 966 | mara, |
| 967 | at(0, 10, 31), |
| 968 | "<blockquote itemscope=\"\" itemtype=\"http://schema.skype.com/Reply\" itemid=\"m4\"><strong itemprop=\"mri\">Jonas Ortega</strong><p itemprop=\"preview\">can you take a quick look at this?</p></blockquote><p>Looks good. Small thing: the name could say what it checks.</p>", |
| 969 | &hearts, |
| 970 | true, |
| 971 | ), |
| 972 | message( |
| 973 | chat, |
| 974 | "m5b", |
| 975 | None, |
| 976 | mara, |
| 977 | at(0, 10, 40), |
| 978 | "<p>Rollout for Friday:</p><ul><li>Freeze the release branch</li><li>Run the smoke tests on staging, then post the results in the channel</li><li>Ship<ul><li>Windows installer</li><li>Linux <code>.AppImage</code></li></ul></li></ul><p>Order:</p><ol><li>Tag</li><li>Build</li><li>Announce</li></ol>", |
| 979 | "[]", |
| 980 | false, |
| 981 | ), |
| 982 | message( |
| 983 | chat, |
| 984 | "m5c", |
| 985 | None, |
| 986 | jonas, |
| 987 | at(0, 11, 5), |
| 988 | "<h2>Release notes 0.2</h2><p>The <s>old sync path</s> <u>is gone</u>.</p><table><thead><tr><th>Area</th><th>Owner</th></tr></thead><tbody><tr><td>Sync</td><td>Priya</td></tr><tr><td>Render</td><td>Jonas</td></tr></tbody></table><hr><blockquote>Measure twice, cut once.</blockquote><p><span style=\"background-color: rgb(255, 255, 0)\">Important:</span> <span style=\"color: #c00000\">backup first</span>.</p>", |
| 989 | "[]", |
| 990 | false, |
| 991 | ), |
| 992 | message( |
| 993 | chat, |
| 994 | "m6", |
| 995 | None, |
| 996 | me, |
| 997 | at(0, 13, 35), |
| 998 | "<p>Thanks, renamed. Tagging now.</p>", |
| 999 | "[]", |
| 1000 | false, |
| 1001 | ), |
| 1002 | message( |
| 1003 | chat, |
| 1004 | "m7", |
| 1005 | None, |
| 1006 | me, |
| 1007 | at(0, 13, 36), |
| 1008 | "<p>Build 42 is green, see <a href=\"https://example.com/build/42\">pipeline</a>.</p>", |
| 1009 | &many, |
| 1010 | false, |
| 1011 | ), |
| 1012 | ] |
| 1013 | } |
| 1014 | |
| 1015 | fn unread_messages() -> Vec<MessageRecord> { |
| 1016 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 1017 | let lea = (LEA_ID, "Lea Schneider"); |
| 1018 | let tobias = (TOBIAS_ID, "Tobias Klein"); |
| 1019 | let rows = [ |
| 1020 | ( |
| 1021 | "u1", |
| 1022 | me, |
| 1023 | at(1, 17, 5), |
| 1024 | "<p>Can you enable access for North?</p>", |
| 1025 | ), |
| 1026 | ("u2", tobias, at(1, 17, 20), "<p>I'll check with IT.</p>"), |
| 1027 | ( |
| 1028 | "u3", |
| 1029 | lea, |
| 1030 | at(0, 12, 40), |
| 1031 | "<p>Good morning, access is ready.</p>", |
| 1032 | ), |
| 1033 | ( |
| 1034 | "u4", |
| 1035 | lea, |
| 1036 | at(0, 12, 41), |
| 1037 | "<p>Sign in with your company ID, no password needed.</p>", |
| 1038 | ), |
| 1039 | ("u5", lea, at(0, 12, 50), "<p>Access is enabled now</p>"), |
| 1040 | ]; |
| 1041 | rows.into_iter() |
| 1042 | .map(|(id, sender, time, html)| { |
| 1043 | message(UNREAD_CHAT, id, None, sender, time, html, "[]", false) |
| 1044 | }) |
| 1045 | .collect() |
| 1046 | } |
| 1047 | |
| 1048 | fn demo_team_layout(channels: &[ChannelRecord]) -> Vec<TeamLayoutRecord> { |
| 1049 | DEMO_TEAM_ORDER |
| 1050 | .iter() |
| 1051 | .map(|team_id| TeamLayoutRecord { |
| 1052 | team_id: (*team_id).to_owned(), |
| 1053 | hidden: *team_id == DEMO_HIDDEN_TEAM, |
| 1054 | channels: channels |
| 1055 | .iter() |
| 1056 | .filter(|channel| channel.team_id == *team_id) |
| 1057 | .map(|channel| ChannelLayoutRecord { |
| 1058 | channel_id: channel.id.clone(), |
| 1059 | general: channel.name == "General", |
| 1060 | hidden: channel.id == DEMO_HIDDEN_CHANNEL, |
| 1061 | }) |
| 1062 | .collect(), |
| 1063 | }) |
| 1064 | .collect() |
| 1065 | } |
| 1066 | |
| 1067 | fn channel_messages() -> Vec<MessageRecord> { |
| 1068 | let channel = "demo-channel-1-1"; |
| 1069 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 1070 | let tobias = (TOBIAS_ID, "Tobias Klein"); |
| 1071 | vec![ |
| 1072 | message( |
| 1073 | channel, |
| 1074 | "t1", |
| 1075 | None, |
| 1076 | mara, |
| 1077 | at(0, 8, 0), |
| 1078 | "<p>Weekly sync notes are up.</p>", |
| 1079 | "[]", |
| 1080 | false, |
| 1081 | ), |
| 1082 | message( |
| 1083 | channel, |
| 1084 | "t1r1", |
| 1085 | Some("t1"), |
| 1086 | tobias, |
| 1087 | at(0, 8, 20), |
| 1088 | "<p>Thanks, adding my items.</p>", |
| 1089 | "[]", |
| 1090 | false, |
| 1091 | ), |
| 1092 | message( |
| 1093 | channel, |
| 1094 | "t3", |
| 1095 | None, |
| 1096 | tobias, |
| 1097 | at(0, 11, 30), |
| 1098 | "<p>Release build 42 is green, deploying to staging now.</p>", |
| 1099 | "[]", |
| 1100 | false, |
| 1101 | ), |
| 1102 | message( |
| 1103 | channel, |
| 1104 | "t2", |
| 1105 | None, |
| 1106 | tobias, |
| 1107 | at(0, 11, 0), |
| 1108 | "<p>Who owns the on-call handover?</p>", |
| 1109 | "[]", |
| 1110 | false, |
| 1111 | ), |
| 1112 | message( |
| 1113 | channel, |
| 1114 | "t4", |
| 1115 | None, |
| 1116 | mara, |
| 1117 | at(3, 8, 42), |
| 1118 | "<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>", |
| 1119 | "[]", |
| 1120 | false, |
| 1121 | ), |
| 1122 | message( |
| 1123 | channel, |
| 1124 | "t4r1", |
| 1125 | Some("t4"), |
| 1126 | tobias, |
| 1127 | at(0, 9, 15), |
| 1128 | "<p>Yes, all done.</p>", |
| 1129 | "[]", |
| 1130 | false, |
| 1131 | ), |
| 1132 | ] |
| 1133 | } |
| 1134 | |
| 1135 | fn crc32(bytes: &[u8]) -> u32 { |
| 1136 | let mut crc = 0xffff_ffffu32; |
| 1137 | for byte in bytes { |
| 1138 | crc ^= u32::from(*byte); |
| 1139 | for _ in 0..8 { |
| 1140 | crc = if crc & 1 == 1 { |
| 1141 | (crc >> 1) ^ 0xedb8_8320 |
| 1142 | } else { |
| 1143 | crc >> 1 |
| 1144 | }; |
| 1145 | } |
| 1146 | } |
| 1147 | !crc |
| 1148 | } |
| 1149 | |
| 1150 | fn adler32(bytes: &[u8]) -> u32 { |
| 1151 | let (mut low, mut high) = (1u32, 0u32); |
| 1152 | for byte in bytes { |
| 1153 | low = (low + u32::from(*byte)) % 65521; |
| 1154 | high = (high + low) % 65521; |
| 1155 | } |
| 1156 | (high << 16) | low |
| 1157 | } |
| 1158 | |
| 1159 | fn png_chunk(output: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) { |
| 1160 | output.extend_from_slice(&(data.len() as u32).to_be_bytes()); |
| 1161 | let mut body = kind.to_vec(); |
| 1162 | body.extend_from_slice(data); |
| 1163 | output.extend_from_slice(&body); |
| 1164 | output.extend_from_slice(&crc32(&body).to_be_bytes()); |
| 1165 | } |
| 1166 | |
| 1167 | fn inside(x: f32, y: f32, center: (f32, f32), radius: (f32, f32)) -> bool { |
| 1168 | ((x - center.0) / radius.0).powi(2) + ((y - center.1) / radius.1).powi(2) <= 1. |
| 1169 | } |
| 1170 | |
| 1171 | fn staging_pixel(column: usize, row: usize) -> [u8; 3] { |
| 1172 | let (width, height) = (STAGING_SIZE.0 as f32, STAGING_SIZE.1 as f32); |
| 1173 | let (x, y) = (column as f32, row as f32); |
| 1174 | let shade = y / height; |
| 1175 | let background = [ |
| 1176 | (30. - 14. * shade) as u8, |
| 1177 | (41. - 16. * shade) as u8, |
| 1178 | (59. - 14. * shade) as u8, |
| 1179 | ]; |
| 1180 | let bar_width = width / 14.; |
| 1181 | let slot = (x / bar_width) as usize; |
| 1182 | let in_bar = x - slot as f32 * bar_width > bar_width * 0.18; |
| 1183 | let bar_height = height * (0.25 + 0.5 * (((slot * 7 + 3) % 11) as f32 / 11.)); |
| 1184 | let on_bars = in_bar && y > height * 0.86 - bar_height && y < height * 0.86 && slot < 14; |
| 1185 | let header = y < height * 0.1; |
| 1186 | if header { |
| 1187 | [22, 30, 45] |
| 1188 | } else if on_bars { |
| 1189 | if slot % 4 == 3 { |
| 1190 | [226, 87, 47] |
| 1191 | } else { |
| 1192 | [88, 133, 196] |
| 1193 | } |
| 1194 | } else if (y - height * 0.86).abs() < 1.5 { |
| 1195 | [71, 85, 105] |
| 1196 | } else { |
| 1197 | background |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | fn encode_png(palette: &Palette) -> Vec<u8> { |
| 1202 | encode_rgb(PHOTO_SIZE, PHOTO_SIZE, |column, row| { |
| 1203 | let (x, y) = (column as f32 + 0.5, row as f32 + 0.5); |
| 1204 | if inside(x, y, (24., 21.), (8.5, 9.5)) { |
| 1205 | palette.skin |
| 1206 | } else if inside(x, y, (24., 19.), (10.5, 11.)) && y < 22. { |
| 1207 | palette.hair |
| 1208 | } else if inside(x, y, (24., 50.), (22., 19.)) { |
| 1209 | palette.shirt |
| 1210 | } else { |
| 1211 | palette.background |
| 1212 | } |
| 1213 | }) |
| 1214 | } |
| 1215 | |
| 1216 | fn encode_rgb(width: usize, height: usize, pixel: impl Fn(usize, usize) -> [u8; 3]) -> Vec<u8> { |
| 1217 | let mut raw = Vec::with_capacity(height * (width * 3 + 1)); |
| 1218 | for row in 0..height { |
| 1219 | raw.push(0); |
| 1220 | for column in 0..width { |
| 1221 | raw.extend_from_slice(&pixel(column, row)); |
| 1222 | } |
| 1223 | } |
| 1224 | let mut zlib = vec![0x78, 0x01]; |
| 1225 | let mut blocks = raw.chunks(65535).peekable(); |
| 1226 | while let Some(block) = blocks.next() { |
| 1227 | zlib.push(u8::from(blocks.peek().is_none())); |
| 1228 | zlib.extend_from_slice(&(block.len() as u16).to_le_bytes()); |
| 1229 | zlib.extend_from_slice(&(!(block.len() as u16)).to_le_bytes()); |
| 1230 | zlib.extend_from_slice(block); |
| 1231 | } |
| 1232 | zlib.extend_from_slice(&adler32(&raw).to_be_bytes()); |
| 1233 | let mut header = Vec::new(); |
| 1234 | header.extend_from_slice(&(width as u32).to_be_bytes()); |
| 1235 | header.extend_from_slice(&(height as u32).to_be_bytes()); |
| 1236 | header.extend_from_slice(&[8, 2, 0, 0, 0]); |
| 1237 | let mut png = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]; |
| 1238 | png_chunk(&mut png, b"IHDR", &header); |
| 1239 | png_chunk(&mut png, b"IDAT", &zlib); |
| 1240 | png_chunk(&mut png, b"IEND", &[]); |
| 1241 | png |
| 1242 | } |
| 1243 | |
| 1244 | #[cfg(test)] |
| 1245 | mod tests { |
| 1246 | use super::*; |
| 1247 | |
| 1248 | #[test] |
| 1249 | fn png_has_signature_and_ends_with_iend() { |
| 1250 | let png = encode_png(&MARA); |
| 1251 | assert_eq!(&png[..8], &[0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]); |
| 1252 | assert_eq!(&png[png.len() - 8..png.len() - 4], b"IEND"); |
| 1253 | } |
| 1254 | |
| 1255 | #[test] |
| 1256 | fn crc_and_adler_match_known_values() { |
| 1257 | assert_eq!(crc32(b"IEND"), 0xae42_6082); |
| 1258 | assert_eq!(adler32(b"Wikipedia"), 0x11e6_0398); |
| 1259 | } |
| 1260 | |
| 1261 | #[test] |
| 1262 | fn mention_candidates_filter_by_name_and_add_scope_in_channels() { |
| 1263 | let chat = mention_candidates("demo-chat-release", "ma"); |
| 1264 | assert_eq!(chat.len(), 1); |
| 1265 | assert_eq!(chat[0].display_name(), "Mara Lindqvist"); |
| 1266 | let channel = mention_candidates("demo-channel-1-1", "gen"); |
| 1267 | assert!(matches!(channel[0], MentionCandidate::Channel { .. })); |
| 1268 | assert_eq!(mention_candidates("demo-chat-release", "").len(), 5); |
| 1269 | } |
| 1270 | |
| 1271 | #[test] |
| 1272 | fn new_group_chat_gets_a_title_and_the_first_message() { |
| 1273 | let people = vec![ |
| 1274 | (MARA_ID.to_owned(), "Mara Lindqvist".to_owned()), |
| 1275 | (JONAS_ID.to_owned(), "Jonas Ortega".to_owned()), |
| 1276 | ]; |
| 1277 | let now = Utc::now(); |
| 1278 | let (chat, record) = new_chat( |
| 1279 | &[], |
| 1280 | &people, |
| 1281 | Some("Crew"), |
| 1282 | "Hello all", |
| 1283 | "Hello <i>all</i>", |
| 1284 | now, |
| 1285 | ); |
| 1286 | assert_eq!(chat.id, "demo-chat-new-1"); |
| 1287 | assert_eq!(chat.kind, "group"); |
| 1288 | assert_eq!(chat.title, "Crew"); |
| 1289 | assert_eq!(chat.member_summary, "Mara Lindqvist, Jonas Ortega"); |
| 1290 | assert_eq!(chat.members.len(), 3); |
| 1291 | assert_eq!(chat.members[0].user_id.as_deref(), Some(DEMO_USER_ID)); |
| 1292 | assert_eq!(chat.last_message_preview.as_deref(), Some("Hello all")); |
| 1293 | assert_eq!(chat.last_message_at, Some(now)); |
| 1294 | assert_eq!(record.conversation_id, chat.id); |
| 1295 | assert_eq!(record.sender_id.as_deref(), Some(DEMO_USER_ID)); |
| 1296 | assert!(record.body_html.contains("Hello")); |
| 1297 | } |
| 1298 | |
| 1299 | #[test] |
| 1300 | fn new_one_on_one_chat_has_no_title_and_counts_up() { |
| 1301 | let people = vec![(LEA_ID.to_owned(), "Lea Schneider".to_owned())]; |
| 1302 | let (first, _) = new_chat(&[], &people, None, "Hi", "Hi", Utc::now()); |
| 1303 | assert_eq!(first.kind, "oneOnOne"); |
| 1304 | assert!(first.title.is_empty()); |
| 1305 | let (second, _) = new_chat(&[first], &people, None, "Hi again", "Hi again", Utc::now()); |
| 1306 | assert_eq!(second.id, "demo-chat-new-2"); |
| 1307 | } |
| 1308 | |
| 1309 | #[test] |
| 1310 | fn sending_into_a_chat_refreshes_its_preview() { |
| 1311 | let base = ChatRecord { |
| 1312 | id: "demo-chat-x".to_owned(), |
| 1313 | unread: true, |
| 1314 | ..Default::default() |
| 1315 | }; |
| 1316 | let (chat, record) = send_in_chat(&base, "Ping", "Ping", Utc::now()); |
| 1317 | assert!(!chat.unread); |
| 1318 | assert_eq!(chat.last_message_preview.as_deref(), Some("Ping")); |
| 1319 | assert_eq!(record.conversation_id, "demo-chat-x"); |
| 1320 | } |
| 1321 | |
| 1322 | #[test] |
| 1323 | fn demo_covers_the_sidebar_edge_cases() { |
| 1324 | let chats = demo_chats(); |
| 1325 | assert!(chats.iter().any(|chat| chat.deleted)); |
| 1326 | assert!(chats.iter().any(|chat| chat.title.len() > 40)); |
| 1327 | assert!(chats.iter().filter(|chat| chat.unread).count() >= 3); |
| 1328 | } |
| 1329 | } |