use std::collections::{BTreeMap, HashMap}; use std::ops::Range; use std::sync::Arc; use chrono::{DateTime, Duration, FixedOffset, NaiveDate, Utc}; use gpui_kit::Image; use store::{MessageImportance, MessageRecord}; use teams_core::roster_event::{RosterEvent, roster_event}; use teams_core::{ JumboEmoji, jumbo_emoji, AdaptiveCard, CallEvent, CallPhase, Draft, FileCard, ForwardHeader, ForwardInfo, ImageRef, LinkPreview, forwards, ReactionInfo, Span, adaptive_cards, call_event, card_texts, files, forwarded_texts, images, is_forwarded, link_preview, linked_message_spans_with, meeting_link_in_html, message_spans, quotes, reactions, readable_text, translated_spans, }; pub use crate::call_rows::{ CallCard, CallIcon, CallNotice, CallParticipant, MeetingInfo, MeetingSlot, RecapCard, RecordingCard, }; use crate::call_rows::{CallClock, CallNames, CallSource, CallTimeline, call_card, roster_card}; use crate::card_state::CardOverride; use crate::format; use crate::reaction_model::{Reactor, UNKNOWN_REACTOR}; use crate::render::blocks::{Inline, strip_image_placeholders}; use crate::render::{Block, layout_blocks}; use crate::translation::{TranslationContext, TranslationLine}; const POST_VISIBLE_REPLIES: usize = 3; pub const LOAD_OLDER_KEY: &str = "load-older"; pub const LOAD_NEWER_KEY: &str = "load-newer"; const UNKNOWN_AUTHOR: &str = "Unknown"; const DELETED_TEXT: &str = "Message deleted"; const SERIES_GAP_MINUTES: i64 = 5; const CORNER_RADIUS: f32 = 14.; const CORNER_SERIES: f32 = 4.; #[derive(Debug, Clone, PartialEq, Eq)] pub struct ReactionChip { pub reaction_type: String, pub label: String, pub click_glyph: String, pub count: usize, pub mine: bool, pub reactors: Vec, } impl ReactionChip { pub fn glyph(&self) -> String { reaction_glyph(&self.reaction_type) } pub fn custom_emoji_image(&self) -> Option { custom_emoji_image(&self.reaction_type) } } /// Text for places without images: the org emoji's name instead of its raw key. pub fn reaction_text(reaction_type: &str) -> String { match chatsvc::custom_emoji(reaction_type) { Some(emoji) => format!(":{}:", emoji.name), None => reaction_glyph(reaction_type), } } pub fn custom_emoji_image(reaction_type: &str) -> Option { let emoji = chatsvc::custom_emoji(reaction_type)?; Some(ImageRef { id: emoji.object_id.to_owned(), url: emoji.image_url(), width: None, height: None, alt: Some(emoji.name.to_owned()), }) } #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub struct Series { pub has_prev: bool, pub has_next: bool, } #[derive(Debug, Clone, Copy, PartialEq)] pub struct Corners { pub top_left: f32, pub top_right: f32, pub bottom_right: f32, pub bottom_left: f32, } pub fn bubble_corners(own: bool, series: Series) -> Corners { let joined = |joined: bool| if joined { CORNER_SERIES } else { CORNER_RADIUS }; if own { Corners { top_left: CORNER_RADIUS, top_right: joined(series.has_prev), bottom_right: joined(series.has_next), bottom_left: CORNER_RADIUS, } } else { Corners { top_left: joined(series.has_prev), top_right: CORNER_RADIUS, bottom_right: CORNER_RADIUS, bottom_left: joined(series.has_next), } } } #[derive(Debug, Clone, Copy, Default, PartialEq, Eq)] pub enum Receipt { #[default] Hidden, Sent, Read, ReadLive, } #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum ScheduledState { Waiting, DeliveryFailed, ChangeFailed, } #[derive(Debug, Clone, PartialEq, Eq)] pub enum Delivery { Delivered, Sending, Failed(String), Scheduled(ScheduledState), } /// An image of a message that is still being sent, shown from its local bytes. #[derive(Debug, Clone, PartialEq)] pub struct LocalImage { pub image: Arc, pub size: Option<(u32, u32)>, } #[derive(Debug, Clone, PartialEq)] pub struct MessageRow { pub key: String, pub conversation_id: String, pub author: String, pub sender_id: Option, pub application_id: Option, pub created_at: DateTime, pub series: Series, pub time: String, pub day_header: Option, pub blocks: Vec, pub jumbo: Option>, pub edited: bool, pub deleted: bool, pub reactions: Vec, pub images: Vec, pub local_images: Vec, pub files: Vec, pub link_preview: Option, pub meeting_link: Option, pub adaptive_cards: Vec, pub subject: Option, pub importance: MessageImportance, pub new_marker: bool, pub reply_root: Option, pub delivery: Delivery, pub own: bool, pub receipt: Receipt, pub forwarded: bool, pub translation: Option, pub call: Option, } #[derive(Debug, Clone, PartialEq, Eq)] pub struct StartInfo { pub title: String, pub user_id: Option, } pub const START_KEY: &str = "conversation-start"; #[derive(Debug, Clone, PartialEq)] pub enum Row { LoadOlder, LoadNewer, Start(StartInfo), Skeleton(Skeleton), Message(Box), Post(Box), } #[derive(Debug, Clone, Copy, PartialEq)] pub struct Skeleton { pub key: &'static str, pub own: bool, pub width_ratio: f32, pub lines: u8, } const SKELETON_PLACEHOLDERS: [Skeleton; 4] = [ Skeleton { key: "skeleton-0", own: false, width_ratio: 0.42, lines: 1, }, Skeleton { key: "skeleton-1", own: true, width_ratio: 0.3, lines: 1, }, Skeleton { key: "skeleton-2", own: true, width_ratio: 0.48, lines: 2, }, Skeleton { key: "skeleton-3", own: false, width_ratio: 0.26, lines: 1, }, ]; pub fn placeholder_rows() -> Vec { SKELETON_PLACEHOLDERS .into_iter() .map(Row::Skeleton) .collect() } fn sender_key(row: &MessageRow) -> &str { row.sender_id.as_deref().unwrap_or(&row.author) } fn continues_series(previous: &MessageRow, next: &MessageRow) -> bool { previous.call.is_none() && next.call.is_none() && next.day_header.is_none() && !next.new_marker && sender_key(previous) == sender_key(next) && next.created_at - previous.created_at < Duration::minutes(SERIES_GAP_MINUTES) && next.created_at >= previous.created_at } pub fn assign_series(rows: &mut [Row]) { let mut previous: Option = None; for index in 0..rows.len() { let Row::Message(_) = &rows[index] else { previous = None; continue; }; if let Some(before) = previous { let joined = match (&rows[before], &rows[index]) { (Row::Message(a), Row::Message(b)) => continues_series(a, b), _ => false, }; if joined { if let Row::Message(a) = &mut rows[before] { a.series.has_next = true; } if let Row::Message(b) = &mut rows[index] { b.series.has_prev = true; } } } previous = Some(index); } } impl Row { pub fn key(&self) -> &str { match self { Row::LoadOlder => LOAD_OLDER_KEY, Row::LoadNewer => LOAD_NEWER_KEY, Row::Start(_) => START_KEY, Row::Skeleton(skeleton) => skeleton.key, Row::Message(message) => &message.key, Row::Post(post) => &post.root.key, } } pub fn messages(&self) -> Box + '_> { match self { Row::Message(message) => Box::new(std::iter::once(message.as_ref())), Row::Post(post) => Box::new(post.messages()), Row::LoadOlder | Row::LoadNewer | Row::Start(_) | Row::Skeleton(_) => { Box::new(std::iter::empty()) } } } } #[derive(Debug, Clone, PartialEq)] pub struct PostRow { pub root: MessageRow, pub replies: Vec, pub hidden_reply_count: usize, pub hidden_repliers: Vec, } impl PostRow { pub fn local(root: MessageRow) -> Self { PostRow { root, replies: Vec::new(), hidden_reply_count: 0, hidden_repliers: Vec::new(), } } pub fn messages(&self) -> impl Iterator { std::iter::once(&self.root).chain(&self.replies) } pub fn hidden_replies_label(&self) -> Option { (self.hidden_reply_count > 0) .then(|| replies_label(self.hidden_reply_count, &self.hidden_repliers)) } } fn first_name(name: &str) -> &str { name.split_whitespace().next().unwrap_or(name) } pub fn replies_label(count: usize, repliers: &[String]) -> String { let noun = if count == 1 { "reply" } else { "replies" }; let mut names: Vec<&str> = Vec::new(); for name in repliers.iter().map(|name| first_name(name)) { if !names.contains(&name) { names.push(name); } } let people = match names.as_slice() { [] => return format!("{count} {noun}"), [only] => (*only).to_owned(), [first, second] => format!("{first} and {second}"), [first, second, third] => format!("{first}, {second} and {third}"), [first, second, rest @ ..] => format!("{first}, {second} and {} others", rest.len()), }; format!("{count} {noun} from {people}") } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Thread { pub root_id: String, pub reply_ids: Vec, } pub fn clock_label(time: DateTime, offset: FixedOffset) -> String { time.with_timezone(&offset).format("%H:%M").to_string() } pub fn reaction_label(reaction_type: &str) -> String { match reaction_type { "like" => "\u{1F44D}".to_owned(), "heart" => "\u{2764}".to_owned(), "laugh" => "\u{1F606}".to_owned(), "surprised" => "\u{1F62E}".to_owned(), "sad" => "\u{1F641}".to_owned(), "angry" => "\u{1F620}".to_owned(), other => other.to_owned(), } } pub fn reaction_glyph(reaction_type: &str) -> String { let label = reaction_label(reaction_type); let glyph = if label == reaction_type { chatsvc::reaction_glyph_for_key(reaction_type).unwrap_or(label) } else { label }; glyph.trim_end_matches('\u{FE0F}').to_owned() } fn same_reaction(left: &str, right: &str) -> bool { crate::emoticons::without_tone(left) == crate::emoticons::without_tone(right) } /// Graph rejects the legacy names ("Unicode 'like' in the payload is not supported"). pub fn api_reaction(glyph: &str) -> String { match glyph { "\u{2764}" => "\u{2764}\u{FE0F}".to_owned(), other => other.to_owned(), } } /// The reaction type Teams uses for a glyph: the legacy name for the six classic ones. pub fn reaction_type_for(glyph: &str) -> String { let bare = glyph.trim_end_matches('\u{FE0F}'); ["like", "heart", "laugh", "surprised", "sad", "angry"] .into_iter() .find(|name| reaction_label(name) == bare) .map_or_else(|| bare.to_owned(), str::to_owned) } const MAX_TONE_VARIANTS: usize = 3; pub type PendingReactions = HashMap>; fn is_own_reaction(reaction: &ReactionInfo, wanted: &str, my_user_id: Option<&str>) -> bool { my_user_id.is_some() && reaction.user_id.as_deref() == my_user_id && same_reaction(&reaction_glyph(&reaction.reaction_type), wanted) } /// The glyph of the own reaction that matches `glyph` regardless of skin tone. pub fn own_reaction_glyph( reactions: &[ReactionInfo], glyph: &str, my_user_id: Option<&str>, ) -> Option { let wanted = reaction_glyph(glyph); reactions .iter() .find(|reaction| is_own_reaction(reaction, &wanted, my_user_id)) .map(|reaction| reaction_glyph(&reaction.reaction_type)) } pub fn has_own_reaction(reactions: &[ReactionInfo], glyph: &str, my_user_id: Option<&str>) -> bool { let wanted = reaction_glyph(glyph); reactions .iter() .any(|reaction| is_own_reaction(reaction, &wanted, my_user_id)) } pub fn set_own_reaction( reactions: &mut Vec, glyph: &str, my_user_id: Option<&str>, present: bool, now: DateTime, ) { let wanted = reaction_glyph(glyph); if !present { reactions.retain(|reaction| !is_own_reaction(reaction, &wanted, my_user_id)); } else if !has_own_reaction(reactions, glyph, my_user_id) { reactions.push(ReactionInfo { reaction_type: reaction_type_for(glyph), user_id: my_user_id.map(str::to_owned), user_name: Some("You".to_owned()), created_at: Some(now), }); } } pub fn apply_pending_reactions( reactions: &mut Vec, pending: Option<&Vec<(String, bool)>>, my_user_id: Option<&str>, now: DateTime, ) { for (glyph, present) in pending.into_iter().flatten() { set_own_reaction(reactions, glyph, my_user_id, *present, now); } } pub fn reaction_chips(reactions: &[ReactionInfo], context: &RowContext) -> Vec { let my_user_id = context.my_user_id.as_deref(); let mut chips: Vec = Vec::new(); let mut variants: Vec> = Vec::new(); for reaction in reactions { let glyph = reaction_glyph(&reaction.reaction_type); let position = match chips .iter() .position(|chip| same_reaction(&chip.glyph(), &glyph)) { Some(position) => position, None => { chips.push(ReactionChip { reaction_type: reaction.reaction_type.clone(), label: String::new(), click_glyph: crate::emoticons::with_tone(&glyph, context.skin_tone), count: 0, mine: false, reactors: Vec::new(), }); variants.push(Vec::new()); chips.len() - 1 } }; let label = glyph; let shown = &mut variants[position]; if shown.len() < MAX_TONE_VARIANTS && !shown.contains(&label) { shown.push(label); } let chip = &mut chips[position]; chip.count += 1; chip.mine |= my_user_id.is_some() && reaction.user_id.as_deref() == my_user_id; chip.reactors.push(Reactor { user_id: reaction.user_id.clone(), name: reactor_name(reaction, context), created_at: reaction.created_at, }); } for (chip, shown) in chips.iter_mut().zip(variants) { chip.label = shown.concat(); } chips } fn reactor_name(reaction: &ReactionInfo, context: &RowContext) -> String { reaction .user_name .clone() .or_else(|| { reaction .user_id .as_ref() .and_then(|user_id| context.names.get(user_id).cloned()) }) .unwrap_or_else(|| UNKNOWN_REACTOR.to_owned()) } const REPLY_EXCERPT_CHARS: usize = 90; pub fn message_text(record: &MessageRecord) -> String { let own_spans: Vec = message_spans(record) .into_iter() .filter(|span| !matches!(span, Span::Quote { .. })) .collect(); strip_image_placeholders(layout_blocks(&own_spans)) .into_iter() .filter_map(|block| block.markdown()) .chain(forwarded_texts(record)) .collect::>() .join("\n") } /// The message as the composer edits it, formatting kept. pub fn message_draft(record: &MessageRecord) -> Draft { let own_spans: Vec = message_spans(record) .into_iter() .filter(|span| !matches!(span, Span::Quote { .. })) .collect(); Draft::from_spans(&own_spans).trimmed() } pub fn reply_excerpt(record: &MessageRecord) -> String { let own_spans: Vec = message_spans(record) .into_iter() .filter(|span| !matches!(span, Span::Quote { .. })) .collect(); let text = strip_image_placeholders(layout_blocks(&own_spans)) .into_iter() .find_map(|block| block.first_line()) .unwrap_or_default(); let first_line = text.lines().map(str::trim).find(|line| !line.is_empty()); let excerpt = match first_line { Some(line) => line.to_owned(), None if let Some(line) = forwarded_texts(record) .iter() .flat_map(|text| text.lines()) .map(str::trim) .find(|line| !line.is_empty()) => { line.to_owned() } None if !images(record).is_empty() => "Image".to_owned(), None if let Some(line) = card_texts(record) .iter() .flat_map(|text| text.lines()) .next() => { line.to_owned() } None => files(record) .into_iter() .next() .map_or_else(|| "Message".to_owned(), |card| card.name), }; if excerpt.chars().count() > REPLY_EXCERPT_CHARS { let cut: String = excerpt.chars().take(REPLY_EXCERPT_CHARS).collect(); format!("{}...", cut.trim_end()) } else { excerpt } } pub struct RowContext { pub offset: FixedOffset, pub today: NaiveDate, pub my_user_id: Option, pub names: HashMap, pub pending_reactions: PendingReactions, pub card_overrides: HashMap, pub translation: TranslationContext, pub skin_tone: u8, pub meeting: MeetingInfo, } fn visible_reactions(record: &MessageRecord, context: &RowContext) -> Vec { let mut visible = reactions(record); apply_pending_reactions( &mut visible, context.pending_reactions.get(&record.message_id), context.my_user_id.as_deref(), Utc::now(), ); visible } fn call_row( record: &MessageRecord, event: &CallEvent, context: &RowContext, timeline: &CallTimeline, ) -> MessageRow { let my_user_id = context.my_user_id.as_deref(); let time = clock_label(record.created_at, context.offset); let names = CallNames { my_user_id, names: &context.names, }; let author = event .initiator_name .clone() .or_else(|| { event .initiator_id .as_ref() .and_then(|user_id| context.names.get(user_id).cloned()) }) .unwrap_or_else(|| UNKNOWN_AUTHOR.to_owned()); let card = call_card( CallSource { event, message_id: &record.message_id, created_at: record.created_at, time: &time, }, &names, &CallClock { offset: context.offset, today: context.today, meeting: &context.meeting, }, timeline, ); event_row( record, context, EventAuthor { sender_id: event.initiator_id.clone(), name: author, own: event.initiated_by(my_user_id), }, card, time, ) } fn roster_row(record: &MessageRecord, event: &RosterEvent, context: &RowContext) -> MessageRow { let my_user_id = context.my_user_id.as_deref(); let names = CallNames { my_user_id, names: &context.names, }; let card = roster_card( event, record.created_at, &names, &CallClock { offset: context.offset, today: context.today, meeting: &context.meeting, }, ); let author = event .initiator_name .clone() .or_else(|| { event .initiator_id .as_ref() .and_then(|user_id| context.names.get(user_id).cloned()) }) .unwrap_or_else(|| UNKNOWN_AUTHOR.to_owned()); let own = my_user_id.is_some() && event.initiator_id.as_deref() == my_user_id; event_row( record, context, EventAuthor { sender_id: event.initiator_id.clone(), name: author, own, }, card, clock_label(record.created_at, context.offset), ) } struct EventAuthor { sender_id: Option, name: String, own: bool, } fn event_row( record: &MessageRecord, context: &RowContext, author: EventAuthor, card: CallCard, time: String, ) -> MessageRow { MessageRow { key: record.message_id.clone(), conversation_id: record.conversation_id.clone(), sender_id: author.sender_id, application_id: None, created_at: record.created_at, series: Series::default(), author: author.name, day_header: Some(format::day_label( record .created_at .with_timezone(&context.offset) .date_naive(), context.today, )), blocks: Vec::new(), jumbo: None, edited: false, deleted: false, reactions: Vec::new(), images: Vec::new(), local_images: Vec::new(), files: Vec::new(), link_preview: None, meeting_link: None, adaptive_cards: Vec::new(), subject: None, importance: MessageImportance::Normal, new_marker: false, reply_root: None, delivery: Delivery::Delivered, receipt: Receipt::Hidden, own: author.own, forwarded: false, translation: None, call: Some(card), time, } } const QUOTE_LINES: usize = 5; pub fn quoted_texts(records: &[MessageRecord]) -> HashMap { let wanted: std::collections::HashSet = records .iter() .flat_map(quotes) .map(|quote| quote.message_id) .collect(); records .iter() .filter(|record| !record.deleted && wanted.contains(&record.message_id)) .map(|record| { let mut text = readable_text(record); if text.is_empty() { text = forwarded_texts(record).join("\n"); } let lines: Vec<&str> = text.lines().filter(|line| !line.trim().is_empty()).collect(); let mut shown = lines.iter().take(QUOTE_LINES).copied().collect::>().join("\n"); if lines.len() > QUOTE_LINES { shown.push_str(" …"); } (record.message_id.clone(), shown) }) .collect() } fn forward_header(forward: &ForwardInfo, context: &RowContext) -> ForwardHeader { ForwardHeader { sender: forward .sender_id .as_ref() .and_then(|user_id| context.names.get(user_id).cloned()), time: forward .sent_at .map(|time| format::post_time_label(time, context.today, context.offset)), } } /// User ids a row needs a name for beyond its author: original senders of forwards. pub fn forward_sender_ids(records: &[MessageRecord]) -> Vec { let mut user_ids: Vec = Vec::new(); for user_id in records .iter() .flat_map(forwards) .filter_map(|forward| forward.sender_id) { if !user_ids.contains(&user_id) { user_ids.push(user_id); } } user_ids } pub fn message_row(record: &MessageRecord, context: &RowContext) -> MessageRow { message_row_with(record, context, &HashMap::new()) } fn message_row_with( record: &MessageRecord, context: &RowContext, quoted: &HashMap, ) -> MessageRow { if let Some(event) = call_event(record) { let events = [Some(event.clone())]; return call_row(record, &event, context, &CallTimeline::new(&events)); } if let Some(event) = roster_event(record) { return roster_row(record, &event, context); } let local = record.created_at.with_timezone(&context.offset); let images = images(record); let own = context.my_user_id.is_some() && record.sender_id == context.my_user_id; let translation = context.translation.resolve(record, own); let forward_header = |forward: &ForwardInfo| forward_header(forward, context); let blocks = if record.deleted { vec![Block::Paragraph(Inline::plain(DELETED_TEXT))] } else if let Some(html) = &translation.translated_html { layout_blocks(&translated_spans(record, html, &forward_header)) } else { layout_blocks(&linked_message_spans_with( record, &|id| quoted.get(id).cloned(), &forward_header, )) }; let blocks = if images.is_empty() { blocks } else { strip_image_placeholders(blocks) }; MessageRow { key: record.message_id.clone(), conversation_id: record.conversation_id.clone(), sender_id: record.sender_id.clone(), application_id: record.sender_application_id.clone(), created_at: record.created_at, series: Series::default(), author: record .sender_name .clone() .unwrap_or_else(|| UNKNOWN_AUTHOR.to_owned()), time: clock_label(record.created_at, context.offset), day_header: Some(format::day_label(local.date_naive(), context.today)), blocks, jumbo: (!record.deleted && !is_forwarded(record)) .then(|| jumbo_emoji(&record.body_html)) .flatten(), edited: record.edited_at.is_some() && !record.deleted, deleted: record.deleted, reactions: reaction_chips(&visible_reactions(record, context), context), images, local_images: Vec::new(), files: files(record), link_preview: link_preview(record), meeting_link: (!record.deleted) .then(|| meeting_link_in_html(&record.body_html)) .flatten(), adaptive_cards: match context.card_overrides.get(&record.message_id) { Some(replaced) if replaced.basis == record.attachments_json => replaced.cards.clone(), _ => adaptive_cards(record), }, subject: record.subject.clone(), importance: if record.deleted { MessageImportance::Normal } else { record.importance }, new_marker: false, reply_root: record.reply_to_id.clone(), delivery: Delivery::Delivered, receipt: Receipt::Hidden, own, forwarded: is_forwarded(record), translation: translation.line, call: None, } } pub fn flat_rows(records: &[MessageRecord], context: &RowContext, has_older: bool) -> Vec { let mut rows = Vec::with_capacity(records.len() + 1); if has_older { rows.push(Row::LoadOlder); } let events: Vec> = records.iter().map(call_event).collect(); let timeline = CallTimeline::with_records(&events, &records.iter().collect::>()); let mut previous_day: Option = None; let quoted = quoted_texts(records); for (index, (record, event)) in records.iter().zip(&events).enumerate() { if timeline.hides(index) { continue; } let mut row = match event { Some(event) => call_row(record, event, context, &timeline), None => message_row_with(record, context, "ed), }; if let Some(call) = row.call.as_mut() { call.started &= timeline.is_live_start(index); } let day = record .created_at .with_timezone(&context.offset) .date_naive(); if previous_day == Some(day) { row.day_header = None; } previous_day = Some(day); rows.push(Row::Message(Box::new(row))); } assign_series(&mut rows); rows } pub fn group_threads(records: &[MessageRecord]) -> Vec { let mut threads: Vec = Vec::new(); let mut index_by_root: BTreeMap<&str, usize> = BTreeMap::new(); for record in records.iter().filter(|record| record.reply_to_id.is_none()) { index_by_root.insert(record.message_id.as_str(), threads.len()); threads.push(Thread { root_id: record.message_id.clone(), reply_ids: Vec::new(), }); } for record in records { let Some(root_id) = record.reply_to_id.as_deref() else { continue; }; if let Some(&index) = index_by_root.get(root_id) { threads[index].reply_ids.push(record.message_id.clone()); } } threads } fn last_activity(thread: &Thread, by_id: &BTreeMap<&str, &MessageRecord>) -> DateTime { thread .reply_ids .iter() .chain(std::iter::once(&thread.root_id)) .filter_map(|id| by_id.get(id.as_str())) .map(|record| record.created_at) .max() .unwrap_or_default() } fn post_message_row( record: &MessageRecord, context: &RowContext, timeline: &CallTimeline, ) -> MessageRow { let mut row = match call_event(record) { Some(event) => call_row(record, &event, context, timeline), None => message_row(record, context), }; row.day_header = None; row.time = format::post_time_label(record.created_at, context.today, context.offset); row } fn meeting_root_row( root: &MessageRecord, started: &CallEvent, ended: Option<(&MessageRecord, CallEvent)>, context: &RowContext, timeline: &CallTimeline, ) -> MessageRow { let Some((ended_record, ended_event)) = ended else { let mut row = post_message_row(root, context, timeline); if let Some(call) = row.call.as_mut() { call.started = !timeline.finished(&started.call_id); } return row; }; let mut summary = CallEvent { initiator_id: started.initiator_id.clone(), initiator_name: started .initiator_name .clone() .or(ended_event.initiator_name.clone()), ..ended_event }; summary.call_type = started.call_type; let mut row = call_row(ended_record, &summary, context, timeline); row.key = root.message_id.clone(); row.created_at = root.created_at; row.reply_root = None; row.day_header = None; row.time = format::post_time_label(root.created_at, context.today, context.offset); row } fn distinct_names<'a>(records: impl Iterator) -> Vec { let mut names: Vec = Vec::new(); for record in records.filter(|record| record.call_event_json.is_none()) { let name = record .sender_name .clone() .unwrap_or_else(|| UNKNOWN_AUTHOR.to_owned()); if !names.contains(&name) { names.push(name); } } names } fn post_row( root: MessageRow, mut replies: Vec<&MessageRecord>, context: &RowContext, timeline: &CallTimeline, ) -> PostRow { replies.sort_by_key(|reply| reply.created_at); let hidden = replies.len().saturating_sub(POST_VISIBLE_REPLIES); PostRow { root, hidden_reply_count: hidden, hidden_repliers: distinct_names(replies[..hidden].iter().copied()), replies: replies[hidden..] .iter() .map(|reply| post_message_row(reply, context, timeline)) .collect(), } } /// Channel feed: Teams shows a Meet now meeting as its own post and keeps the other call notices /// inside the thread; a saved recording stays visible as a reply. fn shows_in_feed(record: &MessageRecord, event: Option<&CallEvent>) -> bool { match event { None => true, Some(event) if record.reply_to_id.is_none() => event.phase == CallPhase::Started, Some(event) => event.phase == CallPhase::RecordingSaved, } } pub fn thread_list_rows( records: &[MessageRecord], context: &RowContext, has_older: bool, ) -> Vec { let events: Vec> = records.iter().map(call_event).collect(); let timeline = CallTimeline::new(&events); let ended: HashMap<&str, (&MessageRecord, &CallEvent)> = records .iter() .zip(&events) .filter_map(|(record, event)| { let event = event .as_ref() .filter(|event| event.phase == CallPhase::Ended)?; Some((event.call_id.as_str(), (record, event))) }) .collect(); let by_id: BTreeMap<&str, (&MessageRecord, Option<&CallEvent>)> = records .iter() .zip(&events) .filter(|(record, event)| shows_in_feed(record, event.as_ref())) .map(|(record, event)| (record.message_id.as_str(), (record, event.as_ref()))) .collect(); let records_by_id: BTreeMap<&str, &MessageRecord> = by_id .iter() .map(|(id, (record, _))| (*id, *record)) .collect(); let mut threads = group_threads(records); threads.sort_by_key(|thread| std::cmp::Reverse(last_activity(thread, &records_by_id))); let mut rows: Vec = threads .iter() .filter_map(|thread| { let (root, root_event) = by_id.get(thread.root_id.as_str())?; let root_row = match root_event { Some(started) => meeting_root_row( root, started, ended .get(started.call_id.as_str()) .map(|(record, event)| (*record, (*event).clone())), context, &timeline, ), None => post_message_row(root, context, &timeline), }; let replies = thread .reply_ids .iter() .filter_map(|id| records_by_id.get(id.as_str()).copied()) .collect(); Some(Row::Post(Box::new(post_row( root_row, replies, context, &timeline, )))) }) .collect(); if has_older { rows.push(Row::LoadOlder); } rows } pub fn place_local_rows(rows: &mut Vec, local: Vec) { for message in local { let Some(root_id) = message.reply_root.clone() else { rows.insert(0, Row::Post(Box::new(PostRow::local(message)))); continue; }; let position = rows .iter() .position(|row| matches!(row, Row::Post(post) if post.root.key == root_id)); if let Some(position) = position { let mut row = rows.remove(position); if let Row::Post(post) = &mut row { post.replies.push(message); } rows.insert(0, row); } } } pub fn thread_rows(records: &[MessageRecord], root_id: &str, context: &RowContext) -> Vec { let members: Vec = records .iter() .filter(|record| { record.message_id == root_id || record.reply_to_id.as_deref() == Some(root_id) }) .cloned() .collect(); flat_rows(&members, context, false) } #[derive(Debug, Clone, PartialEq, Eq)] pub struct Splice { pub range: Range, pub count: usize, } pub fn diff_keys(old: &[String], new: &[String]) -> Option { if old == new { return None; } let prefix = old .iter() .zip(new) .take_while(|(left, right)| left == right) .count(); let suffix = old[prefix..] .iter() .rev() .zip(new[prefix..].iter().rev()) .take_while(|(left, right)| left == right) .count(); Some(Splice { range: prefix..old.len() - suffix, count: new.len() - prefix - suffix, }) } pub fn changed_indices(old: &[Row], new: &[Row]) -> Vec { let prefix = old .iter() .zip(new) .take_while(|(left, right)| left.key() == right.key()) .count(); let suffix = old[prefix..] .iter() .rev() .zip(new[prefix..].iter().rev()) .take_while(|(left, right)| left.key() == right.key()) .count(); let mut changed: Vec = (0..prefix) .filter(|&index| old[index] != new[index]) .collect(); changed.extend((0..suffix).filter_map(|offset| { let old_index = old.len() - 1 - offset; let new_index = new.len() - 1 - offset; (old[old_index] != new[new_index]).then_some(new_index) })); changed } #[cfg(test)] mod tests { use chrono::TimeZone; use teams_core::CallType; use super::*; fn record(id: &str, reply_to: Option<&str>, hour: u32, day: u32) -> MessageRecord { MessageRecord { conversation_id: "c".into(), message_id: id.into(), reply_to_id: reply_to.map(str::to_owned), sender_id: Some("u".into()), sender_name: Some("Author".into()), sender_application_id: None, links_json: "[]".to_owned(), created_at: Utc.with_ymd_and_hms(2026, 10, day, hour, 0, 0).unwrap(), edited_at: None, deleted: false, body_html: format!("

