BlueGit

ict rusty-teams

Files

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

recap.rs

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