BlueGit

ict rusty-teams

Files

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

recap.rs

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