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