| 1 | use chrono::{FixedOffset, NaiveDate}; |
| 2 | use teams_core::ImageRef; |
| 3 | |
| 4 | use crate::format; |
| 5 | use crate::rows::{MessageRow, Row}; |
| 6 | |
| 7 | #[derive(Debug, Clone, PartialEq)] |
| 8 | pub struct GalleryItem { |
| 9 | pub message_key: String, |
| 10 | pub image: ImageRef, |
| 11 | pub sender_id: Option<String>, |
| 12 | pub author: String, |
| 13 | pub time: String, |
| 14 | pub caption: String, |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone, PartialEq)] |
| 18 | pub struct ViewerRequest { |
| 19 | pub conversation_id: String, |
| 20 | pub conversation_title: String, |
| 21 | pub items: Vec<GalleryItem>, |
| 22 | pub index: usize, |
| 23 | } |
| 24 | |
| 25 | pub fn caption_of(row: &MessageRow) -> String { |
| 26 | row.blocks |
| 27 | .iter() |
| 28 | .filter_map(|block| block.markdown()) |
| 29 | .collect::<Vec<_>>() |
| 30 | .join(" ") |
| 31 | .split_whitespace() |
| 32 | .collect::<Vec<_>>() |
| 33 | .join(" ") |
| 34 | } |
| 35 | |
| 36 | pub fn viewer_time_label(row: &MessageRow, today: NaiveDate, offset: FixedOffset) -> String { |
| 37 | let label = format::post_time_label(row.created_at, today, offset); |
| 38 | if row.created_at.with_timezone(&offset).date_naive() == today { |
| 39 | format!("Today {label}") |
| 40 | } else { |
| 41 | label |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub fn collect_gallery(rows: &[Row], today: NaiveDate, offset: FixedOffset) -> Vec<GalleryItem> { |
| 46 | rows.iter() |
| 47 | .flat_map(Row::messages) |
| 48 | .filter(|message| !message.deleted) |
| 49 | .flat_map(|message| { |
| 50 | let caption = caption_of(message); |
| 51 | let time = viewer_time_label(message, today, offset); |
| 52 | message.images.iter().map(move |image| GalleryItem { |
| 53 | message_key: message.key.clone(), |
| 54 | image: image.clone(), |
| 55 | sender_id: message.sender_id.clone(), |
| 56 | author: message.author.clone(), |
| 57 | time: time.clone(), |
| 58 | caption: caption.clone(), |
| 59 | }) |
| 60 | }) |
| 61 | .collect() |
| 62 | } |
| 63 | |
| 64 | pub fn position_of(items: &[GalleryItem], message_key: &str, url: &str) -> Option<usize> { |
| 65 | items |
| 66 | .iter() |
| 67 | .position(|item| item.message_key == message_key && item.image.url == url) |
| 68 | } |
| 69 | |
| 70 | #[cfg(test)] |
| 71 | mod tests { |
| 72 | use std::collections::HashMap; |
| 73 | |
| 74 | use chrono::{TimeZone, Utc}; |
| 75 | use store::MessageRecord; |
| 76 | |
| 77 | use super::*; |
| 78 | use crate::rows::{MeetingInfo, RowContext, message_row}; |
| 79 | use crate::translation::TranslationContext; |
| 80 | |
| 81 | fn offset() -> FixedOffset { |
| 82 | FixedOffset::east_opt(0).unwrap() |
| 83 | } |
| 84 | |
| 85 | fn today() -> NaiveDate { |
| 86 | NaiveDate::from_ymd_opt(2026, 10, 7).unwrap() |
| 87 | } |
| 88 | |
| 89 | fn context() -> RowContext { |
| 90 | RowContext { |
| 91 | offset: offset(), |
| 92 | today: today(), |
| 93 | my_user_id: None, |
| 94 | names: HashMap::new(), |
| 95 | pending_reactions: HashMap::new(), |
| 96 | card_overrides: HashMap::new(), |
| 97 | translation: TranslationContext::default(), |
| 98 | skin_tone: 0, |
| 99 | meeting: MeetingInfo::default(), |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | fn record(id: &str, day: u32, body_html: &str) -> MessageRecord { |
| 104 | MessageRecord { |
| 105 | conversation_id: "c".into(), |
| 106 | message_id: id.into(), |
| 107 | reply_to_id: None, |
| 108 | sender_id: Some("u".into()), |
| 109 | sender_name: Some("Mara".into()), |
| 110 | sender_application_id: None, |
| 111 | links_json: "[]".to_owned(), |
| 112 | created_at: Utc.with_ymd_and_hms(2026, 10, day, 13, 40, 0).unwrap(), |
| 113 | edited_at: None, |
| 114 | deleted: false, |
| 115 | body_html: body_html.to_owned(), |
| 116 | attachments_json: "[]".into(), |
| 117 | reactions_json: "[]".into(), |
| 118 | mentions_json: "[]".into(), |
| 119 | subject: None, |
| 120 | call_event_json: None, |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | fn rows(records: &[MessageRecord]) -> Vec<Row> { |
| 125 | records |
| 126 | .iter() |
| 127 | .map(|record| Row::Message(Box::new(message_row(record, &context())))) |
| 128 | .collect() |
| 129 | } |
| 130 | |
| 131 | #[test] |
| 132 | fn images_come_in_message_order_and_skip_text_only_and_deleted_messages() { |
| 133 | let mut deleted = record("d", 7, "<img src=\"https://x/deleted\">"); |
| 134 | deleted.deleted = true; |
| 135 | let records = [ |
| 136 | record( |
| 137 | "a", |
| 138 | 6, |
| 139 | "<p>first</p><img src=\"https://x/one\"><img src=\"https://x/two\">", |
| 140 | ), |
| 141 | record("b", 6, "<p>no image</p>"), |
| 142 | deleted, |
| 143 | record("c", 7, "<img src=\"https://x/three\">"), |
| 144 | ]; |
| 145 | let items = collect_gallery(&rows(&records), today(), offset()); |
| 146 | let urls: Vec<&str> = items.iter().map(|item| item.image.url.as_str()).collect(); |
| 147 | assert_eq!(urls, ["https://x/one", "https://x/two", "https://x/three"]); |
| 148 | assert_eq!(items[0].message_key, "a"); |
| 149 | assert_eq!(items[2].message_key, "c"); |
| 150 | } |
| 151 | |
| 152 | #[test] |
| 153 | fn items_carry_author_caption_and_time() { |
| 154 | let records = [ |
| 155 | record( |
| 156 | "a", |
| 157 | 6, |
| 158 | "<p>Release notes</p><p>second line</p><img src=\"https://x/one\">", |
| 159 | ), |
| 160 | record("b", 7, "<img src=\"https://x/two\">"), |
| 161 | ]; |
| 162 | let items = collect_gallery(&rows(&records), today(), offset()); |
| 163 | assert_eq!(items[0].author, "Mara"); |
| 164 | assert_eq!(items[0].caption, "Release notes second line"); |
| 165 | assert_eq!(items[0].time, "Yesterday 13:40"); |
| 166 | assert_eq!(items[1].caption, ""); |
| 167 | assert_eq!(items[1].time, "Today 13:40"); |
| 168 | } |
| 169 | |
| 170 | #[test] |
| 171 | fn position_matches_message_and_url() { |
| 172 | let records = [ |
| 173 | record("a", 6, "<img src=\"https://x/one\">"), |
| 174 | record("b", 6, "<img src=\"https://x/one\">"), |
| 175 | ]; |
| 176 | let items = collect_gallery(&rows(&records), today(), offset()); |
| 177 | assert_eq!(position_of(&items, "b", "https://x/one"), Some(1)); |
| 178 | assert_eq!(position_of(&items, "a", "https://x/one"), Some(0)); |
| 179 | assert_eq!(position_of(&items, "a", "https://x/other"), None); |
| 180 | } |
| 181 | } |