| 1 | use std::collections::HashMap; |
| 2 | use std::path::PathBuf; |
| 3 | use std::sync::{Arc, OnceLock}; |
| 4 | use std::time::Instant; |
| 5 | |
| 6 | use chrono::{DateTime, Duration, Local, TimeZone, Utc}; |
| 7 | use gpui_kit::{App, Entity, Image}; |
| 8 | use store::{ |
| 9 | ChannelLayoutRecord, ChannelNotificationLevel, ChannelNotifications, ChannelRecord, |
| 10 | ChannelTabRecord, ChatRecord, MemberRecord, MessageRecord, Store, TeamLayoutRecord, TeamRecord, |
| 11 | }; |
| 12 | use teams_core::{ |
| 13 | Activity, CardActionOutcome, ChatApp, Gif, Language, LanguageSettings, LinkPreview, |
| 14 | MentionCandidate, PersonCandidate, PersonSource, PinnedMessage, SavedMessage, TaskDialog, |
| 15 | TaskDialogKind, Translation, TranslationBehavior, TranslationStatus, |
| 16 | }; |
| 17 | |
| 18 | use crate::activity::{Actor, Entry, Kind}; |
| 19 | use crate::app_state::{AppState, Selection}; |
| 20 | use crate::card_actions::{CardAnswer, CardTask}; |
| 21 | use crate::data::{FolderInfo, FolderKind, Person, PresenceKind}; |
| 22 | use crate::demo_gifs; |
| 23 | use crate::message_actions::ForwardSource; |
| 24 | |
| 25 | static SEARCHABLE_GIFS: OnceLock<Vec<Gif>> = OnceLock::new(); |
| 26 | |
| 27 | pub const DEMO_USER_ID: &str = "demo-me"; |
| 28 | const DEMO_USER_NAME: &str = "Dana Demo"; |
| 29 | const DEMO_USER_EMAIL: &str = "dana.demo@example.com"; |
| 30 | const DEMO_TIME_ZONE: &str = "W. Europe Standard Time"; |
| 31 | const PHOTO_SIZE: usize = 48; |
| 32 | const STAGING_IMAGE_KEY: &str = "demo://staging-dashboard"; |
| 33 | const PENDING_IMAGE_KEY: &str = "demo://pending-screenshot"; |
| 34 | const STAGING_SIZE: (usize, usize) = (960, 540); |
| 35 | const LINK_IMAGE_KEY: &str = |
| 36 | "https://de-prod.asyncgw.teams.microsoft.com/urlp/v1/url/image/Thumbnail?url=demo"; |
| 37 | const LINK_IMAGE_SIZE: (u32, u32) = (320, 180); |
| 38 | const PRIORITY_CHAT: &str = "demo-chat-priya"; |
| 39 | const BOT_CHAT: &str = "demo-chat-wiki-bot"; |
| 40 | const BOT_NAME: &str = "Wiki Bot"; |
| 41 | const BOT_APP_NAME: &str = "Wiki Cloud"; |
| 42 | const BOT_ID: &str = "00000000-0000-0000-0000-00000000b07a"; |
| 43 | const CARD_ANSWER_DELAY: std::time::Duration = std::time::Duration::from_millis(700); |
| 44 | const CARD_PERSON_URL: &str = "https://avatars.githubusercontent.com/u/9919?s=64"; |
| 45 | const CARD_PAGE_URL: &str = "https://avatars.githubusercontent.com/u/9919?s=64"; |
| 46 | const NEW_CHAT_PREFIX: &str = "demo-chat-new-"; |
| 47 | const PEOPLE_DIRECTORY: [(&str, &str, &str, &str); 5] = [ |
| 48 | ( |
| 49 | MARA_ID, |
| 50 | "Mara Lindqvist", |
| 51 | "Product Owner", |
| 52 | "mara.lindqvist@example.com", |
| 53 | ), |
| 54 | ( |
| 55 | JONAS_ID, |
| 56 | "Jonas Ortega", |
| 57 | "Backend Engineer", |
| 58 | "jonas.ortega@example.com", |
| 59 | ), |
| 60 | ( |
| 61 | PRIYA_ID, |
| 62 | "Priya Nair", |
| 63 | "Engineering Manager", |
| 64 | "priya.nair@example.com", |
| 65 | ), |
| 66 | ( |
| 67 | LEA_ID, |
| 68 | "Lea Schneider", |
| 69 | "Customer Success", |
| 70 | "lea.schneider@example.com", |
| 71 | ), |
| 72 | ( |
| 73 | TOBIAS_ID, |
| 74 | "Tobias Klein", |
| 75 | "Support Engineer", |
| 76 | "tobias.klein@example.com", |
| 77 | ), |
| 78 | ]; |
| 79 | |
| 80 | struct Palette { |
| 81 | background: [u8; 3], |
| 82 | skin: [u8; 3], |
| 83 | hair: [u8; 3], |
| 84 | shirt: [u8; 3], |
| 85 | } |
| 86 | |
| 87 | const MARA: Palette = Palette { |
| 88 | background: [0x3f, 0x5f, 0x7a], |
| 89 | skin: [0xe0, 0xb2, 0x93], |
| 90 | hair: [0xd9, 0xa4, 0x41], |
| 91 | shirt: [0x1e, 0x29, 0x3b], |
| 92 | }; |
| 93 | const PRIYA: Palette = Palette { |
| 94 | background: [0x4b, 0x3b, 0x5c], |
| 95 | skin: [0x9a, 0x6a, 0x4a], |
| 96 | hair: [0x1c, 0x19, 0x17], |
| 97 | shirt: [0xbe, 0x12, 0x3c], |
| 98 | }; |
| 99 | const TOBIAS: Palette = Palette { |
| 100 | background: [0x3b, 0x4a, 0x3f], |
| 101 | skin: [0xf1, 0xc7, 0xa5], |
| 102 | hair: [0x57, 0x53, 0x4e], |
| 103 | shirt: [0x0f, 0x76, 0x6e], |
| 104 | }; |
| 105 | |
| 106 | pub const MARA_ID: &str = "demo-mara"; |
| 107 | pub const JONAS_ID: &str = "demo-jonas"; |
| 108 | pub const PRIYA_ID: &str = "demo-priya"; |
| 109 | pub const LEA_ID: &str = "demo-lea"; |
| 110 | pub const TOBIAS_ID: &str = "demo-tobias"; |
| 111 | |
| 112 | const DEMO_TEAMS: [(&str, &str, [&str; 3]); 4] = [ |
| 113 | ( |
| 114 | "demo-team-1", |
| 115 | "Platform", |
| 116 | ["General", "Incidents", "Releases"], |
| 117 | ), |
| 118 | ("demo-team-2", "Product", ["General", "Roadmap", "Feedback"]), |
| 119 | ( |
| 120 | "demo-team-3", |
| 121 | "Operations", |
| 122 | ["General", "On call", "Runbooks"], |
| 123 | ), |
| 124 | ( |
| 125 | "demo-team-4", |
| 126 | "Archive 2025", |
| 127 | ["General", "Trade fair", "Office move"], |
| 128 | ), |
| 129 | ]; |
| 130 | const DEMO_TEAM_ORDER: [&str; 4] = ["demo-team-2", "demo-team-1", "demo-team-3", "demo-team-4"]; |
| 131 | const DEMO_HIDDEN_TEAM: &str = "demo-team-4"; |
| 132 | const DEMO_HIDDEN_CHANNEL: &str = "demo-channel-3-3"; |
| 133 | const DEMO_CHANNEL_NOTIFICATIONS: [(&str, ChannelNotificationLevel, bool); 4] = [ |
| 134 | ( |
| 135 | "demo-channel-1-1", |
| 136 | ChannelNotificationLevel::BannerAndFeed, |
| 137 | false, |
| 138 | ), |
| 139 | ( |
| 140 | "demo-channel-1-2", |
| 141 | ChannelNotificationLevel::BannerAndFeed, |
| 142 | true, |
| 143 | ), |
| 144 | ("demo-channel-1-3", ChannelNotificationLevel::Off, false), |
| 145 | ("demo-channel-2-2", ChannelNotificationLevel::Off, false), |
| 146 | ]; |
| 147 | |
| 148 | const UNREAD_CHAT: &str = "demo-chat-atlas"; |
| 149 | const RELEASE_CHAT: &str = "demo-chat-release"; |
| 150 | const PINNED_MESSAGE: &str = "m5b"; |
| 151 | const FRENCH_MESSAGE: &str = "m10"; |
| 152 | const SPANISH_MESSAGE: &str = "m11"; |
| 153 | const SAVED_MESSAGES: [&str; 2] = ["m4", "m5c"]; |
| 154 | const FORWARD_ITEMTYPE: &str = "http://schema.skype.com/Forward"; |
| 155 | const FAVORITES_ID: &str = "demo-folder-favorites"; |
| 156 | const CUSTOMERS_FOLDER: &str = "demo-folder-customers"; |
| 157 | const ATLAS_FOLDER: &str = "demo-folder-atlas"; |
| 158 | const TODO_FOLDER: &str = "demo-folder-todo"; |
| 159 | const MUTED_CHATS: [&str; 6] = [ |
| 160 | "demo-chat-muted-lunch", |
| 161 | "demo-chat-muted-offsite", |
| 162 | "demo-chat-muted-kudos", |
| 163 | "demo-chat-muted-bikes", |
| 164 | "demo-chat-muted-book", |
| 165 | "demo-chat-muted-games", |
| 166 | ]; |
| 167 | |
| 168 | fn member(user_id: &str, name: &str) -> MemberRecord { |
| 169 | MemberRecord { |
| 170 | user_id: Some(user_id.to_owned()), |
| 171 | display_name: name.to_owned(), |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | fn people(extra: &[(&str, &str)]) -> Vec<MemberRecord> { |
| 176 | std::iter::once(member(DEMO_USER_ID, DEMO_USER_NAME)) |
| 177 | .chain(extra.iter().map(|(id, name)| member(id, name))) |
| 178 | .collect() |
| 179 | } |
| 180 | |
| 181 | pub fn at(days_ago: i64, hour: u32, minute: u32) -> DateTime<Utc> { |
| 182 | let day = Local::now().date_naive() - Duration::days(days_ago); |
| 183 | let local = day.and_hms_opt(hour, minute, 0).expect("valid time"); |
| 184 | Local |
| 185 | .from_local_datetime(&local) |
| 186 | .earliest() |
| 187 | .map_or_else(Utc::now, |time| time.with_timezone(&Utc)) |
| 188 | } |
| 189 | |
| 190 | struct DemoChat { |
| 191 | id: &'static str, |
| 192 | kind: &'static str, |
| 193 | title: &'static str, |
| 194 | members: Vec<MemberRecord>, |
| 195 | time: DateTime<Utc>, |
| 196 | unread: bool, |
| 197 | preview: Option<(&'static str, &'static str, &'static str)>, |
| 198 | deleted: bool, |
| 199 | } |
| 200 | |
| 201 | fn demo_chats() -> Vec<DemoChat> { |
| 202 | let group = |extra: &[(&str, &str)]| people(extra); |
| 203 | vec![ |
| 204 | DemoChat { |
| 205 | id: BOT_CHAT, |
| 206 | kind: "oneOnOne", |
| 207 | title: BOT_NAME, |
| 208 | members: people(&[]), |
| 209 | time: at(0, 13, 45), |
| 210 | unread: true, |
| 211 | preview: Some(("", BOT_NAME, "Mara Lindqvist edited your page")), |
| 212 | deleted: false, |
| 213 | }, |
| 214 | DemoChat { |
| 215 | id: "demo-chat-mara", |
| 216 | kind: "oneOnOne", |
| 217 | title: "Mara Lindqvist", |
| 218 | members: people(&[(MARA_ID, "Mara Lindqvist")]), |
| 219 | time: at(0, 13, 40), |
| 220 | unread: false, |
| 221 | preview: Some((MARA_ID, "Mara Lindqvist", "Sure, 2 pm works")), |
| 222 | deleted: false, |
| 223 | }, |
| 224 | DemoChat { |
| 225 | id: RELEASE_CHAT, |
| 226 | kind: "group", |
| 227 | title: "Release planning", |
| 228 | members: group(&[ |
| 229 | (MARA_ID, "Mara Lindqvist"), |
| 230 | (JONAS_ID, "Jonas Ortega"), |
| 231 | (PRIYA_ID, "Priya Nair"), |
| 232 | (LEA_ID, "Lea Schneider"), |
| 233 | ]), |
| 234 | time: at(0, 13, 35), |
| 235 | unread: false, |
| 236 | preview: Some(( |
| 237 | DEMO_USER_ID, |
| 238 | DEMO_USER_NAME, |
| 239 | "Build 42 is green, see pipeline", |
| 240 | )), |
| 241 | deleted: false, |
| 242 | }, |
| 243 | DemoChat { |
| 244 | id: "demo-chat-customer-1", |
| 245 | kind: "group", |
| 246 | title: "Customer North - Operations", |
| 247 | members: group(&[(LEA_ID, "Lea Schneider"), (TOBIAS_ID, "Tobias Klein")]), |
| 248 | time: at(0, 12, 50), |
| 249 | unread: true, |
| 250 | preview: Some((LEA_ID, "Lea Schneider", "Access is enabled now")), |
| 251 | deleted: false, |
| 252 | }, |
| 253 | DemoChat { |
| 254 | id: "demo-chat-customer-2", |
| 255 | kind: "group", |
| 256 | title: "Customer South - Rollout", |
| 257 | members: group(&[(JONAS_ID, "Jonas Ortega"), (PRIYA_ID, "Priya Nair")]), |
| 258 | time: at(0, 9, 5), |
| 259 | unread: true, |
| 260 | preview: Some((JONAS_ID, "Jonas Ortega", "Meeting is set for Friday")), |
| 261 | deleted: false, |
| 262 | }, |
| 263 | DemoChat { |
| 264 | id: "demo-chat-customer-3", |
| 265 | kind: "group", |
| 266 | title: "Customer West - Support", |
| 267 | members: group(&[(TOBIAS_ID, "Tobias Klein"), (LEA_ID, "Lea Schneider")]), |
| 268 | time: at(1, 16, 0), |
| 269 | unread: false, |
| 270 | preview: Some((TOBIAS_ID, "Tobias Klein", "Ticket is closed")), |
| 271 | deleted: false, |
| 272 | }, |
| 273 | DemoChat { |
| 274 | id: "demo-chat-customer-4", |
| 275 | kind: "group", |
| 276 | title: "Customer East - Pilot", |
| 277 | members: group(&[(MARA_ID, "Mara Lindqvist"), (PRIYA_ID, "Priya Nair")]), |
| 278 | time: at(3, 10, 0), |
| 279 | unread: false, |
| 280 | preview: Some((DEMO_USER_ID, DEMO_USER_NAME, "I'll send the documents")), |
| 281 | deleted: false, |
| 282 | }, |
| 283 | DemoChat { |
| 284 | id: "demo-chat-atlas", |
| 285 | kind: "group", |
| 286 | title: "Atlas core team", |
| 287 | members: group(&[ |
| 288 | (PRIYA_ID, "Priya Nair"), |
| 289 | (LEA_ID, "Lea Schneider"), |
| 290 | (MARA_ID, "Mara Lindqvist"), |
| 291 | ]), |
| 292 | time: at(0, 13, 12), |
| 293 | unread: true, |
| 294 | preview: Some((PRIYA_ID, "Priya Nair", "Who is taking the review?")), |
| 295 | deleted: false, |
| 296 | }, |
| 297 | DemoChat { |
| 298 | id: "demo-chat-jonas", |
| 299 | kind: "oneOnOne", |
| 300 | title: "Jonas Ortega", |
| 301 | members: people(&[(JONAS_ID, "Jonas Ortega")]), |
| 302 | time: at(0, 11, 2), |
| 303 | unread: false, |
| 304 | preview: Some((DEMO_USER_ID, DEMO_USER_NAME, "Thanks, it's merged")), |
| 305 | deleted: false, |
| 306 | }, |
| 307 | DemoChat { |
| 308 | id: "demo-chat-priya", |
| 309 | kind: "oneOnOne", |
| 310 | title: "Priya Nair", |
| 311 | members: people(&[(PRIYA_ID, "Priya Nair")]), |
| 312 | time: at(0, 10, 47), |
| 313 | unread: false, |
| 314 | preview: Some((PRIYA_ID, "Priya Nair", "Image")), |
| 315 | deleted: false, |
| 316 | }, |
| 317 | DemoChat { |
| 318 | id: "demo-chat-tobias", |
| 319 | kind: "oneOnOne", |
| 320 | title: "Tobias Klein", |
| 321 | members: people(&[(TOBIAS_ID, "Tobias Klein")]), |
| 322 | time: at(2, 9, 30), |
| 323 | unread: false, |
| 324 | preview: Some((TOBIAS_ID, "Tobias Klein", "ok")), |
| 325 | deleted: false, |
| 326 | }, |
| 327 | DemoChat { |
| 328 | id: "demo-chat-allhands", |
| 329 | kind: "group", |
| 330 | title: "All-hands announcements", |
| 331 | members: group(&[(LEA_ID, "Lea Schneider"), (MARA_ID, "Mara Lindqvist")]), |
| 332 | time: at(0, 9, 0), |
| 333 | unread: true, |
| 334 | preview: Some((LEA_ID, "Lea Schneider", "New vacation policy from November")), |
| 335 | deleted: false, |
| 336 | }, |
| 337 | DemoChat { |
| 338 | id: "demo-chat-design", |
| 339 | kind: "group", |
| 340 | title: "Design review", |
| 341 | members: group(&[(MARA_ID, "Mara Lindqvist"), (TOBIAS_ID, "Tobias Klein")]), |
| 342 | time: at(2, 15, 0), |
| 343 | unread: false, |
| 344 | preview: Some((MARA_ID, "Mara Lindqvist", "alt")), |
| 345 | deleted: true, |
| 346 | }, |
| 347 | DemoChat { |
| 348 | id: "demo-chat-quarterly", |
| 349 | kind: "group", |
| 350 | title: "Quarterly numbers, finance review and planning 2027", |
| 351 | members: group(&[(LEA_ID, "Lea Schneider"), (JONAS_ID, "Jonas Ortega")]), |
| 352 | time: at(4, 11, 0), |
| 353 | unread: false, |
| 354 | preview: Some((LEA_ID, "Lea Schneider", "Draft attached")), |
| 355 | deleted: false, |
| 356 | }, |
| 357 | DemoChat { |
| 358 | id: "demo-chat-meeting-standup", |
| 359 | kind: "meeting", |
| 360 | title: "Daily standup", |
| 361 | members: group(&[(JONAS_ID, "Jonas Ortega"), (PRIYA_ID, "Priya Nair")]), |
| 362 | time: at(0, 9, 45), |
| 363 | unread: true, |
| 364 | preview: Some((JONAS_ID, "Jonas Ortega", "Recording is available")), |
| 365 | deleted: false, |
| 366 | }, |
| 367 | DemoChat { |
| 368 | id: "demo-chat-meeting-retro", |
| 369 | kind: "meeting", |
| 370 | title: "Sprint retrospective", |
| 371 | members: group(&[(MARA_ID, "Mara Lindqvist"), (TOBIAS_ID, "Tobias Klein")]), |
| 372 | time: at(3, 15, 30), |
| 373 | unread: false, |
| 374 | preview: Some((MARA_ID, "Mara Lindqvist", "Notes are in the wiki")), |
| 375 | deleted: false, |
| 376 | }, |
| 377 | DemoChat { |
| 378 | id: "demo-chat-muted-lunch", |
| 379 | kind: "group", |
| 380 | title: "Lunch crew", |
| 381 | members: group(&[(JONAS_ID, "Jonas Ortega"), (LEA_ID, "Lea Schneider")]), |
| 382 | time: at(0, 12, 5), |
| 383 | unread: true, |
| 384 | preview: Some((JONAS_ID, "Jonas Ortega", "Pizza on Friday?")), |
| 385 | deleted: false, |
| 386 | }, |
| 387 | DemoChat { |
| 388 | id: "demo-chat-muted-offsite", |
| 389 | kind: "group", |
| 390 | title: "Team offsite 2027", |
| 391 | members: group(&[(LEA_ID, "Lea Schneider"), (LEA_ID, "Lea Schneider")]), |
| 392 | time: at(1, 16, 20), |
| 393 | unread: false, |
| 394 | preview: Some((LEA_ID, "Lea Schneider", "Voting closes tomorrow")), |
| 395 | deleted: false, |
| 396 | }, |
| 397 | DemoChat { |
| 398 | id: "demo-chat-muted-kudos", |
| 399 | kind: "group", |
| 400 | title: "Kudos corner", |
| 401 | members: group(&[(MARA_ID, "Mara Lindqvist"), (LEA_ID, "Lea Schneider")]), |
| 402 | time: at(2, 10, 0), |
| 403 | unread: false, |
| 404 | preview: Some((MARA_ID, "Mara Lindqvist", "Thanks for the release!")), |
| 405 | deleted: false, |
| 406 | }, |
| 407 | DemoChat { |
| 408 | id: "demo-chat-muted-bikes", |
| 409 | kind: "group", |
| 410 | title: "Bike to work", |
| 411 | members: group(&[(TOBIAS_ID, "Tobias Klein"), (LEA_ID, "Lea Schneider")]), |
| 412 | time: at(4, 8, 15), |
| 413 | unread: false, |
| 414 | preview: Some((TOBIAS_ID, "Tobias Klein", "Route map attached")), |
| 415 | deleted: false, |
| 416 | }, |
| 417 | DemoChat { |
| 418 | id: "demo-chat-muted-book", |
| 419 | kind: "group", |
| 420 | title: "Book club", |
| 421 | members: group(&[(PRIYA_ID, "Priya Nair"), (LEA_ID, "Lea Schneider")]), |
| 422 | time: at(6, 19, 40), |
| 423 | unread: false, |
| 424 | preview: Some((PRIYA_ID, "Priya Nair", "Next pick: Dune")), |
| 425 | deleted: false, |
| 426 | }, |
| 427 | DemoChat { |
| 428 | id: "demo-chat-muted-games", |
| 429 | kind: "group", |
| 430 | title: "Board game night", |
| 431 | members: group(&[(JONAS_ID, "Jonas Ortega"), (LEA_ID, "Lea Schneider")]), |
| 432 | time: at(9, 18, 0), |
| 433 | unread: false, |
| 434 | preview: Some((JONAS_ID, "Jonas Ortega", "Who brings the snacks?")), |
| 435 | deleted: false, |
| 436 | }, |
| 437 | ] |
| 438 | } |
| 439 | |
| 440 | pub fn mention_candidates(conversation_id: &str, query: &str) -> Vec<MentionCandidate> { |
| 441 | let needle = query.trim().to_lowercase(); |
| 442 | let matches = |name: &str| needle.is_empty() || name.to_lowercase().contains(&needle); |
| 443 | let people = PEOPLE_DIRECTORY |
| 444 | .iter() |
| 445 | .filter(|(_, name, _, _)| matches(name)) |
| 446 | .map(|(user_id, name, job_title, mail)| { |
| 447 | MentionCandidate::Person(PersonCandidate { |
| 448 | user_id: (*user_id).to_owned(), |
| 449 | display_name: (*name).to_owned(), |
| 450 | mail: Some((*mail).to_owned()), |
| 451 | job_title: Some((*job_title).to_owned()), |
| 452 | source: PersonSource::Member, |
| 453 | }) |
| 454 | }); |
| 455 | let scope: Vec<MentionCandidate> = if conversation_id.starts_with("demo-channel") { |
| 456 | [ |
| 457 | MentionCandidate::Channel { |
| 458 | channel_id: conversation_id.to_owned(), |
| 459 | name: "General".to_owned(), |
| 460 | }, |
| 461 | MentionCandidate::Team { |
| 462 | team_id: "demo-team-1".to_owned(), |
| 463 | name: "Platform".to_owned(), |
| 464 | }, |
| 465 | ] |
| 466 | .into_iter() |
| 467 | .filter(|candidate| matches(candidate.display_name())) |
| 468 | .collect() |
| 469 | } else { |
| 470 | Vec::new() |
| 471 | }; |
| 472 | people.chain(scope).collect() |
| 473 | } |
| 474 | |
| 475 | pub fn send_in_chat( |
| 476 | chat: &ChatRecord, |
| 477 | preview: &str, |
| 478 | html: &str, |
| 479 | now: DateTime<Utc>, |
| 480 | ) -> (ChatRecord, MessageRecord) { |
| 481 | let mut chat = chat.clone(); |
| 482 | chat.last_message_at = Some(now); |
| 483 | chat.last_message_preview = Some(preview.to_owned()); |
| 484 | chat.last_message_sender_id = Some(DEMO_USER_ID.to_owned()); |
| 485 | chat.last_message_sender_name = Some(DEMO_USER_NAME.to_owned()); |
| 486 | chat.last_message_deleted = false; |
| 487 | chat.unread = false; |
| 488 | let record = message( |
| 489 | &chat.id, |
| 490 | &format!("demo-sent-{}", now.timestamp_millis()), |
| 491 | None, |
| 492 | (DEMO_USER_ID, DEMO_USER_NAME), |
| 493 | now, |
| 494 | html, |
| 495 | "[]", |
| 496 | false, |
| 497 | ); |
| 498 | (chat, record) |
| 499 | } |
| 500 | |
| 501 | pub fn new_chat( |
| 502 | existing: &[ChatRecord], |
| 503 | people: &[(String, String)], |
| 504 | topic: Option<&str>, |
| 505 | preview: &str, |
| 506 | html: &str, |
| 507 | now: DateTime<Utc>, |
| 508 | ) -> (ChatRecord, MessageRecord) { |
| 509 | let number = existing |
| 510 | .iter() |
| 511 | .filter(|chat| chat.id.starts_with(NEW_CHAT_PREFIX)) |
| 512 | .count() |
| 513 | + 1; |
| 514 | let one_on_one = people.len() == 1; |
| 515 | let members = std::iter::once(member(DEMO_USER_ID, DEMO_USER_NAME)) |
| 516 | .chain(people.iter().map(|(user_id, name)| member(user_id, name))) |
| 517 | .collect(); |
| 518 | let chat = ChatRecord { |
| 519 | id: format!("{NEW_CHAT_PREFIX}{number}"), |
| 520 | kind: if one_on_one { "oneOnOne" } else { "group" }.to_owned(), |
| 521 | title: if one_on_one { |
| 522 | String::new() |
| 523 | } else { |
| 524 | topic.unwrap_or_default().to_owned() |
| 525 | }, |
| 526 | member_summary: people |
| 527 | .iter() |
| 528 | .map(|(_, name)| name.as_str()) |
| 529 | .collect::<Vec<_>>() |
| 530 | .join(", "), |
| 531 | members, |
| 532 | ..Default::default() |
| 533 | }; |
| 534 | send_in_chat(&chat, preview, html, now) |
| 535 | } |
| 536 | |
| 537 | fn image_directory() -> PathBuf { |
| 538 | std::env::temp_dir().join("rusty-teams-demo") |
| 539 | } |
| 540 | |
| 541 | pub fn search_gifs(query: &str) -> Vec<Gif> { |
| 542 | let query = query.trim().to_lowercase(); |
| 543 | SEARCHABLE_GIFS |
| 544 | .get() |
| 545 | .into_iter() |
| 546 | .flatten() |
| 547 | .filter(|gif| gif.title.to_lowercase().contains(&query)) |
| 548 | .cloned() |
| 549 | .collect() |
| 550 | } |
| 551 | |
| 552 | pub fn language_settings() -> LanguageSettings { |
| 553 | LanguageSettings { |
| 554 | display_locale: Some("en-US".to_owned()), |
| 555 | target_locale: Some("en".to_owned()), |
| 556 | behavior: TranslationBehavior::Ask, |
| 557 | authoring_locales: vec!["de-DE".to_owned(), "en-US".to_owned()], |
| 558 | preferences: serde_json::Map::new(), |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | pub fn language_list() -> Vec<Language> { |
| 563 | [ |
| 564 | ("de", "German"), |
| 565 | ("en", "English"), |
| 566 | ("es", "Spanish"), |
| 567 | ("fr", "French"), |
| 568 | ("it", "Italian"), |
| 569 | ("ja", "Japanese"), |
| 570 | ("nl", "Dutch"), |
| 571 | ("pt", "Portuguese"), |
| 572 | ] |
| 573 | .into_iter() |
| 574 | .map(|(code, name)| Language { |
| 575 | code: code.to_owned(), |
| 576 | name: name.to_owned(), |
| 577 | }) |
| 578 | .collect() |
| 579 | } |
| 580 | |
| 581 | pub fn language_stamps(conversation_id: &str) -> HashMap<String, String> { |
| 582 | if conversation_id != RELEASE_CHAT { |
| 583 | return HashMap::new(); |
| 584 | } |
| 585 | HashMap::from([ |
| 586 | ( |
| 587 | FRENCH_MESSAGE.to_owned(), |
| 588 | "languages=fr:100;en:12;length:104;&detector=Bling".to_owned(), |
| 589 | ), |
| 590 | ( |
| 591 | SPANISH_MESSAGE.to_owned(), |
| 592 | "languages=es:100;pt:31;length:94;&detector=Bling".to_owned(), |
| 593 | ), |
| 594 | ]) |
| 595 | } |
| 596 | |
| 597 | pub fn translations(message_ids: &[String]) -> Vec<Translation> { |
| 598 | let translated = |message_id: &str| match message_id { |
| 599 | FRENCH_MESSAGE => Some( |
| 600 | "<p>Hello everyone, <strong>version 0.2</strong> is ready. Can you check the checklist before Friday?</p>", |
| 601 | ), |
| 602 | SPANISH_MESSAGE => Some( |
| 603 | "<p>Thanks for the heads-up. I will test the build this afternoon and let you know if anything breaks.</p>", |
| 604 | ), |
| 605 | _ => None, |
| 606 | }; |
| 607 | message_ids |
| 608 | .iter() |
| 609 | .map(|message_id| Translation { |
| 610 | message_id: message_id.clone(), |
| 611 | version: None, |
| 612 | status: if translated(message_id).is_some() { |
| 613 | TranslationStatus::Done |
| 614 | } else { |
| 615 | TranslationStatus::Failed |
| 616 | }, |
| 617 | content_html: translated(message_id).map(str::to_owned), |
| 618 | subject: None, |
| 619 | title: None, |
| 620 | }) |
| 621 | .collect() |
| 622 | } |
| 623 | |
| 624 | pub fn first_unread(conversation_id: &str) -> Option<String> { |
| 625 | (conversation_id == UNREAD_CHAT).then(|| "u3".to_owned()) |
| 626 | } |
| 627 | |
| 628 | pub fn read_message_ids(records: &[MessageRecord]) -> std::collections::HashSet<String> { |
| 629 | let newest = records.iter().max_by_key(|record| record.created_at); |
| 630 | records |
| 631 | .iter() |
| 632 | .filter(|record| { |
| 633 | Some(record.message_id.as_str()) != newest.map(|newest| newest.message_id.as_str()) |
| 634 | }) |
| 635 | .map(|record| record.message_id.clone()) |
| 636 | .collect() |
| 637 | } |
| 638 | |
| 639 | pub fn start_session(state: &Entity<AppState>, cx: &mut App) { |
| 640 | state.update(cx, |state, cx| { |
| 641 | seed_directory(state); |
| 642 | state.demo_live_meetings(); |
| 643 | state.select(first_selection(), cx); |
| 644 | }); |
| 645 | } |
| 646 | |
| 647 | pub fn first_selection() -> Selection { |
| 648 | Selection::Chat(RELEASE_CHAT.to_owned()) |
| 649 | } |
| 650 | |
| 651 | pub fn activity_entries() -> Vec<Entry> { |
| 652 | let actor = |user_id: &str, name: &str| Actor { |
| 653 | user_id: Some(user_id.to_owned()), |
| 654 | name: name.to_owned(), |
| 655 | }; |
| 656 | let entry = |conversation_id: &str, |
| 657 | kind: Kind, |
| 658 | message_id: &str, |
| 659 | actors: Vec<Actor>, |
| 660 | preview: &str, |
| 661 | count: u32, |
| 662 | updated_at: DateTime<Utc>, |
| 663 | read: bool| Entry { |
| 664 | id: 0, |
| 665 | conversation_id: conversation_id.to_owned(), |
| 666 | kind, |
| 667 | message_id: message_id.to_owned(), |
| 668 | actors, |
| 669 | preview: preview.to_owned(), |
| 670 | glyphs: Vec::new(), |
| 671 | count, |
| 672 | updated_at, |
| 673 | read, |
| 674 | }; |
| 675 | let reaction = Entry { |
| 676 | glyphs: vec!["\u{1F602}".to_owned(), "\u{1F44D}".to_owned()], |
| 677 | ..entry( |
| 678 | RELEASE_CHAT, |
| 679 | Kind::Reaction, |
| 680 | "m3", |
| 681 | vec![ |
| 682 | actor(PRIYA_ID, "Priya Nair"), |
| 683 | actor(LEA_ID, "Lea Schneider"), |
| 684 | ], |
| 685 | "I'll take the changelog.", |
| 686 | 2, |
| 687 | at(0, 13, 20), |
| 688 | false, |
| 689 | ) |
| 690 | }; |
| 691 | vec![ |
| 692 | entry( |
| 693 | UNREAD_CHAT, |
| 694 | Kind::Messages, |
| 695 | "u3", |
| 696 | vec![ |
| 697 | actor(LEA_ID, "Lea Schneider"), |
| 698 | actor(PRIYA_ID, "Priya Nair"), |
| 699 | ], |
| 700 | "Who is taking the review?", |
| 701 | 3, |
| 702 | at(0, 13, 12), |
| 703 | false, |
| 704 | ), |
| 705 | entry( |
| 706 | "demo-channel-1-1", |
| 707 | Kind::Mention, |
| 708 | "t1", |
| 709 | vec![actor(MARA_ID, "Mara Lindqvist")], |
| 710 | "@Dana can you approve the merge? The pipeline is waiting.", |
| 711 | 1, |
| 712 | at(0, 11, 40), |
| 713 | false, |
| 714 | ), |
| 715 | reaction, |
| 716 | entry( |
| 717 | "demo-chat-customer-3", |
| 718 | Kind::Messages, |
| 719 | "demo-customer-3-closed", |
| 720 | vec![actor(TOBIAS_ID, "Tobias Klein")], |
| 721 | "Ticket is closed", |
| 722 | 1, |
| 723 | at(1, 16, 0), |
| 724 | true, |
| 725 | ), |
| 726 | ] |
| 727 | } |
| 728 | |
| 729 | pub fn seed(store: &Store) { |
| 730 | let chats: Vec<ChatRecord> = demo_chats() |
| 731 | .into_iter() |
| 732 | .map(|demo| ChatRecord { |
| 733 | id: demo.id.to_owned(), |
| 734 | kind: demo.kind.to_owned(), |
| 735 | title: if demo.kind == "oneOnOne" { |
| 736 | String::new() |
| 737 | } else { |
| 738 | demo.title.to_owned() |
| 739 | }, |
| 740 | member_summary: demo.title.to_owned(), |
| 741 | last_message_at: Some(demo.time), |
| 742 | unread: demo.unread, |
| 743 | members: demo.members, |
| 744 | last_message_preview: demo.preview.map(|(_, _, text)| text.to_owned()), |
| 745 | last_message_sender_id: demo.preview.map(|(id, _, _)| id.to_owned()), |
| 746 | last_message_sender_name: demo.preview.map(|(_, name, _)| name.to_owned()), |
| 747 | last_message_deleted: demo.deleted, |
| 748 | ..Default::default() |
| 749 | }) |
| 750 | .collect(); |
| 751 | let _ = store.upsert_chats(&chats); |
| 752 | for chat_id in MUTED_CHATS { |
| 753 | let _ = store.set_chat_muted(chat_id, true); |
| 754 | } |
| 755 | let teams: Vec<TeamRecord> = DEMO_TEAMS |
| 756 | .iter() |
| 757 | .map(|(id, name, _)| TeamRecord { |
| 758 | id: (*id).to_owned(), |
| 759 | name: (*name).to_owned(), |
| 760 | }) |
| 761 | .collect(); |
| 762 | let _ = store.upsert_teams(&teams); |
| 763 | let mut channels = Vec::new(); |
| 764 | for (team_index, (team_id, _, names)) in DEMO_TEAMS.iter().enumerate() { |
| 765 | for (channel_index, name) in names.iter().enumerate() { |
| 766 | channels.push(ChannelRecord { |
| 767 | id: format!("demo-channel-{}-{}", team_index + 1, channel_index + 1), |
| 768 | team_id: (*team_id).to_owned(), |
| 769 | name: (*name).to_owned(), |
| 770 | membership_type: None, |
| 771 | last_message_at: Some(at(channel_index as i64, 12, 10)), |
| 772 | unread: (team_index == 0 && channel_index == 1) |
| 773 | || (team_index == 1 && channel_index == 0), |
| 774 | }); |
| 775 | } |
| 776 | } |
| 777 | let _ = store.upsert_channels(&channels); |
| 778 | let _ = store.replace_team_layout(&demo_team_layout(&channels)); |
| 779 | let _ = store.set_meta("me_user_id", DEMO_USER_ID); |
| 780 | let _ = store.set_meta("me_display_name", DEMO_USER_NAME); |
| 781 | let _ = store.upsert_messages(&release_messages()); |
| 782 | let _ = store.upsert_messages(&unread_messages()); |
| 783 | let _ = store.upsert_messages(&channel_messages()); |
| 784 | let _ = store.upsert_messages(&priority_messages()); |
| 785 | let _ = store.upsert_messages(&meeting_link_messages()); |
| 786 | let _ = store.upsert_messages(&standup_messages()); |
| 787 | let _ = store.upsert_messages(&bot_messages()); |
| 788 | seed_agent_access(store); |
| 789 | } |
| 790 | |
| 791 | const AGENT_DEMO_PROPOSAL: &str = "I ran the regression suite on staging: 412 passed, 0 failed. The release candidate is good to ship, @Mara Lindqvist."; |
| 792 | |
| 793 | fn seed_agent_access(store: &Store) { |
| 794 | let selected = |
| 795 | |ids: &[&str]| mcp::Access::Selected(ids.iter().map(|id| (*id).to_owned()).collect()); |
| 796 | mcp::save_policy( |
| 797 | store, |
| 798 | &mcp::AgentPolicy { |
| 799 | enabled: true, |
| 800 | chats: selected(&[RELEASE_CHAT, "demo-chat-mara", "demo-chat-meeting-standup"]), |
| 801 | channels: selected(&["demo-channel-1-1"]), |
| 802 | propose: true, |
| 803 | calendar: true, |
| 804 | transcripts: false, |
| 805 | ..mcp::AgentPolicy::default() |
| 806 | }, |
| 807 | ); |
| 808 | let _ = store.insert_agent_proposal(&store::AgentProposalRecord { |
| 809 | id: "demo-proposal".to_owned(), |
| 810 | conversation_id: RELEASE_CHAT.to_owned(), |
| 811 | reply_to_message_id: Some("m1".to_owned()), |
| 812 | text: AGENT_DEMO_PROPOSAL.to_owned(), |
| 813 | mentions: vec![store::AgentProposalMention { |
| 814 | user_id: MARA_ID.to_owned(), |
| 815 | name: "Mara Lindqvist".to_owned(), |
| 816 | }], |
| 817 | images: vec![store::AttachmentImage { |
| 818 | name: "image-1.png".to_owned(), |
| 819 | format: "image/png".to_owned(), |
| 820 | bytes: encode_png(&MARA), |
| 821 | width: Some(PHOTO_SIZE as u32), |
| 822 | height: Some(PHOTO_SIZE as u32), |
| 823 | }], |
| 824 | agent: Some("claude".to_owned()), |
| 825 | session_id: None, |
| 826 | created_at: Utc::now() - Duration::minutes(1), |
| 827 | state: "pending".to_owned(), |
| 828 | resolved_at: None, |
| 829 | }); |
| 830 | let _ = store.insert_agent_reaction(&store::AgentReactionRecord { |
| 831 | id: "demo-reaction".to_owned(), |
| 832 | conversation_id: "demo-chat-mara".to_owned(), |
| 833 | message_id: "ml1".to_owned(), |
| 834 | emoji: "\u{1F44D}".to_owned(), |
| 835 | agent: Some("claude".to_owned()), |
| 836 | session_id: None, |
| 837 | created_at: Utc::now() - Duration::minutes(1), |
| 838 | state: "pending".to_owned(), |
| 839 | resolved_at: None, |
| 840 | }); |
| 841 | } |
| 842 | |
| 843 | fn photo(palette: &Palette) -> Arc<Image> { |
| 844 | crate::data::avatar_image_from("image/png", encode_png(palette)) |
| 845 | } |
| 846 | |
| 847 | pub fn seed_directory(state: &mut AppState) { |
| 848 | state.own_status = crate::own_status::demo_status(); |
| 849 | state.own_email = DEMO_USER_EMAIL.to_owned(); |
| 850 | state.own_time_zone = Some(DEMO_TIME_ZONE.to_owned()); |
| 851 | state.translation.settings = Some(language_settings()); |
| 852 | state.translation.languages = language_list(); |
| 853 | let directory = &mut state.directory; |
| 854 | directory.me = Some(Person { |
| 855 | user_id: DEMO_USER_ID.to_owned(), |
| 856 | display_name: DEMO_USER_NAME.to_owned(), |
| 857 | }); |
| 858 | let ids = |list: &[&str]| list.iter().map(|id| (*id).to_owned()).collect::<Vec<_>>(); |
| 859 | directory.folders = vec![ |
| 860 | FolderInfo { |
| 861 | id: FAVORITES_ID.to_owned(), |
| 862 | name: String::new(), |
| 863 | kind: FolderKind::Favorites, |
| 864 | expanded: None, |
| 865 | conversation_ids: ids(&["demo-chat-mara", RELEASE_CHAT]), |
| 866 | }, |
| 867 | FolderInfo { |
| 868 | id: CUSTOMERS_FOLDER.to_owned(), |
| 869 | name: "Customers".to_owned(), |
| 870 | kind: FolderKind::UserCreated, |
| 871 | expanded: None, |
| 872 | conversation_ids: ids(&[ |
| 873 | "demo-chat-customer-1", |
| 874 | "demo-chat-customer-2", |
| 875 | "demo-chat-customer-3", |
| 876 | "demo-chat-customer-4", |
| 877 | ]), |
| 878 | }, |
| 879 | FolderInfo { |
| 880 | id: ATLAS_FOLDER.to_owned(), |
| 881 | name: "Project Atlas".to_owned(), |
| 882 | kind: FolderKind::UserCreated, |
| 883 | expanded: None, |
| 884 | conversation_ids: ids(&["demo-chat-atlas", "demo-chat-jonas"]), |
| 885 | }, |
| 886 | FolderInfo { |
| 887 | id: TODO_FOLDER.to_owned(), |
| 888 | name: "To do".to_owned(), |
| 889 | kind: FolderKind::UserCreated, |
| 890 | expanded: None, |
| 891 | conversation_ids: Vec::new(), |
| 892 | }, |
| 893 | ]; |
| 894 | state.bot_apps.insert(BOT_CHAT.to_owned(), vec![bot_app()]); |
| 895 | let directory = &mut state.directory; |
| 896 | directory.pinned_channels = ids(&["demo-channel-1-1", "demo-channel-3-2"]); |
| 897 | directory |
| 898 | .unread_counts |
| 899 | .insert("demo-chat-atlas".to_owned(), 5); |
| 900 | directory |
| 901 | .unread_counts |
| 902 | .insert("demo-chat-customer-1".to_owned(), 2); |
| 903 | directory |
| 904 | .unread_counts |
| 905 | .insert("demo-chat-customer-2".to_owned(), 1); |
| 906 | directory |
| 907 | .unread_counts |
| 908 | .insert("demo-chat-allhands".to_owned(), 120); |
| 909 | directory.set_avatar(MARA_ID, Some(photo(&MARA))); |
| 910 | directory.set_avatar(PRIYA_ID, Some(photo(&PRIYA))); |
| 911 | directory.set_avatar(TOBIAS_ID, Some(photo(&TOBIAS))); |
| 912 | for (user_id, kind) in [ |
| 913 | (MARA_ID, PresenceKind::Available), |
| 914 | (JONAS_ID, PresenceKind::Busy), |
| 915 | (PRIYA_ID, PresenceKind::Away), |
| 916 | (TOBIAS_ID, PresenceKind::Offline), |
| 917 | (LEA_ID, PresenceKind::Busy), |
| 918 | (DEMO_USER_ID, PresenceKind::Available), |
| 919 | ] { |
| 920 | directory.set_presence(user_id, kind); |
| 921 | } |
| 922 | directory.set_activity(JONAS_ID, Some(Activity::InACall)); |
| 923 | directory.set_activity(LEA_ID, Some(Activity::InAConferenceCall)); |
| 924 | let directory_path = image_directory(); |
| 925 | if std::fs::create_dir_all(&directory_path).is_ok() { |
| 926 | let path = directory_path.join("staging-dashboard.png"); |
| 927 | let png = encode_rgb(STAGING_SIZE.0, STAGING_SIZE.1, staging_pixel); |
| 928 | if std::fs::write(&path, png).is_ok() { |
| 929 | state.directory.set_image(STAGING_IMAGE_KEY, &path); |
| 930 | state.directory.set_image(LINK_IMAGE_KEY, &path); |
| 931 | } |
| 932 | let _ = SEARCHABLE_GIFS.set(demo_gifs::generate(&directory_path)); |
| 933 | state.directory.set_image( |
| 934 | demo_gifs::RECEIVED_GIF_KEY, |
| 935 | &directory_path.join(demo_gifs::file_name(demo_gifs::RECEIVED_GIF_NAME)), |
| 936 | ); |
| 937 | } |
| 938 | state.typing.start( |
| 939 | "demo-chat-mara", |
| 940 | MARA_ID, |
| 941 | "Mara Lindqvist", |
| 942 | Instant::now(), |
| 943 | Utc::now(), |
| 944 | ); |
| 945 | state.collapsed.insert(CUSTOMERS_FOLDER.to_owned()); |
| 946 | state.last_sync = Some(Utc::now()); |
| 947 | state.apply_bot_titles(); |
| 948 | seed_message_actions(state); |
| 949 | } |
| 950 | |
| 951 | fn seed_message_actions(state: &mut AppState) { |
| 952 | state.pins.insert( |
| 953 | RELEASE_CHAT.to_owned(), |
| 954 | vec![PinnedMessage { |
| 955 | message_id: PINNED_MESSAGE.to_owned(), |
| 956 | pinned_at: Some(at(0, 11, 30)), |
| 957 | parent_id: None, |
| 958 | }], |
| 959 | ); |
| 960 | let ids: Vec<String> = SAVED_MESSAGES.iter().map(|id| (*id).to_owned()).collect(); |
| 961 | let records = state |
| 962 | .store |
| 963 | .messages_by_id(RELEASE_CHAT, &ids) |
| 964 | .unwrap_or_default(); |
| 965 | let saved = SAVED_MESSAGES |
| 966 | .iter() |
| 967 | .enumerate() |
| 968 | .filter_map(|(position, id)| { |
| 969 | let record = records.get(*id)?; |
| 970 | Some(SavedMessage { |
| 971 | conversation_id: record.conversation_id.clone(), |
| 972 | message_id: record.message_id.clone(), |
| 973 | root_id: record.message_id.clone(), |
| 974 | author_id: record.sender_id.clone(), |
| 975 | author_name: record.sender_name.clone(), |
| 976 | preview: crate::rows::reply_excerpt(record), |
| 977 | saved_at: at(position as i64, 15, 20), |
| 978 | topic: None, |
| 979 | }) |
| 980 | }) |
| 981 | .collect(); |
| 982 | state.saved.replace(saved); |
| 983 | } |
| 984 | |
| 985 | pub fn forwarded_message( |
| 986 | chat: &ChatRecord, |
| 987 | source: &ForwardSource, |
| 988 | comment: &str, |
| 989 | now: DateTime<Utc>, |
| 990 | ) -> (ChatRecord, MessageRecord) { |
| 991 | let comment_html = if comment.is_empty() { |
| 992 | String::new() |
| 993 | } else { |
| 994 | format!("<p>{}</p>", teams_core::escape_html(comment)) |
| 995 | }; |
| 996 | let html = format!( |
| 997 | "{comment_html}<blockquote itemscope=\"\" itemtype=\"{FORWARD_ITEMTYPE}\" itemid=\"{}\"><strong>{}</strong><p>{}</p></blockquote>", |
| 998 | teams_core::escape_html(&source.message_id), |
| 999 | teams_core::escape_html(&source.author), |
| 1000 | teams_core::escape_html(&source.text), |
| 1001 | ); |
| 1002 | send_in_chat(chat, &source.text, &html, now) |
| 1003 | } |
| 1004 | |
| 1005 | #[allow(clippy::too_many_arguments)] |
| 1006 | fn message( |
| 1007 | conversation: &str, |
| 1008 | id: &str, |
| 1009 | reply_to: Option<&str>, |
| 1010 | sender: (&str, &str), |
| 1011 | time: DateTime<Utc>, |
| 1012 | html: &str, |
| 1013 | reactions_json: &str, |
| 1014 | edited: bool, |
| 1015 | ) -> MessageRecord { |
| 1016 | MessageRecord { |
| 1017 | conversation_id: conversation.to_owned(), |
| 1018 | message_id: id.to_owned(), |
| 1019 | reply_to_id: reply_to.map(str::to_owned), |
| 1020 | sender_id: Some(sender.0.to_owned()), |
| 1021 | sender_name: Some(sender.1.to_owned()), |
| 1022 | sender_application_id: None, |
| 1023 | links_json: "[]".to_owned(), |
| 1024 | created_at: time, |
| 1025 | edited_at: edited.then_some(time + Duration::minutes(2)), |
| 1026 | deleted: false, |
| 1027 | body_html: html.to_owned(), |
| 1028 | attachments_json: "[]".to_owned(), |
| 1029 | reactions_json: reactions_json.to_owned(), |
| 1030 | mentions_json: "[]".to_owned(), |
| 1031 | subject: None, |
| 1032 | } |
| 1033 | } |
| 1034 | |
| 1035 | fn with_subject(mut record: MessageRecord, subject: &str) -> MessageRecord { |
| 1036 | record.subject = Some(subject.to_owned()); |
| 1037 | record |
| 1038 | } |
| 1039 | |
| 1040 | fn with_attachments(mut record: MessageRecord, attachments_json: &str) -> MessageRecord { |
| 1041 | record.attachments_json = attachments_json.to_owned(); |
| 1042 | record |
| 1043 | } |
| 1044 | |
| 1045 | fn with_links(mut record: MessageRecord, links_json: String) -> MessageRecord { |
| 1046 | record.links_json = links_json; |
| 1047 | record |
| 1048 | } |
| 1049 | |
| 1050 | pub fn link_preview(url: &str) -> Option<LinkPreview> { |
| 1051 | Some(LinkPreview { |
| 1052 | url: url.to_owned(), |
| 1053 | title: Some("Release checklist Q4".to_owned()), |
| 1054 | description: Some( |
| 1055 | "Everything that has to be green before the release candidate goes out on Friday." |
| 1056 | .to_owned(), |
| 1057 | ), |
| 1058 | image_url: Some(LINK_IMAGE_KEY.to_owned()), |
| 1059 | image_width: Some(LINK_IMAGE_SIZE.0), |
| 1060 | image_height: Some(LINK_IMAGE_SIZE.1), |
| 1061 | }) |
| 1062 | } |
| 1063 | |
| 1064 | fn bot_messages() -> Vec<MessageRecord> { |
| 1065 | let card = serde_json::json!({ |
| 1066 | "type": "AdaptiveCard", |
| 1067 | "body": [ |
| 1068 | {"type": "ColumnSet", "columns": [ |
| 1069 | {"type": "Column", "width": "auto", "verticalContentAlignment": "center", "items": [ |
| 1070 | {"type": "Image", "url": CARD_PERSON_URL, "size": "small", "style": "person", "height": "32px"} |
| 1071 | ]}, |
| 1072 | {"type": "Column", "width": "stretch", "verticalContentAlignment": "center", "items": [ |
| 1073 | {"type": "TextBlock", "size": "medium", "weight": "bolder", "wrap": true, "text": "Mara Lindqvist edited your page"} |
| 1074 | ]} |
| 1075 | ]}, |
| 1076 | {"type": "ColumnSet", "separator": true, "columns": [ |
| 1077 | {"type": "Column", "width": "auto", "items": [ |
| 1078 | {"type": "Image", "url": CARD_PAGE_URL, "width": "24px", "height": "24px"} |
| 1079 | ]}, |
| 1080 | {"type": "Column", "width": "stretch", "items": [ |
| 1081 | {"type": "TextBlock", "weight": "bolder", "wrap": true, "text": "[**Release Checklist Q4**](https://example.com/wiki/release-checklist) in [**Platform**](https://example.com/wiki/platform)"}, |
| 1082 | {"type": "TextBlock", "spacing": "small", "isSubtle": true, "wrap": true, "text": "Owned by: Dana Demo"} |
| 1083 | ]} |
| 1084 | ]}, |
| 1085 | {"type": "TextBlock", "id": "owner-note", "isVisible": false, "isSubtle": true, "wrap": true, "text": "Owner notes are only shown on request."}, |
| 1086 | {"type": "ActionSet", "actions": [ |
| 1087 | {"type": "Action.OpenUrl", "title": "View page", "url": "https://example.com/wiki/release-checklist"}, |
| 1088 | {"type": "Action.OpenUrl", "title": "View changes", "url": "https://example.com/wiki/release-checklist/changes"}, |
| 1089 | {"type": "Action.Submit", "title": "Watch page", "data": {"action": "watch"}}, |
| 1090 | {"type": "Action.Submit", "title": "Stop watching", "data": {"action": "unwatch"}}, |
| 1091 | {"type": "Action.Submit", "title": "Settings", "data": {"msteams": {"type": "task/fetch"}}}, |
| 1092 | {"type": "Action.ToggleVisibility", "title": "Owner notes", "targetElements": ["owner-note"]}, |
| 1093 | {"type": "Action.ShowCard", "title": "Details", "card": {"body": [ |
| 1094 | {"type": "TextBlock", "wrap": true, "text": "Last edited by **Mara Lindqvist** on Tuesday."} |
| 1095 | ]}} |
| 1096 | ]} |
| 1097 | ] |
| 1098 | }); |
| 1099 | let mut messages = vec![ |
| 1100 | bot_card_message("b1", at(0, 13, 45), &card), |
| 1101 | bot_card_message("b2", at(0, 13, 52), &input_card()), |
| 1102 | bot_card_message( |
| 1103 | "b3", |
| 1104 | at(0, 13, 58), |
| 1105 | &crate::demo_input_cards::all_inputs_card(), |
| 1106 | ), |
| 1107 | ]; |
| 1108 | messages.extend( |
| 1109 | crate::demo_cards::behaviour_cards() |
| 1110 | .iter() |
| 1111 | .zip(0u32..) |
| 1112 | .map(|(card, index)| bot_card_message(&format!("g{index}"), at(0, 14, index), card)), |
| 1113 | ); |
| 1114 | messages.extend(crate::demo_chart_cards::cards().iter().zip(0..).map( |
| 1115 | |(chart_card, minute)| { |
| 1116 | bot_card_message(&format!("c{minute}"), at(0, 15, minute), chart_card) |
| 1117 | }, |
| 1118 | )); |
| 1119 | messages |
| 1120 | } |
| 1121 | |
| 1122 | fn bot_card_message(id: &str, time: DateTime<Utc>, card: &serde_json::Value) -> MessageRecord { |
| 1123 | let attachments = serde_json::json!([{ |
| 1124 | "content_type": "application/vnd.microsoft.card.adaptive", |
| 1125 | "name": null, |
| 1126 | "url": null, |
| 1127 | "text": null, |
| 1128 | "content": card.to_string() |
| 1129 | }]); |
| 1130 | let mut record = with_attachments( |
| 1131 | message( |
| 1132 | BOT_CHAT, |
| 1133 | id, |
| 1134 | None, |
| 1135 | ("", BOT_NAME), |
| 1136 | time, |
| 1137 | &format!("<attachment id=\"{id}\"></attachment>"), |
| 1138 | "[]", |
| 1139 | false, |
| 1140 | ), |
| 1141 | &attachments.to_string(), |
| 1142 | ); |
| 1143 | record.sender_id = None; |
| 1144 | record.sender_application_id = Some(BOT_ID.to_owned()); |
| 1145 | record |
| 1146 | } |
| 1147 | |
| 1148 | fn input_card() -> serde_json::Value { |
| 1149 | serde_json::json!({ |
| 1150 | "type": "AdaptiveCard", |
| 1151 | "body": [ |
| 1152 | {"type": "TextBlock", "size": "medium", "weight": "bolder", "text": "Release request"}, |
| 1153 | {"type": "Input.Text", "id": "title", "label": "Title", "placeholder": "Short summary", "isRequired": true, "errorMessage": "Give the release a title"}, |
| 1154 | {"type": "Input.Text", "id": "notes", "label": "Notes", "placeholder": "Anything the reviewers should know", "isMultiline": true}, |
| 1155 | {"type": "Input.Text", "id": "token", "label": "Access code", "style": "Password", "regex": "^\\d{4}$", "errorMessage": "Enter four digits"}, |
| 1156 | {"type": "ColumnSet", "columns": [ |
| 1157 | {"type": "Column", "width": "stretch", "items": [ |
| 1158 | {"type": "Input.Number", "id": "count", "label": "Servers", "placeholder": "1-20", "min": 1, "max": 20, "value": 4} |
| 1159 | ]}, |
| 1160 | {"type": "Column", "width": "stretch", "items": [ |
| 1161 | {"type": "Input.Date", "id": "day", "label": "Release day", "value": "2026-11-02"} |
| 1162 | ]}, |
| 1163 | {"type": "Column", "width": "stretch", "items": [ |
| 1164 | {"type": "Input.Time", "id": "hour", "label": "Start time", "value": "09:30"} |
| 1165 | ]} |
| 1166 | ]}, |
| 1167 | {"type": "Input.ChoiceSet", "id": "channel", "label": "Channel", "placeholder": "Pick a channel", "isRequired": true, "choices": [ |
| 1168 | {"title": "Stable", "value": "stable"}, {"title": "Beta", "value": "beta"}, {"title": "Nightly", "value": "nightly"} |
| 1169 | ]}, |
| 1170 | {"type": "Input.ChoiceSet", "id": "region", "label": "Region", "style": "expanded", "value": "eu", "choices": [ |
| 1171 | {"title": "Europe", "value": "eu"}, {"title": "North America", "value": "na"} |
| 1172 | ]}, |
| 1173 | {"type": "Input.ChoiceSet", "id": "checks", "label": "Checks", "style": "expanded", "isMultiSelect": true, "value": "lint", "choices": [ |
| 1174 | {"title": "Lint", "value": "lint"}, {"title": "Unit tests", "value": "unit"}, {"title": "Smoke tests", "value": "smoke"} |
| 1175 | ]}, |
| 1176 | {"type": "Input.Toggle", "id": "notify", "title": "Notify the team", "value": "yes", "valueOn": "yes", "valueOff": "no"} |
| 1177 | ], |
| 1178 | "actions": [ |
| 1179 | {"type": "Action.Submit", "title": "Request release", "data": {"action": "release"}}, |
| 1180 | {"type": "Action.Submit", "title": "Cancel", "associatedInputs": "none", "data": {"action": "cancel"}} |
| 1181 | ] |
| 1182 | }) |
| 1183 | } |
| 1184 | |
| 1185 | fn bot_app() -> ChatApp { |
| 1186 | ChatApp { |
| 1187 | app_id: "demo-app-wiki".to_owned(), |
| 1188 | name: BOT_APP_NAME.to_owned(), |
| 1189 | bot_ids: vec![BOT_ID.to_owned()], |
| 1190 | small_image_url: None, |
| 1191 | accent_color: None, |
| 1192 | web_application_resource: None, |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | pub async fn card_answer(task: CardTask) -> CardAnswer { |
| 1197 | tokio::time::sleep(CARD_ANSWER_DELAY).await; |
| 1198 | let data = match task { |
| 1199 | CardTask::Submit(_) => { |
| 1200 | return Ok(( |
| 1201 | CardActionOutcome::Message("Settings saved".into()), |
| 1202 | Some((bot_app(), BOT_ID.to_owned())), |
| 1203 | )); |
| 1204 | } |
| 1205 | CardTask::Action(action) => match action.invoke_payload() { |
| 1206 | Some(payload) => payload.value, |
| 1207 | None => return Err("Not supported".to_owned()), |
| 1208 | }, |
| 1209 | }; |
| 1210 | let outcome = if data.pointer("/data/type").and_then(|kind| kind.as_str()) == Some("task/fetch") |
| 1211 | { |
| 1212 | CardActionOutcome::Dialog(TaskDialog { |
| 1213 | title: None, |
| 1214 | width: 520, |
| 1215 | height: 300, |
| 1216 | kind: TaskDialogKind::Card(settings_card().to_string()), |
| 1217 | }) |
| 1218 | } else if data.get("action").and_then(|action| action.as_str()) == Some("unwatch") { |
| 1219 | CardActionOutcome::Failed("The app answered 500: demo failure".into()) |
| 1220 | } else { |
| 1221 | CardActionOutcome::Sent |
| 1222 | }; |
| 1223 | Ok((outcome, Some((bot_app(), BOT_ID.to_owned())))) |
| 1224 | } |
| 1225 | |
| 1226 | pub async fn refresh_answer() -> CardAnswer { |
| 1227 | tokio::time::sleep(CARD_ANSWER_DELAY).await; |
| 1228 | Ok(( |
| 1229 | CardActionOutcome::ReplaceCard(crate::demo_cards::refreshed_card().to_string()), |
| 1230 | Some((bot_app(), BOT_ID.to_owned())), |
| 1231 | )) |
| 1232 | } |
| 1233 | |
| 1234 | fn settings_card() -> serde_json::Value { |
| 1235 | serde_json::json!({ |
| 1236 | "type": "AdaptiveCard", |
| 1237 | "body": [ |
| 1238 | {"type": "TextBlock", "weight": "bolder", "text": "Notification settings"}, |
| 1239 | {"type": "TextBlock", "wrap": true, "isSubtle": true, "text": "Choose how the bot tells you about page changes."}, |
| 1240 | {"type": "Input.ChoiceSet", "id": "frequency", "label": "Frequency", "style": "expanded", "value": "daily", "choices": [ |
| 1241 | {"title": "Immediately", "value": "now"}, {"title": "Daily digest", "value": "daily"} |
| 1242 | ]}, |
| 1243 | {"type": "Input.Toggle", "id": "mentions", "title": "Only when I am mentioned", "value": "false"} |
| 1244 | ], |
| 1245 | "actions": [ |
| 1246 | {"type": "Action.Submit", "title": "Save", "data": {"save": true}} |
| 1247 | ] |
| 1248 | }) |
| 1249 | } |
| 1250 | |
| 1251 | const DEMO_MEETING_LINK: &str = "https://teams.microsoft.com/l/meetup-join/19%3ameeting_ZGVtbw%40thread.v2/0?context=%7b%22Tid%22%3a%22demo-tenant%22%2c%22Oid%22%3a%22demo-mara%22%7d"; |
| 1252 | |
| 1253 | fn meeting_link_messages() -> Vec<MessageRecord> { |
| 1254 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 1255 | vec![message( |
| 1256 | "demo-chat-mara", |
| 1257 | "ml1", |
| 1258 | None, |
| 1259 | mara, |
| 1260 | at(0, 13, 38), |
| 1261 | &format!( |
| 1262 | "<p>Let's sync live, here is the meeting: <a href=\"{DEMO_MEETING_LINK}\">Join the Teams meeting</a></p>" |
| 1263 | ), |
| 1264 | "[]", |
| 1265 | false, |
| 1266 | )] |
| 1267 | } |
| 1268 | |
| 1269 | fn standup_messages() -> Vec<MessageRecord> { |
| 1270 | let jonas = (JONAS_ID, "Jonas Ortega"); |
| 1271 | let priya = (PRIYA_ID, "Priya Nair"); |
| 1272 | vec![ |
| 1273 | with_attachments( |
| 1274 | message( |
| 1275 | "demo-chat-meeting-standup", |
| 1276 | "su1", |
| 1277 | None, |
| 1278 | priya, |
| 1279 | at(0, 9, 30), |
| 1280 | "<p>Notes from today.</p>", |
| 1281 | "[]", |
| 1282 | false, |
| 1283 | ), |
| 1284 | r#"[{"content_type":"reference","name":"Standup-notes.docx","url":"https://example.com/files/standup-notes.docx","text":null,"size":38900}]"#, |
| 1285 | ), |
| 1286 | message( |
| 1287 | "demo-chat-meeting-standup", |
| 1288 | "su2", |
| 1289 | None, |
| 1290 | jonas, |
| 1291 | at(0, 9, 45), |
| 1292 | "<p>Recording is available</p>", |
| 1293 | "[]", |
| 1294 | false, |
| 1295 | ), |
| 1296 | with_links( |
| 1297 | message( |
| 1298 | "demo-chat-meeting-standup", |
| 1299 | "su3", |
| 1300 | None, |
| 1301 | jonas, |
| 1302 | at(0, 9, 50), |
| 1303 | "<p><a href=\"https://example.com/checklist\">https://example.com/checklist</a></p>", |
| 1304 | "[]", |
| 1305 | false, |
| 1306 | ), |
| 1307 | link_preview("https://example.com/checklist") |
| 1308 | .map(|preview| preview.links_json()) |
| 1309 | .unwrap_or_default(), |
| 1310 | ), |
| 1311 | message( |
| 1312 | "demo-chat-meeting-standup", |
| 1313 | "su4", |
| 1314 | None, |
| 1315 | priya, |
| 1316 | at(0, 9, 52), |
| 1317 | "<p>I will quickly fix the comments and then push the final version before the review starts.</p>", |
| 1318 | "[]", |
| 1319 | false, |
| 1320 | ), |
| 1321 | ] |
| 1322 | } |
| 1323 | |
| 1324 | fn priority_messages() -> Vec<MessageRecord> { |
| 1325 | let priya = (PRIYA_ID, "Priya Nair"); |
| 1326 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 1327 | vec![ |
| 1328 | message( |
| 1329 | PRIORITY_CHAT, |
| 1330 | "p1", |
| 1331 | None, |
| 1332 | me, |
| 1333 | at(0, 10, 40), |
| 1334 | "<p>Have you seen the error on staging yet?</p>", |
| 1335 | "[]", |
| 1336 | false, |
| 1337 | ), |
| 1338 | message( |
| 1339 | PRIORITY_CHAT, |
| 1340 | "p2", |
| 1341 | None, |
| 1342 | priya, |
| 1343 | at(0, 10, 47), |
| 1344 | &format!( |
| 1345 | "<p>Yes, here is the screenshot:</p><img src=\"{PENDING_IMAGE_KEY}\" width=\"1280\" height=\"720\">" |
| 1346 | ), |
| 1347 | "[]", |
| 1348 | false, |
| 1349 | ), |
| 1350 | ] |
| 1351 | } |
| 1352 | |
| 1353 | fn reaction(kind: &str, users: &[(&str, &str, DateTime<Utc>)]) -> Vec<serde_json::Value> { |
| 1354 | users |
| 1355 | .iter() |
| 1356 | .map(|(user_id, user_name, created_at)| { |
| 1357 | serde_json::json!({ |
| 1358 | "reaction_type": kind, |
| 1359 | "user_id": user_id, |
| 1360 | "user_name": user_name, |
| 1361 | "created_at": created_at, |
| 1362 | }) |
| 1363 | }) |
| 1364 | .collect() |
| 1365 | } |
| 1366 | |
| 1367 | fn reactions_json(groups: Vec<Vec<serde_json::Value>>) -> String { |
| 1368 | serde_json::Value::Array(groups.into_iter().flatten().collect()).to_string() |
| 1369 | } |
| 1370 | |
| 1371 | fn release_messages() -> Vec<MessageRecord> { |
| 1372 | let chat = RELEASE_CHAT; |
| 1373 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 1374 | let jonas = (JONAS_ID, "Jonas Ortega"); |
| 1375 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 1376 | let jonas_name = "Jonas Ortega"; |
| 1377 | let priya_name = "Priya Nair"; |
| 1378 | let lea_name = "Lea Schneider"; |
| 1379 | let mara_name = "Mara Lindqvist"; |
| 1380 | let tobias_name = "Tobias Klein"; |
| 1381 | let likes = reactions_json(vec![reaction( |
| 1382 | "like", |
| 1383 | &[ |
| 1384 | (JONAS_ID, jonas_name, at(1, 16, 30)), |
| 1385 | (PRIYA_ID, priya_name, at(1, 17, 2)), |
| 1386 | (LEA_ID, lea_name, at(0, 8, 15)), |
| 1387 | ], |
| 1388 | )]); |
| 1389 | let hearts = reactions_json(vec![reaction( |
| 1390 | "heart", |
| 1391 | &[ |
| 1392 | (DEMO_USER_ID, DEMO_USER_NAME, at(0, 10, 35)), |
| 1393 | (JONAS_ID, jonas_name, at(0, 10, 33)), |
| 1394 | ], |
| 1395 | )]); |
| 1396 | let single = reactions_json(vec![reaction( |
| 1397 | "laugh", |
| 1398 | &[(PRIYA_ID, priya_name, at(0, 9, 10))], |
| 1399 | )]); |
| 1400 | let many = reactions_json(vec![ |
| 1401 | reaction( |
| 1402 | "like", |
| 1403 | &[ |
| 1404 | (MARA_ID, mara_name, at(0, 13, 37)), |
| 1405 | (JONAS_ID, jonas_name, at(0, 13, 38)), |
| 1406 | (PRIYA_ID, priya_name, at(0, 13, 40)), |
| 1407 | (LEA_ID, lea_name, at(0, 13, 45)), |
| 1408 | (TOBIAS_ID, tobias_name, at(0, 13, 50)), |
| 1409 | ], |
| 1410 | ), |
| 1411 | reaction( |
| 1412 | "heart", |
| 1413 | &[ |
| 1414 | (LEA_ID, lea_name, at(0, 13, 46)), |
| 1415 | (TOBIAS_ID, tobias_name, at(0, 13, 51)), |
| 1416 | ], |
| 1417 | ), |
| 1418 | reaction("\u{1F389}", &[(MARA_ID, mara_name, at(0, 13, 39))]), |
| 1419 | ]); |
| 1420 | vec![ |
| 1421 | message( |
| 1422 | chat, |
| 1423 | "m1", |
| 1424 | None, |
| 1425 | mara, |
| 1426 | at(1, 16, 20), |
| 1427 | "<p>Release candidate is on staging. Can someone run the regression suite?</p>", |
| 1428 | "[]", |
| 1429 | false, |
| 1430 | ), |
| 1431 | message( |
| 1432 | chat, |
| 1433 | "m2", |
| 1434 | None, |
| 1435 | mara, |
| 1436 | at(1, 16, 22), |
| 1437 | "<p>Checklist lives in <a href=\"https://example.com/checklist\">release checklist</a>.</p>", |
| 1438 | &likes, |
| 1439 | false, |
| 1440 | ), |
| 1441 | with_links( |
| 1442 | message( |
| 1443 | chat, |
| 1444 | "m2a", |
| 1445 | None, |
| 1446 | mara, |
| 1447 | at(1, 16, 23), |
| 1448 | "<p><a href=\"https://example.com/checklist\">https://example.com/checklist</a></p>", |
| 1449 | "[]", |
| 1450 | false, |
| 1451 | ), |
| 1452 | link_preview("https://example.com/checklist") |
| 1453 | .map(|preview| preview.links_json()) |
| 1454 | .unwrap_or_default(), |
| 1455 | ), |
| 1456 | message( |
| 1457 | chat, |
| 1458 | "m2b", |
| 1459 | None, |
| 1460 | mara, |
| 1461 | at(1, 16, 40), |
| 1462 | &format!( |
| 1463 | "<p>Staging dashboard right now:</p><img src=\"{STAGING_IMAGE_KEY}\" width=\"{}\" height=\"{}\">", |
| 1464 | STAGING_SIZE.0, STAGING_SIZE.1 |
| 1465 | ), |
| 1466 | "[]", |
| 1467 | false, |
| 1468 | ), |
| 1469 | with_attachments( |
| 1470 | message( |
| 1471 | chat, |
| 1472 | "m2c", |
| 1473 | None, |
| 1474 | mara, |
| 1475 | at(1, 16, 41), |
| 1476 | "<p>Checklist and rollout plan attached.</p>", |
| 1477 | "[]", |
| 1478 | false, |
| 1479 | ), |
| 1480 | 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}]"#, |
| 1481 | ), |
| 1482 | message( |
| 1483 | chat, |
| 1484 | "m3", |
| 1485 | None, |
| 1486 | me, |
| 1487 | at(0, 9, 2), |
| 1488 | "<p>I'll take the changelog.</p>", |
| 1489 | &single, |
| 1490 | false, |
| 1491 | ), |
| 1492 | message( |
| 1493 | chat, |
| 1494 | "m4", |
| 1495 | None, |
| 1496 | jonas, |
| 1497 | at(0, 10, 14), |
| 1498 | "<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>", |
| 1499 | "[]", |
| 1500 | false, |
| 1501 | ), |
| 1502 | message( |
| 1503 | chat, |
| 1504 | "m5", |
| 1505 | None, |
| 1506 | mara, |
| 1507 | at(0, 10, 31), |
| 1508 | "<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>", |
| 1509 | &hearts, |
| 1510 | true, |
| 1511 | ), |
| 1512 | message( |
| 1513 | chat, |
| 1514 | "m5b", |
| 1515 | None, |
| 1516 | mara, |
| 1517 | at(0, 10, 40), |
| 1518 | "<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>", |
| 1519 | "[]", |
| 1520 | false, |
| 1521 | ), |
| 1522 | message( |
| 1523 | chat, |
| 1524 | "m5c", |
| 1525 | None, |
| 1526 | jonas, |
| 1527 | at(0, 11, 5), |
| 1528 | "<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>", |
| 1529 | "[]", |
| 1530 | false, |
| 1531 | ), |
| 1532 | message( |
| 1533 | chat, |
| 1534 | "m5d", |
| 1535 | None, |
| 1536 | (PRIYA_ID, priya_name), |
| 1537 | at(0, 12, 20), |
| 1538 | &format!( |
| 1539 | "<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>" |
| 1540 | ), |
| 1541 | "[]", |
| 1542 | false, |
| 1543 | ), |
| 1544 | message( |
| 1545 | chat, |
| 1546 | "m6", |
| 1547 | None, |
| 1548 | me, |
| 1549 | at(0, 13, 35), |
| 1550 | "<p>Thanks, renamed. Tagging now.</p>", |
| 1551 | "[]", |
| 1552 | false, |
| 1553 | ), |
| 1554 | message( |
| 1555 | chat, |
| 1556 | "m7", |
| 1557 | None, |
| 1558 | me, |
| 1559 | at(0, 13, 36), |
| 1560 | "<p>Build 42 is green, see <a href=\"https://example.com/build/42\">pipeline</a>.</p>", |
| 1561 | &many, |
| 1562 | false, |
| 1563 | ), |
| 1564 | with_links( |
| 1565 | message( |
| 1566 | chat, |
| 1567 | "m7a", |
| 1568 | None, |
| 1569 | me, |
| 1570 | at(0, 13, 37), |
| 1571 | "<p>Notes are in <a href=\"https://example.com/notes\">https://example.com/notes</a></p>", |
| 1572 | "[]", |
| 1573 | false, |
| 1574 | ), |
| 1575 | LinkPreview { |
| 1576 | url: "https://example.com/notes".to_owned(), |
| 1577 | title: Some("Release notes 0.2".to_owned()), |
| 1578 | description: None, |
| 1579 | image_url: None, |
| 1580 | image_width: None, |
| 1581 | image_height: None, |
| 1582 | } |
| 1583 | .links_json(), |
| 1584 | ), |
| 1585 | message( |
| 1586 | chat, |
| 1587 | "m8", |
| 1588 | None, |
| 1589 | jonas, |
| 1590 | at(0, 13, 40), |
| 1591 | "<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>", |
| 1592 | "[]", |
| 1593 | false, |
| 1594 | ), |
| 1595 | message( |
| 1596 | chat, |
| 1597 | "m9", |
| 1598 | None, |
| 1599 | (PRIYA_ID, priya_name), |
| 1600 | at(0, 13, 45), |
| 1601 | &format!( |
| 1602 | "<p>Ship it!</p><img src=\"{}\" width=\"{}\" height=\"{}\" alt=\"Party\" itemtype=\"http://schema.skype.com/Giphy\">", |
| 1603 | demo_gifs::RECEIVED_GIF_KEY, |
| 1604 | demo_gifs::RECEIVED_GIF_SIZE.0, |
| 1605 | demo_gifs::RECEIVED_GIF_SIZE.1 |
| 1606 | ), |
| 1607 | "[]", |
| 1608 | false, |
| 1609 | ), |
| 1610 | message( |
| 1611 | chat, |
| 1612 | FRENCH_MESSAGE, |
| 1613 | None, |
| 1614 | (LEA_ID, lea_name), |
| 1615 | at(0, 13, 47), |
| 1616 | "<p>Bonjour \u{e0} tous, la <strong>version 0.2</strong> est pr\u{ea}te. Pouvez-vous v\u{e9}rifier la liste de contr\u{f4}le avant vendredi\u{a0}?</p>", |
| 1617 | "[]", |
| 1618 | false, |
| 1619 | ), |
| 1620 | message( |
| 1621 | chat, |
| 1622 | SPANISH_MESSAGE, |
| 1623 | None, |
| 1624 | jonas, |
| 1625 | at(0, 13, 49), |
| 1626 | "<p>Gracias por el aviso. Probar\u{e9} la compilaci\u{f3}n esta tarde y os dir\u{e9} si algo falla.</p>", |
| 1627 | "[]", |
| 1628 | false, |
| 1629 | ), |
| 1630 | ] |
| 1631 | } |
| 1632 | |
| 1633 | fn unread_messages() -> Vec<MessageRecord> { |
| 1634 | let me = (DEMO_USER_ID, DEMO_USER_NAME); |
| 1635 | let lea = (LEA_ID, "Lea Schneider"); |
| 1636 | let tobias = (TOBIAS_ID, "Tobias Klein"); |
| 1637 | let rows = [ |
| 1638 | ( |
| 1639 | "u1", |
| 1640 | me, |
| 1641 | at(1, 17, 5), |
| 1642 | "<p>Can you enable access for North?</p>", |
| 1643 | ), |
| 1644 | ("u2", tobias, at(1, 17, 20), "<p>I'll check with IT.</p>"), |
| 1645 | ( |
| 1646 | "u3", |
| 1647 | lea, |
| 1648 | at(0, 12, 40), |
| 1649 | "<p>Good morning, access is ready.</p>", |
| 1650 | ), |
| 1651 | ( |
| 1652 | "u4", |
| 1653 | lea, |
| 1654 | at(0, 12, 41), |
| 1655 | "<p>Sign in with your company ID, no password needed.</p>", |
| 1656 | ), |
| 1657 | ("u5", lea, at(0, 12, 50), "<p>Access is enabled now</p>"), |
| 1658 | ]; |
| 1659 | rows.into_iter() |
| 1660 | .map(|(id, sender, time, html)| { |
| 1661 | message(UNREAD_CHAT, id, None, sender, time, html, "[]", false) |
| 1662 | }) |
| 1663 | .collect() |
| 1664 | } |
| 1665 | |
| 1666 | const DEMO_EXTRA_TABS_CHANNEL: &str = "demo-channel-1-2"; |
| 1667 | const DEMO_EXTRA_TABS: [&str; 6] = [ |
| 1668 | "Incident dashboard", |
| 1669 | "Postmortems", |
| 1670 | "Escalation policy", |
| 1671 | "Status page", |
| 1672 | "Vendor contacts", |
| 1673 | "On-call calendar", |
| 1674 | ]; |
| 1675 | |
| 1676 | fn demo_tab( |
| 1677 | channel_id: &str, |
| 1678 | name: &str, |
| 1679 | definition_id: &str, |
| 1680 | open_url: Option<&str>, |
| 1681 | ) -> ChannelTabRecord { |
| 1682 | ChannelTabRecord { |
| 1683 | tab_id: format!("{channel_id}-{}", name.to_lowercase().replace(' ', "-")), |
| 1684 | name: name.to_owned(), |
| 1685 | definition_id: definition_id.to_owned(), |
| 1686 | open_url: open_url.map(str::to_owned), |
| 1687 | } |
| 1688 | } |
| 1689 | |
| 1690 | fn demo_channel_tabs(channel_id: &str) -> Vec<ChannelTabRecord> { |
| 1691 | let mut tabs = vec![ |
| 1692 | demo_tab( |
| 1693 | channel_id, |
| 1694 | "Runbook", |
| 1695 | "com.microsoft.teamspace.tab.web", |
| 1696 | Some("https://wiki.example.com/display/TEAM/Runbook"), |
| 1697 | ), |
| 1698 | demo_tab( |
| 1699 | channel_id, |
| 1700 | "Sprint board", |
| 1701 | "com.microsoft.teamspace.tab.planner", |
| 1702 | None, |
| 1703 | ), |
| 1704 | demo_tab( |
| 1705 | channel_id, |
| 1706 | "Team notes", |
| 1707 | "0d820ecd-def2-4297-adad-78056cde7c78", |
| 1708 | Some("https://onenote.example.com/notes"), |
| 1709 | ), |
| 1710 | ]; |
| 1711 | if channel_id == DEMO_EXTRA_TABS_CHANNEL { |
| 1712 | tabs.extend(DEMO_EXTRA_TABS.iter().map(|name| { |
| 1713 | demo_tab( |
| 1714 | channel_id, |
| 1715 | name, |
| 1716 | "com.microsoft.teamspace.tab.web", |
| 1717 | Some("https://wiki.example.com/display/TEAM/Page"), |
| 1718 | ) |
| 1719 | })); |
| 1720 | } |
| 1721 | tabs |
| 1722 | } |
| 1723 | |
| 1724 | fn demo_team_layout(channels: &[ChannelRecord]) -> Vec<TeamLayoutRecord> { |
| 1725 | DEMO_TEAM_ORDER |
| 1726 | .iter() |
| 1727 | .map(|team_id| TeamLayoutRecord { |
| 1728 | team_id: (*team_id).to_owned(), |
| 1729 | hidden: *team_id == DEMO_HIDDEN_TEAM, |
| 1730 | channels: channels |
| 1731 | .iter() |
| 1732 | .filter(|channel| channel.team_id == *team_id) |
| 1733 | .map(|channel| ChannelLayoutRecord { |
| 1734 | channel_id: channel.id.clone(), |
| 1735 | general: channel.name == "General", |
| 1736 | hidden: channel.id == DEMO_HIDDEN_CHANNEL, |
| 1737 | tabs: demo_channel_tabs(&channel.id), |
| 1738 | notifications: DEMO_CHANNEL_NOTIFICATIONS |
| 1739 | .iter() |
| 1740 | .find(|(id, _, _)| *id == channel.id) |
| 1741 | .map(|(_, level, include_replies)| ChannelNotifications { |
| 1742 | level: *level, |
| 1743 | include_replies: *include_replies, |
| 1744 | }) |
| 1745 | .unwrap_or_default(), |
| 1746 | }) |
| 1747 | .collect(), |
| 1748 | }) |
| 1749 | .collect() |
| 1750 | } |
| 1751 | |
| 1752 | const ROLLOUT_REPLIES: [(&str, &str); 12] = [ |
| 1753 | (MARA_ID, "Staging looks good on my side."), |
| 1754 | (JONAS_ID, "Smoke tests passed, no regressions."), |
| 1755 | (PRIYA_ID, "Docs for the new settings page are merged."), |
| 1756 | (LEA_ID, "Support is briefed, nothing blocking."), |
| 1757 | (MARA_ID, "Can we keep the old export for one more release?"), |
| 1758 | (JONAS_ID, "Yes, behind a flag. I will add it today."), |
| 1759 | (PRIYA_ID, "Changelog draft is ready for review."), |
| 1760 | (LEA_ID, "Customer success wants a heads-up the day before."), |
| 1761 | (MARA_ID, "I will send that mail on Thursday."), |
| 1762 | (JONAS_ID, "Flag is in, rebuilding staging now."), |
| 1763 | (PRIYA_ID, "Rollback steps are in the runbook."), |
| 1764 | (LEA_ID, "All green from QA. Go for Friday."), |
| 1765 | ]; |
| 1766 | |
| 1767 | fn channel_messages() -> Vec<MessageRecord> { |
| 1768 | let channel = "demo-channel-1-1"; |
| 1769 | let mara = (MARA_ID, "Mara Lindqvist"); |
| 1770 | let tobias = (TOBIAS_ID, "Tobias Klein"); |
| 1771 | let people = [ |
| 1772 | (MARA_ID, "Mara Lindqvist"), |
| 1773 | (JONAS_ID, "Jonas Ortega"), |
| 1774 | (PRIYA_ID, "Priya Nair"), |
| 1775 | (LEA_ID, "Lea Schneider"), |
| 1776 | ]; |
| 1777 | let rollout_replies = ROLLOUT_REPLIES |
| 1778 | .iter() |
| 1779 | .enumerate() |
| 1780 | .map(|(index, (sender, text))| { |
| 1781 | let sender = people |
| 1782 | .iter() |
| 1783 | .copied() |
| 1784 | .find(|(id, _)| id == sender) |
| 1785 | .expect("known person"); |
| 1786 | message( |
| 1787 | channel, |
| 1788 | &format!("t3r{}", index + 1), |
| 1789 | Some("t3"), |
| 1790 | sender, |
| 1791 | at(0, 9, 40) + Duration::minutes(6 * index as i64), |
| 1792 | &format!("<p>{text}</p>"), |
| 1793 | "[]", |
| 1794 | false, |
| 1795 | ) |
| 1796 | }); |
| 1797 | let mut records = vec![ |
| 1798 | message( |
| 1799 | channel, |
| 1800 | "t1", |
| 1801 | None, |
| 1802 | mara, |
| 1803 | at(0, 8, 0), |
| 1804 | "<p>Weekly sync notes are up.</p>", |
| 1805 | "[]", |
| 1806 | false, |
| 1807 | ), |
| 1808 | message( |
| 1809 | channel, |
| 1810 | "t1r1", |
| 1811 | Some("t1"), |
| 1812 | tobias, |
| 1813 | at(0, 8, 20), |
| 1814 | "<p>Thanks, adding my items.</p>", |
| 1815 | "[]", |
| 1816 | false, |
| 1817 | ), |
| 1818 | with_subject( |
| 1819 | message( |
| 1820 | channel, |
| 1821 | "t3", |
| 1822 | None, |
| 1823 | tobias, |
| 1824 | at(0, 9, 30), |
| 1825 | "<p>Release build 42 is green, deploying to staging now.</p>", |
| 1826 | "[]", |
| 1827 | false, |
| 1828 | ), |
| 1829 | "Release 42 rollout", |
| 1830 | ), |
| 1831 | message( |
| 1832 | channel, |
| 1833 | "t2", |
| 1834 | None, |
| 1835 | tobias, |
| 1836 | at(0, 10, 0), |
| 1837 | "<p>Who owns the on-call handover?</p>", |
| 1838 | "[]", |
| 1839 | false, |
| 1840 | ), |
| 1841 | message( |
| 1842 | channel, |
| 1843 | "t4", |
| 1844 | None, |
| 1845 | mara, |
| 1846 | at(3, 8, 42), |
| 1847 | "<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>", |
| 1848 | "[]", |
| 1849 | false, |
| 1850 | ), |
| 1851 | message( |
| 1852 | channel, |
| 1853 | "t4r1", |
| 1854 | Some("t4"), |
| 1855 | tobias, |
| 1856 | at(0, 9, 15), |
| 1857 | "<p>Yes, all done.</p>", |
| 1858 | "[]", |
| 1859 | false, |
| 1860 | ), |
| 1861 | ]; |
| 1862 | records.extend(rollout_replies); |
| 1863 | records.extend(shared_channel_messages(channel, mara, tobias)); |
| 1864 | records |
| 1865 | } |
| 1866 | |
| 1867 | fn shared_channel_messages( |
| 1868 | channel: &str, |
| 1869 | mara: (&str, &str), |
| 1870 | tobias: (&str, &str), |
| 1871 | ) -> Vec<MessageRecord> { |
| 1872 | let priya = (PRIYA_ID, "Priya Nair"); |
| 1873 | let jonas = (JONAS_ID, "Jonas Ortega"); |
| 1874 | let reference = |name: &str, size: u64| { |
| 1875 | format!( |
| 1876 | r#"{{"content_type":"reference","name":"{name}","url":"https://demo.sharepoint.example/sites/demo/{name}","text":null,"size":{size}}}"# |
| 1877 | ) |
| 1878 | }; |
| 1879 | vec![ |
| 1880 | with_attachments( |
| 1881 | with_subject( |
| 1882 | message( |
| 1883 | channel, |
| 1884 | "t5", |
| 1885 | None, |
| 1886 | priya, |
| 1887 | at(1, 14, 5), |
| 1888 | "<p>Roadmap deck and checklist for Q4 are attached.</p>", |
| 1889 | "[]", |
| 1890 | false, |
| 1891 | ), |
| 1892 | "Q4 planning files", |
| 1893 | ), |
| 1894 | &format!( |
| 1895 | "[{},{}]", |
| 1896 | reference("Roadmap-Q4.pptx", 3_412_000), |
| 1897 | reference("Release-Checklist-Q4.xlsx", 48_213) |
| 1898 | ), |
| 1899 | ), |
| 1900 | message( |
| 1901 | channel, |
| 1902 | "t5r1", |
| 1903 | Some("t5"), |
| 1904 | jonas, |
| 1905 | at(1, 14, 30), |
| 1906 | "<p>Rollback steps are here: <a href=\"https://docs.example.org/runbooks/rollback\">https://docs.example.org/runbooks/rollback</a></p>", |
| 1907 | "[]", |
| 1908 | false, |
| 1909 | ), |
| 1910 | with_attachments( |
| 1911 | message( |
| 1912 | channel, |
| 1913 | "t6", |
| 1914 | None, |
| 1915 | mara, |
| 1916 | at(12, 10, 0), |
| 1917 | "<p>Postmortem draft for the September outage. Status page: <a href=\"https://status.example.com/incidents/2026-09\">https://status.example.com/incidents/2026-09</a></p>", |
| 1918 | "[]", |
| 1919 | false, |
| 1920 | ), |
| 1921 | &format!("[{}]", reference("Postmortem-Draft.docx", 91_400)), |
| 1922 | ), |
| 1923 | with_attachments( |
| 1924 | message( |
| 1925 | channel, |
| 1926 | "t7", |
| 1927 | None, |
| 1928 | tobias, |
| 1929 | at(20, 9, 0), |
| 1930 | "<p>Rollout plan for the new export, please review.</p>", |
| 1931 | "[]", |
| 1932 | false, |
| 1933 | ), |
| 1934 | &format!("[{}]", reference("Rollout-Plan.pdf", 2_306_867)), |
| 1935 | ), |
| 1936 | ] |
| 1937 | } |
| 1938 | |
| 1939 | fn crc32(bytes: &[u8]) -> u32 { |
| 1940 | let mut crc = 0xffff_ffffu32; |
| 1941 | for byte in bytes { |
| 1942 | crc ^= u32::from(*byte); |
| 1943 | for _ in 0..8 { |
| 1944 | crc = if crc & 1 == 1 { |
| 1945 | (crc >> 1) ^ 0xedb8_8320 |
| 1946 | } else { |
| 1947 | crc >> 1 |
| 1948 | }; |
| 1949 | } |
| 1950 | } |
| 1951 | !crc |
| 1952 | } |
| 1953 | |
| 1954 | fn adler32(bytes: &[u8]) -> u32 { |
| 1955 | let (mut low, mut high) = (1u32, 0u32); |
| 1956 | for byte in bytes { |
| 1957 | low = (low + u32::from(*byte)) % 65521; |
| 1958 | high = (high + low) % 65521; |
| 1959 | } |
| 1960 | (high << 16) | low |
| 1961 | } |
| 1962 | |
| 1963 | fn png_chunk(output: &mut Vec<u8>, kind: &[u8; 4], data: &[u8]) { |
| 1964 | output.extend_from_slice(&(data.len() as u32).to_be_bytes()); |
| 1965 | let mut body = kind.to_vec(); |
| 1966 | body.extend_from_slice(data); |
| 1967 | output.extend_from_slice(&body); |
| 1968 | output.extend_from_slice(&crc32(&body).to_be_bytes()); |
| 1969 | } |
| 1970 | |
| 1971 | fn inside(x: f32, y: f32, center: (f32, f32), radius: (f32, f32)) -> bool { |
| 1972 | ((x - center.0) / radius.0).powi(2) + ((y - center.1) / radius.1).powi(2) <= 1. |
| 1973 | } |
| 1974 | |
| 1975 | fn staging_pixel(column: usize, row: usize) -> [u8; 3] { |
| 1976 | let (width, height) = (STAGING_SIZE.0 as f32, STAGING_SIZE.1 as f32); |
| 1977 | let (x, y) = (column as f32, row as f32); |
| 1978 | let shade = y / height; |
| 1979 | let background = [ |
| 1980 | (30. - 14. * shade) as u8, |
| 1981 | (41. - 16. * shade) as u8, |
| 1982 | (59. - 14. * shade) as u8, |
| 1983 | ]; |
| 1984 | let bar_width = width / 14.; |
| 1985 | let slot = (x / bar_width) as usize; |
| 1986 | let in_bar = x - slot as f32 * bar_width > bar_width * 0.18; |
| 1987 | let bar_height = height * (0.25 + 0.5 * (((slot * 7 + 3) % 11) as f32 / 11.)); |
| 1988 | let on_bars = in_bar && y > height * 0.86 - bar_height && y < height * 0.86 && slot < 14; |
| 1989 | let header = y < height * 0.1; |
| 1990 | if header { |
| 1991 | [22, 30, 45] |
| 1992 | } else if on_bars { |
| 1993 | if slot % 4 == 3 { |
| 1994 | [226, 87, 47] |
| 1995 | } else { |
| 1996 | [88, 133, 196] |
| 1997 | } |
| 1998 | } else if (y - height * 0.86).abs() < 1.5 { |
| 1999 | [71, 85, 105] |
| 2000 | } else { |
| 2001 | background |
| 2002 | } |
| 2003 | } |
| 2004 | |
| 2005 | fn encode_png(palette: &Palette) -> Vec<u8> { |
| 2006 | encode_rgb(PHOTO_SIZE, PHOTO_SIZE, |column, row| { |
| 2007 | let (x, y) = (column as f32 + 0.5, row as f32 + 0.5); |
| 2008 | if inside(x, y, (24., 21.), (8.5, 9.5)) { |
| 2009 | palette.skin |
| 2010 | } else if inside(x, y, (24., 19.), (10.5, 11.)) && y < 22. { |
| 2011 | palette.hair |
| 2012 | } else if inside(x, y, (24., 50.), (22., 19.)) { |
| 2013 | palette.shirt |
| 2014 | } else { |
| 2015 | palette.background |
| 2016 | } |
| 2017 | }) |
| 2018 | } |
| 2019 | |
| 2020 | fn encode_rgb(width: usize, height: usize, pixel: impl Fn(usize, usize) -> [u8; 3]) -> Vec<u8> { |
| 2021 | let mut raw = Vec::with_capacity(height * (width * 3 + 1)); |
| 2022 | for row in 0..height { |
| 2023 | raw.push(0); |
| 2024 | for column in 0..width { |
| 2025 | raw.extend_from_slice(&pixel(column, row)); |
| 2026 | } |
| 2027 | } |
| 2028 | let mut zlib = vec![0x78, 0x01]; |
| 2029 | let mut blocks = raw.chunks(65535).peekable(); |
| 2030 | while let Some(block) = blocks.next() { |
| 2031 | zlib.push(u8::from(blocks.peek().is_none())); |
| 2032 | zlib.extend_from_slice(&(block.len() as u16).to_le_bytes()); |
| 2033 | zlib.extend_from_slice(&(!(block.len() as u16)).to_le_bytes()); |
| 2034 | zlib.extend_from_slice(block); |
| 2035 | } |
| 2036 | zlib.extend_from_slice(&adler32(&raw).to_be_bytes()); |
| 2037 | let mut header = Vec::new(); |
| 2038 | header.extend_from_slice(&(width as u32).to_be_bytes()); |
| 2039 | header.extend_from_slice(&(height as u32).to_be_bytes()); |
| 2040 | header.extend_from_slice(&[8, 2, 0, 0, 0]); |
| 2041 | let mut png = vec![0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]; |
| 2042 | png_chunk(&mut png, b"IHDR", &header); |
| 2043 | png_chunk(&mut png, b"IDAT", &zlib); |
| 2044 | png_chunk(&mut png, b"IEND", &[]); |
| 2045 | png |
| 2046 | } |
| 2047 | |
| 2048 | #[cfg(test)] |
| 2049 | mod tests { |
| 2050 | use super::*; |
| 2051 | |
| 2052 | #[test] |
| 2053 | fn png_has_signature_and_ends_with_iend() { |
| 2054 | let png = encode_png(&MARA); |
| 2055 | assert_eq!(&png[..8], &[0x89, b'P', b'N', b'G', 0x0d, 0x0a, 0x1a, 0x0a]); |
| 2056 | assert_eq!(&png[png.len() - 8..png.len() - 4], b"IEND"); |
| 2057 | } |
| 2058 | |
| 2059 | #[test] |
| 2060 | fn crc_and_adler_match_known_values() { |
| 2061 | assert_eq!(crc32(b"IEND"), 0xae42_6082); |
| 2062 | assert_eq!(adler32(b"Wikipedia"), 0x11e6_0398); |
| 2063 | } |
| 2064 | |
| 2065 | #[test] |
| 2066 | fn mention_candidates_filter_by_name_and_add_scope_in_channels() { |
| 2067 | let chat = mention_candidates("demo-chat-release", "ma"); |
| 2068 | assert_eq!(chat.len(), 1); |
| 2069 | assert_eq!(chat[0].display_name(), "Mara Lindqvist"); |
| 2070 | let channel = mention_candidates("demo-channel-1-1", "gen"); |
| 2071 | assert!(matches!(channel[0], MentionCandidate::Channel { .. })); |
| 2072 | assert_eq!(mention_candidates("demo-chat-release", "").len(), 5); |
| 2073 | } |
| 2074 | |
| 2075 | #[test] |
| 2076 | fn new_group_chat_gets_a_title_and_the_first_message() { |
| 2077 | let people = vec![ |
| 2078 | (MARA_ID.to_owned(), "Mara Lindqvist".to_owned()), |
| 2079 | (JONAS_ID.to_owned(), "Jonas Ortega".to_owned()), |
| 2080 | ]; |
| 2081 | let now = Utc::now(); |
| 2082 | let (chat, record) = new_chat( |
| 2083 | &[], |
| 2084 | &people, |
| 2085 | Some("Crew"), |
| 2086 | "Hello all", |
| 2087 | "Hello <i>all</i>", |
| 2088 | now, |
| 2089 | ); |
| 2090 | assert_eq!(chat.id, "demo-chat-new-1"); |
| 2091 | assert_eq!(chat.kind, "group"); |
| 2092 | assert_eq!(chat.title, "Crew"); |
| 2093 | assert_eq!(chat.member_summary, "Mara Lindqvist, Jonas Ortega"); |
| 2094 | assert_eq!(chat.members.len(), 3); |
| 2095 | assert_eq!(chat.members[0].user_id.as_deref(), Some(DEMO_USER_ID)); |
| 2096 | assert_eq!(chat.last_message_preview.as_deref(), Some("Hello all")); |
| 2097 | assert_eq!(chat.last_message_at, Some(now)); |
| 2098 | assert_eq!(record.conversation_id, chat.id); |
| 2099 | assert_eq!(record.sender_id.as_deref(), Some(DEMO_USER_ID)); |
| 2100 | assert!(record.body_html.contains("Hello")); |
| 2101 | } |
| 2102 | |
| 2103 | #[test] |
| 2104 | fn new_one_on_one_chat_has_no_title_and_counts_up() { |
| 2105 | let people = vec![(LEA_ID.to_owned(), "Lea Schneider".to_owned())]; |
| 2106 | let (first, _) = new_chat(&[], &people, None, "Hi", "Hi", Utc::now()); |
| 2107 | assert_eq!(first.kind, "oneOnOne"); |
| 2108 | assert!(first.title.is_empty()); |
| 2109 | let (second, _) = new_chat(&[first], &people, None, "Hi again", "Hi again", Utc::now()); |
| 2110 | assert_eq!(second.id, "demo-chat-new-2"); |
| 2111 | } |
| 2112 | |
| 2113 | #[test] |
| 2114 | fn sending_into_a_chat_refreshes_its_preview() { |
| 2115 | let base = ChatRecord { |
| 2116 | id: "demo-chat-x".to_owned(), |
| 2117 | unread: true, |
| 2118 | ..Default::default() |
| 2119 | }; |
| 2120 | let (chat, record) = send_in_chat(&base, "Ping", "Ping", Utc::now()); |
| 2121 | assert!(!chat.unread); |
| 2122 | assert_eq!(chat.last_message_preview.as_deref(), Some("Ping")); |
| 2123 | assert_eq!(record.conversation_id, "demo-chat-x"); |
| 2124 | } |
| 2125 | |
| 2126 | #[test] |
| 2127 | fn demo_covers_the_sidebar_edge_cases() { |
| 2128 | let chats = demo_chats(); |
| 2129 | assert!(chats.iter().any(|chat| chat.deleted)); |
| 2130 | assert!(chats.iter().any(|chat| chat.title.len() > 40)); |
| 2131 | assert!(chats.iter().filter(|chat| chat.unread).count() >= 3); |
| 2132 | } |
| 2133 | } |