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