| 1 | use std::path::PathBuf; |
| 2 | use std::sync::{Arc, OnceLock}; |
| 3 | use std::time::Instant; |
| 4 | |
| 5 | use chrono::{DateTime, Duration, Local, TimeZone, Utc}; |
| 6 | use gpui_kit::Image; |
| 7 | use store::{ |
| 8 | ChannelLayoutRecord, ChannelRecord, ChatRecord, MemberRecord, MessageRecord, Store, |
| 9 | TeamLayoutRecord, TeamRecord, |
| 10 | }; |
| 11 | use teams_core::{ |
| 12 | CardActionOutcome, ChatApp, Gif, LinkPreview, MentionCandidate, PersonCandidate, PersonSource, |
| 13 | PinnedMessage, SavedMessage, TaskDialog, TaskDialogKind, |
| 14 | }; |
| 15 | |
| 16 | use crate::activity::{Actor, Entry, Kind}; |
| 17 | use crate::app_state::{AppState, Selection}; |
| 18 | use crate::card_actions::{CardAnswer, CardTask}; |
| 19 | use crate::data::{FolderInfo, FolderKind, Person, PresenceKind}; |
| 20 | use crate::demo_gifs; |
| 21 | use crate::message_actions::ForwardSource; |
| 22 | |
| 23 | static SEARCHABLE_GIFS: OnceLock<Vec<Gif>> = OnceLock::new(); |
| 24 | |
| 25 | pub const DEMO_USER_ID: &str = "demo-me"; |
| 26 | const DEMO_USER_NAME: &str = "Dana Demo"; |
| 27 | const DEMO_USER_EMAIL: &str = "dana.demo@example.com"; |
| 28 | const PHOTO_SIZE: usize = 48; |
| 29 | const STAGING_IMAGE_KEY: &str = "demo://staging-dashboard"; |
| 30 | const PENDING_IMAGE_KEY: &str = "demo://pending-screenshot"; |
| 31 | const STAGING_SIZE: (usize, usize) = (960, 540); |
| 32 | const LINK_IMAGE_KEY: &str = |
| 33 | "https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/image/Thumbnail?url=demo"; |
| 34 | const LINK_IMAGE_SIZE: (u32, u32) = (320, 180); |
| 35 | const PRIORITY_CHAT: &str = "demo-chat-priya"; |
| 36 | const BOT_CHAT: &str = "demo-chat-wiki-bot"; |
| 37 | const BOT_NAME: &str = "Wiki Bot"; |
| 38 | const BOT_APP_NAME: &str = "Wiki Cloud"; |
| 39 | const BOT_ID: &str = "00000000-0000-0000-0000-00000000b07a"; |
| 40 | const CARD_ANSWER_DELAY: std::time::Duration = std::time::Duration::from_millis(700); |
| 41 | const CARD_PERSON_URL: &str = "https://avatars.githubusercontent.com/u/9919?s=64"; |
| 42 | const CARD_PAGE_URL: &str = "https://avatars.githubusercontent.com/u/9919?s=64"; |
| 43 | const NEW_CHAT_PREFIX: &str = "demo-chat-new-"; |
| 44 | const PEOPLE_DIRECTORY: [(&str, &str, &str, &str); 5] = [ |
| 45 | ( |
| 46 | MARA_ID, |
| 47 | "Mara Lindqvist", |
| 48 | "Product Owner", |
| 49 | "mara.lindqvist@example.com", |
| 50 | ), |
| 51 | ( |
| 52 | JONAS_ID, |
| 53 | "Jonas Ortega", |
| 54 | "Backend Engineer", |
| 55 | "jonas.ortega@example.com", |
| 56 | ), |
| 57 | ( |
| 58 | PRIYA_ID, |
| 59 | "Priya Nair", |
| 60 | "Engineering Manager", |
| 61 | "priya.nair@example.com", |
| 62 | ), |
| 63 | ( |
| 64 | LEA_ID, |
| 65 | "Lea Schneider", |
| 66 | "Customer Success", |
| 67 | "lea.schneider@example.com", |
| 68 | ), |
| 69 | ( |
| 70 | TOBIAS_ID, |
| 71 | "Tobias Klein", |
| 72 | "Support Engineer", |
| 73 | "tobias.klein@example.com", |
| 74 | ), |
| 75 | ]; |
| 76 | |
| 77 | struct Palette { |
| 78 | background: [u8; 3], |
| 79 | skin: [u8; 3], |
| 80 | hair: [u8; 3], |
| 81 | shirt: [u8; 3], |
| 82 | } |
| 83 | |
| 84 | const MARA: Palette = Palette { |
| 85 | background: [0x3f, 0x5f, 0x7a], |
| 86 | skin: [0xe0, 0xb2, 0x93], |
| 87 | hair: [0xd9, 0xa4, 0x41], |
| 88 | shirt: [0x1e, 0x29, 0x3b], |
| 89 | }; |
| 90 | const PRIYA: Palette = Palette { |
| 91 | background: [0x4b, 0x3b, 0x5c], |
| 92 | skin: [0x9a, 0x6a, 0x4a], |
| 93 | hair: [0x1c, 0x19, 0x17], |
| 94 | shirt: [0xbe, 0x12, 0x3c], |
| 95 | }; |
| 96 | const TOBIAS: Palette = Palette { |
| 97 | background: [0x3b, 0x4a, 0x3f], |
| 98 | skin: [0xf1, 0xc7, 0xa5], |
| 99 | hair: [0x57, 0x53, 0x4e], |
| 100 | shirt: [0x0f, 0x76, 0x6e], |
| 101 | }; |
| 102 | |
| 103 | pub const MARA_ID: &str = "demo-mara"; |
| 104 | pub const JONAS_ID: &str = "demo-jonas"; |
| 105 | pub const PRIYA_ID: &str = "demo-priya"; |
| 106 | pub const LEA_ID: &str = "demo-lea"; |
| 107 | pub const TOBIAS_ID: &str = "demo-tobias"; |
| 108 | |
| 109 | const DEMO_TEAMS: [(&str, &str, [&str; 3]); 4] = [ |
| 110 | ( |
| 111 | "demo-team-1", |
| 112 | "Platform", |
| 113 | ["General", "Incidents", "Releases"], |
| 114 | ), |
| 115 | ("demo-team-2", "Product", ["General", "Roadmap", "Feedback"]), |
| 116 | ( |
| 117 | "demo-team-3", |
| 118 | "Operations", |
| 119 | ["General", "On call", "Runbooks"], |
| 120 | ), |
| 121 | ( |
| 122 | "demo-team-4", |
| 123 | "Archive 2025", |
| 124 | ["General", "Trade fair", "Office move"], |
| 125 | ), |
| 126 | ]; |
| 127 | const DEMO_TEAM_ORDER: [&str; 4] = ["demo-team-2", "demo-team-1", "demo-team-3", "demo-team-4"]; |
| 128 | const DEMO_HIDDEN_TEAM: &str = "demo-team-4"; |
| 129 | const DEMO_HIDDEN_CHANNEL: &str = "demo-channel-3-3"; |
| 130 | |
| 131 | const UNREAD_CHAT: &str = "demo-chat-atlas"; |
| 132 | const RELEASE_CHAT: &str = "demo-chat-release"; |
| 133 | const PINNED_MESSAGE: &str = "m5b"; |
| 134 | const SAVED_MESSAGES: [&str; 2] = ["m4", "m5c"]; |
| 135 | const FORWARD_ITEMTYPE: &str = "http://schema.skype.com/Forward"; |
| 136 | const FAVORITES_ID: &str = "demo-folder-favorites"; |
| 137 | const CUSTOMERS_FOLDER: &str = "demo-folder-customers"; |
| 138 | const ATLAS_FOLDER: &str = "demo-folder-atlas"; |
| 139 | const TODO_FOLDER: &str = "demo-folder-todo"; |
| 140 | |
| 141 | fn member(user_id: &str, name: &str) -> MemberRecord { |
| 142 | MemberRecord { |
| 143 | user_id: Some(user_id.to_owned()), |
| 144 | display_name: name.to_owned(), |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | fn people(extra: &[(&str, &str)]) -> Vec<MemberRecord> { |
| 149 | std::iter::once(member(DEMO_USER_ID, DEMO_USER_NAME)) |
| 150 | .chain(extra.iter().map(|(id, name)| member(id, name))) |
| 151 | .collect() |
| 152 | } |
| 153 | |
| 154 | pub fn at(days_ago: i64, hour: u32, minute: u32) -> DateTime<Utc> { |
| 155 | let day = Local::now().date_naive() - Duration::days(days_ago); |
| 156 | let local = day.and_hms_opt(hour, minute, 0).expect("valid time"); |
| 157 | Local |
| 158 | .from_local_datetime(&local) |
| 159 | .earliest() |
| 160 | .map_or_else(Utc::now, |time| time.with_timezone(&Utc)) |
| 161 | } |
| 162 | |
| 163 | struct DemoChat { |
| 164 | id: &'static str, |
| 165 | kind: &'static str, |
| 166 | title: &'static str, |
| 167 | members: Vec<MemberRecord>, |
| 168 | time: DateTime<Utc>, |
| 169 | unread: bool, |
| 170 | preview: Option<(&'static str, &'static str, &'static str)>, |
| 171 | deleted: bool, |
| 172 | } |
| 173 | |
| 174 | fn demo_chats() -> Vec<DemoChat> { |
| 175 | let group = |extra: &[(&str, &str)]| people(extra); |
| 176 | vec![ |
| 177 | DemoChat { |
| 178 | id: BOT_CHAT, |
| 179 | kind: "oneOnOne", |
| 180 | title: BOT_NAME, |
| 181 | members: people(&[]), |
| 182 | time: at(0, 13, 45), |
| 183 | unread: true, |
| 184 | preview: Some(("", BOT_NAME, "Mara Lindqvist edited your page")), |
| 185 | deleted: false, |
| 186 | }, |
| 187 | DemoChat { |
| 188 | id: "demo-chat-mara", |
| 189 | kind: "oneOnOne", |
| 190 | title: "Mara Lindqvist", |
| 191 | members: people(&[(MARA_ID, "Mara Lindqvist")]), |
| 192 | time: at(0, 13, 40), |
| 193 | unread: false, |
| 194 | preview: Some((MARA_ID, "Mara Lindqvist", "Sure, 2 pm works")), |
| 195 | deleted: false, |
| 196 | }, |
| 197 | DemoChat { |
| 198 | id: RELEASE_CHAT, |
| 199 | kind: "group", |
| 200 | title: "Release planning", |
| 201 | members: group(&[ |
| 202 | (MARA_ID, "Mara Lindqvist"), |
| 203 | (JONAS_ID, "Jonas Ortega"), |
| 204 | (PRIYA_ID, "Priya Nair"), |
| 205 | (LEA_ID, "Lea Schneider"), |
| 206 | ]), |
| 207 | time: at(0, 13, 35), |
| 208 | unread: false, |
| 209 | preview: Some(( |
| 210 | DEMO_USER_ID, |
| 211 | DEMO_USER_NAME, |
| 212 | "Build 42 is green, see pipeline", |
| 213 | )), |
| 214 | deleted: false, |
| 215 | }, |
| 216 | DemoChat { |
| 217 | id: "demo-chat-customer-1", |
| 218 | kind: "group", |
| 219 | title: "Customer North - Operations", |
| 220 | members: group(&[(LEA_ID, "Lea Schneider"), (TOBIAS_ID, "Tobias Klein")]), |
| 221 | time: at(0, 12, 50), |
| 222 | unread: true, |
| 223 | preview: Some((LEA_ID, "Lea Schneider", "Access is enabled now")), |
| 224 | deleted: false, |
| 225 | }, |
| 226 | DemoChat { |
| 227 | id: "demo-chat-customer-2", |
| 228 | kind: "group", |
| 229 | title: "Customer South - Rollout", |
| 230 | members: group(&[(JONAS_ID, "Jonas Ortega"), (PRIYA_ID, "Priya Nair")]), |
| 231 | time: at(0, 9, 5), |
| 232 | unread: true, |
| 233 | preview: Some((JONAS_ID, "Jonas Ortega", "Meeting is set for Friday")), |
| 234 | deleted: false, |
| 235 | }, |
| 236 | DemoChat { |
| 237 | id: "demo-chat-customer-3", |
| 238 | kind: "group", |
| 239 | title: "Customer West - Support", |
| 240 | members: group(&[(TOBIAS_ID, "Tobias Klein"), (LEA_ID, "Lea Schneider")]), |
| 241 | time: at(1, 16, 0), |
| 242 | unread: false, |
| 243 | preview: Some((TOBIAS_ID, "Tobias Klein", "Ticket is closed")), |
| 244 | deleted: false, |
| 245 | }, |
| 246 | DemoChat { |
| 247 | id: "demo-chat-customer-4", |
| 248 | kind: "group", |
| 249 | title: "Customer East - Pilot", |
| 250 | members: group(&[(MARA_ID, "Mara Lindqvist"), (PRIYA_ID, "Priya Nair")]), |
| 251 | time: at(3, 10, 0), |
| 252 | unread: false, |
| 253 | preview: Some((DEMO_USER_ID, DEMO_USER_NAME, "I'll send the documents")), |
| 254 | deleted: false, |
| 255 | }, |
| 256 | DemoChat { |
| 257 | id: "demo-chat-atlas", |
| 258 | kind: "group", |
| 259 | title: "Atlas core team", |
| 260 | members: group(&[ |
| 261 | (PRIYA_ID, "Priya Nair"), |
| 262 | (LEA_ID, "Lea Schneider"), |
| 263 | (MARA_ID, "Mara Lindqvist"), |
| 264 | ]), |
| 265 | time: at(0, 13, 12), |
| 266 | unread: true, |
| 267 | preview: Some((PRIYA_ID, "Priya Nair", "Who is taking the review?")), |
| 268 | deleted: false, |
| 269 | }, |
| 270 | DemoChat { |
| 271 | id: "demo-chat-jonas", |
| 272 | kind: "oneOnOne", |
| 273 | title: "Jonas Ortega", |
| 274 | members: people(&[(JONAS_ID, "Jonas Ortega")]), |
| 275 | time: at(0, 11, 2), |
| 276 | unread: false, |
| 277 | preview: Some((DEMO_USER_ID, DEMO_USER_NAME, "Thanks, it's merged")), |
| 278 | deleted: false, |
| 279 | }, |
| 280 | DemoChat { |
| 281 | id: "demo-chat-priya", |
| 282 | kind: "oneOnOne", |
| 283 | title: "Priya Nair", |
| 284 | members: people(&[(PRIYA_ID, "Priya Nair")]), |
| 285 | time: at(0, 10, 47), |
| 286 | unread: false, |
| 287 | preview: Some((PRIYA_ID, "Priya Nair", "Image")), |
| 288 | deleted: false, |
| 289 | }, |
| 290 | DemoChat { |
| 291 | id: "demo-chat-tobias", |
| 292 | kind: "oneOnOne", |
| 293 | title: "Tobias Klein", |
| 294 | members: people(&[(TOBIAS_ID, "Tobias Klein")]), |
| 295 | time: at(2, 9, 30), |
| 296 | unread: false, |
| 297 | preview: Some((TOBIAS_ID, "Tobias Klein", "ok")), |
| 298 | deleted: false, |
| 299 | }, |
| 300 | DemoChat { |
| 301 | id: "demo-chat-allhands", |
| 302 | kind: "group", |
| 303 | title: "All-hands announcements", |
| 304 | members: group(&[(LEA_ID, "Lea Schneider"), (MARA_ID, "Mara Lindqvist")]), |
| 305 | time: at(0, 9, 0), |
| 306 | unread: true, |
| 307 | preview: Some((LEA_ID, "Lea Schneider", "New vacation policy from November")), |
| 308 | deleted: false, |
| 309 | }, |
| 310 | DemoChat { |
| 311 | id: "demo-chat-design", |
| 312 | kind: "group", |
| 313 | title: "Design review", |
| 314 | members: group(&[(MARA_ID, "Mara Lindqvist"), (TOBIAS_ID, "Tobias Klein")]), |
| 315 | time: at(2, 15, 0), |
| 316 | unread: false, |
| 317 | preview: Some((MARA_ID, "Mara Lindqvist", "alt")), |
| 318 | deleted: true, |
| 319 | }, |
| 320 | DemoChat { |
| 321 | id: "demo-chat-quarterly", |
| 322 | kind: "group", |
| 323 | title: "Quarterly numbers, finance review and planning 2027", |
| 324 | members: group(&[(LEA_ID, "Lea Schneider"), (JONAS_ID, "Jonas Ortega")]), |
| 325 | time: at(4, 11, 0), |
| 326 | unread: false, |
| 327 | preview: Some((LEA_ID, "Lea Schneider", "Draft attached")), |
| 328 | deleted: false, |
| 329 | }, |
| 330 | ] |
| 331 | } |
| 332 | |
| 333 | pub fn mention_candidates(conversation_id: &str, query: &str) -> Vec<MentionCandidate> { |
| 334 | let needle = query.trim().to_lowercase(); |
| 335 | let matches = |name: &str| needle.is_empty() || name.to_lowercase().contains(&needle); |
| 336 | let people = PEOPLE_DIRECTORY |
| 337 | .iter() |
| 338 | .filter(|(_, name, _, _)| matches(name)) |
| 339 | .map(|(user_id, name, job_title, mail)| { |
| 340 | MentionCandidate::Person(PersonCandidate { |
| 341 | user_id: (*user_id).to_owned(), |
| 342 | display_name: (*name).to_owned(), |
| 343 | mail: Some((*mail).to_owned()), |
| 344 | job_title: Some((*job_title).to_owned()), |
| 345 | source: PersonSource::Member, |
| 346 | }) |
| 347 | }); |
| 348 | let scope: Vec<MentionCandidate> = if conversation_id.starts_with("demo-channel") { |
| 349 | [ |
| 350 | MentionCandidate::Channel { |
| 351 | channel_id: conversation_id.to_owned(), |
| 352 | name: "General".to_owned(), |
| 353 | }, |
| 354 | MentionCandidate::Team { |
| 355 | team_id: "demo-team-1".to_owned(), |
| 356 | name: "Platform".to_owned(), |
| 357 | }, |
| 358 | ] |
| 359 | .into_iter() |
| 360 | .filter(|candidate| matches(candidate.display_name())) |
| 361 | .collect() |
| 362 | } else { |
| 363 | Vec::new() |
| 364 | }; |
| 365 | people.chain(scope).collect() |
| 366 | } |
| 367 | |
| 368 | pub fn send_in_chat( |
| 369 | chat: &ChatRecord, |
| 370 | preview: &str, |
| 371 | html: &str, |
| 372 | now: DateTime<Utc>, |
| 373 | ) -> (ChatRecord, MessageRecord) { |
| 374 | let mut chat = chat.clone(); |
| 375 | chat.last_message_at = Some(now); |
| 376 | chat.last_message_preview = Some(preview.to_owned()); |
| 377 | chat.last_message_sender_id = Some(DEMO_USER_ID.to_owned()); |
| 378 | chat.last_message_sender_name = Some(DEMO_USER_NAME.to_owned()); |
| 379 | chat.last_message_deleted = false; |
| 380 | chat.unread = false; |
| 381 | let record = message( |
| 382 | &chat.id, |
| 383 | &format!("demo-sent-{}", now.timestamp_millis()), |
| 384 | None, |
| 385 | (DEMO_USER_ID, DEMO_USER_NAME), |
| 386 | now, |
| 387 | html, |
| 388 | "[]", |
| 389 | false, |
| 390 | ); |
| 391 | (chat, record) |
| 392 | } |
| 393 | |
| 394 | pub fn new_chat( |
| 395 | existing: &[ChatRecord], |
| 396 | people: &[(String, String)], |
| 397 | topic: Option<&str>, |
| 398 | preview: &str, |
| 399 | html: &str, |
| 400 | now: DateTime<Utc>, |
| 401 | ) -> (ChatRecord, MessageRecord) { |
| 402 | let number = existing |
| 403 | .iter() |
| 404 | .filter(|chat| chat.id.starts_with(NEW_CHAT_PREFIX)) |
| 405 | .count() |
| 406 | + 1; |
| 407 | let one_on_one = people.len() == 1; |
| 408 | let members = std::iter::once(member(DEMO_USER_ID, DEMO_USER_NAME)) |
| 409 | .chain(people.iter().map(|(user_id, name)| member(user_id, name))) |
| 410 | .collect(); |
| 411 | let chat = ChatRecord { |
| 412 | id: format!("{NEW_CHAT_PREFIX}{number}"), |
| 413 | kind: if one_on_one { "oneOnOne" } else { "group" }.to_owned(), |
| 414 | title: if one_on_one { |
| 415 | String::new() |
| 416 | } else { |
| 417 | topic.unwrap_or_default().to_owned() |
| 418 | }, |
| 419 | member_summary: people |
| 420 | .iter() |
| 421 | .map(|(_, name)| name.as_str()) |
| 422 | .collect::<Vec<_>>() |
| 423 | .join(", "), |
| 424 | members, |
| 425 | ..Default::default() |
| 426 | }; |
| 427 | send_in_chat(&chat, preview, html, now) |
| 428 | } |
| 429 | |
| 430 | fn image_directory() -> PathBuf { |
| 431 | std::env::temp_dir().join("rusty-teams-demo") |
| 432 | } |
| 433 | |
| 434 | pub fn search_gifs(query: &str) -> Vec<Gif> { |
| 435 | let query = query.trim().to_lowercase(); |
| 436 | SEARCHABLE_GIFS |
| 437 | .get() |
| 438 | .into_iter() |
| 439 | .flatten() |
| 440 | .filter(|gif| gif.title.to_lowercase().contains(&query)) |
| 441 | .cloned() |
| 442 | .collect() |
| 443 | } |
| 444 | |
| 445 | pub fn first_unread(conversation_id: &str) -> Option<String> { |
| 446 | (conversation_id == UNREAD_CHAT).then(|| "u3".to_owned()) |
| 447 | } |
| 448 | |
| 449 | pub fn read_message_ids(records: &[MessageRecord]) -> std::collections::HashSet<String> { |
| 450 | let newest = records.iter().max_by_key(|record| record.created_at); |
| 451 | records |
| 452 | .iter() |
| 453 | .filter(|record| { |
| 454 | Some(record.message_id.as_str()) != newest.map(|newest| newest.message_id.as_str()) |
| 455 | }) |
| 456 | .map(|record| record.message_id.clone()) |
| 457 | .collect() |
| 458 | } |
| 459 | |
| 460 | pub fn first_selection() -> Selection { |
| 461 | Selection::Chat(RELEASE_CHAT.to_owned()) |
| 462 | } |
| 463 | |
| 464 | pub fn activity_entries() -> Vec<Entry> { |
| 465 | let actor = |user_id: &str, name: &str| Actor { |
| 466 | user_id: Some(user_id.to_owned()), |
| 467 | name: name.to_owned(), |
| 468 | }; |
| 469 | let entry = |conversation_id: &str, |
| 470 | kind: Kind, |
| 471 | message_id: &str, |
| 472 | actors: Vec<Actor>, |
| 473 | preview: &str, |
| 474 | count: u32, |
| 475 | updated_at: DateTime<Utc>, |
| 476 | read: bool| Entry { |
| 477 | id: 0, |
| 478 | conversation_id: conversation_id.to_owned(), |
| 479 | kind, |
| 480 | message_id: message_id.to_owned(), |
| 481 | actors, |
| 482 | preview: preview.to_owned(), |
| 483 | glyphs: Vec::new(), |
| 484 | count, |
| 485 | updated_at, |
| 486 | read, |
| 487 | }; |
| 488 | let reaction = Entry { |
| 489 | glyphs: vec!["\u{1F602}".to_owned(), "\u{1F44D}".to_owned()], |
| 490 | ..entry( |
| 491 | RELEASE_CHAT, |
| 492 | Kind::Reaction, |
| 493 | "m3", |
| 494 | vec![ |
| 495 | actor(PRIYA_ID, "Priya Nair"), |
| 496 | actor(LEA_ID, "Lea Schneider"), |
| 497 | ], |
| 498 | "I'll take the changelog.", |
| 499 | 2, |
| 500 | at(0, 13, 20), |
| 501 | false, |
| 502 | ) |
| 503 | }; |
| 504 | vec![ |
| 505 | entry( |
| 506 | UNREAD_CHAT, |
| 507 | Kind::Messages, |
| 508 | "u3", |
| 509 | vec![ |
| 510 | actor(LEA_ID, "Lea Schneider"), |
| 511 | actor(PRIYA_ID, "Priya Nair"), |
| 512 | ], |
| 513 | "Who is taking the review?", |
| 514 | 3, |
| 515 | at(0, 13, 12), |
| 516 | false, |
| 517 | ), |
| 518 | entry( |
| 519 | "demo-channel-1-1", |
| 520 | Kind::Mention, |
| 521 | "t1", |
| 522 | vec![actor(MARA_ID, "Mara Lindqvist")], |
| 523 | "@Dana can you approve the merge? The pipeline is waiting.", |
| 524 | 1, |
| 525 | at(0, 11, 40), |
| 526 | false, |
| 527 | ), |
| 528 | reaction, |
| 529 | entry( |
| 530 | "demo-chat-customer-3", |
| 531 | Kind::Messages, |
| 532 | "demo-customer-3-closed", |
| 533 | vec![actor(TOBIAS_ID, "Tobias Klein")], |
| 534 | "Ticket is closed", |
| 535 | 1, |
| 536 | at(1, 16, 0), |
| 537 | true, |
| 538 | ), |
| 539 | ] |
| 540 | } |
| 541 | |
| 542 | pub fn seed(store: &Store) { |
| 543 | let chats: Vec<ChatRecord> = demo_chats() |
| 544 | .into_iter() |
| 545 | .map(|demo| ChatRecord { |
| 546 | id: demo.id.to_owned(), |
| 547 | kind: demo.kind.to_owned(), |
| 548 | title: if demo.kind == "oneOnOne" { |
| 549 | String::new() |
| 550 | } else { |
| 551 | demo.title.to_owned() |
| 552 | }, |
| 553 | member_summary: demo.title.to_owned(), |
| 554 | last_message_at: Some(demo.time), |
| 555 | unread: demo.unread, |
| 556 | members: demo.members, |
| 557 | last_message_preview: demo.preview.map(|(_, _, text)| text.to_owned()), |
| 558 | last_message_sender_id: demo.preview.map(|(id, _, _)| id.to_owned()), |
| 559 | last_message_sender_name: demo.preview.map(|(_, name, _)| name.to_owned()), |
| 560 | last_message_deleted: demo.deleted, |
| 561 | ..Default::default() |
| 562 | }) |
| 563 | .collect(); |
| 564 | let _ = store.upsert_chats(&chats); |
| 565 | let teams: Vec<TeamRecord> = DEMO_TEAMS |
| 566 | .iter() |
| 567 | .map(|(id, name, _)| TeamRecord { |
| 568 | id: (*id).to_owned(), |
| 569 | name: (*name).to_owned(), |
| 570 | }) |
| 571 | .collect(); |
| 572 | let _ = store.upsert_teams(&teams); |
| 573 | let mut channels = Vec::new(); |
| 574 | for (team_index, (team_id, _, names)) in DEMO_TEAMS.iter().enumerate() { |
| 575 | for (channel_index, name) in names.iter().enumerate() { |
| 576 | channels.push(ChannelRecord { |
| 577 | id: format!("demo-channel-{}-{}", team_index + 1, channel_index + 1), |
| 578 | team_id: (*team_id).to_owned(), |
| 579 | name: (*name).to_owned(), |
| 580 | membership_type: None, |
| 581 | last_message_at: Some(at(channel_index as i64, 12, 10)), |
| 582 | unread: (team_index == 0 && channel_index == 1) |
| 583 | || (team_index == 1 && channel_index == 0), |
| 584 | }); |
| 585 | } |
| 586 | } |
| 587 | let _ = store.upsert_channels(&channels); |
| 588 | let _ = store.replace_team_layout(&demo_team_layout(&channels)); |
| 589 | let _ = store.set_meta("me_user_id", DEMO_USER_ID); |
| 590 | let _ = store.set_meta("me_display_name", DEMO_USER_NAME); |
| 591 | let _ = store.upsert_messages(&release_messages()); |
| 592 | let _ = store.upsert_messages(&unread_messages()); |
| 593 | let _ = store.upsert_messages(&channel_messages()); |
| 594 | let _ = store.upsert_messages(&priority_messages()); |
| 595 | let _ = store.upsert_messages(&bot_messages()); |
| 596 | } |
| 597 | |
| 598 | fn photo(palette: &Palette) -> Arc<Image> { |
| 599 | crate::data::avatar_image_from("image/png", encode_png(palette)) |
| 600 | } |
| 601 | |
| 602 | pub fn seed_directory(state: &mut AppState) { |
| 603 | state.own_status = crate::own_status::demo_status(); |
| 604 | state.own_email = DEMO_USER_EMAIL.to_owned(); |
| 605 | let directory = &mut state.directory; |
| 606 | directory.me = Some(Person { |
| 607 | user_id: DEMO_USER_ID.to_owned(), |
| 608 | display_name: DEMO_USER_NAME.to_owned(), |
| 609 | }); |
| 610 | let ids = |list: &[&str]| list.iter().map(|id| (*id).to_owned()).collect::<Vec<_>>(); |
| 611 | directory.folders = vec![ |
| 612 | FolderInfo { |
| 613 | id: FAVORITES_ID.to_owned(), |
| 614 | name: String::new(), |
| 615 | kind: FolderKind::Favorites, |
| 616 | conversation_ids: ids(&["demo-chat-mara", RELEASE_CHAT]), |
| 617 | }, |
| 618 | FolderInfo { |
| 619 | id: CUSTOMERS_FOLDER.to_owned(), |
| 620 | name: "Customers".to_owned(), |
| 621 | kind: FolderKind::UserCreated, |
| 622 | conversation_ids: ids(&[ |
| 623 | "demo-chat-customer-1", |
| 624 | "demo-chat-customer-2", |
| 625 | "demo-chat-customer-3", |
| 626 | "demo-chat-customer-4", |
| 627 | ]), |
| 628 | }, |
| 629 | FolderInfo { |
| 630 | id: ATLAS_FOLDER.to_owned(), |
| 631 | name: "Project Atlas".to_owned(), |
| 632 | kind: FolderKind::UserCreated, |
| 633 | conversation_ids: ids(&["demo-chat-atlas", "demo-chat-jonas"]), |
| 634 | }, |
| 635 | FolderInfo { |
| 636 | id: TODO_FOLDER.to_owned(), |
| 637 | name: "To do".to_owned(), |
| 638 | kind: FolderKind::UserCreated, |
| 639 | conversation_ids: Vec::new(), |
| 640 | }, |
| 641 | ]; |
| 642 | state.bot_apps.insert(BOT_CHAT.to_owned(), vec![bot_app()]); |
| 643 | let directory = &mut state.directory; |
| 644 | directory.pinned_channels = ids(&["demo-channel-1-1", "demo-channel-3-2"]); |
| 645 | directory |
| 646 | .unread_counts |
| 647 | .insert("demo-chat-atlas".to_owned(), 5); |
| 648 | directory |
| 649 | .unread_counts |
| 650 | .insert("demo-chat-customer-1".to_owned(), 2); |
| 651 | directory |
| 652 | .unread_counts |
| 653 | .insert("demo-chat-customer-2".to_owned(), 1); |
| 654 | directory |
| 655 | .unread_counts |
| 656 | .insert("demo-chat-allhands".to_owned(), 120); |
| 657 | directory.set_avatar(MARA_ID, Some(photo(&MARA))); |
| 658 | directory.set_avatar(PRIYA_ID, Some(photo(&PRIYA))); |
| 659 | directory.set_avatar(TOBIAS_ID, Some(photo(&TOBIAS))); |
| 660 | for (user_id, kind) in [ |
| 661 | (MARA_ID, PresenceKind::Available), |
| 662 | (JONAS_ID, PresenceKind::DoNotDisturb), |
| 663 | (PRIYA_ID, PresenceKind::Away), |
| 664 | (TOBIAS_ID, PresenceKind::Offline), |
| 665 | (LEA_ID, PresenceKind::DoNotDisturb), |
| 666 | (DEMO_USER_ID, PresenceKind::Available), |
| 667 | ] { |
| 668 | directory.set_presence(user_id, kind); |
| 669 | } |
| 670 | let directory_path = image_directory(); |
| 671 | if std::fs::create_dir_all(&directory_path).is_ok() { |
| 672 | let path = directory_path.join("staging-dashboard.png"); |
| 673 | let png = encode_rgb(STAGING_SIZE.0, STAGING_SIZE.1, staging_pixel); |
| 674 | if std::fs::write(&path, png).is_ok() { |
| 675 | state.directory.set_image(STAGING_IMAGE_KEY, &path); |
| 676 | state.directory.set_image(LINK_IMAGE_KEY, &path); |
| 677 | } |
| 678 | let _ = SEARCHABLE_GIFS.set(demo_gifs::generate(&directory_path)); |
| 679 | state.directory.set_image( |
| 680 | demo_gifs::RECEIVED_GIF_KEY, |
| 681 | &directory_path.join(demo_gifs::file_name(demo_gifs::RECEIVED_GIF_NAME)), |
| 682 | ); |
| 683 | } |
| 684 | state.typing.start( |
| 685 | "demo-chat-mara", |
| 686 | MARA_ID, |
| 687 | "Mara Lindqvist", |
| 688 | Instant::now(), |
| 689 | Utc::now(), |
| 690 | ); |
| 691 | state.collapsed.insert(CUSTOMERS_FOLDER.to_owned()); |
| 692 | state.last_sync = Some(Utc::now()); |
| 693 | state.apply_bot_titles(); |
| 694 | seed_message_actions(state); |
| 695 | } |
| 696 | |
| 697 | fn seed_message_actions(state: &mut AppState) { |
| 698 | state.pins.insert( |
| 699 | RELEASE_CHAT.to_owned(), |
| 700 | vec![PinnedMessage { |
| 701 | message_id: PINNED_MESSAGE.to_owned(), |
| 702 | pinned_at: Some(at(0, 11, 30)), |
| 703 | parent_id: None, |
| 704 | }], |
| 705 | ); |
| 706 | let ids: Vec<String> = SAVED_MESSAGES.iter().map(|id| (*id).to_owned()).collect(); |
| 707 | let records = state |
| 708 | .store |
| 709 | .messages_by_id(RELEASE_CHAT, &ids) |
| 710 | .unwrap_or_default(); |
| 711 | let saved = SAVED_MESSAGES |
| 712 | .iter() |
| 713 | .enumerate() |
| 714 | .filter_map(|(position, id)| { |
| 715 | let record = records.get(*id)?; |
| 716 | Some(SavedMessage { |
| 717 | conversation_id: record.conversation_id.clone(), |
| 718 | message_id: record.message_id.clone(), |
| 719 | root_id: record.message_id.clone(), |
| 720 | author_id: record.sender_id.clone(), |
| 721 | author_name: record.sender_name.clone(), |
| 722 | preview: crate::rows::reply_excerpt(record), |
| 723 | saved_at: at(position as i64, 15, 20), |
| 724 | topic: None, |
| 725 | }) |
| 726 | }) |
| 727 | .collect(); |
| 728 | state.saved.replace(saved); |
| 729 | } |
| 730 | |
| 731 | pub fn forwarded_message( |
| 732 | chat: &ChatRecord, |
| 733 | source: &ForwardSource, |
| 734 | comment: &str, |
| 735 | now: DateTime<Utc>, |
| 736 | ) -> (ChatRecord, MessageRecord) { |
| 737 | let comment_html = if comment.is_empty() { |
| 738 | String::new() |
| 739 | } else { |
| 740 | format!("<p>{}</p>", teams_core::escape_html(comment)) |
| 741 | }; |
| 742 | let html = format!( |
| 743 | "{comment_html}<blockquote itemscope=\"\" itemtype=\"{FORWARD_ITEMTYPE}\" itemid=\"{}\"><strong>{}</strong><p>{}</p></blockquote>", |
| 744 | teams_core::escape_html(&source.message_id), |
| 745 | teams_core::escape_html(&source.author), |
| 746 | teams_core::escape_html(&source.text), |
| 747 | ); |
| 748 | send_in_chat(chat, &source.text, &html, now) |
| 749 | } |
| 750 | |
| 751 | #[allow(clippy::too_many_arguments)] |
| 752 | fn message( |
| 753 | conversation: &str, |
| 754 | id: &str, |
| 755 | reply_to: Option<&str>, |
| 756 | sender: (&str, &str), |
| 757 | time: DateTime<Utc>, |
| 758 | html: &str, |
| 759 | reactions_json: &str, |
| 760 | edited: bool, |
| 761 | ) -> MessageRecord { |
| 762 | MessageRecord { |
| 763 | conversation_id: conversation.to_owned(), |
| 764 | message_id: id.to_owned(), |
| 765 | reply_to_id: reply_to.map(str::to_owned), |
| 766 | sender_id: Some(sender.0.to_owned()), |
| 767 | sender_name: Some(sender.1.to_owned()), |
| 768 | sender_application_id: None, |
| 769 | links_json: "[]".to_owned(), |
| 770 | created_at: time, |
| 771 | edited_at: edited.then_some(time + Duration::minutes(2)), |
| 772 | deleted: false, |
| 773 | body_html: html.to_owned(), |
| 774 | attachments_json: "[]".to_owned(), |
| 775 | reactions_json: reactions_json.to_owned(), |
| 776 | mentions_json: "[]".to_owned(), |
| 777 | subject: None, |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | fn with_subject(mut record: MessageRecord, subject: &str) -> MessageRecord { |
| 782 | record.subject = Some(subject.to_owned()); |
| 783 | record |
| 784 | } |
| 785 | |
| 786 | fn with_attachments(mut record: MessageRecord, attachments_json: &str) -> MessageRecord { |
| 787 | record.attachments_json = attachments_json.to_owned(); |
| 788 | record |
| 789 | } |
| 790 | |
| 791 | fn with_links(mut record: MessageRecord, links_json: String) -> MessageRecord { |
| 792 | record.links_json = links_json; |
| 793 | record |
| 794 | } |
| 795 | |
| 796 | pub fn link_preview(url: &str) -> Option<LinkPreview> { |
| 797 | Some(LinkPreview { |
| 798 | url: url.to_owned(), |
| 799 | title: Some("Release checklist Q4".to_owned()), |
| 800 | description: Some( |
| 801 | "Everything that has to be green before the release candidate goes out on Friday." |
| 802 | .to_owned(), |
| 803 | ), |
| 804 | image_url: Some(LINK_IMAGE_KEY.to_owned()), |
| 805 | image_width: Some(LINK_IMAGE_SIZE.0), |
| 806 | image_height: Some(LINK_IMAGE_SIZE.1), |
| 807 | }) |
| 808 | } |
| 809 | |
| 810 | fn bot_messages() -> Vec<MessageRecord> { |
| 811 | let card = serde_json::json!({ |
| 812 | "type": "AdaptiveCard", |
| 813 | "body": [ |
| 814 | {"type": "ColumnSet", "columns": [ |
| 815 | {"type": "Column", "width": "auto", "verticalContentAlignment": "center", "items": [ |
| 816 | {"type": "Image", "url": CARD_PERSON_URL, "size": "small", "style": "person", "height": "32px"} |
| 817 | ]}, |
| 818 | {"type": "Column", "width": "stretch", "verticalContentAlignment": "center", "items": [ |
| 819 | {"type": "TextBlock", "size": "medium", "weight": "bolder", "wrap": true, "text": "Mara Lindqvist edited your page"} |
| 820 | ]} |
| 821 | ]}, |
| 822 | {"type": "ColumnSet", "separator": true, "columns": [ |
| 823 | {"type": "Column", "width": "auto", "items": [ |
| 824 | {"type": "Image", "url": CARD_PAGE_URL, "width": "24px", "height": "24px"} |
| 825 | ]}, |
| 826 | {"type": "Column", "width": "stretch", "items": [ |
| 827 | {"type": "TextBlock", "weight": "bolder", "wrap": true, "text": "[**Release Checklist Q4**](https://example.com/wiki/release-checklist) in [**Platform**](https://example.com/wiki/platform)"}, |
| 828 | {"type": "TextBlock", "spacing": "small", "isSubtle": true, "wrap": true, "text": "Owned by: Dana Demo"} |
| 829 | ]} |
| 830 | ]}, |
| 831 | {"type": "TextBlock", "id": "owner-note", "isVisible": false, "isSubtle": true, "wrap": true, "text": "Owner notes are only shown on request."}, |
| 832 | {"type": "ActionSet", "actions": [ |
| 833 | {"type": "Action.OpenUrl", "title": "View page", "url": "https://example.com/wiki/release-checklist"}, |
| 834 | {"type": "Action.OpenUrl", "title": "View changes", "url": "https://example.com/wiki/release-checklist/changes"}, |
| 835 | {"type": "Action.Submit", "title": "Watch page", "data": {"action": "watch"}}, |
| 836 | {"type": "Action.Submit", "title": "Stop watching", "data": {"action": "unwatch"}}, |
| 837 | {"type": "Action.Submit", "title": "Settings", "data": {"msteams": {"type": "task/fetch"}}}, |
| 838 | {"type": "Action.ToggleVisibility", "title": "Owner notes", "targetElements": ["owner-note"]}, |
| 839 | {"type": "Action.ShowCard", "title": "Details", "card": {"body": [ |
| 840 | {"type": "TextBlock", "wrap": true, "text": "Last edited by **Mara Lindqvist** on Tuesday."} |
| 841 | ]}} |
| 842 | ]} |
| 843 | ] |
| 844 | }); |
| 845 | let mut messages = vec![ |
| 846 | bot_card_message("b1", at(0, 13, 45), &card), |
| 847 | bot_card_message("b2", at(0, 13, 52), &input_card()), |
| 848 | bot_card_message("b3", at(0, 13, 58), &crate::demo_input_cards::all_inputs_card()), |
| 849 | ]; |
| 850 | messages.extend( |
| 851 | crate::demo_cards::behaviour_cards() |
| 852 | .iter() |
| 853 | .zip(0u32..) |
| 854 | .map(|(card, index)| bot_card_message(&format!("g{index}"), at(0, 14, index), card)), |
| 855 | ); |
| 856 | messages.extend( |
| 857 | crate::demo_chart_cards::cards() |
| 858 | .iter() |
| 859 | .zip(0..) |
| 860 | .map(|(chart_card, minute)| { |
| 861 | bot_card_message(&format!("c{minute}"), at(0, 15, minute), chart_card) |
| 862 | }), |
| 863 | ); |
| 864 | messages |
| 865 | } |
| 866 | |
| 867 | fn bot_card_message(id: &str, time: DateTime<Utc>, card: &serde_json::Value) -> MessageRecord { |
| 868 | let attachments = serde_json::json!([{ |
| 869 | "content_type": "application/vnd.microsoft.card.adaptive", |
| 870 | "name": null, |
| 871 | "url": null, |
| 872 | "text": null, |
| 873 | "content": card.to_string() |
| 874 | }]); |
| 875 | let mut record = with_attachments( |
| 876 | message( |
| 877 | BOT_CHAT, |
| 878 | id, |
| 879 | None, |
| 880 | ("", BOT_NAME), |
| 881 | time, |
| 882 | &format!("<attachment id=\"{id}\"></attachment>"), |
| 883 | "[]", |
| 884 | false, |
| 885 | ), |
| 886 | &attachments.to_string(), |
| 887 | ); |
| 888 | record.sender_id = None; |
| 889 | record.sender_application_id = Some(BOT_ID.to_owned()); |
| 890 | record |
| 891 | } |
| 892 | |
| 893 | fn input_card() -> serde_json::Value { |
| 894 | serde_json::json!({ |
| 895 | "type": "AdaptiveCard", |
| 896 | "body": [ |
| 897 | {"type": "TextBlock", "size": "medium", "weight": "bolder", "text": "Release request"}, |
| 898 | {"type": "Input.Text", "id": "title", "label": "Title", "placeholder": "Short summary", "isRequired": true, "errorMessage": "Give the release a title"}, |
| 899 | {"type": "Input.Text", "id": "notes", "label": "Notes", "placeholder": "Anything the reviewers should know", "isMultiline": true}, |
| 900 | {"type": "Input.Text", "id": "token", "label": "Access code", "style": "Password", "regex": "^\\d{4}$", "errorMessage": "Enter four digits"}, |
| 901 | {"type": "ColumnSet", "columns": [ |
| 902 | {"type": "Column", "width": "stretch", "items": [ |
| 903 | {"type": "Input.Number", "id": "count", "label": "Servers", "placeholder": "1-20", "min": 1, "max": 20, "value": 4} |
| 904 | ]}, |
| 905 | {"type": "Column", "width": "stretch", "items": [ |
| 906 | {"type": "Input.Date", "id": "day", "label": "Release day", "value": "2026-11-02"} |
| 907 | ]}, |
| 908 | {"type": "Column", "width": "stretch", "items": [ |
| 909 | {"type": "Input.Time", "id": "hour", "label": "Start time", "value": "09:30"} |
| 910 | ]} |
| 911 | ]}, |
| 912 | {"type": "Input.ChoiceSet", "id": "channel", "label": "Channel", "placeholder": "Pick a channel", "isRequired": true, "choices": [ |
| 913 | {"title": "Stable", "value": "stable"}, {"title": "Beta", "value": "beta"}, {"title": "Nightly", "value": "nightly"} |
| 914 | ]}, |
| 915 | {"type": "Input.ChoiceSet", "id": "region", "label": "Region", "style": "expanded", "value": "eu", "choices": [ |
| 916 | {"title": "Europe", "value": "eu"}, {"title": "North America", "value": "na"} |
| 917 | ]}, |
| 918 | {"type": "Input.ChoiceSet", "id": "checks", "label": "Checks", "style": "expanded", "isMultiSelect": true, "value": "lint", "choices": [ |
| 919 | {"title": "Lint", "value": "lint"}, {"title": "Unit tests", "value": "unit"}, {"title": "Smoke tests", "value": "smoke"} |
| 920 | ]}, |
| 921 | {"type": "Input.Toggle", "id": "notify", "title": "Notify the team", "value": "yes", "valueOn": "yes", "valueOff": "no"} |
| 922 | ], |
| 923 | "actions": [ |
| 924 | {"type": "Action.Submit", "title": "Request release", "data": {"action": "release"}}, |
| 925 | {"type": "Action.Submit", "title": "Cancel", "associatedInputs": "none", "data": {"action": "cancel"}} |
| 926 | ] |
| 927 | }) |
| 928 | } |
| 929 | |
| 930 | fn bot_app() -> ChatApp { |
| 931 | ChatApp { |
| 932 | app_id: "demo-app-wiki".to_owned(), |
| 933 | name: BOT_APP_NAME.to_owned(), |
| 934 | bot_ids: vec![BOT_ID.to_owned()], |
| 935 | small_image_url: None, |
| 936 | accent_color: None, |
| 937 | web_application_resource: None, |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | pub async fn card_answer(task: CardTask) -> CardAnswer { |
| 942 | tokio::time::sleep(CARD_ANSWER_DELAY).await; |
| 943 | let data = match task { |
| 944 | CardTask::Submit(_) => { |
| 945 | return Ok(( |
| 946 | CardActionOutcome::Message("Settings saved".into()), |
| 947 | Some((bot_app(), BOT_ID.to_owned())), |
| 948 | )); |
| 949 | } |
| 950 | CardTask::Action(action) => match action.invoke_payload() { |
| 951 | Some(payload) => payload.value, |
| 952 | None => return Err("Not supported".to_owned()), |
| 953 | }, |
| 954 | }; |
| 955 | let outcome = if data.pointer("/data/type").and_then(|kind| kind.as_str()) == Some("task/fetch") |
| 956 | { |
| 957 | CardActionOutcome::Dialog(TaskDialog { |
| 958 | title: None, |
| 959 | width: 520, |
| 960 | height: 300, |
| 961 | kind: TaskDialogKind::Card(settings_card().to_string()), |
| 962 | }) |
| 963 | } else if data.get("action").and_then(|action| action.as_str()) == Some("unwatch") { |
| 964 | CardActionOutcome::Failed("The app answered 500: demo failure".into()) |
| 965 | } else { |
| 966 | CardActionOutcome::Sent |
| 967 | }; |
| 968 | Ok((outcome, Some((bot_app(), BOT_ID.to_owned())))) |
| 969 | } |
| 970 | |
| 971 | pub async fn refresh_answer() -> CardAnswer { |
| 972 | tokio::time::sleep(CARD_ANSWER_DELAY).await; |
| 973 | Ok(( |
| 974 | CardActionOutcome::ReplaceCard(crate::demo_cards::refreshed_card().to_string()), |
| 975 | Some((bot_app(), BOT_ID.to_owned())), |
| 976 | )) |
| 977 | } |
| 978 | |
| 979 | fn settings_card() -> serde_json::Value { |
| 980 | serde_json::json!({ |
| 981 | "type": "AdaptiveCard", |
| 982 | "body": [ |
| 983 | {"type": "TextBlock", "weight": "bolder", "text": "Notification settings"}, |
| 984 | {"type": "TextBlock", "wrap": true, "isSubtle": true, "text": "Choose how the bot tells you about page changes."}, |
| 985 | {"type": "Input.ChoiceSet", "id": "frequency", "label": "Frequency", "style": "expanded", "value": "daily", "choices": [ |
| 986 | {"title": "Immediately", "value": "now"}, {"title": "Daily digest", "value": "daily"} |
| 987 | ]}, |
| 988 | {"type": "Input.Toggle", "id": "mentions", "title": "Only when I am mentioned", "value": "false"} |
| 989 | ], |
| 990 | "actions": [ |
| 991 | {"type": "Action.Submit", "title": "Save", "data": {"save": true}} |
| 992 | ] |
| 993 | }) |
| 994 | } |
| 995 | |
| 996 | fn priority_messages() -> Vec<MessageRecord> { |
| 997 | let priya = (PRIYA_ID, "Priya Nair"); |
| 998 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 999 | vec![ |
| 1000 | message( |
| 1001 | PRIORITY_CHAT, |
| 1002 | "p1", |
| 1003 | None, |
| 1004 | me, |
| 1005 | at(0, 10, 40), |
| 1006 | "<p>Have you seen the error on staging yet?</p>", |
| 1007 | "[]", |
| 1008 | false, |
| 1009 | ), |
| 1010 | message( |
| 1011 | PRIORITY_CHAT, |
| 1012 | "p2", |
| 1013 | None, |
| 1014 | priya, |
| 1015 | at(0, 10, 47), |
| 1016 | &format!( |
| 1017 | "<p>Yes, here is the screenshot:</p><img src=\"{PENDING_IMAGE_KEY}\" width=\"1280\" height=\"720\">" |
| 1018 | ), |
| 1019 | "[]", |
| 1020 | false, |
| 1021 | ), |
| 1022 | ] |
| 1023 | } |
| 1024 | |
| 1025 | fn reaction(kind: &str, users: &[(&str, &str, DateTime<Utc>)]) -> Vec<serde_json::Value> { |
| 1026 | users |
| 1027 | .iter() |
| 1028 | .map(|(user_id, user_name, created_at)| { |
| 1029 | serde_json::json!({ |
| 1030 | "reaction_type": kind, |
| 1031 | "user_id": user_id, |
| 1032 | "user_name": user_name, |
| 1033 | "created_at": created_at, |
| 1034 | }) |
| 1035 | }) |
| 1036 | .collect() |
| 1037 | } |
| 1038 | |
| 1039 | fn reactions_json(groups: Vec<Vec<serde_json::Value>>) -> String { |
| 1040 | serde_json::Value::Array(groups.into_iter().flatten().collect()).to_string() |
| 1041 | } |
| 1042 | |
| 1043 | fn release_messages() -> Vec<MessageRecord> { |
| 1044 | let chat = RELEASE_CHAT; |
| 1045 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 1046 | let jonas = (JONAS_ID, "Jonas Ortega"); |
| 1047 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 1048 | let jonas_name = "Jonas Ortega"; |
| 1049 | let priya_name = "Priya Nair"; |
| 1050 | let lea_name = "Lea Schneider"; |
| 1051 | let mara_name = "Mara Lindqvist"; |
| 1052 | let tobias_name = "Tobias Klein"; |
| 1053 | let likes = reactions_json(vec![reaction( |
| 1054 | "like", |
| 1055 | &[ |
| 1056 | (JONAS_ID, jonas_name, at(1, 16, 30)), |
| 1057 | (PRIYA_ID, priya_name, at(1, 17, 2)), |
| 1058 | (LEA_ID, lea_name, at(0, 8, 15)), |
| 1059 | ], |
| 1060 | )]); |
| 1061 | let hearts = reactions_json(vec![reaction( |
| 1062 | "heart", |
| 1063 | &[ |
| 1064 | (DEMO_USER_ID, DEMO_USER_NAME, at(0, 10, 35)), |
| 1065 | (JONAS_ID, jonas_name, at(0, 10, 33)), |
| 1066 | ], |
| 1067 | )]); |
| 1068 | let single = reactions_json(vec![reaction( |
| 1069 | "laugh", |
| 1070 | &[(PRIYA_ID, priya_name, at(0, 9, 10))], |
| 1071 | )]); |
| 1072 | let many = reactions_json(vec![ |
| 1073 | reaction( |
| 1074 | "like", |
| 1075 | &[ |
| 1076 | (MARA_ID, mara_name, at(0, 13, 37)), |
| 1077 | (JONAS_ID, jonas_name, at(0, 13, 38)), |
| 1078 | (PRIYA_ID, priya_name, at(0, 13, 40)), |
| 1079 | (LEA_ID, lea_name, at(0, 13, 45)), |
| 1080 | (TOBIAS_ID, tobias_name, at(0, 13, 50)), |
| 1081 | ], |
| 1082 | ), |
| 1083 | reaction( |
| 1084 | "heart", |
| 1085 | &[ |
| 1086 | (LEA_ID, lea_name, at(0, 13, 46)), |
| 1087 | (TOBIAS_ID, tobias_name, at(0, 13, 51)), |
| 1088 | ], |
| 1089 | ), |
| 1090 | reaction("\u{1F389}", &[(MARA_ID, mara_name, at(0, 13, 39))]), |
| 1091 | ]); |
| 1092 | vec![ |
| 1093 | message( |
| 1094 | chat, |
| 1095 | "m1", |
| 1096 | None, |
| 1097 | mara, |
| 1098 | at(1, 16, 20), |
| 1099 | "<p>Release candidate is on staging. Can someone run the regression suite?</p>", |
| 1100 | "[]", |
| 1101 | false, |
| 1102 | ), |
| 1103 | message( |
| 1104 | chat, |
| 1105 | "m2", |
| 1106 | None, |
| 1107 | mara, |
| 1108 | at(1, 16, 22), |
| 1109 | "<p>Checklist lives in <a href=\"https://example.com/checklist\">release checklist</a>.</p>", |
| 1110 | &likes, |
| 1111 | false, |
| 1112 | ), |
| 1113 | with_links( |
| 1114 | message( |
| 1115 | chat, |
| 1116 | "m2a", |
| 1117 | None, |
| 1118 | mara, |
| 1119 | at(1, 16, 23), |
| 1120 | "<p><a href=\"https://example.com/checklist\">https://example.com/checklist</a></p>", |
| 1121 | "[]", |
| 1122 | false, |
| 1123 | ), |
| 1124 | link_preview("https://example.com/checklist") |
| 1125 | .map(|preview| preview.links_json()) |
| 1126 | .unwrap_or_default(), |
| 1127 | ), |
| 1128 | message( |
| 1129 | chat, |
| 1130 | "m2b", |
| 1131 | None, |
| 1132 | mara, |
| 1133 | at(1, 16, 40), |
| 1134 | &format!( |
| 1135 | "<p>Staging dashboard right now:</p><img src=\"{STAGING_IMAGE_KEY}\" width=\"{}\" height=\"{}\">", |
| 1136 | STAGING_SIZE.0, STAGING_SIZE.1 |
| 1137 | ), |
| 1138 | "[]", |
| 1139 | false, |
| 1140 | ), |
| 1141 | with_attachments( |
| 1142 | message( |
| 1143 | chat, |
| 1144 | "m2c", |
| 1145 | None, |
| 1146 | mara, |
| 1147 | at(1, 16, 41), |
| 1148 | "<p>Checklist and rollout plan attached.</p>", |
| 1149 | "[]", |
| 1150 | false, |
| 1151 | ), |
| 1152 | 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}]"#, |
| 1153 | ), |
| 1154 | message( |
| 1155 | chat, |
| 1156 | "m3", |
| 1157 | None, |
| 1158 | me, |
| 1159 | at(0, 9, 2), |
| 1160 | "<p>I'll take the changelog.</p>", |
| 1161 | &single, |
| 1162 | false, |
| 1163 | ), |
| 1164 | message( |
| 1165 | chat, |
| 1166 | "m4", |
| 1167 | None, |
| 1168 | jonas, |
| 1169 | at(0, 10, 14), |
| 1170 | "<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>", |
| 1171 | "[]", |
| 1172 | false, |
| 1173 | ), |
| 1174 | message( |
| 1175 | chat, |
| 1176 | "m5", |
| 1177 | None, |
| 1178 | mara, |
| 1179 | at(0, 10, 31), |
| 1180 | "<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>", |
| 1181 | &hearts, |
| 1182 | true, |
| 1183 | ), |
| 1184 | message( |
| 1185 | chat, |
| 1186 | "m5b", |
| 1187 | None, |
| 1188 | mara, |
| 1189 | at(0, 10, 40), |
| 1190 | "<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>", |
| 1191 | "[]", |
| 1192 | false, |
| 1193 | ), |
| 1194 | message( |
| 1195 | chat, |
| 1196 | "m5c", |
| 1197 | None, |
| 1198 | jonas, |
| 1199 | at(0, 11, 5), |
| 1200 | "<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>", |
| 1201 | "[]", |
| 1202 | false, |
| 1203 | ), |
| 1204 | message( |
| 1205 | chat, |
| 1206 | "m5d", |
| 1207 | None, |
| 1208 | (PRIYA_ID, priya_name), |
| 1209 | at(0, 12, 20), |
| 1210 | &format!( |
| 1211 | "<p>FYI, the customer asked for this.</p><blockquote itemscope=\"\" itemtype=\"{FORWARD_ITEMTYPE}\" itemid=\"1790000000000\"><strong>Tobias Klein</strong><p>Can we move the freeze to Thursday? The customer demo is on Friday morning.</p></blockquote>" |
| 1212 | ), |
| 1213 | "[]", |
| 1214 | false, |
| 1215 | ), |
| 1216 | message( |
| 1217 | chat, |
| 1218 | "m6", |
| 1219 | None, |
| 1220 | me, |
| 1221 | at(0, 13, 35), |
| 1222 | "<p>Thanks, renamed. Tagging now.</p>", |
| 1223 | "[]", |
| 1224 | false, |
| 1225 | ), |
| 1226 | message( |
| 1227 | chat, |
| 1228 | "m7", |
| 1229 | None, |
| 1230 | me, |
| 1231 | at(0, 13, 36), |
| 1232 | "<p>Build 42 is green, see <a href=\"https://example.com/build/42\">pipeline</a>.</p>", |
| 1233 | &many, |
| 1234 | false, |
| 1235 | ), |
| 1236 | with_links( |
| 1237 | message( |
| 1238 | chat, |
| 1239 | "m7a", |
| 1240 | None, |
| 1241 | me, |
| 1242 | at(0, 13, 37), |
| 1243 | "<p>Notes are in <a href=\"https://example.com/notes\">https://example.com/notes</a></p>", |
| 1244 | "[]", |
| 1245 | false, |
| 1246 | ), |
| 1247 | LinkPreview { |
| 1248 | url: "https://example.com/notes".to_owned(), |
| 1249 | title: Some("Release notes 0.2".to_owned()), |
| 1250 | description: None, |
| 1251 | image_url: None, |
| 1252 | image_width: None, |
| 1253 | image_height: None, |
| 1254 | } |
| 1255 | .links_json(), |
| 1256 | ), |
| 1257 | message( |
| 1258 | chat, |
| 1259 | "m8", |
| 1260 | None, |
| 1261 | jonas, |
| 1262 | at(0, 13, 40), |
| 1263 | "<p>Formula check: H<sub>2</sub>O and x<sup>2</sup> + y<sup>2</sup>. <span style=\"font-size:xx-small;\">Small print, reviewed by <at id=\"0\">Mara Lindqvist</at>.</span> <span style=\"font-size:x-large;\">Big news:</span> the <span style=\"font-size:x-large;\"><a href=\"https://example.com/notes\">release notes</a></span> are out.</p>", |
| 1264 | "[]", |
| 1265 | false, |
| 1266 | ), |
| 1267 | message( |
| 1268 | chat, |
| 1269 | "m9", |
| 1270 | None, |
| 1271 | (PRIYA_ID, priya_name), |
| 1272 | at(0, 13, 45), |
| 1273 | &format!( |
| 1274 | "<p>Ship it!</p><img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"Party\" itemtype=\"http://schema.skype.com/Giphy\">", |
| 1275 | demo_gifs::RECEIVED_GIF_KEY, |
| 1276 | demo_gifs::RECEIVED_GIF_SIZE.0, |
| 1277 | demo_gifs::RECEIVED_GIF_SIZE.1 |
| 1278 | ), |
| 1279 | "[]", |
| 1280 | false, |
| 1281 | ), |
| 1282 | ] |
| 1283 | } |
| 1284 | |
| 1285 | fn unread_messages() -> Vec<MessageRecord> { |
| 1286 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 1287 | let lea = (LEA_ID, "Lea Schneider"); |
| 1288 | let tobias = (TOBIAS_ID, "Tobias Klein"); |
| 1289 | let rows = [ |
| 1290 | ( |
| 1291 | "u1", |
| 1292 | me, |
| 1293 | at(1, 17, 5), |
| 1294 | "<p>Can you enable access for North?</p>", |
| 1295 | ), |
| 1296 | ("u2", tobias, at(1, 17, 20), "<p>I'll check with IT.</p>"), |
| 1297 | ( |
| 1298 | "u3", |
| 1299 | lea, |
| 1300 | at(0, 12, 40), |
| 1301 | "<p>Good morning, access is ready.</p>", |
| 1302 | ), |
| 1303 | ( |
| 1304 | "u4", |
| 1305 | lea, |
| 1306 | at(0, 12, 41), |
| 1307 | "<p>Sign in with your company ID, no password needed.</p>", |
| 1308 | ), |
| 1309 | ("u5", lea, at(0, 12, 50), "<p>Access is enabled now</p>"), |
| 1310 | ]; |
| 1311 | rows.into_iter() |
| 1312 | .map(|(id, sender, time, html)| { |
| 1313 | message(UNREAD_CHAT, id, None, sender, time, html, "[]", false) |
| 1314 | }) |
| 1315 | .collect() |
| 1316 | } |
| 1317 | |
| 1318 | fn demo_team_layout(channels: &[ChannelRecord]) -> Vec<TeamLayoutRecord> { |
| 1319 | DEMO_TEAM_ORDER |
| 1320 | .iter() |
| 1321 | .map(|team_id| TeamLayoutRecord { |
| 1322 | team_id: (*team_id).to_owned(), |
| 1323 | hidden: *team_id == DEMO_HIDDEN_TEAM, |
| 1324 | channels: channels |
| 1325 | .iter() |
| 1326 | .filter(|channel| channel.team_id == *team_id) |
| 1327 | .map(|channel| ChannelLayoutRecord { |
| 1328 | channel_id: channel.id.clone(), |
| 1329 | general: channel.name == "General", |
| 1330 | hidden: channel.id == DEMO_HIDDEN_CHANNEL, |
| 1331 | }) |
| 1332 | .collect(), |
| 1333 | }) |
| 1334 | .collect() |
| 1335 | } |
| 1336 | |
| 1337 | const ROLLOUT_REPLIES: [(&str, &str); 12] = [ |
| 1338 | (MARA_ID, "Staging looks good on my side."), |
| 1339 | (JONAS_ID, "Smoke tests passed, no regressions."), |
| 1340 | (PRIYA_ID, "Docs for the new settings page are merged."), |
| 1341 | (LEA_ID, "Support is briefed, nothing blocking."), |
| 1342 | (MARA_ID, "Can we keep the old export for one more release?"), |
| 1343 | (JONAS_ID, "Yes, behind a flag. I will add it today."), |
| 1344 | (PRIYA_ID, "Changelog draft is ready for review."), |
| 1345 | (LEA_ID, "Customer success wants a heads-up the day before."), |
| 1346 | (MARA_ID, "I will send that mail on Thursday."), |
| 1347 | (JONAS_ID, "Flag is in, rebuilding staging now."), |
| 1348 | (PRIYA_ID, "Rollback steps are in the runbook."), |
| 1349 | (LEA_ID, "All green from QA. Go for Friday."), |
| 1350 | ]; |
| 1351 | |
| 1352 | fn channel_messages() -> Vec<MessageRecord> { |
| 1353 | let channel = "demo-channel-1-1"; |
| 1354 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 1355 | let tobias = (TOBIAS_ID, "Tobias Klein"); |
| 1356 | let people = [ |
| 1357 | (MARA_ID, "Mara Lindqvist"), |
| 1358 | (JONAS_ID, "Jonas Ortega"), |
| 1359 | (PRIYA_ID, "Priya Nair"), |
| 1360 | (LEA_ID, "Lea Schneider"), |
| 1361 | ]; |
| 1362 | let rollout_replies = ROLLOUT_REPLIES |
| 1363 | .iter() |
| 1364 | .enumerate() |
| 1365 | .map(|(index, (sender, text))| { |
| 1366 | let sender = people |
| 1367 | .iter() |
| 1368 | .copied() |
| 1369 | .find(|(id, _)| id == sender) |
| 1370 | .expect("known person"); |
| 1371 | message( |
| 1372 | channel, |
| 1373 | &format!("t3r{}", index + 1), |
| 1374 | Some("t3"), |
| 1375 | sender, |
| 1376 | at(0, 9, 40) + Duration::minutes(6 * index as i64), |
| 1377 | &format!("<p>{text}</p>"), |
| 1378 | "[]", |
| 1379 | false, |
| 1380 | ) |
| 1381 | }); |
| 1382 | let mut records = vec![ |
| 1383 | message( |
| 1384 | channel, |
| 1385 | "t1", |
| 1386 | None, |
| 1387 | mara, |
| 1388 | at(0, 8, 0), |
| 1389 | "<p>Weekly sync notes are up.</p>", |
| 1390 | "[]", |
| 1391 | false, |
| 1392 | ), |
| 1393 | message( |
| 1394 | channel, |
| 1395 | "t1r1", |
| 1396 | Some("t1"), |
| 1397 | tobias, |
| 1398 | at(0, 8, 20), |
| 1399 | "<p>Thanks, adding my items.</p>", |
| 1400 | "[]", |
| 1401 | false, |
| 1402 | ), |
| 1403 | with_subject( |
| 1404 | message( |
| 1405 | channel, |
| 1406 | "t3", |
| 1407 | None, |
| 1408 | tobias, |
| 1409 | at(0, 9, 30), |
| 1410 | "<p>Release build 42 is green, deploying to staging now.</p>", |
| 1411 | "[]", |
| 1412 | false, |
| 1413 | ), |
| 1414 | "Release 42 rollout", |
| 1415 | ), |
| 1416 | message( |
| 1417 | channel, |
| 1418 | "t2", |
| 1419 | None, |
| 1420 | tobias, |
| 1421 | at(0, 10, 0), |
| 1422 | "<p>Who owns the on-call handover?</p>", |
| 1423 | "[]", |
| 1424 | false, |
| 1425 | ), |
| 1426 | message( |
| 1427 | channel, |
| 1428 | "t4", |
| 1429 | None, |
| 1430 | mara, |
| 1431 | at(3, 8, 42), |
| 1432 | "<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>", |
| 1433 | "[]", |
| 1434 | false, |
| 1435 | ), |
| 1436 | message( |
| 1437 | channel, |
| 1438 | "t4r1", |
| 1439 | Some("t4"), |
| 1440 | tobias, |
| 1441 | at(0, 9, 15), |
| 1442 | "<p>Yes, all done.</p>", |
| 1443 | "[]", |
| 1444 | false, |
| 1445 | ), |
| 1446 | ]; |
| 1447 | records.extend(rollout_replies); |
| 1448 | records |
| 1449 | } |
| 1450 | |
| 1451 | fn crc32(bytes: &[u8]) -> u32 { |
| 1452 | let mut crc = 0xffff_ffffu32; |
| 1453 | for byte in bytes { |
| 1454 | crc ^= u32::from(*byte); |
| 1455 | for _ in 0..8 { |
| 1456 | crc = if crc & 1 == 1 { |
| 1457 | (crc >> 1) ^ 0xedb8_8320 |
| 1458 | } else { |
| 1459 | crc >> 1 |
| 1460 | }; |
| 1461 | } |
| 1462 | } |
| 1463 | !crc |
| 1464 | } |
| 1465 | |
| 1466 | fn adler32(bytes: &[u8]) -> u32 { |
| 1467 | let (mut low, mut high) = (1u32, 0u32); |
| 1468 | for byte in bytes { |
| 1469 | low = (low + u32::from(*byte)) % 65521; |
| 1470 | high = (high + low) % 65521; |
| 1471 | } |
| 1472 | (high << 16) | low |
| 1473 | } |
| 1474 | |
| 1475 | fn png_chunk(output: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) { |
| 1476 | output.extend_from_slice(&(data.len() as u32).to_be_bytes()); |
| 1477 | let mut body = kind.to_vec(); |
| 1478 | body.extend_from_slice(data); |
| 1479 | output.extend_from_slice(&body); |
| 1480 | output.extend_from_slice(&crc32(&body).to_be_bytes()); |
| 1481 | } |
| 1482 | |
| 1483 | fn inside(x: f32, y: f32, center: (f32, f32), radius: (f32, f32)) -> bool { |
| 1484 | ((x - center.0) / radius.0).powi(2) + ((y - center.1) / radius.1).powi(2) <= 1. |
| 1485 | } |
| 1486 | |
| 1487 | fn staging_pixel(column: usize, row: usize) -> [u8; 3] { |
| 1488 | let (width, height) = (STAGING_SIZE.0 as f32, STAGING_SIZE.1 as f32); |
| 1489 | let (x, y) = (column as f32, row as f32); |
| 1490 | let shade = y / height; |
| 1491 | let background = [ |
| 1492 | (30. - 14. * shade) as u8, |
| 1493 | (41. - 16. * shade) as u8, |
| 1494 | (59. - 14. * shade) as u8, |
| 1495 | ]; |
| 1496 | let bar_width = width / 14.; |
| 1497 | let slot = (x / bar_width) as usize; |
| 1498 | let in_bar = x - slot as f32 * bar_width > bar_width * 0.18; |
| 1499 | let bar_height = height * (0.25 + 0.5 * (((slot * 7 + 3) % 11) as f32 / 11.)); |
| 1500 | let on_bars = in_bar && y > height * 0.86 - bar_height && y < height * 0.86 && slot < 14; |
| 1501 | let header = y < height * 0.1; |
| 1502 | if header { |
| 1503 | [22, 30, 45] |
| 1504 | } else if on_bars { |
| 1505 | if slot % 4 == 3 { |
| 1506 | [226, 87, 47] |
| 1507 | } else { |
| 1508 | [88, 133, 196] |
| 1509 | } |
| 1510 | } else if (y - height * 0.86).abs() < 1.5 { |
| 1511 | [71, 85, 105] |
| 1512 | } else { |
| 1513 | background |
| 1514 | } |
| 1515 | } |
| 1516 | |
| 1517 | fn encode_png(palette: &Palette) -> Vec<u8> { |
| 1518 | encode_rgb(PHOTO_SIZE, PHOTO_SIZE, |column, row| { |
| 1519 | let (x, y) = (column as f32 + 0.5, row as f32 + 0.5); |
| 1520 | if inside(x, y, (24., 21.), (8.5, 9.5)) { |
| 1521 | palette.skin |
| 1522 | } else if inside(x, y, (24., 19.), (10.5, 11.)) && y < 22. { |
| 1523 | palette.hair |
| 1524 | } else if inside(x, y, (24., 50.), (22., 19.)) { |
| 1525 | palette.shirt |
| 1526 | } else { |
| 1527 | palette.background |
| 1528 | } |
| 1529 | }) |
| 1530 | } |
| 1531 | |
| 1532 | fn encode_rgb(width: usize, height: usize, pixel: impl Fn(usize, usize) -> [u8; 3]) -> Vec<u8> { |
| 1533 | let mut raw = Vec::with_capacity(height * (width * 3 + 1)); |
| 1534 | for row in 0..height { |
| 1535 | raw.push(0); |
| 1536 | for column in 0..width { |
| 1537 | raw.extend_from_slice(&pixel(column, row)); |
| 1538 | } |
| 1539 | } |
| 1540 | let mut zlib = vec![0x78, 0x01]; |
| 1541 | let mut blocks = raw.chunks(65535).peekable(); |
| 1542 | while let Some(block) = blocks.next() { |
| 1543 | zlib.push(u8::from(blocks.peek().is_none())); |
| 1544 | zlib.extend_from_slice(&(block.len() as u16).to_le_bytes()); |
| 1545 | zlib.extend_from_slice(&(!(block.len() as u16)).to_le_bytes()); |
| 1546 | zlib.extend_from_slice(block); |
| 1547 | } |
| 1548 | zlib.extend_from_slice(&adler32(&raw).to_be_bytes()); |
| 1549 | let mut header = Vec::new(); |
| 1550 | header.extend_from_slice(&(width as u32).to_be_bytes()); |
| 1551 | header.extend_from_slice(&(height as u32).to_be_bytes()); |
| 1552 | header.extend_from_slice(&[8, 2, 0, 0, 0]); |
| 1553 | let mut png = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]; |
| 1554 | png_chunk(&mut png, b"IHDR", &header); |
| 1555 | png_chunk(&mut png, b"IDAT", &zlib); |
| 1556 | png_chunk(&mut png, b"IEND", &[]); |
| 1557 | png |
| 1558 | } |
| 1559 | |
| 1560 | #[cfg(test)] |
| 1561 | mod tests { |
| 1562 | use super::*; |
| 1563 | |
| 1564 | #[test] |
| 1565 | fn png_has_signature_and_ends_with_iend() { |
| 1566 | let png = encode_png(&MARA); |
| 1567 | assert_eq!(&png[..8], &[0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]); |
| 1568 | assert_eq!(&png[png.len() - 8..png.len() - 4], b"IEND"); |
| 1569 | } |
| 1570 | |
| 1571 | #[test] |
| 1572 | fn crc_and_adler_match_known_values() { |
| 1573 | assert_eq!(crc32(b"IEND"), 0xae42_6082); |
| 1574 | assert_eq!(adler32(b"Wikipedia"), 0x11e6_0398); |
| 1575 | } |
| 1576 | |
| 1577 | #[test] |
| 1578 | fn mention_candidates_filter_by_name_and_add_scope_in_channels() { |
| 1579 | let chat = mention_candidates("demo-chat-release", "ma"); |
| 1580 | assert_eq!(chat.len(), 1); |
| 1581 | assert_eq!(chat[0].display_name(), "Mara Lindqvist"); |
| 1582 | let channel = mention_candidates("demo-channel-1-1", "gen"); |
| 1583 | assert!(matches!(channel[0], MentionCandidate::Channel { .. })); |
| 1584 | assert_eq!(mention_candidates("demo-chat-release", "").len(), 5); |
| 1585 | } |
| 1586 | |
| 1587 | #[test] |
| 1588 | fn new_group_chat_gets_a_title_and_the_first_message() { |
| 1589 | let people = vec![ |
| 1590 | (MARA_ID.to_owned(), "Mara Lindqvist".to_owned()), |
| 1591 | (JONAS_ID.to_owned(), "Jonas Ortega".to_owned()), |
| 1592 | ]; |
| 1593 | let now = Utc::now(); |
| 1594 | let (chat, record) = new_chat( |
| 1595 | &[], |
| 1596 | &people, |
| 1597 | Some("Crew"), |
| 1598 | "Hello all", |
| 1599 | "Hello <i>all</i>", |
| 1600 | now, |
| 1601 | ); |
| 1602 | assert_eq!(chat.id, "demo-chat-new-1"); |
| 1603 | assert_eq!(chat.kind, "group"); |
| 1604 | assert_eq!(chat.title, "Crew"); |
| 1605 | assert_eq!(chat.member_summary, "Mara Lindqvist, Jonas Ortega"); |
| 1606 | assert_eq!(chat.members.len(), 3); |
| 1607 | assert_eq!(chat.members[0].user_id.as_deref(), Some(DEMO_USER_ID)); |
| 1608 | assert_eq!(chat.last_message_preview.as_deref(), Some("Hello all")); |
| 1609 | assert_eq!(chat.last_message_at, Some(now)); |
| 1610 | assert_eq!(record.conversation_id, chat.id); |
| 1611 | assert_eq!(record.sender_id.as_deref(), Some(DEMO_USER_ID)); |
| 1612 | assert!(record.body_html.contains("Hello")); |
| 1613 | } |
| 1614 | |
| 1615 | #[test] |
| 1616 | fn new_one_on_one_chat_has_no_title_and_counts_up() { |
| 1617 | let people = vec![(LEA_ID.to_owned(), "Lea Schneider".to_owned())]; |
| 1618 | let (first, _) = new_chat(&[], &people, None, "Hi", "Hi", Utc::now()); |
| 1619 | assert_eq!(first.kind, "oneOnOne"); |
| 1620 | assert!(first.title.is_empty()); |
| 1621 | let (second, _) = new_chat(&[first], &people, None, "Hi again", "Hi again", Utc::now()); |
| 1622 | assert_eq!(second.id, "demo-chat-new-2"); |
| 1623 | } |
| 1624 | |
| 1625 | #[test] |
| 1626 | fn sending_into_a_chat_refreshes_its_preview() { |
| 1627 | let base = ChatRecord { |
| 1628 | id: "demo-chat-x".to_owned(), |
| 1629 | unread: true, |
| 1630 | ..Default::default() |
| 1631 | }; |
| 1632 | let (chat, record) = send_in_chat(&base, "Ping", "Ping", Utc::now()); |
| 1633 | assert!(!chat.unread); |
| 1634 | assert_eq!(chat.last_message_preview.as_deref(), Some("Ping")); |
| 1635 | assert_eq!(record.conversation_id, "demo-chat-x"); |
| 1636 | } |
| 1637 | |
| 1638 | #[test] |
| 1639 | fn demo_covers_the_sidebar_edge_cases() { |
| 1640 | let chats = demo_chats(); |
| 1641 | assert!(chats.iter().any(|chat| chat.deleted)); |
| 1642 | assert!(chats.iter().any(|chat| chat.title.len() > 40)); |
| 1643 | assert!(chats.iter().filter(|chat| chat.unread).count() >= 3); |
| 1644 | } |
| 1645 | } |