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