BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-state / src / card_refresh.rs

card_refresh.rs

d39bdfed History
card_refresh.rs 3.8 KiB Download Raw
1use gpui_kit::*;
2use teams_core::{AdaptiveCard, CardActionOutcome, ExecuteAction, ExecuteTrigger};
3use tokio::sync::oneshot;
4
5use crate::app_state::AppState;
6use crate::card_actions::CardAnswer;
7use crate::card_state::{CardScope, short_reason};
8use crate::runtime;
9
10impl AppState {
11 pub fn request_card_refreshes(
12 &mut self,
13 conversation_id: &str,
14 message_id: &str,
15 cards: &[AdaptiveCard],
16 cx: &mut Context<Self>,
17 ) {
18 if self.mode.read_only {
19 return;
20 }
21 let Some((card_index, refresh)) = cards
22 .iter()
23 .enumerate()
24 .find_map(|(index, card)| card.refresh.as_ref().map(|refresh| (index, refresh)))
25 else {
26 return;
27 };
28 let member_count = self
29 .sidebar
30 .chats
31 .iter()
32 .find(|chat| chat.id == conversation_id)
33 .map(|chat| chat.members.len());
34 let my_user_id = self.directory.me.as_ref().map(|me| me.user_id.as_str());
35 if !refresh.runs_automatically(member_count, my_user_id) {
36 return;
37 }
38 let scope = CardScope::message(conversation_id, message_id, card_index);
39 self.start_card_refresh(scope, refresh.action.clone(), ExecuteTrigger::Automatic, cx);
40 }
41
42 pub fn refresh_card_manually(
43 &mut self,
44 scope: CardScope,
45 action: ExecuteAction,
46 cx: &mut Context<Self>,
47 ) {
48 self.start_card_refresh(scope, action, ExecuteTrigger::Manual, cx);
49 }
50
51 fn start_card_refresh(
52 &mut self,
53 scope: CardScope,
54 action: ExecuteAction,
55 trigger: ExecuteTrigger,
56 cx: &mut Context<Self>,
57 ) {
58 let manual = trigger == ExecuteTrigger::Manual;
59 if !self
60 .cards
61 .begin_refresh(&scope.conversation_id, &scope.message_id, manual)
62 {
63 return;
64 }
65 let Some(receiver) = self.refresh_answer(&scope, action, trigger) else {
66 self.cards
67 .cancel_refresh(&scope.conversation_id, &scope.message_id);
68 return;
69 };
70 cx.spawn(async move |this, cx| {
71 let answer = receiver
72 .await
73 .unwrap_or_else(|_| Err("cancelled".to_owned()));
74 this.update(cx, |state, cx| {
75 state.finish_card_refresh(&scope, answer, cx)
76 })
77 .ok();
78 })
79 .detach();
80 }
81
82 fn refresh_answer(
83 &self,
84 scope: &CardScope,
85 action: ExecuteAction,
86 trigger: ExecuteTrigger,
87 ) -> Option<oneshot::Receiver<CardAnswer>> {
88 if self.mode.demo {
89 return Some(runtime::spawn(crate::demo::refresh_answer()));
90 }
91 let engine = self.engine.clone()?;
92 let (conversation_id, message_id) =
93 (scope.conversation_id.clone(), scope.message_id.clone());
94 Some(runtime::spawn(async move {
95 let outcome = engine
96 .card_refresh(&conversation_id, &message_id, &action, trigger)
97 .await
98 .map_err(|error| short_reason(&error.to_string()))?;
99 Ok((outcome, None))
100 }))
101 }
102
103 fn finish_card_refresh(
104 &mut self,
105 scope: &CardScope,
106 answer: CardAnswer,
107 cx: &mut Context<Self>,
108 ) {
109 let was_full = self.cards.finish_refresh();
110 let replaced = match answer {
111 Ok((CardActionOutcome::ReplaceCard(json), _)) => self.replace_card(scope, &json, cx),
112 Ok((CardActionOutcome::Failed(reason), _)) | Err(reason) => {
113 crate::diagnostics::record(format!("card refresh failed: {reason}"));
114 false
115 }
116 Ok(_) => false,
117 };
118 if was_full && !replaced {
119 self.announce_cards(scope, cx);
120 }
121 }
122}