BlueGit

ict rusty-teams

Files

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

meeting_create.rs

meeting_create.rs 6.3 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 &mut accounts,
51 &target,
52 &event,
53 &join_url,
54 outlook_invites,
55 )
56 .await
57 {
58 Ok(id) => {
59 if first_id.is_empty() {
60 first_id = id;
61 }
62 }
63 Err(error) => failed.push(format!("{} ({error})", target.key())),
64 }
65 }
66 let _ = store::save_calendar_accounts(&store, &accounts);
67 if !failed.is_empty() {
68 crate::diagnostics::record(partial_create_notice(&join_url, &failed));
69 if first_id.is_empty() {
70 return Err(partial_create_notice(&join_url, &failed));
71 }
72 }
73 Ok(CreatedEvent {
74 id: if first_id.is_empty() {
75 join_url.clone()
76 } else {
77 first_id
78 },
79 join_url: Some(join_url),
80 })
81}
82
83async fn write_target(
84 launcher: &CallLauncher,
85 accounts: &mut CalendarAccounts,
86 target: &CalendarRef,
87 event: &NewEvent,
88 join_url: &str,
89 outlook_invites: bool,
90) -> Result<String, String> {
91 match target.provider {
92 CalendarProvider::Outlook => launcher
93 .create_meeting_with_join(event.clone(), join_url)
94 .await
95 .map(|created| created.id),
96 CalendarProvider::Google => {
97 let calendar_id = if target.calendar_id.is_empty() {
98 "primary"
99 } else {
100 target.calendar_id.as_str()
101 };
102 let mut draft = GoogleEventDraft::from(event);
103 draft.join_url = Some(join_url.to_owned());
104 crate::google_calendar_sync::insert_google_event(
105 accounts,
106 &target.account_id,
107 calendar_id,
108 &draft,
109 )
110 .await?;
111 Ok(format!("google:{}:{}", target.account_id, calendar_id))
112 }
113 CalendarProvider::CalDav => {
114 let attendees: Vec<String> = event
115 .attendees
116 .iter()
117 .map(|attendee| attendee.address.clone())
118 .collect();
119 let draft = crate::caldav_sync::draft_from_meeting(
120 &event.subject,
121 event.start,
122 event.end,
123 Some(join_url),
124 &attendees,
125 !outlook_invites,
126 event.repeat,
127 );
128 crate::caldav_sync::insert_caldav_event(
129 accounts,
130 &target.account_id,
131 &target.calendar_id,
132 &draft,
133 )
134 .await?;
135 Ok(format!(
136 "caldav:{}:{}",
137 target.account_id, target.calendar_id
138 ))
139 }
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use store::{CalendarRef, GoogleAccount, GoogleCalendarInfo, GoogleTokenSet};
146
147 use super::*;
148
149 fn google_home() -> CalendarAccounts {
150 let mut accounts = CalendarAccounts::default();
151 accounts.home = CalendarRef::google("g1", "primary");
152 accounts.google_accounts.push(GoogleAccount {
153 id: "g1".into(),
154 email: "joe@example.com".into(),
155 calendars: vec![GoogleCalendarInfo {
156 id: "primary".into(),
157 summary: "Joe".into(),
158 selected: true,
159 primary: true,
160 }],
161 tokens: GoogleTokenSet::default(),
162 });
163 accounts
164 }
165
166 #[test]
167 fn outlook_only_home_keeps_one_step() {
168 let accounts = CalendarAccounts::default();
169 assert!(!needs_join_first(&accounts));
170 assert_eq!(create_targets(&accounts).len(), 1);
171 assert_eq!(
172 create_targets(&accounts)[0].provider,
173 CalendarProvider::Outlook
174 );
175 }
176
177 #[test]
178 fn google_home_needs_join_first() {
179 assert!(needs_join_first(&google_home()));
180 }
181
182 #[test]
183 fn caldav_only_home_needs_join_first() {
184 let mut accounts = CalendarAccounts::default();
185 accounts.home = CalendarRef::caldav("c1", "/cal/");
186 assert!(needs_join_first(&accounts));
187 }
188
189 #[test]
190 fn outlook_home_with_google_extra_needs_join_first() {
191 let mut accounts = CalendarAccounts::default();
192 accounts
193 .write_targets
194 .push(CalendarRef::google("g1", "primary"));
195 assert!(needs_join_first(&accounts));
196 assert_eq!(create_targets(&accounts).len(), 2);
197 }
198
199 #[test]
200 fn partial_notice_keeps_join_url() {
201 let notice = partial_create_notice(
202 "https://teams.microsoft.com/l/meetup-join/x",
203 &["Google:primary".into()],
204 );
205 assert!(notice.contains("teams.microsoft.com"));
206 assert!(notice.contains("Google:primary"));
207 }
208}