BlueGit

ict rusty-teams

Files

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

recap.rs

refactor(app): extract app-base crate
metis Daniel Däschle · pushed by metis · 6 weeks ago
16e522c2 History
recap.rs 7.9 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("%H:%M")
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| time.with_timezone(&offset).format("%H:%M").to_string(),
91 )
92}
93
94pub fn matching_turns(turns: &[TranscriptTurn], query: &str) -> Vec<usize> {
95 let needle = query.trim().to_lowercase();
96 turns
97 .iter()
98 .enumerate()
99 .filter(|(_, turn)| {
100 needle.is_empty()
101 || turn.text.to_lowercase().contains(&needle)
102 || turn.speaker.to_lowercase().contains(&needle)
103 })
104 .map(|(index, _)| index)
105 .collect()
106}
107
108pub fn highlight_segments(text: &str, query: &str) -> Vec<(String, bool)> {
109 let needle = query.trim().to_lowercase();
110 if needle.is_empty() {
111 return vec![(text.to_owned(), false)];
112 }
113 let mut segments = Vec::new();
114 let mut plain = String::new();
115 let mut rest = text;
116 while !rest.is_empty() {
117 let hit = rest
118 .char_indices()
119 .map(|(start, _)| start)
120 .find_map(|start| matched_len(&rest[start..], &needle).map(|length| (start, length)));
121 match hit {
122 Some((start, length)) => {
123 plain.push_str(&rest[..start]);
124 if !plain.is_empty() {
125 segments.push((std::mem::take(&mut plain), false));
126 }
127 segments.push((rest[start..start + length].to_owned(), true));
128 rest = &rest[start + length..];
129 }
130 None => {
131 plain.push_str(rest);
132 rest = "";
133 }
134 }
135 }
136 if !plain.is_empty() {
137 segments.push((plain, false));
138 }
139 segments
140}
141
142fn matched_len(text: &str, lowered_needle: &str) -> Option<usize> {
143 let mut lowered = String::new();
144 for (position, character) in text.char_indices() {
145 lowered.extend(character.to_lowercase());
146 if lowered == lowered_needle {
147 return Some(position + character.len_utf8());
148 }
149 if !lowered_needle.starts_with(&lowered) {
150 return None;
151 }
152 }
153 None
154}
155
156pub fn save_name(occurrence: &Occurrence, extension: &str) -> String {
157 let stem = occurrence
158 .recording_name
159 .as_deref()
160 .map(|name| name.rsplit_once('.').map_or(name, |(stem, _)| stem))
161 .unwrap_or("Meeting recording");
162 format!("{stem}.{extension}")
163}
164
165#[cfg(test)]
166mod tests {
167 use super::*;
168
169 fn turn(speaker: &str, text: &str) -> TranscriptTurn {
170 TranscriptTurn {
171 speaker: speaker.to_owned(),
172 start_seconds: 0.,
173 end_seconds: 1.,
174 text: text.to_owned(),
175 }
176 }
177
178 fn occurrence(name: Option<&str>) -> Occurrence {
179 Occurrence {
180 message_id: "1".to_owned(),
181 started_at: None,
182 recording_url: "https://a.example/x.mp4".to_owned(),
183 recording_name: name.map(str::to_owned),
184 duration_seconds: None,
185 organizer_id: None,
186 audio_only: false,
187 }
188 }
189
190 #[test]
191 fn transcript_only_sessions_show_no_recording_card() {
192 let details = |transcript_only| RecordingDetails {
193 item_url: String::new(),
194 audio_only: false,
195 transcript_only,
196 duration_seconds: None,
197 expires_at: None,
198 transcript_id: None,
199 };
200 assert!(!shows_recording(&Load::Ready(details(true))));
201 assert!(shows_recording(&Load::Ready(details(false))));
202 assert!(shows_recording(&Load::Loading));
203 }
204
205 #[test]
206 fn expiry_wording_and_urgency() {
207 assert_eq!(expiry_label(0), "Expires today");
208 assert_eq!(expiry_label(1), "Expires in 1 day");
209 assert_eq!(expiry_label(42), "Expires in 42 days");
210 assert!(expiry_is_urgent(6));
211 assert!(!expiry_is_urgent(7));
212 }
213
214 #[test]
215 fn clocks_and_spans() {
216 assert_eq!(clock_label(1832), "30:32");
217 assert_eq!(clock_label(3723), "1:02:03");
218 assert_eq!(span_label(20), "< 1 min");
219 assert_eq!(span_label(1500), "25 min");
220 assert_eq!(span_label(3600), "1 h");
221 assert_eq!(span_label(3900), "1 h 5 min");
222 }
223
224 #[test]
225 fn search_matches_text_and_speaker_ignoring_case() {
226 let turns = [turn("Alex", "Ship it"), turn("Sam", "Later"), turn("Sam", "SHIP?")];
227 assert_eq!(matching_turns(&turns, ""), vec![0, 1, 2]);
228 assert_eq!(matching_turns(&turns, " ship "), vec![0, 2]);
229 assert_eq!(matching_turns(&turns, "sam"), vec![1, 2]);
230 assert!(matching_turns(&turns, "zzz").is_empty());
231 }
232
233 #[test]
234 fn highlight_splits_around_every_hit() {
235 assert_eq!(
236 highlight_segments("Ship it, ship", "SHIP"),
237 vec![
238 ("Ship".to_owned(), true),
239 (" it, ".to_owned(), false),
240 ("ship".to_owned(), true),
241 ]
242 );
243 assert_eq!(
244 highlight_segments("nothing", "x"),
245 vec![("nothing".to_owned(), false)]
246 );
247 assert_eq!(highlight_segments("a", ""), vec![("a".to_owned(), false)]);
248 }
249
250 #[test]
251 fn save_names_keep_the_recording_stem() {
252 let named = occurrence(Some("Weekly sync-20260302-Meeting Recording.mp4"));
253 assert_eq!(
254 save_name(&named, "vtt"),
255 "Weekly sync-20260302-Meeting Recording.vtt"
256 );
257 assert_eq!(save_name(&occurrence(None), "docx"), "Meeting recording.docx");
258 }
259}