text {id}

"), attachments_json: "[]".into(), reactions_json: "[]".into(), mentions_json: "[]".into(), subject: None, call_event_json: None, importance: Default::default(), } } #[test] fn a_quote_shows_the_lines_of_the_quoted_message_when_it_is_loaded() { let mut quoted = record("q1", None, 8, 6); quoted.body_html = "

first link

second link

".into(); let mut answer = record("a1", None, 9, 6); answer.body_html = "

please again

".into(); answer.attachments_json = r#"[{"content_type":"messageReference","name":null,"url":null,"text":null,"id":"q1","quote":{"message_id":"q1","preview":"first link second link","sender_name":"Robin"}}]"#.into(); let rows = flat_rows(&[quoted, answer], &context(), false); let reply = rows .iter() .find_map(|row| match row { Row::Message(row) if row.key == "a1" => Some(row.blocks.clone()), _ => None, }) .unwrap(); let Some(Block::Reply { children, .. }) = reply.first() else { panic!("{reply:?}") }; let text = format!("{children:?}"); assert!(text.contains(r"first link\nsecond link"), "{text}"); } fn own_reaction(reaction_type: &str) -> ReactionInfo { ReactionInfo { reaction_type: reaction_type.into(), user_id: Some("me".into()), user_name: None, created_at: None, } } #[test] fn setting_a_reaction_is_idempotent_and_only_touches_mine() { let now = Utc.with_ymd_and_hms(2026, 10, 1, 8, 0, 0).unwrap(); let other = ReactionInfo { user_id: Some("them".into()), ..own_reaction("like") }; let mut reactions = vec![other.clone()]; set_own_reaction(&mut reactions, "\u{1F44D}", Some("me"), true, now); set_own_reaction(&mut reactions, "\u{1F44D}", Some("me"), true, now); assert_eq!(reactions.len(), 2); assert_eq!(reactions[1].reaction_type, "like"); assert_eq!(reactions[1].user_id.as_deref(), Some("me")); set_own_reaction(&mut reactions, "\u{1F44D}", Some("me"), false, now); set_own_reaction(&mut reactions, "\u{1F44D}", Some("me"), false, now); assert_eq!(reactions, vec![other]); } #[test] fn setting_matches_legacy_names_and_variation_selectors() { let now = Utc.with_ymd_and_hms(2026, 10, 1, 8, 0, 0).unwrap(); let mut reactions = vec![own_reaction("heart")]; assert!(has_own_reaction(&reactions, "\u{2764}\u{FE0F}", Some("me"))); set_own_reaction(&mut reactions, "\u{2764}\u{FE0F}", Some("me"), false, now); assert!(reactions.is_empty()); set_own_reaction(&mut reactions, "\u{1F389}", Some("me"), true, now); assert_eq!(reactions[0].reaction_type, "\u{1F389}"); } #[test] fn setting_without_a_known_user_never_matches() { let now = Utc.with_ymd_and_hms(2026, 10, 1, 8, 0, 0).unwrap(); let mut reactions = Vec::new(); set_own_reaction(&mut reactions, "\u{1F44D}", None, true, now); set_own_reaction(&mut reactions, "\u{1F44D}", None, true, now); assert_eq!(reactions.len(), 2); } #[test] fn pending_reactions_overlay_the_stored_ones() { let mut stored = record("a", None, 8, 6); stored.reactions_json = serde_json::to_string(&[own_reaction("like")]).unwrap(); let mut context = context(); context.my_user_id = Some("me".into()); context.pending_reactions.insert( "a".into(), vec![("\u{1F44D}".into(), false), ("\u{1F389}".into(), true)], ); let chips = message_row(&stored, &context).reactions; assert_eq!(chips.len(), 1); assert_eq!(chips[0].reaction_type, "\u{1F389}"); assert!(chips[0].mine); } #[test] fn org_emoji_shows_its_name_and_loads_its_image() { let key = "absolute-cinema;0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8"; assert_eq!(reaction_text(key), ":absolute-cinema:"); assert_eq!(reaction_text("like"), "\u{1F44D}"); let image = custom_emoji_image(key).expect("image"); assert!(image.url.ends_with("/0-wcde-d3-5f5e4f463d6bb959b08614fd1d9680b8/views/imgt2_anim")); assert_eq!(custom_emoji_image("like"), None); } #[test] fn reaction_type_maps_classic_glyphs_to_names() { assert_eq!(reaction_type_for("\u{1F44D}"), "like"); assert_eq!(reaction_type_for("\u{2764}\u{FE0F}"), "heart"); assert_eq!(reaction_type_for("\u{1F389}"), "\u{1F389}"); assert_eq!(reaction_glyph("like"), reaction_glyph("\u{1F44D}")); assert_eq!(reaction_glyph("heart"), reaction_glyph("\u{2764}\u{FE0F}")); assert_eq!(api_reaction(&reaction_glyph("heart")), "\u{2764}\u{FE0F}"); assert_eq!(api_reaction(&reaction_glyph("like")), "\u{1F44D}"); } #[test] fn forwarded_messages_are_detected_by_the_forward_blockquote() { let mut forwarded = record("f", None, 8, 6); forwarded.body_html = r#"

