BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / recap.rs

recap.rs

recap.rs 8.0 KiB Download Raw
1use chatsvc::{Occurrence, RecordingDetails, TranscriptTurn};
2use chrono::{DateTime, FixedOffset, NaiveDate, Utc};
3
4use crate::format;
5
6pub const EMPTY_RECAP: &str = "No recording or transcript for this meeting.";
7pub const NO_TRANSCRIPT: &str = "No transcript for this recording.";
8pub const ATTENDANCE_OFF: &str = "Attendance reports are turned off for your organization.";
9pub const ATTENDANCE_FAILED: &str = "Attendance could not be loaded.";
10pub const DELETE_QUESTION: &str = "Delete this recording for everyone?";
11const URGENT_EXPIRY_DAYS: i64 = 7;
12const SECONDS_PER_MINUTE: u64 = 60;
13const SECONDS_PER_HOUR: u64 = 3600;
14
15#[derive(Debug, Clone, PartialEq, Eq)]
16pub enum Load<T> {
17 Loading,
18 Failed(String),
19 Ready(T),
20}
21
22impl<T> Load<T> {
23 pub fn ready(&self) -> Option<&T> {
24 match self {
25 Load::Ready(value) => Some(value),
26 _ => None,
27 }
28 }
29}
30
31pub fn shows_recording(details: &Load<RecordingDetails>) -> bool {
32 !details.ready().is_some_and(|details| details.transcript_only)
33}
34
35pub fn expiry_label(days: i64) -> String {
36 match days {
37 0 => "Expires today".to_owned(),
38 1 => "Expires in 1 day".to_owned(),
39 days => format!("Expires in {days} days"),
40 }
41}
42
43pub fn expiry_is_urgent(days: i64) -> bool {
44 days < URGENT_EXPIRY_DAYS
45}
46
47pub fn clock_label(seconds: u64) -> String {
48 let hours = seconds / SECONDS_PER_HOUR;
49 let minutes = seconds % SECONDS_PER_HOUR / SECONDS_PER_MINUTE;
50 let rest = seconds % SECONDS_PER_MINUTE;
51 if hours > 0 {
52 format!("{hours}:{minutes:02}:{rest:02}")
53 } else {
54 format!("{minutes}:{rest:02}")
55 }
56}
57
58pub fn span_label(seconds: u64) -> String {
59 let hours = seconds / SECONDS_PER_HOUR;
60 let minutes = seconds % SECONDS_PER_HOUR / SECONDS_PER_MINUTE;
61 match (hours, minutes) {
62 (0, 0) => "< 1 min".to_owned(),
63 (0, minutes) => format!("{minutes} min"),
64 (hours, 0) => format!("{hours} h"),
65 (hours, minutes) => format!("{hours} h {minutes} min"),
66 }
67}
68
69pub fn occurrence_label(
70 occurrence: &Occurrence,
71 today: NaiveDate,
72 offset: FixedOffset,
73) -> String {
74 occurrence.started_at.map_or_else(
75 || occurrence.recording_name.clone().unwrap_or_default(),
76 |started_at| {
77 let local = started_at.with_timezone(&offset);
78 format!(
79 "{}, {}",
80 format::day_label(local.date_naive(), today),
81 local.format(format::clock_format())
82 )
83 },
84 )
85}
86
87pub fn time_label(time: Option<DateTime<Utc>>, offset: FixedOffset) -> String {
88 time.map_or_else(
89 || "-".to_owned(),
90 |time| {
91 time.with_timezone(&offset)
92 .format(format::clock_format())
93 .to_string()
94 },
95 )
96}
97
98pub fn matching_turns(turns: &[TranscriptTurn], query: &str) -> Vec<usize> {
99 let needle = query.trim().to_lowercase();
100 turns
101 .iter()
102 .enumerate()
103 .filter(|(_, turn)| {
104 needle.is_empty()
105 || turn.text.to_lowercase().contains(&needle)
106 || turn.speaker.to_lowercase().contains(&needle)
107 })
108 .map(|(index, _)| index)
109 .collect()
110}
111
112pub fn highlight_segments(text: &str, query: &str) -> Vec<(String, bool)> {
113 let needle = query.trim().to_lowercase();
114 if needle.is_empty() {
115 return vec![(text.to_owned(), false)];
116 }
117 let mut segments = Vec::new();
118 let mut plain = String::new();
119 let mut rest = text;
120 while !rest.is_empty() {
121 let hit = rest
122 .char_indices()
123 .map(|(start, _)| start)
124 .find_map(|start| matched_len(&rest[start..], &needle).map(|length| (start, length)));
125 match hit {
126 Some((start, length)) => {
127 plain.push_str(&rest[..start]);
128 if !plain.is_empty() {
129 segments.push((std::mem::take(&mut plain), false));
130 }
131 segments.push((rest[start..start + length].to_owned(), true));
132 rest = &rest[start + length..];
133 }
134 None => {
135 plain.push_str(rest);
136 rest = "";
137 }
138 }
139 }
140 if !plain.is_empty() {
141 segments.push((plain, false));
142 }
143 segments
144}
145
146fn matched_len(text: &str, lowered_needle: &str) -> Option<usize> {
147 let mut lowered = String::new();
148 for (position, character) in text.char_indices() {
149 lowered.extend(character.to_lowercase());
150 if lowered == lowered_needle {
151 return Some(position + character.len_utf8());
152 }
153 if !lowered_needle.starts_with(&lowered) {
154 return None;
155 }
156 }
157 None
158}
159
160pub fn save_name(occurrence: &Occurrence, extension: &str) -> String {
161 let stem = occurrence
162 .recording_name
163 .as_deref()
164 .map(|name| name.rsplit_once('.').map_or(name, |(stem, _)| stem))
165 .unwrap_or("Meeting recording");
166 format!("{stem}.{extension}")
167}
168
169#[cfg(test)]
170mod tests {
171 use super::*;
172
173 fn turn(speaker: &str, text: &str) -> TranscriptTurn {
174 TranscriptTurn {
175 speaker: speaker.to_owned(),
176 start_seconds: 0.,
177 end_seconds: 1.,
178 text: text.to_owned(),
179 }
180 }
181
182 fn occurrence(name: Option<&str>) -> Occurrence {
183 Occurrence {
184 message_id: "1".to_owned(),
185 started_at: None,
186 recording_url: "https://a.example/x.mp4".to_owned(),
187 recording_name: name.map(str::to_owned),
188 duration_seconds: None,
189 organizer_id: None,
190 audio_only: false,
191 }
192 }
193
194 #[test]
195 fn transcript_only_sessions_show_no_recording_card() {
196 let details = |transcript_only| RecordingDetails {
197 item_url: String::new(),
198 audio_only: false,
199 transcript_only,
200 duration_seconds: None,
201 expires_at: None,
202 transcript_id: None,
203 };
204 assert!(!shows_recording(&Load::Ready(details(true))));
205 assert!(shows_recording(&Load::Ready(details(false))));
206 assert!(shows_recording(&Load::Loading));
207 }
208
209 #[test]
210 fn expiry_wording_and_urgency() {
211 assert_eq!(expiry_label(0), "Expires today");
212 assert_eq!(expiry_label(1), "Expires in 1 day");
213 assert_eq!(expiry_label(42), "Expires in 42 days");
214 assert!(expiry_is_urgent(6));
215 assert!(!expiry_is_urgent(7));
216 }
217
218 #[test]
219 fn clocks_and_spans() {
220 assert_eq!(clock_label(1832), "30:32");
221 assert_eq!(clock_label(3723), "1:02:03");
222 assert_eq!(span_label(20), "< 1 min");
223 assert_eq!(span_label(1500), "25 min");
224 assert_eq!(span_label(3600), "1 h");
225 assert_eq!(span_label(3900), "1 h 5 min");
226 }
227
228 #[test]
229 fn search_matches_text_and_speaker_ignoring_case() {
230 let turns = [turn("Alex", "Ship it"), turn("Sam", "Later"), turn("Sam", "SHIP?")];
231 assert_eq!(matching_turns(&turns, ""), vec![0, 1, 2]);
232 assert_eq!(matching_turns(&turns, " ship "), vec![0, 2]);
233 assert_eq!(matching_turns(&turns, "sam"), vec![1, 2]);
234 assert!(matching_turns(&turns, "zzz").is_empty());
235 }
236
237 #[test]
238 fn highlight_splits_around_every_hit() {
239 assert_eq!(
240 highlight_segments("Ship it, ship", "SHIP"),
241 vec![
242 ("Ship".to_owned(), true),
243 (" it, ".to_owned(), false),
244 ("ship".to_owned(), true),
245 ]
246 );
247 assert_eq!(
248 highlight_segments("nothing", "x"),
249 vec![("nothing".to_owned(), false)]
250 );
251 assert_eq!(highlight_segments("a", ""), vec![("a".to_owned(), false)]);
252 }
253
254 #[test]
255 fn save_names_keep_the_recording_stem() {
256 let named = occurrence(Some("Weekly sync-20260302-Meeting Recording.mp4"));
257 assert_eq!(
258 save_name(&named, "vtt"),
259 "Weekly sync-20260302-Meeting Recording.vtt"
260 );
261 assert_eq!(save_name(&occurrence(None), "docx"), "Meeting recording.docx");
262 }
263}