BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / image_view_gallery.rs

image_view_gallery.rs

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