fyi

text
"#.into(); assert!(message_row(&forwarded, &context()).forwarded); forwarded.body_html = r#"
text
"#.into(); assert!(!message_row(&forwarded, &context()).forwarded); forwarded.body_html = r#"
text
"#.into(); forwarded.deleted = true; assert!(!message_row(&forwarded, &context()).forwarded); } #[test] fn a_graph_forward_shows_the_original_sender_and_text_in_a_quote() { let mut forwarded = record("f", None, 8, 6); forwarded.body_html = r#""#.into(); forwarded.attachments_json = serde_json::json!([{ "content_type": "forwardedMessageReference", "name": null, "url": null, "text": null, "id": "9", "content": serde_json::json!({ "originalMessageId": "9", "originalMessageContent": "

original words

", "originalSentDateTime": "2026-10-06T07:00:00.000+00:00", "originalMessageSender": {"user": {"id": "orig", "displayName": null}} }).to_string() }]) .to_string(); let mut context = context(); context.names.insert("orig".into(), "Priya Nair".into()); let row = message_row(&forwarded, &context); assert!(row.forwarded); assert!(row.jumbo.is_none()); let [Block::Quote(children)] = row.blocks.as_slice() else { panic!("expected one forward quote, got {:?}", row.blocks); }; let lines: Vec = children.iter().filter_map(Block::markdown).collect(); assert!(lines[0].contains("Priya Nair Yesterday 07:00"), "{lines:?}"); assert!(lines.iter().any(|line| line.contains("original words")), "{lines:?}"); assert_eq!(reply_excerpt(&forwarded), "original words"); assert_eq!(message_text(&forwarded), "original words"); } #[test] fn editing_loads_the_message_formatted_without_its_reply_quote() { let mut edited = record("a", None, 8, 6); edited.body_html = "
old
\

