BlueGit

ict rusty-teams

Files

rusty-teams / crates / chatsvc / src / google_calendar / merge.rs

merge.rs

merge.rs 5.0 KiB Download Raw
1use crate::calendar::{CalendarEvent, CalendarSource};
2
3/// Collapse duplicate meetings that share an iCal UID or the same Teams join URL.
4pub fn merge_calendar_events(events: Vec<CalendarEvent>) -> Vec<CalendarEvent> {
5 let mut merged: Vec<CalendarEvent> = Vec::new();
6 for event in events {
7 if let Some(existing) = merged
8 .iter_mut()
9 .find(|candidate| same_meeting(candidate, &event))
10 {
11 if !existing.sources.contains(&event.source) {
12 existing.sources.push(event.source);
13 }
14 if existing.join_url.is_none() {
15 existing.join_url = event.join_url.clone();
16 existing.online = existing.online || event.online;
17 }
18 if existing.thread_id.is_none() {
19 existing.thread_id = event.thread_id.clone();
20 }
21 // Prefer Outlook object id when both sides contribute.
22 if existing.source == CalendarSource::Google && event.source == CalendarSource::Outlook
23 {
24 existing.object_id = event.object_id.clone();
25 existing.source = CalendarSource::Outlook;
26 existing.account_id = event.account_id.clone();
27 existing.calendar_id = event.calendar_id.clone();
28 existing.response = event.response;
29 existing.show_as = event.show_as;
30 }
31 continue;
32 }
33 merged.push(event);
34 }
35 merged.sort_by_key(|event| (event.start, event.end));
36 merged
37}
38
39fn same_meeting(left: &CalendarEvent, right: &CalendarEvent) -> bool {
40 let same_all_day = left.all_day
41 && right.all_day
42 && (left.start - right.start).abs() < chrono::Duration::days(1);
43 if left.start != right.start && !same_all_day {
44 return false;
45 }
46 if let (Some(a), Some(b)) = (&left.ical_uid, &right.ical_uid) {
47 if !a.is_empty() && a == b {
48 return true;
49 }
50 }
51 match (
52 left.join_url.as_deref().map(normalize_join_url),
53 right.join_url.as_deref().map(normalize_join_url),
54 ) {
55 (Some(a), Some(b)) if !a.is_empty() && a == b => true,
56 _ => false,
57 }
58}
59
60pub fn normalize_join_url(url: &str) -> String {
61 let trimmed = url.trim().trim_end_matches('/');
62 let without_query = trimmed.split('?').next().unwrap_or(trimmed);
63 without_query.to_ascii_lowercase()
64}
65
66#[cfg(test)]
67mod tests {
68 use chrono::TimeZone;
69
70 use super::*;
71
72 fn event(
73 id: &str,
74 source: CalendarSource,
75 ical: Option<&str>,
76 join: Option<&str>,
77 ) -> CalendarEvent {
78 CalendarEvent {
79 object_id: id.into(),
80 subject: "Sync".into(),
81 start: chrono::Utc
82 .with_ymd_and_hms(2026, 11, 11, 10, 0, 0)
83 .unwrap(),
84 end: chrono::Utc
85 .with_ymd_and_hms(2026, 11, 11, 10, 30, 0)
86 .unwrap(),
87 ical_uid: ical.map(str::to_owned),
88 join_url: join.map(str::to_owned),
89 online: join.is_some(),
90 source,
91 sources: vec![source],
92 ..Default::default()
93 }
94 }
95
96 #[test]
97 fn merges_on_ical_uid_and_keeps_both_sources() {
98 let merged = merge_calendar_events(vec![
99 event("o1", CalendarSource::Outlook, Some("uid"), None),
100 event(
101 "g1",
102 CalendarSource::Google,
103 Some("uid"),
104 Some("https://teams.microsoft.com/l/meetup-join/x"),
105 ),
106 ]);
107 assert_eq!(merged.len(), 1);
108 assert_eq!(merged[0].sources.len(), 2);
109 assert_eq!(merged[0].object_id, "o1");
110 assert!(merged[0].join_url.is_some());
111 }
112
113 #[test]
114 fn occurrences_of_one_series_stay_separate() {
115 let join = Some("https://teams.microsoft.com/l/meetup-join/x");
116 let mut next_day = event("o2", CalendarSource::Outlook, Some("uid"), join);
117 next_day.start += chrono::Duration::days(1);
118 next_day.end += chrono::Duration::days(1);
119 let merged = merge_calendar_events(vec![
120 event("o1", CalendarSource::Outlook, Some("uid"), join),
121 next_day,
122 ]);
123 assert_eq!(merged.len(), 2);
124 }
125
126 #[test]
127 fn merges_on_normalized_join_url() {
128 let merged = merge_calendar_events(vec![
129 event(
130 "o1",
131 CalendarSource::Outlook,
132 None,
133 Some("https://teams.microsoft.com/l/meetup-join/X?x=1"),
134 ),
135 event(
136 "g1",
137 CalendarSource::Google,
138 None,
139 Some("https://teams.microsoft.com/l/meetup-join/x/"),
140 ),
141 ]);
142 assert_eq!(merged.len(), 1);
143 }
144
145 #[test]
146 fn does_not_merge_on_title_and_time_alone() {
147 let a = event("a", CalendarSource::Outlook, Some("a"), None);
148 let mut b = event("b", CalendarSource::Google, Some("b"), None);
149 b.subject = a.subject.clone();
150 assert_eq!(merge_calendar_events(vec![a, b]).len(), 2);
151 }
152}