BlueGit

ict rusty-teams

Files

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

image_view_gallery.rs

0ff9db96 History
image_view_gallery.rs 5.9 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 message.images.iter().map(move |image| GalleryItem {
60 message_key: message.key.clone(),
61 image: image.clone(),
62 sender_id: message.sender_id.clone(),
63 author: message.author.clone(),
64 time: time.clone(),
65 caption: caption.clone(),
66 })
67 })
68 .collect()
69}
70
71pub fn position_of(items: &[GalleryItem], message_key: &str, url: &str) -> Option<usize> {
72 items
73 .iter()
74 .position(|item| item.message_key == message_key && item.image.url == url)
75}
76
77#[cfg(test)]
78mod tests {
79 use chrono::FixedOffset;
80 use std::collections::HashMap;
81
82 use chrono::{TimeZone, Utc};
83 use store::MessageRecord;
84
85 use super::*;
86 use crate::rows::{MeetingInfo, RowContext, message_row};
87 use crate::translation::TranslationContext;
88
89 fn offset() -> FixedOffset {
90 FixedOffset::east_opt(0).unwrap()
91 }
92
93 fn today() -> NaiveDate {
94 NaiveDate::from_ymd_opt(2026, 10, 7).unwrap()
95 }
96
97 fn context() -> RowContext {
98 RowContext {
99 offset: offset().into(),
100 today: today(),
101 my_user_id: None,
102 names: HashMap::new(),
103 pending_reactions: HashMap::new(),
104 card_overrides: HashMap::new(),
105 translation: TranslationContext::default(),
106 skin_tone: 0,
107 meeting: MeetingInfo::default(),
108 }
109 }
110
111 fn record(id: &str, day: u32, body_html: &str) -> MessageRecord {
112 MessageRecord {
113 conversation_id: "c".into(),
114 message_id: id.into(),
115 reply_to_id: None,
116 sender_id: Some("u".into()),
117 sender_name: Some("Mara".into()),
118 sender_application_id: None,
119 links_json: "[]".to_owned(),
120 created_at: Utc.with_ymd_and_hms(2026, 10, day, 13, 40, 0).unwrap(),
121 edited_at: None,
122 deleted: false,
123 body_html: body_html.to_owned(),
124 attachments_json: "[]".into(),
125 reactions_json: "[]".into(),
126 mentions_json: "[]".into(),
127 subject: None,
128 call_event_json: None,
129 importance: Default::default(),
130 }
131 }
132
133 fn rows(records: &[MessageRecord]) -> Vec<Row> {
134 records
135 .iter()
136 .map(|record| Row::Message(Box::new(message_row(record, &context()))))
137 .collect()
138 }
139
140 #[test]
141 fn images_come_in_message_order_and_skip_text_only_and_deleted_messages() {
142 let mut deleted = record("d", 7, "<img src=\"https://x/deleted\">");
143 deleted.deleted = true;
144 let records = [
145 record(
146 "a",
147 6,
148 "<p>first</p><img src=\"https://x/one\"><img src=\"https://x/two\">",
149 ),
150 record("b", 6, "<p>no image</p>"),
151 deleted,
152 record("c", 7, "<img src=\"https://x/three\">"),
153 ];
154 let items = collect_gallery(&rows(&records), today(), offset());
155 let urls: Vec<&str> = items.iter().map(|item| item.image.url.as_str()).collect();
156 assert_eq!(urls, ["https://x/one", "https://x/two", "https://x/three"]);
157 assert_eq!(items[0].message_key, "a");
158 assert_eq!(items[2].message_key, "c");
159 }
160
161 #[test]
162 fn items_carry_author_caption_and_time() {
163 let records = [
164 record(
165 "a",
166 6,
167 "<p>Release notes</p><p>second line</p><img src=\"https://x/one\">",
168 ),
169 record("b", 7, "<img src=\"https://x/two\">"),
170 ];
171 let items = collect_gallery(&rows(&records), today(), offset());
172 assert_eq!(items[0].author, "Mara");
173 assert_eq!(items[0].caption, "Release notes second line");
174 assert_eq!(items[0].time, "Yesterday 13:40");
175 assert_eq!(items[1].caption, "");
176 assert_eq!(items[1].time, "Today 13:40");
177 }
178
179 #[test]
180 fn position_matches_message_and_url() {
181 let records = [
182 record("a", 6, "<img src=\"https://x/one\">"),
183 record("b", 6, "<img src=\"https://x/one\">"),
184 ];
185 let items = collect_gallery(&rows(&records), today(), offset());
186 assert_eq!(position_of(&items, "b", "https://x/one"), Some(1));
187 assert_eq!(position_of(&items, "a", "https://x/one"), Some(0));
188 assert_eq!(position_of(&items, "a", "https://x/other"), None);
189 }
190}