| 1 | use std::collections::{BTreeMap, HashMap}; |
| 2 | use std::ops::Range; |
| 3 | use std::sync::Arc; |
| 4 | |
| 5 | use chrono::{DateTime, Duration, FixedOffset, NaiveDate, Utc}; |
| 6 | use gpui_kit::Image; |
| 7 | use store::MessageRecord; |
| 8 | use teams_core::{ |
| 9 | AdaptiveCard, Draft, FileCard, ImageRef, LinkPreview, ReactionInfo, Span, adaptive_cards, |
| 10 | card_texts, files, images, link_preview, message_spans, reactions, |
| 11 | }; |
| 12 | |
| 13 | use crate::card_state::CardOverride; |
| 14 | use crate::format; |
| 15 | use crate::reaction_model::{Reactor, UNKNOWN_REACTOR}; |
| 16 | use crate::render::blocks::{Inline, strip_image_placeholders}; |
| 17 | use crate::render::{Block, layout_blocks}; |
| 18 | use crate::sidebar_model::Face; |
| 19 | |
| 20 | const REPLY_FACE_LIMIT: usize = 3; |
| 21 | |
| 22 | pub const LOAD_OLDER_KEY: &str = "load-older"; |
| 23 | const UNKNOWN_AUTHOR: &str = "Unknown"; |
| 24 | const DELETED_TEXT: &str = "Message deleted"; |
| 25 | const SERIES_GAP_MINUTES: i64 = 5; |
| 26 | const CORNER_RADIUS: f32 = 14.; |
| 27 | const CORNER_SERIES: f32 = 4.; |
| 28 | |
| 29 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 30 | pub struct ReactionChip { |
| 31 | pub reaction_type: String, |
| 32 | pub label: String, |
| 33 | pub count: usize, |
| 34 | pub mine: bool, |
| 35 | pub reactors: Vec<Reactor>, |
| 36 | } |
| 37 | |
| 38 | impl ReactionChip { |
| 39 | pub fn glyph(&self) -> String { |
| 40 | reaction_glyph(&self.reaction_type) |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 45 | pub struct Series { |
| 46 | pub has_prev: bool, |
| 47 | pub has_next: bool, |
| 48 | } |
| 49 | |
| 50 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 51 | pub struct Corners { |
| 52 | pub top_left: f32, |
| 53 | pub top_right: f32, |
| 54 | pub bottom_right: f32, |
| 55 | pub bottom_left: f32, |
| 56 | } |
| 57 | |
| 58 | pub fn bubble_corners(own: bool, series: Series) -> Corners { |
| 59 | if own { |
| 60 | let top_right = if series.has_prev || !series.has_next { |
| 61 | CORNER_SERIES |
| 62 | } else { |
| 63 | CORNER_RADIUS |
| 64 | }; |
| 65 | let bottom_right = if series.has_next || !series.has_prev { |
| 66 | CORNER_SERIES |
| 67 | } else { |
| 68 | CORNER_RADIUS |
| 69 | }; |
| 70 | Corners { |
| 71 | top_left: CORNER_RADIUS, |
| 72 | top_right, |
| 73 | bottom_right, |
| 74 | bottom_left: CORNER_RADIUS, |
| 75 | } |
| 76 | } else { |
| 77 | Corners { |
| 78 | top_left: CORNER_SERIES, |
| 79 | top_right: CORNER_RADIUS, |
| 80 | bottom_right: CORNER_RADIUS, |
| 81 | bottom_left: if series.has_next { |
| 82 | CORNER_SERIES |
| 83 | } else { |
| 84 | CORNER_RADIUS |
| 85 | }, |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] |
| 91 | pub enum Receipt { |
| 92 | #[default] |
| 93 | Hidden, |
| 94 | Pending, |
| 95 | Sent, |
| 96 | Read, |
| 97 | } |
| 98 | |
| 99 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 100 | pub enum ScheduledState { |
| 101 | Waiting, |
| 102 | DeliveryFailed, |
| 103 | ChangeFailed, |
| 104 | } |
| 105 | |
| 106 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 107 | pub enum Delivery { |
| 108 | Delivered, |
| 109 | Sending, |
| 110 | Failed(String), |
| 111 | Scheduled(ScheduledState), |
| 112 | } |
| 113 | |
| 114 | /// An image of a message that is still being sent, shown from its local bytes. |
| 115 | #[derive(Debug, Clone, PartialEq)] |
| 116 | pub struct LocalImage { |
| 117 | pub image: Arc<Image>, |
| 118 | pub size: Option<(u32, u32)>, |
| 119 | } |
| 120 | |
| 121 | #[derive(Debug, Clone, PartialEq)] |
| 122 | pub struct MessageRow { |
| 123 | pub key: String, |
| 124 | pub conversation_id: String, |
| 125 | pub author: String, |
| 126 | pub sender_id: Option<String>, |
| 127 | pub application_id: Option<String>, |
| 128 | pub created_at: DateTime<Utc>, |
| 129 | pub series: Series, |
| 130 | pub card: bool, |
| 131 | pub time: String, |
| 132 | pub day_header: Option<String>, |
| 133 | pub blocks: Vec<Block>, |
| 134 | pub edited: bool, |
| 135 | pub deleted: bool, |
| 136 | pub reactions: Vec<ReactionChip>, |
| 137 | pub images: Vec<ImageRef>, |
| 138 | pub local_images: Vec<LocalImage>, |
| 139 | pub files: Vec<FileCard>, |
| 140 | pub link_preview: Option<LinkPreview>, |
| 141 | pub adaptive_cards: Vec<AdaptiveCard>, |
| 142 | pub reply_count: Option<usize>, |
| 143 | pub new_marker: bool, |
| 144 | pub reply_faces: Vec<Face>, |
| 145 | pub last_reply_time: Option<String>, |
| 146 | pub open_thread: Option<String>, |
| 147 | pub is_reply: bool, |
| 148 | pub delivery: Delivery, |
| 149 | pub own: bool, |
| 150 | pub receipt: Receipt, |
| 151 | } |
| 152 | |
| 153 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 154 | pub struct StartInfo { |
| 155 | pub title: String, |
| 156 | pub user_id: Option<String>, |
| 157 | } |
| 158 | |
| 159 | pub const START_KEY: &str = "conversation-start"; |
| 160 | |
| 161 | #[derive(Debug, Clone, PartialEq)] |
| 162 | pub enum Row { |
| 163 | LoadOlder, |
| 164 | Start(StartInfo), |
| 165 | Skeleton(Skeleton), |
| 166 | Message(Box<MessageRow>), |
| 167 | } |
| 168 | |
| 169 | #[derive(Debug, Clone, Copy, PartialEq)] |
| 170 | pub struct Skeleton { |
| 171 | pub key: &'static str, |
| 172 | pub own: bool, |
| 173 | pub width_ratio: f32, |
| 174 | pub lines: u8, |
| 175 | } |
| 176 | |
| 177 | const SKELETON_TRAILING: Skeleton = Skeleton { |
| 178 | key: "skeleton-trailing", |
| 179 | own: false, |
| 180 | width_ratio: 0.32, |
| 181 | lines: 1, |
| 182 | }; |
| 183 | |
| 184 | const SKELETON_PLACEHOLDERS: [Skeleton; 4] = [ |
| 185 | Skeleton { |
| 186 | key: "skeleton-0", |
| 187 | own: false, |
| 188 | width_ratio: 0.42, |
| 189 | lines: 1, |
| 190 | }, |
| 191 | Skeleton { |
| 192 | key: "skeleton-1", |
| 193 | own: true, |
| 194 | width_ratio: 0.3, |
| 195 | lines: 1, |
| 196 | }, |
| 197 | Skeleton { |
| 198 | key: "skeleton-2", |
| 199 | own: true, |
| 200 | width_ratio: 0.48, |
| 201 | lines: 2, |
| 202 | }, |
| 203 | Skeleton { |
| 204 | key: "skeleton-3", |
| 205 | own: false, |
| 206 | width_ratio: 0.26, |
| 207 | lines: 1, |
| 208 | }, |
| 209 | ]; |
| 210 | |
| 211 | pub fn placeholder_rows() -> Vec<Row> { |
| 212 | SKELETON_PLACEHOLDERS |
| 213 | .into_iter() |
| 214 | .map(Row::Skeleton) |
| 215 | .collect() |
| 216 | } |
| 217 | |
| 218 | pub fn trailing_skeleton() -> Row { |
| 219 | Row::Skeleton(SKELETON_TRAILING) |
| 220 | } |
| 221 | |
| 222 | fn sender_key(row: &MessageRow) -> &str { |
| 223 | row.sender_id.as_deref().unwrap_or(&row.author) |
| 224 | } |
| 225 | |
| 226 | fn continues_series(previous: &MessageRow, next: &MessageRow) -> bool { |
| 227 | !previous.card |
| 228 | && !next.card |
| 229 | && next.day_header.is_none() |
| 230 | && !next.new_marker |
| 231 | && sender_key(previous) == sender_key(next) |
| 232 | && next.created_at - previous.created_at < Duration::minutes(SERIES_GAP_MINUTES) |
| 233 | && next.created_at >= previous.created_at |
| 234 | } |
| 235 | |
| 236 | pub fn assign_series(rows: &mut [Row]) { |
| 237 | let mut previous: Option<usize> = None; |
| 238 | for index in 0..rows.len() { |
| 239 | let Row::Message(_) = &rows[index] else { |
| 240 | previous = None; |
| 241 | continue; |
| 242 | }; |
| 243 | if let Some(before) = previous { |
| 244 | let joined = match (&rows[before], &rows[index]) { |
| 245 | (Row::Message(a), Row::Message(b)) => continues_series(a, b), |
| 246 | _ => false, |
| 247 | }; |
| 248 | if joined { |
| 249 | if let Row::Message(a) = &mut rows[before] { |
| 250 | a.series.has_next = true; |
| 251 | } |
| 252 | if let Row::Message(b) = &mut rows[index] { |
| 253 | b.series.has_prev = true; |
| 254 | } |
| 255 | } |
| 256 | } |
| 257 | previous = Some(index); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | impl Row { |
| 262 | pub fn key(&self) -> &str { |
| 263 | match self { |
| 264 | Row::LoadOlder => LOAD_OLDER_KEY, |
| 265 | Row::Start(_) => START_KEY, |
| 266 | Row::Skeleton(skeleton) => skeleton.key, |
| 267 | Row::Message(message) => &message.key, |
| 268 | } |
| 269 | } |
| 270 | } |
| 271 | |
| 272 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 273 | pub struct Thread { |
| 274 | pub root_id: String, |
| 275 | pub reply_ids: Vec<String>, |
| 276 | } |
| 277 | |
| 278 | pub fn clock_label(time: DateTime<Utc>, offset: FixedOffset) -> String { |
| 279 | time.with_timezone(&offset).format("%H:%M").to_string() |
| 280 | } |
| 281 | |
| 282 | pub fn reaction_label(reaction_type: &str) -> String { |
| 283 | match reaction_type { |
| 284 | "like" => "\u{1F44D}".to_owned(), |
| 285 | "heart" => "\u{2764}".to_owned(), |
| 286 | "laugh" => "\u{1F606}".to_owned(), |
| 287 | "surprised" => "\u{1F62E}".to_owned(), |
| 288 | "sad" => "\u{1F641}".to_owned(), |
| 289 | "angry" => "\u{1F620}".to_owned(), |
| 290 | other => other.to_owned(), |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | pub fn reaction_glyph(reaction_type: &str) -> String { |
| 295 | reaction_label(reaction_type) |
| 296 | .trim_end_matches('\u{FE0F}') |
| 297 | .to_owned() |
| 298 | } |
| 299 | |
| 300 | /// Graph rejects the legacy names ("Unicode 'like' in the payload is not supported"). |
| 301 | pub fn api_reaction(glyph: &str) -> String { |
| 302 | match glyph { |
| 303 | "\u{2764}" => "\u{2764}\u{FE0F}".to_owned(), |
| 304 | other => other.to_owned(), |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | /// The reaction type Teams uses for a glyph: the legacy name for the six classic ones. |
| 309 | pub fn reaction_type_for(glyph: &str) -> String { |
| 310 | let bare = glyph.trim_end_matches('\u{FE0F}'); |
| 311 | ["like", "heart", "laugh", "surprised", "sad", "angry"] |
| 312 | .into_iter() |
| 313 | .find(|name| reaction_label(name) == bare) |
| 314 | .map_or_else(|| bare.to_owned(), str::to_owned) |
| 315 | } |
| 316 | |
| 317 | pub type PendingReactions = HashMap<String, Vec<(String, bool)>>; |
| 318 | |
| 319 | fn is_own_reaction(reaction: &ReactionInfo, wanted: &str, my_user_id: Option<&str>) -> bool { |
| 320 | my_user_id.is_some() |
| 321 | && reaction.user_id.as_deref() == my_user_id |
| 322 | && reaction_glyph(&reaction.reaction_type) == wanted |
| 323 | } |
| 324 | |
| 325 | pub fn has_own_reaction(reactions: &[ReactionInfo], glyph: &str, my_user_id: Option<&str>) -> bool { |
| 326 | let wanted = reaction_glyph(glyph); |
| 327 | reactions |
| 328 | .iter() |
| 329 | .any(|reaction| is_own_reaction(reaction, &wanted, my_user_id)) |
| 330 | } |
| 331 | |
| 332 | pub fn set_own_reaction( |
| 333 | reactions: &mut Vec<ReactionInfo>, |
| 334 | glyph: &str, |
| 335 | my_user_id: Option<&str>, |
| 336 | present: bool, |
| 337 | now: DateTime<Utc>, |
| 338 | ) { |
| 339 | let wanted = reaction_glyph(glyph); |
| 340 | if !present { |
| 341 | reactions.retain(|reaction| !is_own_reaction(reaction, &wanted, my_user_id)); |
| 342 | } else if !has_own_reaction(reactions, glyph, my_user_id) { |
| 343 | reactions.push(ReactionInfo { |
| 344 | reaction_type: reaction_type_for(glyph), |
| 345 | user_id: my_user_id.map(str::to_owned), |
| 346 | user_name: Some("You".to_owned()), |
| 347 | created_at: Some(now), |
| 348 | }); |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | pub fn apply_pending_reactions( |
| 353 | reactions: &mut Vec<ReactionInfo>, |
| 354 | pending: Option<&Vec<(String, bool)>>, |
| 355 | my_user_id: Option<&str>, |
| 356 | now: DateTime<Utc>, |
| 357 | ) { |
| 358 | for (glyph, present) in pending.into_iter().flatten() { |
| 359 | set_own_reaction(reactions, glyph, my_user_id, *present, now); |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | pub fn reaction_chips(reactions: &[ReactionInfo], context: &RowContext) -> Vec<ReactionChip> { |
| 364 | let my_user_id = context.my_user_id.as_deref(); |
| 365 | let mut chips: Vec<ReactionChip> = Vec::new(); |
| 366 | for reaction in reactions { |
| 367 | let glyph = reaction_glyph(&reaction.reaction_type); |
| 368 | let position = match chips.iter().position(|chip| chip.glyph() == glyph) { |
| 369 | Some(position) => position, |
| 370 | None => { |
| 371 | chips.push(ReactionChip { |
| 372 | reaction_type: reaction.reaction_type.clone(), |
| 373 | label: reaction_label(&reaction.reaction_type), |
| 374 | count: 0, |
| 375 | mine: false, |
| 376 | reactors: Vec::new(), |
| 377 | }); |
| 378 | chips.len() - 1 |
| 379 | } |
| 380 | }; |
| 381 | let chip = &mut chips[position]; |
| 382 | chip.count += 1; |
| 383 | chip.mine |= my_user_id.is_some() && reaction.user_id.as_deref() == my_user_id; |
| 384 | chip.reactors.push(Reactor { |
| 385 | user_id: reaction.user_id.clone(), |
| 386 | name: reactor_name(reaction, context), |
| 387 | created_at: reaction.created_at, |
| 388 | }); |
| 389 | } |
| 390 | chips |
| 391 | } |
| 392 | |
| 393 | fn reactor_name(reaction: &ReactionInfo, context: &RowContext) -> String { |
| 394 | reaction |
| 395 | .user_name |
| 396 | .clone() |
| 397 | .or_else(|| { |
| 398 | reaction |
| 399 | .user_id |
| 400 | .as_ref() |
| 401 | .and_then(|user_id| context.names.get(user_id).cloned()) |
| 402 | }) |
| 403 | .unwrap_or_else(|| UNKNOWN_REACTOR.to_owned()) |
| 404 | } |
| 405 | |
| 406 | const REPLY_EXCERPT_CHARS: usize = 90; |
| 407 | |
| 408 | pub fn message_text(record: &MessageRecord) -> String { |
| 409 | let own_spans: Vec<Span> = message_spans(record) |
| 410 | .into_iter() |
| 411 | .filter(|span| !matches!(span, Span::Quote(_))) |
| 412 | .collect(); |
| 413 | strip_image_placeholders(layout_blocks(&own_spans)) |
| 414 | .into_iter() |
| 415 | .filter_map(|block| block.markdown()) |
| 416 | .collect::<Vec<_>>() |
| 417 | .join("\n") |
| 418 | } |
| 419 | |
| 420 | /// The message as the composer edits it, formatting kept. |
| 421 | pub fn message_draft(record: &MessageRecord) -> Draft { |
| 422 | let own_spans: Vec<Span> = message_spans(record) |
| 423 | .into_iter() |
| 424 | .filter(|span| !matches!(span, Span::Quote(_))) |
| 425 | .collect(); |
| 426 | Draft::from_spans(&own_spans).trimmed() |
| 427 | } |
| 428 | |
| 429 | pub fn reply_excerpt(record: &MessageRecord) -> String { |
| 430 | let own_spans: Vec<Span> = message_spans(record) |
| 431 | .into_iter() |
| 432 | .filter(|span| !matches!(span, Span::Quote(_))) |
| 433 | .collect(); |
| 434 | let text = strip_image_placeholders(layout_blocks(&own_spans)) |
| 435 | .into_iter() |
| 436 | .find_map(|block| block.first_line()) |
| 437 | .unwrap_or_default(); |
| 438 | let first_line = text.lines().map(str::trim).find(|line| !line.is_empty()); |
| 439 | let excerpt = match first_line { |
| 440 | Some(line) => line.to_owned(), |
| 441 | None if !images(record).is_empty() => "Image".to_owned(), |
| 442 | None if let Some(line) = card_texts(record) |
| 443 | .iter() |
| 444 | .flat_map(|text| text.lines()) |
| 445 | .next() => |
| 446 | { |
| 447 | line.to_owned() |
| 448 | } |
| 449 | None => files(record) |
| 450 | .into_iter() |
| 451 | .next() |
| 452 | .map_or_else(|| "Message".to_owned(), |card| card.name), |
| 453 | }; |
| 454 | if excerpt.chars().count() > REPLY_EXCERPT_CHARS { |
| 455 | let cut: String = excerpt.chars().take(REPLY_EXCERPT_CHARS).collect(); |
| 456 | format!("{}...", cut.trim_end()) |
| 457 | } else { |
| 458 | excerpt |
| 459 | } |
| 460 | } |
| 461 | |
| 462 | pub struct RowContext { |
| 463 | pub offset: FixedOffset, |
| 464 | pub today: NaiveDate, |
| 465 | pub my_user_id: Option<String>, |
| 466 | pub names: HashMap<String, String>, |
| 467 | pub pending_reactions: PendingReactions, |
| 468 | pub card_overrides: HashMap<String, CardOverride>, |
| 469 | } |
| 470 | |
| 471 | fn visible_reactions(record: &MessageRecord, context: &RowContext) -> Vec<ReactionInfo> { |
| 472 | let mut visible = reactions(record); |
| 473 | apply_pending_reactions( |
| 474 | &mut visible, |
| 475 | context.pending_reactions.get(&record.message_id), |
| 476 | context.my_user_id.as_deref(), |
| 477 | Utc::now(), |
| 478 | ); |
| 479 | visible |
| 480 | } |
| 481 | |
| 482 | pub fn message_row(record: &MessageRecord, context: &RowContext) -> MessageRow { |
| 483 | let local = record.created_at.with_timezone(&context.offset); |
| 484 | let images = images(record); |
| 485 | let blocks = if record.deleted { |
| 486 | vec![Block::Paragraph(Inline::plain(DELETED_TEXT))] |
| 487 | } else { |
| 488 | layout_blocks(&message_spans(record)) |
| 489 | }; |
| 490 | let blocks = if images.is_empty() { |
| 491 | blocks |
| 492 | } else { |
| 493 | strip_image_placeholders(blocks) |
| 494 | }; |
| 495 | MessageRow { |
| 496 | key: record.message_id.clone(), |
| 497 | conversation_id: record.conversation_id.clone(), |
| 498 | sender_id: record.sender_id.clone(), |
| 499 | application_id: record.sender_application_id.clone(), |
| 500 | created_at: record.created_at, |
| 501 | series: Series::default(), |
| 502 | card: false, |
| 503 | author: record |
| 504 | .sender_name |
| 505 | .clone() |
| 506 | .unwrap_or_else(|| UNKNOWN_AUTHOR.to_owned()), |
| 507 | time: clock_label(record.created_at, context.offset), |
| 508 | day_header: Some(format::day_label(local.date_naive(), context.today)), |
| 509 | blocks, |
| 510 | edited: record.edited_at.is_some() && !record.deleted, |
| 511 | deleted: record.deleted, |
| 512 | reactions: reaction_chips(&visible_reactions(record, context), context), |
| 513 | images, |
| 514 | local_images: Vec::new(), |
| 515 | files: files(record), |
| 516 | link_preview: link_preview(record), |
| 517 | adaptive_cards: match context.card_overrides.get(&record.message_id) { |
| 518 | Some(replaced) if replaced.basis == record.attachments_json => replaced.cards.clone(), |
| 519 | _ => adaptive_cards(record), |
| 520 | }, |
| 521 | reply_count: None, |
| 522 | new_marker: false, |
| 523 | reply_faces: Vec::new(), |
| 524 | last_reply_time: None, |
| 525 | open_thread: None, |
| 526 | is_reply: record.reply_to_id.is_some(), |
| 527 | delivery: Delivery::Delivered, |
| 528 | receipt: Receipt::Hidden, |
| 529 | own: context.my_user_id.is_some() && record.sender_id == context.my_user_id, |
| 530 | } |
| 531 | } |
| 532 | |
| 533 | pub fn flat_rows(records: &[MessageRecord], context: &RowContext, has_older: bool) -> Vec<Row> { |
| 534 | let mut rows = Vec::with_capacity(records.len() + 1); |
| 535 | if has_older { |
| 536 | rows.push(Row::LoadOlder); |
| 537 | } |
| 538 | let mut previous_day: Option<NaiveDate> = None; |
| 539 | for record in records { |
| 540 | let mut row = message_row(record, context); |
| 541 | let day = record |
| 542 | .created_at |
| 543 | .with_timezone(&context.offset) |
| 544 | .date_naive(); |
| 545 | if previous_day == Some(day) { |
| 546 | row.day_header = None; |
| 547 | } |
| 548 | previous_day = Some(day); |
| 549 | rows.push(Row::Message(Box::new(row))); |
| 550 | } |
| 551 | assign_series(&mut rows); |
| 552 | rows |
| 553 | } |
| 554 | |
| 555 | pub fn group_threads(records: &[MessageRecord]) -> Vec<Thread> { |
| 556 | let mut threads: Vec<Thread> = Vec::new(); |
| 557 | let mut index_by_root: BTreeMap<&str, usize> = BTreeMap::new(); |
| 558 | for record in records.iter().filter(|record| record.reply_to_id.is_none()) { |
| 559 | index_by_root.insert(record.message_id.as_str(), threads.len()); |
| 560 | threads.push(Thread { |
| 561 | root_id: record.message_id.clone(), |
| 562 | reply_ids: Vec::new(), |
| 563 | }); |
| 564 | } |
| 565 | for record in records { |
| 566 | let Some(root_id) = record.reply_to_id.as_deref() else { |
| 567 | continue; |
| 568 | }; |
| 569 | if let Some(&index) = index_by_root.get(root_id) { |
| 570 | threads[index].reply_ids.push(record.message_id.clone()); |
| 571 | } |
| 572 | } |
| 573 | threads |
| 574 | } |
| 575 | |
| 576 | fn last_activity(thread: &Thread, by_id: &BTreeMap<&str, &MessageRecord>) -> DateTime<Utc> { |
| 577 | thread |
| 578 | .reply_ids |
| 579 | .iter() |
| 580 | .chain(std::iter::once(&thread.root_id)) |
| 581 | .filter_map(|id| by_id.get(id.as_str())) |
| 582 | .map(|record| record.created_at) |
| 583 | .max() |
| 584 | .unwrap_or_default() |
| 585 | } |
| 586 | |
| 587 | pub fn thread_list_rows( |
| 588 | records: &[MessageRecord], |
| 589 | context: &RowContext, |
| 590 | has_older: bool, |
| 591 | ) -> Vec<Row> { |
| 592 | let by_id: BTreeMap<&str, &MessageRecord> = records |
| 593 | .iter() |
| 594 | .map(|record| (record.message_id.as_str(), record)) |
| 595 | .collect(); |
| 596 | let mut threads = group_threads(records); |
| 597 | threads.sort_by_key(|thread| last_activity(thread, &by_id)); |
| 598 | let mut rows = Vec::with_capacity(threads.len() + 1); |
| 599 | if has_older { |
| 600 | rows.push(Row::LoadOlder); |
| 601 | } |
| 602 | let mut previous_day: Option<NaiveDate> = None; |
| 603 | for thread in threads { |
| 604 | let Some(root) = by_id.get(thread.root_id.as_str()) else { |
| 605 | continue; |
| 606 | }; |
| 607 | let mut row = message_row(root, context); |
| 608 | let day = last_activity(&thread, &by_id) |
| 609 | .with_timezone(&context.offset) |
| 610 | .date_naive(); |
| 611 | row.day_header = (previous_day != Some(day)).then(|| format::day_label(day, context.today)); |
| 612 | previous_day = Some(day); |
| 613 | row.card = true; |
| 614 | row.time = format::post_time_label(root.created_at, context.today, context.offset); |
| 615 | row.reply_count = Some(thread.reply_ids.len()); |
| 616 | let replies: Vec<&&MessageRecord> = thread |
| 617 | .reply_ids |
| 618 | .iter() |
| 619 | .filter_map(|id| by_id.get(id.as_str())) |
| 620 | .collect(); |
| 621 | for reply in &replies { |
| 622 | let face = Face { |
| 623 | user_id: reply.sender_id.clone(), |
| 624 | name: reply |
| 625 | .sender_name |
| 626 | .clone() |
| 627 | .unwrap_or_else(|| UNKNOWN_AUTHOR.to_owned()), |
| 628 | }; |
| 629 | if row.reply_faces.len() < REPLY_FACE_LIMIT && !row.reply_faces.contains(&face) { |
| 630 | row.reply_faces.push(face); |
| 631 | } |
| 632 | } |
| 633 | row.last_reply_time = replies |
| 634 | .iter() |
| 635 | .map(|reply| reply.created_at) |
| 636 | .max() |
| 637 | .map(|time| { |
| 638 | let local = time.with_timezone(&context.offset); |
| 639 | format!( |
| 640 | "{} {}", |
| 641 | format::day_label(local.date_naive(), context.today), |
| 642 | clock_label(time, context.offset) |
| 643 | ) |
| 644 | }); |
| 645 | row.open_thread = Some(thread.root_id.clone()); |
| 646 | rows.push(Row::Message(Box::new(row))); |
| 647 | } |
| 648 | rows |
| 649 | } |
| 650 | |
| 651 | pub fn thread_rows(records: &[MessageRecord], root_id: &str, context: &RowContext) -> Vec<Row> { |
| 652 | let members: Vec<MessageRecord> = records |
| 653 | .iter() |
| 654 | .filter(|record| { |
| 655 | record.message_id == root_id || record.reply_to_id.as_deref() == Some(root_id) |
| 656 | }) |
| 657 | .cloned() |
| 658 | .collect(); |
| 659 | flat_rows(&members, context, false) |
| 660 | } |
| 661 | |
| 662 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 663 | pub struct Splice { |
| 664 | pub range: Range<usize>, |
| 665 | pub count: usize, |
| 666 | } |
| 667 | |
| 668 | pub fn diff_keys(old: &[String], new: &[String]) -> Option<Splice> { |
| 669 | if old == new { |
| 670 | return None; |
| 671 | } |
| 672 | let prefix = old |
| 673 | .iter() |
| 674 | .zip(new) |
| 675 | .take_while(|(left, right)| left == right) |
| 676 | .count(); |
| 677 | let suffix = old[prefix..] |
| 678 | .iter() |
| 679 | .rev() |
| 680 | .zip(new[prefix..].iter().rev()) |
| 681 | .take_while(|(left, right)| left == right) |
| 682 | .count(); |
| 683 | Some(Splice { |
| 684 | range: prefix..old.len() - suffix, |
| 685 | count: new.len() - prefix - suffix, |
| 686 | }) |
| 687 | } |
| 688 | |
| 689 | pub fn changed_indices(old: &[Row], new: &[Row]) -> Vec<usize> { |
| 690 | let prefix = old |
| 691 | .iter() |
| 692 | .zip(new) |
| 693 | .take_while(|(left, right)| left.key() == right.key()) |
| 694 | .count(); |
| 695 | let suffix = old[prefix..] |
| 696 | .iter() |
| 697 | .rev() |
| 698 | .zip(new[prefix..].iter().rev()) |
| 699 | .take_while(|(left, right)| left.key() == right.key()) |
| 700 | .count(); |
| 701 | let mut changed: Vec<usize> = (0..prefix) |
| 702 | .filter(|&index| old[index] != new[index]) |
| 703 | .collect(); |
| 704 | changed.extend((0..suffix).filter_map(|offset| { |
| 705 | let old_index = old.len() - 1 - offset; |
| 706 | let new_index = new.len() - 1 - offset; |
| 707 | (old[old_index] != new[new_index]).then_some(new_index) |
| 708 | })); |
| 709 | changed |
| 710 | } |
| 711 | |
| 712 | #[cfg(test)] |
| 713 | mod tests { |
| 714 | use chrono::TimeZone; |
| 715 | |
| 716 | use super::*; |
| 717 | |
| 718 | fn record(id: &str, reply_to: Option<&str>, hour: u32, day: u32) -> MessageRecord { |
| 719 | MessageRecord { |
| 720 | conversation_id: "c".into(), |
| 721 | message_id: id.into(), |
| 722 | reply_to_id: reply_to.map(str::to_owned), |
| 723 | sender_id: Some("u".into()), |
| 724 | sender_name: Some("Author".into()), |
| 725 | sender_application_id: None, |
| 726 | links_json: "[]".to_owned(), |
| 727 | created_at: Utc.with_ymd_and_hms(2026, 10, day, hour, 0, 0).unwrap(), |
| 728 | edited_at: None, |
| 729 | deleted: false, |
| 730 | body_html: format!("<p>text {id}</p>"), |
| 731 | attachments_json: "[]".into(), |
| 732 | reactions_json: "[]".into(), |
| 733 | mentions_json: "[]".into(), |
| 734 | } |
| 735 | } |
| 736 | |
| 737 | fn own_reaction(reaction_type: &str) -> ReactionInfo { |
| 738 | ReactionInfo { |
| 739 | reaction_type: reaction_type.into(), |
| 740 | user_id: Some("me".into()), |
| 741 | user_name: None, |
| 742 | created_at: None, |
| 743 | } |
| 744 | } |
| 745 | |
| 746 | #[test] |
| 747 | fn setting_a_reaction_is_idempotent_and_only_touches_mine() { |
| 748 | let now = Utc.with_ymd_and_hms(2026, 10, 1, 8, 0, 0).unwrap(); |
| 749 | let other = ReactionInfo { |
| 750 | user_id: Some("them".into()), |
| 751 | ..own_reaction("like") |
| 752 | }; |
| 753 | let mut reactions = vec![other.clone()]; |
| 754 | set_own_reaction(&mut reactions, "\u{1F44D}", Some("me"), true, now); |
| 755 | set_own_reaction(&mut reactions, "\u{1F44D}", Some("me"), true, now); |
| 756 | assert_eq!(reactions.len(), 2); |
| 757 | assert_eq!(reactions[1].reaction_type, "like"); |
| 758 | assert_eq!(reactions[1].user_id.as_deref(), Some("me")); |
| 759 | set_own_reaction(&mut reactions, "\u{1F44D}", Some("me"), false, now); |
| 760 | set_own_reaction(&mut reactions, "\u{1F44D}", Some("me"), false, now); |
| 761 | assert_eq!(reactions, vec![other]); |
| 762 | } |
| 763 | |
| 764 | #[test] |
| 765 | fn setting_matches_legacy_names_and_variation_selectors() { |
| 766 | let now = Utc.with_ymd_and_hms(2026, 10, 1, 8, 0, 0).unwrap(); |
| 767 | let mut reactions = vec![own_reaction("heart")]; |
| 768 | assert!(has_own_reaction(&reactions, "\u{2764}\u{FE0F}", Some("me"))); |
| 769 | set_own_reaction(&mut reactions, "\u{2764}\u{FE0F}", Some("me"), false, now); |
| 770 | assert!(reactions.is_empty()); |
| 771 | set_own_reaction(&mut reactions, "\u{1F389}", Some("me"), true, now); |
| 772 | assert_eq!(reactions[0].reaction_type, "\u{1F389}"); |
| 773 | } |
| 774 | |
| 775 | #[test] |
| 776 | fn setting_without_a_known_user_never_matches() { |
| 777 | let now = Utc.with_ymd_and_hms(2026, 10, 1, 8, 0, 0).unwrap(); |
| 778 | let mut reactions = Vec::new(); |
| 779 | set_own_reaction(&mut reactions, "\u{1F44D}", None, true, now); |
| 780 | set_own_reaction(&mut reactions, "\u{1F44D}", None, true, now); |
| 781 | assert_eq!(reactions.len(), 2); |
| 782 | } |
| 783 | |
| 784 | #[test] |
| 785 | fn pending_reactions_overlay_the_stored_ones() { |
| 786 | let mut stored = record("a", None, 8, 6); |
| 787 | stored.reactions_json = serde_json::to_string(&[own_reaction("like")]).unwrap(); |
| 788 | let mut context = context(); |
| 789 | context.my_user_id = Some("me".into()); |
| 790 | context.pending_reactions.insert( |
| 791 | "a".into(), |
| 792 | vec![("\u{1F44D}".into(), false), ("\u{1F389}".into(), true)], |
| 793 | ); |
| 794 | let chips = message_row(&stored, &context).reactions; |
| 795 | assert_eq!(chips.len(), 1); |
| 796 | assert_eq!(chips[0].reaction_type, "\u{1F389}"); |
| 797 | assert!(chips[0].mine); |
| 798 | } |
| 799 | |
| 800 | #[test] |
| 801 | fn reaction_type_maps_classic_glyphs_to_names() { |
| 802 | assert_eq!(reaction_type_for("\u{1F44D}"), "like"); |
| 803 | assert_eq!(reaction_type_for("\u{2764}\u{FE0F}"), "heart"); |
| 804 | assert_eq!(reaction_type_for("\u{1F389}"), "\u{1F389}"); |
| 805 | assert_eq!(reaction_glyph("like"), reaction_glyph("\u{1F44D}")); |
| 806 | assert_eq!(reaction_glyph("heart"), reaction_glyph("\u{2764}\u{FE0F}")); |
| 807 | assert_eq!(api_reaction(&reaction_glyph("heart")), "\u{2764}\u{FE0F}"); |
| 808 | assert_eq!(api_reaction(&reaction_glyph("like")), "\u{1F44D}"); |
| 809 | } |
| 810 | |
| 811 | #[test] |
| 812 | fn editing_loads_the_message_formatted_without_its_reply_quote() { |
| 813 | let mut edited = record("a", None, 8, 6); |
| 814 | edited.body_html = |
| 815 | "<blockquote itemtype=\"http://schema.skype.com/Reply\">old</blockquote>\ |
| 816 | <p><b>Plan</b> for <a href=\"https://x.test\">today</a></p>\ |
| 817 | <ol><li>one</li><li>two</li></ol>" |
| 818 | .into(); |
| 819 | let draft = message_draft(&edited); |
| 820 | assert_eq!(draft.text(), "Plan for today\n1. one\n2. two"); |
| 821 | assert_eq!( |
| 822 | draft.to_html(), |
| 823 | "<b>Plan</b> for <a href=\"https://x.test\">today</a><ol><li>one</li><li>two</li></ol>" |
| 824 | ); |
| 825 | } |
| 826 | |
| 827 | #[test] |
| 828 | fn reply_excerpt_uses_the_first_line_or_the_attachment() { |
| 829 | let mut text = record("a", None, 8, 6); |
| 830 | text.body_html = "<p>first line</p><p>second</p>".into(); |
| 831 | assert_eq!(reply_excerpt(&text), "first line"); |
| 832 | let mut quoted = record("q", None, 8, 6); |
| 833 | quoted.body_html = r#"<blockquote itemtype="http://schema.skype.com/Reply">Jonas<br>old text</blockquote><p>my answer</p>"#.into(); |
| 834 | assert_eq!(reply_excerpt("ed), "my answer"); |
| 835 | let mut image = record("b", None, 8, 6); |
| 836 | image.body_html = "<img src=\"u\">".into(); |
| 837 | assert_eq!(reply_excerpt(&image), "Image"); |
| 838 | let mut long = record("c", None, 8, 6); |
| 839 | long.body_html = format!("<p>{}</p>", "x".repeat(200)); |
| 840 | assert!(reply_excerpt(&long).ends_with("...")); |
| 841 | assert_eq!( |
| 842 | reply_excerpt(&long).chars().count(), |
| 843 | REPLY_EXCERPT_CHARS + 3 |
| 844 | ); |
| 845 | } |
| 846 | |
| 847 | fn context() -> RowContext { |
| 848 | RowContext { |
| 849 | offset: FixedOffset::east_opt(0).unwrap(), |
| 850 | today: NaiveDate::from_ymd_opt(2026, 10, 7).unwrap(), |
| 851 | my_user_id: None, |
| 852 | names: HashMap::new(), |
| 853 | pending_reactions: HashMap::new(), |
| 854 | card_overrides: HashMap::new(), |
| 855 | } |
| 856 | } |
| 857 | |
| 858 | fn keys(rows: &[Row]) -> Vec<String> { |
| 859 | rows.iter().map(|row| row.key().to_owned()).collect() |
| 860 | } |
| 861 | |
| 862 | #[test] |
| 863 | fn day_header_only_on_first_message_of_a_day() { |
| 864 | let records = vec![ |
| 865 | record("a", None, 8, 6), |
| 866 | record("b", None, 9, 6), |
| 867 | record("c", None, 9, 7), |
| 868 | ]; |
| 869 | let rows = flat_rows(&records, &context(), false); |
| 870 | let headers: Vec<Option<String>> = rows |
| 871 | .iter() |
| 872 | .map(|row| match row { |
| 873 | Row::Message(message) => message.day_header.clone(), |
| 874 | Row::LoadOlder | Row::Start(_) | Row::Skeleton(_) => None, |
| 875 | }) |
| 876 | .collect(); |
| 877 | assert_eq!( |
| 878 | headers, |
| 879 | vec![Some("Yesterday".into()), None, Some("Today".into())] |
| 880 | ); |
| 881 | } |
| 882 | |
| 883 | #[test] |
| 884 | fn load_older_row_only_when_more_exists() { |
| 885 | let records = vec![record("a", None, 8, 6)]; |
| 886 | assert_eq!( |
| 887 | keys(&flat_rows(&records, &context(), true))[0], |
| 888 | LOAD_OLDER_KEY |
| 889 | ); |
| 890 | assert_eq!(keys(&flat_rows(&records, &context(), false)), vec!["a"]); |
| 891 | } |
| 892 | |
| 893 | #[test] |
| 894 | fn threads_group_replies_by_root() { |
| 895 | let records = vec![ |
| 896 | record("r1", None, 8, 6), |
| 897 | record("r2", None, 9, 6), |
| 898 | record("x1", Some("r1"), 10, 6), |
| 899 | record("x2", Some("r1"), 11, 6), |
| 900 | record("orphan", Some("missing"), 12, 6), |
| 901 | ]; |
| 902 | let threads = group_threads(&records); |
| 903 | assert_eq!(threads.len(), 2); |
| 904 | assert_eq!(threads[0].reply_ids, vec!["x1", "x2"]); |
| 905 | assert!(threads[1].reply_ids.is_empty()); |
| 906 | } |
| 907 | |
| 908 | #[test] |
| 909 | fn thread_list_orders_by_last_activity_with_reply_count() { |
| 910 | let records = vec![ |
| 911 | record("r1", None, 8, 6), |
| 912 | record("r2", None, 9, 6), |
| 913 | record("x1", Some("r1"), 10, 6), |
| 914 | ]; |
| 915 | let rows = thread_list_rows(&records, &context(), false); |
| 916 | assert_eq!(keys(&rows), vec!["r2", "r1"]); |
| 917 | let Row::Message(last) = &rows[1] else { |
| 918 | panic!("message expected") |
| 919 | }; |
| 920 | assert_eq!(last.reply_count, Some(1)); |
| 921 | assert_eq!(last.open_thread.as_deref(), Some("r1")); |
| 922 | } |
| 923 | |
| 924 | #[test] |
| 925 | fn thread_view_has_root_then_replies() { |
| 926 | let records = vec![ |
| 927 | record("r1", None, 8, 6), |
| 928 | record("r2", None, 9, 6), |
| 929 | record("x1", Some("r1"), 10, 6), |
| 930 | ]; |
| 931 | assert_eq!( |
| 932 | keys(&thread_rows(&records, "r1", &context())), |
| 933 | vec!["r1", "x1"] |
| 934 | ); |
| 935 | } |
| 936 | |
| 937 | #[test] |
| 938 | fn reactions_are_counted_in_first_seen_order() { |
| 939 | let reaction = |kind: &str| ReactionInfo { |
| 940 | reaction_type: kind.into(), |
| 941 | user_id: None, |
| 942 | user_name: None, |
| 943 | created_at: None, |
| 944 | }; |
| 945 | let chips = reaction_chips( |
| 946 | &[reaction("heart"), reaction("like"), reaction("heart")], |
| 947 | &context(), |
| 948 | ); |
| 949 | assert_eq!(chips.len(), 2); |
| 950 | assert_eq!(chips[0].count, 2); |
| 951 | assert_eq!(chips[1].label, reaction_label("like")); |
| 952 | } |
| 953 | |
| 954 | #[test] |
| 955 | fn legacy_laugh_and_sad_use_the_teams_glyphs() { |
| 956 | assert_eq!(reaction_label("laugh"), "\u{1F606}"); |
| 957 | assert_eq!(reaction_label("sad"), "\u{1F641}"); |
| 958 | assert_eq!(reaction_type_for("\u{1F606}"), "laugh"); |
| 959 | assert_eq!(reaction_type_for("\u{1F641}"), "sad"); |
| 960 | } |
| 961 | |
| 962 | #[test] |
| 963 | fn legacy_and_emoji_reactions_share_a_chip() { |
| 964 | let reaction = |kind: &str| ReactionInfo { |
| 965 | reaction_type: kind.into(), |
| 966 | user_id: None, |
| 967 | user_name: Some("Lea".into()), |
| 968 | created_at: None, |
| 969 | }; |
| 970 | let chips = reaction_chips(&[reaction("like"), reaction("\u{1F44D}")], &context()); |
| 971 | assert_eq!(chips.len(), 1); |
| 972 | assert_eq!(chips[0].count, 2); |
| 973 | } |
| 974 | |
| 975 | #[test] |
| 976 | fn reactor_name_falls_back_to_directory_then_unknown() { |
| 977 | let reaction = |user: &str| ReactionInfo { |
| 978 | reaction_type: "like".into(), |
| 979 | user_id: Some(user.into()), |
| 980 | user_name: None, |
| 981 | created_at: None, |
| 982 | }; |
| 983 | let mut context = context(); |
| 984 | context.names.insert("known".into(), "Priya Nair".into()); |
| 985 | let chips = reaction_chips(&[reaction("known"), reaction("other")], &context); |
| 986 | assert_eq!(chips[0].reactors[0].name, "Priya Nair"); |
| 987 | assert_eq!(chips[0].reactors[1].name, "Unknown"); |
| 988 | } |
| 989 | |
| 990 | fn record_by(id: &str, sender: &str, hour: u32, minute: u32, day: u32) -> MessageRecord { |
| 991 | let mut record = record(id, None, hour, day); |
| 992 | record.sender_id = Some(sender.into()); |
| 993 | record.created_at = Utc |
| 994 | .with_ymd_and_hms(2026, 10, day, hour, minute, 0) |
| 995 | .unwrap(); |
| 996 | record |
| 997 | } |
| 998 | |
| 999 | fn series_of(rows: &[Row]) -> Vec<(bool, bool)> { |
| 1000 | rows.iter() |
| 1001 | .filter_map(|row| match row { |
| 1002 | Row::Message(message) => Some((message.series.has_prev, message.series.has_next)), |
| 1003 | Row::LoadOlder | Row::Start(_) | Row::Skeleton(_) => None, |
| 1004 | }) |
| 1005 | .collect() |
| 1006 | } |
| 1007 | |
| 1008 | #[test] |
| 1009 | fn series_joins_same_sender_within_five_minutes() { |
| 1010 | let records = vec![ |
| 1011 | record_by("a", "ada", 9, 0, 7), |
| 1012 | record_by("b", "ada", 9, 4, 7), |
| 1013 | record_by("c", "ada", 9, 20, 7), |
| 1014 | record_by("d", "bob", 9, 21, 7), |
| 1015 | record_by("e", "ada", 9, 22, 7), |
| 1016 | ]; |
| 1017 | let rows = flat_rows(&records, &context(), false); |
| 1018 | assert_eq!( |
| 1019 | series_of(&rows), |
| 1020 | vec![ |
| 1021 | (false, true), |
| 1022 | (true, false), |
| 1023 | (false, false), |
| 1024 | (false, false), |
| 1025 | (false, false) |
| 1026 | ] |
| 1027 | ); |
| 1028 | } |
| 1029 | |
| 1030 | #[test] |
| 1031 | fn series_window_ends_exactly_at_the_gap_constant() { |
| 1032 | let gap = SERIES_GAP_MINUTES as u32; |
| 1033 | let records = vec![ |
| 1034 | record_by("a", "ada", 9, 0, 7), |
| 1035 | record_by("b", "ada", 9, gap - 1, 7), |
| 1036 | record_by("c", "ada", 9, 2 * gap - 1 + gap, 7), |
| 1037 | ]; |
| 1038 | let rows = flat_rows(&records, &context(), false); |
| 1039 | assert_eq!( |
| 1040 | series_of(&rows), |
| 1041 | vec![(false, true), (true, false), (false, false)] |
| 1042 | ); |
| 1043 | } |
| 1044 | |
| 1045 | #[test] |
| 1046 | fn series_breaks_at_a_new_day() { |
| 1047 | let records = vec![ |
| 1048 | record_by("a", "ada", 23, 58, 6), |
| 1049 | record_by("b", "ada", 0, 1, 7), |
| 1050 | ]; |
| 1051 | let rows = flat_rows(&records, &context(), false); |
| 1052 | assert_eq!(series_of(&rows), vec![(false, false), (false, false)]); |
| 1053 | } |
| 1054 | |
| 1055 | #[test] |
| 1056 | fn thread_list_rows_never_join() { |
| 1057 | let records = vec![ |
| 1058 | record_by("a", "ada", 9, 0, 7), |
| 1059 | record_by("b", "ada", 9, 1, 7), |
| 1060 | ]; |
| 1061 | let rows = thread_list_rows(&records, &context(), false); |
| 1062 | assert_eq!(series_of(&rows), vec![(false, false), (false, false)]); |
| 1063 | } |
| 1064 | |
| 1065 | fn corners(own: bool, has_prev: bool, has_next: bool) -> [f32; 4] { |
| 1066 | let corners = bubble_corners(own, Series { has_prev, has_next }); |
| 1067 | [ |
| 1068 | corners.top_left, |
| 1069 | corners.top_right, |
| 1070 | corners.bottom_right, |
| 1071 | corners.bottom_left, |
| 1072 | ] |
| 1073 | } |
| 1074 | |
| 1075 | #[test] |
| 1076 | fn corners_match_the_mockup_for_other_senders() { |
| 1077 | assert_eq!(corners(false, false, false), [4., 14., 14., 14.]); |
| 1078 | assert_eq!(corners(false, false, true), [4., 14., 14., 4.]); |
| 1079 | assert_eq!(corners(false, true, true), [4., 14., 14., 4.]); |
| 1080 | assert_eq!(corners(false, true, false), [4., 14., 14., 14.]); |
| 1081 | } |
| 1082 | |
| 1083 | #[test] |
| 1084 | fn corners_match_the_mockup_for_own_messages() { |
| 1085 | assert_eq!(corners(true, false, false), [14., 4., 4., 14.]); |
| 1086 | assert_eq!(corners(true, false, true), [14., 14., 4., 14.]); |
| 1087 | assert_eq!(corners(true, true, true), [14., 4., 4., 14.]); |
| 1088 | assert_eq!(corners(true, true, false), [14., 4., 14., 14.]); |
| 1089 | } |
| 1090 | |
| 1091 | #[test] |
| 1092 | fn own_reaction_is_marked() { |
| 1093 | let reaction = |user: &str| ReactionInfo { |
| 1094 | reaction_type: "like".into(), |
| 1095 | user_id: Some(user.into()), |
| 1096 | user_name: None, |
| 1097 | created_at: None, |
| 1098 | }; |
| 1099 | let mut context = context(); |
| 1100 | context.my_user_id = Some("me".into()); |
| 1101 | let chips = reaction_chips(&[reaction("x"), reaction("me")], &context); |
| 1102 | assert!(chips[0].mine); |
| 1103 | assert!(!reaction_chips(&[reaction("x")], &context)[0].mine); |
| 1104 | } |
| 1105 | |
| 1106 | fn strings(values: &[&str]) -> Vec<String> { |
| 1107 | values.iter().map(|value| (*value).to_owned()).collect() |
| 1108 | } |
| 1109 | |
| 1110 | #[test] |
| 1111 | fn diff_detects_append_prepend_and_replace() { |
| 1112 | let base = strings(&["a", "b"]); |
| 1113 | assert_eq!(diff_keys(&base, &base), None); |
| 1114 | assert_eq!( |
| 1115 | diff_keys(&base, &strings(&["a", "b", "c"])), |
| 1116 | Some(Splice { |
| 1117 | range: 2..2, |
| 1118 | count: 1 |
| 1119 | }) |
| 1120 | ); |
| 1121 | assert_eq!( |
| 1122 | diff_keys(&base, &strings(&["x", "a", "b"])), |
| 1123 | Some(Splice { |
| 1124 | range: 0..0, |
| 1125 | count: 1 |
| 1126 | }) |
| 1127 | ); |
| 1128 | assert_eq!( |
| 1129 | diff_keys( |
| 1130 | &strings(&["load-older", "a"]), |
| 1131 | &strings(&["load-older", "x", "y", "a"]) |
| 1132 | ), |
| 1133 | Some(Splice { |
| 1134 | range: 1..1, |
| 1135 | count: 2 |
| 1136 | }) |
| 1137 | ); |
| 1138 | assert_eq!( |
| 1139 | diff_keys(&strings(&["load-older", "a"]), &strings(&["a"])), |
| 1140 | Some(Splice { |
| 1141 | range: 0..1, |
| 1142 | count: 0 |
| 1143 | }) |
| 1144 | ); |
| 1145 | assert_eq!( |
| 1146 | diff_keys(&base, &strings(&["x"])), |
| 1147 | Some(Splice { |
| 1148 | range: 0..2, |
| 1149 | count: 1 |
| 1150 | }) |
| 1151 | ); |
| 1152 | } |
| 1153 | |
| 1154 | #[test] |
| 1155 | fn changed_indices_find_edited_rows() { |
| 1156 | let old = flat_rows( |
| 1157 | &[record("a", None, 8, 6), record("b", None, 9, 6)], |
| 1158 | &context(), |
| 1159 | false, |
| 1160 | ); |
| 1161 | let mut edited = record("b", None, 9, 6); |
| 1162 | edited.body_html = "<p>changed</p>".into(); |
| 1163 | let new = flat_rows(&[record("a", None, 8, 6), edited], &context(), false); |
| 1164 | assert_eq!(changed_indices(&old, &new), vec![1]); |
| 1165 | } |
| 1166 | } |