BlueGit

ict rusty-teams

Files

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

image_view_gallery.rs

image_view_gallery.rs 5.6 KiB Download Raw
1use chrono::{FixedOffset, NaiveDate};
2use teams_core::ImageRef;
3
4use crate::format;
5use crate::rows::{MessageRow, Row};
6
7#[derive(Debug, Clone, PartialEq)]
8pub 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)]
18pub struct ViewerRequest {
19 pub conversation_id: String,
20 pub conversation_title: String,
21 pub items: Vec<GalleryItem>,
22 pub index: usize,
23}
24
25pub 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
36pub 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
45pub 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
64pub 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)]
71mod tests {
72 use std::collections::HashMap;
73
74 use chrono::{TimeZone, Utc};
75 use store::MessageRecord;
76
77 use super::*;
78 use crate::rows::{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 }
99 }
100
101 fn record(id: &str, day: u32, body_html: &str) -> MessageRecord {
102 MessageRecord {
103 conversation_id: "c".into(),
104 message_id: id.into(),
105 reply_to_id: None,
106 sender_id: Some("u".into()),
107 sender_name: Some("Mara".into()),
108 sender_application_id: None,
109 links_json: "[]".to_owned(),
110 created_at: Utc.with_ymd_and_hms(2026, 10, day, 13, 40, 0).unwrap(),
111 edited_at: None,
112 deleted: false,
113 body_html: body_html.to_owned(),
114 attachments_json: "[]".into(),
115 reactions_json: "[]".into(),
116 mentions_json: "[]".into(),
117 subject: None,
118 }
119 }
120
121 fn rows(records: &[MessageRecord]) -> Vec<Row> {
122 records
123 .iter()
124 .map(|record| Row::Message(Box::new(message_row(record, &context()))))
125 .collect()
126 }
127
128 #[test]
129 fn images_come_in_message_order_and_skip_text_only_and_deleted_messages() {
130 let mut deleted = record("d", 7, "<img src=\"https://x/deleted\">");
131 deleted.deleted = true;
132 let records = [
133 record(
134 "a",
135 6,
136 "<p>first</p><img src=\"https://x/one\"><img src=\"https://x/two\">",
137 ),
138 record("b", 6, "<p>no image</p>"),
139 deleted,
140 record("c", 7, "<img src=\"https://x/three\">"),
141 ];
142 let items = collect_gallery(&rows(&records), today(), offset());
143 let urls: Vec<&str> = items.iter().map(|item| item.image.url.as_str()).collect();
144 assert_eq!(urls, ["https://x/one", "https://x/two", "https://x/three"]);
145 assert_eq!(items[0].message_key, "a");
146 assert_eq!(items[2].message_key, "c");
147 }
148
149 #[test]
150 fn items_carry_author_caption_and_time() {
151 let records = [
152 record(
153 "a",
154 6,
155 "<p>Release notes</p><p>second line</p><img src=\"https://x/one\">",
156 ),
157 record("b", 7, "<img src=\"https://x/two\">"),
158 ];
159 let items = collect_gallery(&rows(&records), today(), offset());
160 assert_eq!(items[0].author, "Mara");
161 assert_eq!(items[0].caption, "Release notes second line");
162 assert_eq!(items[0].time, "Yesterday 13:40");
163 assert_eq!(items[1].caption, "");
164 assert_eq!(items[1].time, "Today 13:40");
165 }
166
167 #[test]
168 fn position_matches_message_and_url() {
169 let records = [
170 record("a", 6, "<img src=\"https://x/one\">"),
171 record("b", 6, "<img src=\"https://x/one\">"),
172 ];
173 let items = collect_gallery(&rows(&records), today(), offset());
174 assert_eq!(position_of(&items, "b", "https://x/one"), Some(1));
175 assert_eq!(position_of(&items, "a", "https://x/one"), Some(0));
176 assert_eq!(position_of(&items, "a", "https://x/other"), None);
177 }
178}