Plan for today

\
  1. one
  2. two
" .into(); let draft = message_draft(&edited); assert_eq!(draft.text(), "Plan for today\n1. one\n2. two"); assert_eq!( draft.to_html(), "Plan for today
  1. one
  2. two
" ); } #[test] fn reply_excerpt_uses_the_first_line_or_the_attachment() { let mut text = record("a", None, 8, 6); text.body_html = "

first line

second

".into(); assert_eq!(reply_excerpt(&text), "first line"); let mut quoted = record("q", None, 8, 6); quoted.body_html = r#"
Jonas
old text

my answer

"#.into(); assert_eq!(reply_excerpt("ed), "my answer"); let mut image = record("b", None, 8, 6); image.body_html = "".into(); assert_eq!(reply_excerpt(&image), "Image"); let mut long = record("c", None, 8, 6); long.body_html = format!("

{}

", "x".repeat(200)); assert!(reply_excerpt(&long).ends_with("...")); assert_eq!( reply_excerpt(&long).chars().count(), REPLY_EXCERPT_CHARS + 3 ); } fn context() -> RowContext { RowContext { offset: FixedOffset::east_opt(0).unwrap(), today: NaiveDate::from_ymd_opt(2026, 10, 7).unwrap(), my_user_id: None, names: HashMap::new(), pending_reactions: HashMap::new(), card_overrides: HashMap::new(), translation: TranslationContext::default(), skin_tone: 0, meeting: MeetingInfo::default(), } } fn keys(rows: &[Row]) -> Vec { rows.iter().map(|row| row.key().to_owned()).collect() } fn translation_context(shown: bool) -> RowContext { use crate::translation::CachedTranslation; let mut context = context(); context.translation.enabled = true; context.translation.target = "en".into(); context.translation.stamps.insert( "a".into(), "languages=fr:100;length:80;&detector=Bling".into(), ); context.my_user_id = Some("me".into()); if shown { context.translation.cached.insert( ("a".into(), 0), CachedTranslation { html: "

translated body

".into(), source: Some("fr".into()), }, ); context.translation.shown.insert("a".into()); } context } #[test] fn a_foreign_message_carries_the_offer_and_keeps_its_text() { let row = message_row(&record("a", None, 8, 6), &translation_context(false)); assert!(matches!( row.translation, Some(TranslationLine::Offer { ref language_code, .. }) if language_code == "fr" )); assert_eq!( row.blocks, layout_blocks(&teams_core::linked_message_spans(&record("a", None, 8, 6))) ); } #[test] fn a_shown_translation_replaces_the_body_and_the_original_comes_back() { let original = message_row(&record("a", None, 8, 6), &translation_context(false)).blocks; let translated = message_row(&record("a", None, 8, 6), &translation_context(true)); assert_ne!(translated.blocks, original); assert_eq!( translated.translation, Some(TranslationLine::Translated { language: Some("French".into()), showing_original: false }) ); let mut hidden = translation_context(true); hidden.translation.shown.clear(); let back = message_row(&record("a", None, 8, 6), &hidden); assert_eq!(back.blocks, original); assert_eq!( back.translation, Some(TranslationLine::Translated { language: Some("French".into()), showing_original: true }) ); } #[test] fn own_messages_get_no_offer() { let mut own = record("a", None, 8, 6); own.sender_id = Some("me".into()); assert_eq!( message_row(&own, &translation_context(false)).translation, None ); } #[test] fn day_header_only_on_first_message_of_a_day() { let records = vec![ record("a", None, 8, 6), record("b", None, 9, 6), record("c", None, 9, 7), ]; let rows = flat_rows(&records, &context(), false); let headers: Vec> = rows .iter() .map(|row| match row { Row::Message(message) => message.day_header.clone(), Row::LoadOlder | Row::LoadNewer | Row::Start(_) | Row::Skeleton(_) | Row::Post(_) => None, }) .collect(); assert_eq!( headers, vec![Some("Yesterday".into()), None, Some("Today".into())] ); } #[test] fn load_older_row_only_when_more_exists() { let records = vec![record("a", None, 8, 6)]; assert_eq!( keys(&flat_rows(&records, &context(), true))[0], LOAD_OLDER_KEY ); assert_eq!(keys(&flat_rows(&records, &context(), false)), vec!["a"]); } #[test] fn threads_group_replies_by_root() { let records = vec![ record("r1", None, 8, 6), record("r2", None, 9, 6), record("x1", Some("r1"), 10, 6), record("x2", Some("r1"), 11, 6), record("orphan", Some("missing"), 12, 6), ]; let threads = group_threads(&records); assert_eq!(threads.len(), 2); assert_eq!(threads[0].reply_ids, vec!["x1", "x2"]); assert!(threads[1].reply_ids.is_empty()); } fn post_of(row: &Row) -> &PostRow { let Row::Post(post) = row else { panic!("post expected") }; post } fn reply_by(id: &str, root: &str, sender: &str, hour: u32) -> MessageRecord { let mut reply = record(id, Some(root), hour, 6); reply.sender_name = Some(sender.into()); reply } #[test] fn feed_orders_posts_by_last_activity_newest_first() { let records = vec![ record("r1", None, 8, 6), record("r2", None, 9, 6), record("r3", None, 10, 6), record("x1", Some("r1"), 11, 6), ]; let rows = thread_list_rows(&records, &context(), false); assert_eq!(keys(&rows), vec!["r1", "r3", "r2"]); } #[test] fn feed_puts_load_older_at_the_bottom() { let records = vec![record("r1", None, 8, 6), record("r2", None, 9, 6)]; let rows = thread_list_rows(&records, &context(), true); assert_eq!(keys(&rows), vec!["r2", "r1", LOAD_OLDER_KEY]); } #[test] fn post_shows_the_last_three_replies_oldest_first() { let mut records = vec![record("r1", None, 1, 6)]; for hour in 2..=6 { records.push(reply_by(&format!("x{hour}"), "r1", "Ada Lovelace", hour)); } records.reverse(); let rows = thread_list_rows(&records, &context(), false); let post = post_of(&rows[0]); let shown: Vec<&str> = post .replies .iter() .map(|reply| reply.key.as_str()) .collect(); assert_eq!(shown, vec!["x4", "x5", "x6"]); assert_eq!(post.hidden_reply_count, 2); assert_eq!(post.hidden_repliers, vec!["Ada Lovelace"]); } #[test] fn post_without_hidden_replies_has_no_link() { let records = vec![ record("r1", None, 1, 6), reply_by("x1", "r1", "Ada", 2), reply_by("x2", "r1", "Ben", 3), reply_by("x3", "r1", "Cy", 4), ]; let rows = thread_list_rows(&records, &context(), false); assert_eq!(post_of(&rows[0]).hidden_replies_label(), None); assert_eq!(post_of(&rows[0]).replies.len(), 3); } fn label_for(repliers: &[&str]) -> String { let mut records = vec![record("r1", None, 1, 6)]; for (index, name) in repliers.iter().enumerate() { records.push(reply_by(&format!("h{index}"), "r1", name, 2 + index as u32)); } for index in 0..POST_VISIBLE_REPLIES { records.push(reply_by( &format!("v{index}"), "r1", "Shown", 20 + index as u32, )); } let rows = thread_list_rows(&records, &context(), false); post_of(&rows[0]).hidden_replies_label().unwrap() } #[test] fn replies_label_names_one_to_three_people() { assert_eq!(label_for(&["Anna Meier"]), "1 reply from Anna"); assert_eq!( label_for(&["Anna Meier", "Anna Meier", "Ben Roth"]), "3 replies from Anna and Ben" ); assert_eq!( label_for(&["Anna Meier", "Ben Roth", "Clara Voss", "Ben Roth"]), "4 replies from Anna, Ben and Clara" ); } #[test] fn replies_label_counts_the_others_beyond_three_people() { assert_eq!( label_for(&["Anna Meier", "Ben Roth", "Clara Voss", "Dan Fox", "Eve Ray"]), "5 replies from Anna, Ben and 3 others" ); assert_eq!( label_for(&["Anna Meier", "Ben Roth", "Clara Voss", "Dan Fox"]), "4 replies from Anna, Ben and 2 others" ); } #[test] fn local_reply_lands_in_its_post_and_local_post_on_top() { let records = vec![ record("r1", None, 8, 6), record("r2", None, 9, 6), reply_by("x1", "r1", "Ada", 10), ]; let mut rows = thread_list_rows(&records, &context(), true); let mut pending_reply = message_row(&record("pending-1", Some("r2"), 12, 6), &context()); pending_reply.reply_root = Some("r2".into()); let pending_post = message_row(&record("pending-2", None, 12, 6), &context()); place_local_rows(&mut rows, vec![pending_reply, pending_post]); assert_eq!(keys(&rows), vec!["pending-2", "r2", "r1", LOAD_OLDER_KEY]); let replies: Vec<&str> = post_of(&rows[1]) .replies .iter() .map(|reply| reply.key.as_str()) .collect(); assert_eq!(replies, vec!["pending-1"]); } #[test] fn local_reply_to_an_unloaded_post_is_not_shown() { let mut rows = thread_list_rows(&[record("r1", None, 8, 6)], &context(), false); let mut orphan = message_row(&record("pending-1", Some("gone"), 12, 6), &context()); orphan.reply_root = Some("gone".into()); place_local_rows(&mut rows, vec![orphan]); assert_eq!(keys(&rows), vec!["r1"]); } #[test] fn subject_reaches_the_post_root() { let mut with_subject = record("r1", None, 8, 6); with_subject.subject = Some("Release plan".into()); let rows = thread_list_rows(&[with_subject, record("r2", None, 7, 6)], &context(), false); assert_eq!( post_of(&rows[0]).root.subject.as_deref(), Some("Release plan") ); assert_eq!(post_of(&rows[1]).root.subject, None); } #[test] fn thread_view_has_root_then_replies() { let records = vec![ record("r1", None, 8, 6), record("r2", None, 9, 6), record("x1", Some("r1"), 10, 6), ]; assert_eq!( keys(&thread_rows(&records, "r1", &context())), vec!["r1", "x1"] ); } #[test] fn reactions_are_counted_in_first_seen_order() { let reaction = |kind: &str| ReactionInfo { reaction_type: kind.into(), user_id: None, user_name: None, created_at: None, }; let chips = reaction_chips( &[reaction("heart"), reaction("like"), reaction("heart")], &context(), ); assert_eq!(chips.len(), 2); assert_eq!(chips[0].count, 2); assert_eq!(chips[1].label, reaction_label("like")); } #[test] fn legacy_laugh_and_sad_use_the_teams_glyphs() { assert_eq!(reaction_label("laugh"), "\u{1F606}"); assert_eq!(reaction_label("sad"), "\u{1F641}"); assert_eq!(reaction_type_for("\u{1F606}"), "laugh"); assert_eq!(reaction_type_for("\u{1F641}"), "sad"); } #[test] fn legacy_and_emoji_reactions_share_a_chip() { let reaction = |kind: &str| ReactionInfo { reaction_type: kind.into(), user_id: None, user_name: Some("Lea".into()), created_at: None, }; let chips = reaction_chips(&[reaction("like"), reaction("\u{1F44D}")], &context()); assert_eq!(chips.len(), 1); assert_eq!(chips[0].count, 2); } #[test] fn skin_tones_share_a_chip_showing_up_to_three_variants() { let reaction = |kind: &str| ReactionInfo { reaction_type: kind.into(), user_id: Some(kind.into()), user_name: Some("Lea".into()), created_at: None, }; let kinds = ["yes-tone2", "πŸ‘πŸΏ", "like", "πŸ‘πŸ»", "yes-tone2"]; let mut context = context(); context.skin_tone = 3; let chips = reaction_chips(&kinds.map(reaction), &context); assert_eq!(chips.len(), 1); assert_eq!(chips[0].count, 5); assert_eq!(chips[0].label, "πŸ‘πŸΌπŸ‘πŸΏπŸ‘"); assert_eq!(chips[0].click_glyph, "πŸ‘πŸ½"); } #[test] fn own_toned_reaction_matches_the_plain_glyph() { let mine = ReactionInfo { reaction_type: "πŸ‘πŸΌ".into(), user_id: Some("me".into()), user_name: None, created_at: None, }; let reactions = vec![mine]; assert!(has_own_reaction(&reactions, "\u{1F44D}", Some("me"))); assert_eq!( own_reaction_glyph(&reactions, "like", Some("me")).as_deref(), Some("πŸ‘πŸΌ") ); assert_eq!(reaction_glyph("yes-tone5"), "πŸ‘πŸΏ"); } #[test] fn reactor_name_falls_back_to_directory_then_unknown() { let reaction = |user: &str| ReactionInfo { reaction_type: "like".into(), user_id: Some(user.into()), user_name: None, created_at: None, }; let mut context = context(); context.names.insert("known".into(), "Priya Nair".into()); let chips = reaction_chips(&[reaction("known"), reaction("other")], &context); assert_eq!(chips[0].reactors[0].name, "Priya Nair"); assert_eq!(chips[0].reactors[1].name, "Unknown"); } fn record_by(id: &str, sender: &str, hour: u32, minute: u32, day: u32) -> MessageRecord { let mut record = record(id, None, hour, day); record.sender_id = Some(sender.into()); record.created_at = Utc .with_ymd_and_hms(2026, 10, day, hour, minute, 0) .unwrap(); record } fn series_of(rows: &[Row]) -> Vec<(bool, bool)> { rows.iter() .filter_map(|row| match row { Row::Message(message) => Some((message.series.has_prev, message.series.has_next)), Row::LoadOlder | Row::LoadNewer | Row::Start(_) | Row::Skeleton(_) | Row::Post(_) => None, }) .collect() } #[test] fn series_joins_same_sender_within_five_minutes() { let records = vec![ record_by("a", "ada", 9, 0, 7), record_by("b", "ada", 9, 4, 7), record_by("c", "ada", 9, 20, 7), record_by("d", "bob", 9, 21, 7), record_by("e", "ada", 9, 22, 7), ]; let rows = flat_rows(&records, &context(), false); assert_eq!( series_of(&rows), vec![ (false, true), (true, false), (false, false), (false, false), (false, false) ] ); } #[test] fn series_window_ends_exactly_at_the_gap_constant() { let gap = SERIES_GAP_MINUTES as u32; let records = vec![ record_by("a", "ada", 9, 0, 7), record_by("b", "ada", 9, gap - 1, 7), record_by("c", "ada", 9, 2 * gap - 1 + gap, 7), ]; let rows = flat_rows(&records, &context(), false); assert_eq!( series_of(&rows), vec![(false, true), (true, false), (false, false)] ); } #[test] fn series_breaks_at_a_new_day() { let records = vec![ record_by("a", "ada", 23, 58, 6), record_by("b", "ada", 0, 1, 7), ]; let rows = flat_rows(&records, &context(), false); assert_eq!(series_of(&rows), vec![(false, false), (false, false)]); } fn corners(own: bool, has_prev: bool, has_next: bool) -> [f32; 4] { let corners = bubble_corners(own, Series { has_prev, has_next }); [ corners.top_left, corners.top_right, corners.bottom_right, corners.bottom_left, ] } #[test] fn corners_match_the_mockup_for_other_senders() { assert_eq!(corners(false, false, false), [14., 14., 14., 14.]); assert_eq!(corners(false, false, true), [14., 14., 14., 4.]); assert_eq!(corners(false, true, true), [4., 14., 14., 4.]); assert_eq!(corners(false, true, false), [4., 14., 14., 14.]); } #[test] fn corners_match_the_mockup_for_own_messages() { assert_eq!(corners(true, false, false), [14., 14., 14., 14.]); assert_eq!(corners(true, false, true), [14., 14., 4., 14.]); assert_eq!(corners(true, true, true), [14., 4., 4., 14.]); assert_eq!(corners(true, true, false), [14., 4., 14., 14.]); } #[test] fn own_reaction_is_marked() { let reaction = |user: &str| ReactionInfo { reaction_type: "like".into(), user_id: Some(user.into()), user_name: None, created_at: None, }; let mut context = context(); context.my_user_id = Some("me".into()); let chips = reaction_chips(&[reaction("x"), reaction("me")], &context); assert!(chips[0].mine); assert!(!reaction_chips(&[reaction("x")], &context)[0].mine); } fn strings(values: &[&str]) -> Vec { values.iter().map(|value| (*value).to_owned()).collect() } #[test] fn diff_detects_append_prepend_and_replace() { let base = strings(&["a", "b"]); assert_eq!(diff_keys(&base, &base), None); assert_eq!( diff_keys(&base, &strings(&["a", "b", "c"])), Some(Splice { range: 2..2, count: 1 }) ); assert_eq!( diff_keys(&base, &strings(&["x", "a", "b"])), Some(Splice { range: 0..0, count: 1 }) ); assert_eq!( diff_keys( &strings(&["load-older", "a"]), &strings(&["load-older", "x", "y", "a"]) ), Some(Splice { range: 1..1, count: 2 }) ); assert_eq!( diff_keys(&strings(&["load-older", "a"]), &strings(&["a"])), Some(Splice { range: 0..1, count: 0 }) ); assert_eq!( diff_keys(&base, &strings(&["x"])), Some(Splice { range: 0..2, count: 1 }) ); } #[test] fn changed_indices_find_edited_rows() { let old = flat_rows( &[record("a", None, 8, 6), record("b", None, 9, 6)], &context(), false, ); let mut edited = record("b", None, 9, 6); edited.body_html = "

changed

".into(); let new = flat_rows(&[record("a", None, 8, 6), edited], &context(), false); assert_eq!(changed_indices(&old, &new), vec![1]); } fn call_record(id: &str, hour: u32, event: CallEvent) -> MessageRecord { MessageRecord { sender_id: None, sender_name: None, body_html: String::new(), call_event_json: Some(event.to_json()), ..record(id, None, hour, 7) } } fn call_event_of( phase: CallPhase, call_id: &str, initiator: &str, seconds: Option, participants: &[&str], ) -> CallEvent { CallEvent { phase, call_type: CallType::Call, call_id: call_id.into(), duration_seconds: seconds, initiator_id: Some(initiator.into()), initiator_name: Some(format!("Name {initiator}")), participant_ids: participants.iter().map(|id| (*id).to_owned()).collect(), participant_names: Vec::new(), recording: None, } } fn call_context() -> RowContext { let mut context = context(); context.my_user_id = Some("me".into()); context } fn call_of(row: &Row) -> &CallCard { let Row::Message(message) = row else { panic!("not a message row") }; message.call.as_ref().expect("call row") } #[test] fn started_call_is_hidden_when_the_same_call_ended() { let records = [ call_record( "s1", 9, call_event_of(CallPhase::Started, "one", "u", None, &[]), ), call_record( "e1", 10, call_event_of(CallPhase::Ended, "one", "u", Some(723), &["u", "me"]), ), call_record( "s2", 11, call_event_of(CallPhase::Started, "two", "u", None, &[]), ), ]; let rows = flat_rows(&records, &call_context(), false); assert_eq!(keys(&rows), ["e1", "s2"]); assert_eq!(call_of(&rows[0]).title, "Call ended"); assert_eq!(call_of(&rows[0]).detail, "12m 3s \u{b7} 10:00"); assert_eq!(call_of(&rows[1]).title, "Call started"); assert_eq!(call_of(&rows[1]).detail, "11:00"); assert!(call_of(&rows[1]).started); } #[test] fn only_the_newest_open_start_can_be_joined() { let records = [ call_record( "old", 9, call_event_of(CallPhase::Started, "lost", "u", None, &[]), ), call_record( "new", 11, call_event_of(CallPhase::Started, "live", "u", None, &[]), ), ]; let rows = flat_rows(&records, &call_context(), false); assert_eq!(keys(&rows), ["old", "new"]); assert!(!call_of(&rows[0]).started); assert!(call_of(&rows[1]).started); } #[test] fn missed_call_needs_an_incoming_call_with_fewer_than_two_participants() { let rows = flat_rows( &[ call_record( "incoming", 9, call_event_of(CallPhase::Ended, "a", "u", Some(60), &["u"]), ), call_record( "outgoing", 10, call_event_of(CallPhase::Ended, "b", "me", Some(60), &["me"]), ), call_record( "answered", 11, call_event_of(CallPhase::Ended, "c", "u", Some(60), &["u", "me"]), ), ], &call_context(), false, ); let missed = call_of(&rows[0]); assert!(missed.missed); assert_eq!(missed.title, "Missed call"); assert_eq!(missed.detail, "09:00"); assert_eq!(missed.icon, CallIcon::Missed); let outgoing = call_of(&rows[1]); assert!(!outgoing.missed); assert_eq!(outgoing.title, "Call ended"); assert_eq!(outgoing.detail, "1m \u{b7} 10:00"); assert!(!call_of(&rows[2]).missed); } #[test] fn call_side_follows_the_initiator() { let rows = flat_rows( &[ call_record( "mine", 9, call_event_of(CallPhase::Ended, "a", "me", Some(5), &["me", "u"]), ), call_record( "theirs", 10, call_event_of(CallPhase::Ended, "b", "u", Some(5), &["me", "u"]), ), ], &call_context(), false, ); let sides: Vec<(bool, Option<&str>)> = rows .iter() .map(|row| match row { Row::Message(message) => (message.own, message.sender_id.as_deref()), _ => unreachable!(), }) .collect(); assert_eq!(sides, [(true, Some("me")), (false, Some("u"))]); } #[test] fn meeting_events_use_the_video_icon_and_meeting_titles() { let mut event = call_event_of(CallPhase::Ended, "m", "u", Some(3723), &["u", "me", "x"]); event.call_type = CallType::Meeting; let rows = flat_rows(&[call_record("m", 9, event)], &call_context(), false); let card = call_of(&rows[0]); assert_eq!(card.title, "Meeting ended"); assert_eq!(card.icon, CallIcon::Video); assert_eq!(card.detail, "1h 2m 3s \u{b7} 09:00"); } #[test] fn call_rows_never_join_a_series() { let mut records = vec![ record_by("a", "u", 9, 0, 7), call_record( "c", 9, call_event_of(CallPhase::Ended, "x", "u", Some(5), &["u", "me"]), ), ]; records.push(record_by("b", "u", 9, 1, 7)); let rows = flat_rows(&records, &call_context(), false); let series: Vec = rows .iter() .map(|row| match row { Row::Message(message) => message.series, _ => unreachable!(), }) .collect(); assert!(series.iter().all(|series| *series == Series::default())); } fn fixture_records(name: &str) -> Vec { let path = format!( "{}/../core/tests/fixtures/call_events/{name}", env!("CARGO_MANIFEST_DIR") ); let thread: graph::Message = serde_json::from_str(&std::fs::read_to_string(path).unwrap()).unwrap(); std::iter::once(&thread) .chain(&thread.replies) .filter_map(|message| teams_core::message_record("channel", message)) .collect() } fn feed_post(row: &Row) -> &PostRow { let Row::Post(post) = row else { panic!("not a post") }; post } #[test] fn meet_now_root_is_a_post_with_the_ended_meeting_card() { let records = fixture_records("channel_adhoc_meeting.json"); assert_eq!(records.len(), 2); let rows = thread_list_rows(&records, &call_context(), false); assert_eq!(rows.len(), 1); let post = feed_post(&rows[0]); assert_eq!(post.root.key, records[0].message_id); assert!(post.replies.is_empty()); assert_eq!(post.hidden_reply_count, 0); let card = post.root.call.as_ref().unwrap(); assert_eq!(card.title, "Meeting ended"); assert!(card.detail.starts_with("3h 19m 35s")); assert!(!card.started); assert_eq!(card.participants.len(), 3); assert_eq!(card.extra_participants, 0); } #[test] fn meet_now_root_without_an_end_can_be_joined() { let mut records = fixture_records("channel_adhoc_meeting.json"); records.truncate(1); let rows = thread_list_rows(&records, &call_context(), false); let card = feed_post(&rows[0]).root.call.clone().unwrap(); assert_eq!(card.title, "Meeting started"); assert!(card.started); } #[test] fn scheduled_meeting_post_keeps_call_notices_in_the_thread() { let mut records = fixture_records("channel_scheduled_meeting.json"); records.sort_by_key(|record| record.created_at); assert_eq!(records.len(), 3); let rows = thread_list_rows(&records, &call_context(), false); let post = feed_post(&rows[0]); assert!(post.root.call.is_none()); assert!(post.replies.is_empty()); let thread = thread_rows(&records, &records[0].message_id, &call_context()); let titles: Vec = thread .iter() .filter_map(|row| match row { Row::Message(message) => message.call.as_ref().map(|call| call.title.clone()), _ => None, }) .collect(); assert_eq!(titles, ["Meeting started", "Meeting ended"]); } #[test] fn saved_recording_shows_in_the_channel_feed_as_a_reply() { let mut records = fixture_records("channel_adhoc_meeting.json"); let root_id = records[0].message_id.clone(); let page: serde_json::Value = serde_json::from_str( &std::fs::read_to_string(format!( "{}/../core/tests/fixtures/call_events/meeting_chat_recording.json", env!("CARGO_MANIFEST_DIR") )) .unwrap(), ) .unwrap(); for message in page["value"].as_array().unwrap() { let message: graph::Message = serde_json::from_value(message.clone()).unwrap(); let mut record = teams_core::message_record("channel", &message).unwrap(); record.reply_to_id = Some(root_id.clone()); records.push(record); } let rows = thread_list_rows(&records, &call_context(), false); let post = feed_post(&rows[0]); assert_eq!(post.replies.len(), 1); assert_eq!(post.hidden_reply_count, 0); let saved = post.replies[0].call.as_ref().unwrap(); assert_eq!(saved.icon, CallIcon::Saved); assert_eq!( saved.notice.as_ref().unwrap().text, "Recording has been saved to OneDrive." ); let thread = thread_rows(&records, &root_id, &call_context()); let calls = thread .iter() .filter(|row| matches!(row, Row::Message(message) if message.call.is_some())) .count(); assert_eq!(calls, 7); } #[test] fn thread_lists_leave_call_events_out() { let records = [call_record( "c", 9, call_event_of(CallPhase::Ended, "x", "u", Some(5), &["u", "me"]), )]; assert!(thread_list_rows(&records, &call_context(), false).is_empty()); } }