BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / call / meeting_create.rs

meeting_create.rs

meeting_create.rs 6.4 KiB Download Raw
1use std::sync::Arc;
2
3use chatsvc::{CreatedEvent, GoogleEventDraft, NewEvent};
4use store::{CalendarAccounts, CalendarProvider, CalendarRef, Store};
5
6use super::launcher::CallLauncher;
7use super::schedule::MeetingDraft;
8
9/// Whether create must mint a Teams join URL before per-target inserts.
10pub fn needs_join_first(accounts: &CalendarAccounts) -> bool {
11 accounts
12 .resolved_write_targets()
13 .iter()
14 .any(|target| target.provider != CalendarProvider::Outlook)
15}
16
17/// Write targets used for New meeting (home + extras, de-duplicated).
18pub fn create_targets(accounts: &CalendarAccounts) -> Vec<CalendarRef> {
19 accounts.resolved_write_targets()
20}
21
22/// Format a partial-failure notice when the join link exists but a target failed.
23pub fn partial_create_notice(join_url: &str, failed: &[String]) -> String {
24 format!(
25 "Meeting link ready, but calendar write failed for {}: {join_url}",
26 failed.join(", ")
27 )
28}
29
30pub async fn create_across_targets(
31 launcher: Arc<CallLauncher>,
32 draft: MeetingDraft,
33 mut accounts: CalendarAccounts,
34 store: Arc<Store>,
35) -> Result<CreatedEvent, String> {
36 if !needs_join_first(&accounts) {
37 return launcher.create_meeting(draft).await;
38 }
39 let event = launcher.resolve_new_event(draft).await?;
40 let join_url = launcher.create_meet_now(&event.subject).await?;
41 let targets = create_targets(&accounts);
42 let outlook_invites = targets
43 .iter()
44 .any(|target| target.provider == CalendarProvider::Outlook);
45 let mut failed = Vec::new();
46 let mut first_id = String::new();
47 for target in targets {
48 match write_target(
49 launcher.as_ref(),
50 store.as_ref(),
51 &mut accounts,
52 &target,
53 &event,
54 &join_url,
55 outlook_invites,
56 )
57 .await
58 {
59 Ok(id) => {
60 if first_id.is_empty() {
61 first_id = id;
62 }
63 }
64 Err(error) => failed.push(format!("{} ({error})", target.key())),
65 }
66 }
67 let _ = store::save_calendar_accounts(&store, &accounts);
68 if !failed.is_empty() {
69 crate::diagnostics::record(partial_create_notice(&join_url, &failed));
70 if first_id.is_empty() {
71 return Err(partial_create_notice(&join_url, &failed));
72 }
73 }
74 Ok(CreatedEvent {
75 id: if first_id.is_empty() {
76 join_url.clone()
77 } else {
78 first_id
79 },
80 join_url: Some(join_url),
81 })
82}
83
84async fn write_target(
85 launcher: &CallLauncher,
86 store: &Store,
87 accounts: &mut CalendarAccounts,
88 target: &CalendarRef,
89 event: &NewEvent,
90 join_url: &str,
91 outlook_invites: bool,
92) -> Result<String, String> {
93 match target.provider {
94 CalendarProvider::Outlook => launcher
95 .create_meeting_with_join(event.clone(), join_url)
96 .await
97 .map(|created| created.id),
98 CalendarProvider::Google => {
99 let calendar_id = if target.calendar_id.is_empty() {
100 "primary"
101 } else {
102 target.calendar_id.as_str()
103 };
104 let mut draft = GoogleEventDraft::from(event);
105 draft.join_url = Some(join_url.to_owned());
106 crate::google_calendar_sync::insert_google_event(
107 store,
108 accounts,
109 &target.account_id,
110 calendar_id,
111 &draft,
112 )
113 .await?;
114 Ok(format!("google:{}:{}", target.account_id, calendar_id))
115 }
116 CalendarProvider::CalDav => {
117 let attendees: Vec<String> = event
118 .attendees
119 .iter()
120 .map(|attendee| attendee.address.clone())
121 .collect();
122 let draft = crate::caldav_sync::draft_from_meeting(
123 &event.subject,
124 event.start,
125 event.end,
126 Some(join_url),
127 &attendees,
128 !outlook_invites,
129 event.repeat,
130 );
131 crate::caldav_sync::insert_caldav_event(
132 accounts,
133 &target.account_id,
134 &target.calendar_id,
135 &draft,
136 )
137 .await?;
138 Ok(format!(
139 "caldav:{}:{}",
140 target.account_id, target.calendar_id
141 ))
142 }
143 }
144}
145
146#[cfg(test)]
147mod tests {
148 use store::{CalendarRef, GoogleAccount, GoogleCalendarInfo, GoogleTokenSet};
149
150 use super::*;
151
152 fn google_home() -> CalendarAccounts {
153 let mut accounts = CalendarAccounts::default();
154 accounts.home = CalendarRef::google("g1", "primary");
155 accounts.google_accounts.push(GoogleAccount {
156 id: "g1".into(),
157 email: "joe@example.com".into(),
158 calendars: vec![GoogleCalendarInfo {
159 id: "primary".into(),
160 summary: "Joe".into(),
161 selected: true,
162 primary: true,
163 }],
164 tokens: GoogleTokenSet::default(),
165 });
166 accounts
167 }
168
169 #[test]
170 fn outlook_only_home_keeps_one_step() {
171 let accounts = CalendarAccounts::default();
172 assert!(!needs_join_first(&accounts));
173 assert_eq!(create_targets(&accounts).len(), 1);
174 assert_eq!(
175 create_targets(&accounts)[0].provider,
176 CalendarProvider::Outlook
177 );
178 }
179
180 #[test]
181 fn google_home_needs_join_first() {
182 assert!(needs_join_first(&google_home()));
183 }
184
185 #[test]
186 fn caldav_only_home_needs_join_first() {
187 let mut accounts = CalendarAccounts::default();
188 accounts.home = CalendarRef::caldav("c1", "/cal/");
189 assert!(needs_join_first(&accounts));
190 }
191
192 #[test]
193 fn outlook_home_with_google_extra_needs_join_first() {
194 let mut accounts = CalendarAccounts::default();
195 accounts
196 .write_targets
197 .push(CalendarRef::google("g1", "primary"));
198 assert!(needs_join_first(&accounts));
199 assert_eq!(create_targets(&accounts).len(), 2);
200 }
201
202 #[test]
203 fn partial_notice_keeps_join_url() {
204 let notice = partial_create_notice(
205 "https://teams.microsoft.com/l/meetup-join/x",
206 &["Google:primary".into()],
207 );
208 assert!(notice.contains("teams.microsoft.com"));
209 assert!(notice.contains("Google:primary"));
210 }
211}