BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / agent_api / meetings.rs

meetings.rs

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