| 1 | use std::collections::HashSet; |
| 2 | |
| 3 | use gpui_kit::*; |
| 4 | use mcp::{MeetingOutcome, MeetingProposal, TeamsSource}; |
| 5 | |
| 6 | use super::handle; |
| 7 | use crate::app_state::AppHandle; |
| 8 | use crate::notice::NoticeAction; |
| 9 | |
| 10 | pub fn unannounced<'a>(announced: &HashSet<String>, pending: &'a [MeetingProposal]) -> Vec<&'a MeetingProposal> { |
| 11 | pending |
| 12 | .iter() |
| 13 | .filter(|proposal| !announced.contains(&proposal.id)) |
| 14 | .collect() |
| 15 | } |
| 16 | |
| 17 | pub fn announce(proposal: &MeetingProposal, cx: &mut App) { |
| 18 | let Some(handle) = cx.try_global::<AppHandle>() else { |
| 19 | return; |
| 20 | }; |
| 21 | let text = format!( |
| 22 | "{} proposes a meeting: {}", |
| 23 | super::agent_label(proposal.agent.as_deref()), |
| 24 | proposal.draft.subject |
| 25 | ); |
| 26 | let action = NoticeAction::new("Review", |state, cx| state.open_calendar(cx)); |
| 27 | handle |
| 28 | .0 |
| 29 | .clone() |
| 30 | .update(cx, |state, cx| state.raise_notice(text, Some(action), cx)); |
| 31 | } |
| 32 | |
| 33 | pub fn pending_meetings(cx: &App) -> Vec<MeetingProposal> { |
| 34 | handle(cx) |
| 35 | .map(|api| api.read(cx).hub.pending_meetings()) |
| 36 | .unwrap_or_default() |
| 37 | } |
| 38 | |
| 39 | pub fn discard_meeting(proposal_id: &str, cx: &mut App) { |
| 40 | let Some(api) = handle(cx) else { |
| 41 | return; |
| 42 | }; |
| 43 | api.update(cx, |api, cx| { |
| 44 | api.hub.resolve_meeting(proposal_id, MeetingOutcome::Discarded, None); |
| 45 | cx.notify(); |
| 46 | }); |
| 47 | } |
| 48 | |
| 49 | pub fn create_meeting(proposal_id: &str, cx: &mut App) { |
| 50 | let Some(api) = handle(cx) else { |
| 51 | return; |
| 52 | }; |
| 53 | let (hub, source, demo) = { |
| 54 | let api = api.read(cx); |
| 55 | (api.hub.clone(), api.source.clone(), api.demo) |
| 56 | }; |
| 57 | let Some(proposal) = hub.take_meeting(proposal_id) else { |
| 58 | return; |
| 59 | }; |
| 60 | api.update(cx, |_, cx| cx.notify()); |
| 61 | if !hub.policy().can_organize_meetings() { |
| 62 | hub.resolve_meeting(&proposal.id, MeetingOutcome::Discarded, None); |
| 63 | raise(format!("Organizing meetings is off - {} was not created", proposal.draft.subject), cx); |
| 64 | return; |
| 65 | } |
| 66 | if demo { |
| 67 | hub.resolve_meeting(&proposal.id, MeetingOutcome::Created, None); |
| 68 | return; |
| 69 | } |
| 70 | let draft = proposal.draft.clone(); |
| 71 | let receiver = crate::runtime::spawn(async move { source.create_meeting(draft).await }); |
| 72 | cx.spawn(async move |cx| { |
| 73 | match receiver.await { |
| 74 | Ok(Ok(created)) => hub.resolve_meeting(&proposal.id, MeetingOutcome::Created, created.join_url), |
| 75 | outcome => { |
| 76 | let reason = match outcome { |
| 77 | Ok(Err(error)) => error.to_string(), |
| 78 | _ => "cancelled".to_owned(), |
| 79 | }; |
| 80 | hub.resolve_meeting(&proposal.id, MeetingOutcome::Failed, None); |
| 81 | let text = format!("Could not create {}: {reason}", proposal.draft.subject); |
| 82 | cx.update(|cx| raise(text, cx)); |
| 83 | } |
| 84 | } |
| 85 | }) |
| 86 | .detach(); |
| 87 | } |
| 88 | |
| 89 | fn raise(text: String, cx: &mut App) { |
| 90 | if let Some(handle) = cx.try_global::<AppHandle>() { |
| 91 | handle.0.clone().update(cx, |state, cx| state.raise_notice(text, None, cx)); |
| 92 | } |
| 93 | } |
| 94 | |
| 95 | #[cfg(test)] |
| 96 | mod tests { |
| 97 | use std::collections::HashSet; |
| 98 | |
| 99 | use chrono::Utc; |
| 100 | use mcp::{MeetingDraft, MeetingProposal}; |
| 101 | |
| 102 | use super::unannounced; |
| 103 | |
| 104 | fn proposal(id: &str) -> MeetingProposal { |
| 105 | MeetingProposal { |
| 106 | id: id.to_owned(), |
| 107 | draft: MeetingDraft { |
| 108 | subject: "Sync".to_owned(), |
| 109 | start: Utc::now(), |
| 110 | end: Utc::now(), |
| 111 | attendees: Vec::new(), |
| 112 | description: String::new(), |
| 113 | }, |
| 114 | agent: None, |
| 115 | session_id: None, |
| 116 | created_at: Utc::now(), |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | #[test] |
| 121 | fn only_new_meeting_proposals_are_announced() { |
| 122 | let pending = [proposal("a"), proposal("b")]; |
| 123 | let announced = HashSet::from(["a".to_owned()]); |
| 124 | let fresh: Vec<&str> = unannounced(&announced, &pending).iter().map(|proposal| proposal.id.as_str()).collect(); |
| 125 | assert_eq!(fresh, ["b"]); |
| 126 | } |
| 127 | } |