| 1 | use std::collections::HashMap; |
| 2 | use std::time::Duration; |
| 3 | |
| 4 | use teams_core::AdaptiveCard; |
| 5 | |
| 6 | const DIALOG_KEY: &str = "task-dialog"; |
| 7 | pub const DONE_HOLD: Duration = Duration::from_millis(1600); |
| 8 | pub const MAX_REASON_CHARS: usize = 160; |
| 9 | |
| 10 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 11 | pub enum ActionPhase { |
| 12 | Busy, |
| 13 | Done, |
| 14 | Failed(String), |
| 15 | } |
| 16 | |
| 17 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 18 | pub struct BotIdentity { |
| 19 | pub name: String, |
| 20 | pub icon_url: Option<String>, |
| 21 | } |
| 22 | |
| 23 | #[derive(Debug, Clone, PartialEq)] |
| 24 | pub struct TaskDialogState { |
| 25 | pub title: String, |
| 26 | pub card: AdaptiveCard, |
| 27 | pub scope: CardScope, |
| 28 | pub width: u32, |
| 29 | pub height: u32, |
| 30 | } |
| 31 | |
| 32 | #[derive(Debug, Clone, PartialEq)] |
| 33 | pub struct CardOverride { |
| 34 | pub basis: String, |
| 35 | pub cards: Vec<AdaptiveCard>, |
| 36 | } |
| 37 | |
| 38 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 39 | pub enum Surface { |
| 40 | Message, |
| 41 | Dialog, |
| 42 | } |
| 43 | |
| 44 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 45 | pub struct CardScope { |
| 46 | pub conversation_id: String, |
| 47 | pub message_id: String, |
| 48 | pub card_index: usize, |
| 49 | pub surface: Surface, |
| 50 | } |
| 51 | |
| 52 | impl CardScope { |
| 53 | pub fn message(conversation_id: &str, message_id: &str, card_index: usize) -> Self { |
| 54 | CardScope { |
| 55 | conversation_id: conversation_id.to_owned(), |
| 56 | message_id: message_id.to_owned(), |
| 57 | card_index, |
| 58 | surface: Surface::Message, |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | pub fn card_key(&self) -> String { |
| 63 | match self.surface { |
| 64 | Surface::Message => format!("{}-card-{}", self.message_id, self.card_index), |
| 65 | Surface::Dialog => Self::dialog_key(), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | pub fn dialog_key() -> String { |
| 70 | DIALOG_KEY.to_owned() |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | #[derive(Default)] |
| 75 | pub struct CardState { |
| 76 | phases: HashMap<String, ActionPhase>, |
| 77 | visibility: HashMap<String, bool>, |
| 78 | open_cards: HashMap<String, usize>, |
| 79 | notes: HashMap<String, String>, |
| 80 | overrides: HashMap<(String, String), CardOverride>, |
| 81 | } |
| 82 | |
| 83 | impl CardState { |
| 84 | pub fn phase(&self, action_key: &str) -> Option<&ActionPhase> { |
| 85 | self.phases.get(action_key) |
| 86 | } |
| 87 | |
| 88 | pub fn is_busy(&self, action_key: &str) -> bool { |
| 89 | self.phases.get(action_key) == Some(&ActionPhase::Busy) |
| 90 | } |
| 91 | |
| 92 | pub fn set_phase(&mut self, action_key: &str, phase: ActionPhase) { |
| 93 | self.phases.insert(action_key.to_owned(), phase); |
| 94 | } |
| 95 | |
| 96 | pub fn clear_done(&mut self, action_key: &str) { |
| 97 | if self.phases.get(action_key) == Some(&ActionPhase::Done) { |
| 98 | self.phases.remove(action_key); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | pub fn clear_card(&mut self, card_key: &str) { |
| 103 | self.phases.retain(|key, _| !key.starts_with(card_key)); |
| 104 | self.visibility.retain(|key, _| !key.starts_with(card_key)); |
| 105 | self.open_cards.retain(|key, _| !key.starts_with(card_key)); |
| 106 | self.notes.remove(card_key); |
| 107 | } |
| 108 | |
| 109 | pub fn is_visible(&self, element_key: &str, initial: bool) -> bool { |
| 110 | self.visibility.get(element_key).copied().unwrap_or(initial) |
| 111 | } |
| 112 | |
| 113 | pub fn toggle_visibility(&mut self, element_key: &str, initial: bool, forced: Option<bool>) { |
| 114 | let next = forced.unwrap_or(!self.is_visible(element_key, initial)); |
| 115 | self.visibility.insert(element_key.to_owned(), next); |
| 116 | } |
| 117 | |
| 118 | pub fn open_card(&self, actions_key: &str) -> Option<usize> { |
| 119 | self.open_cards.get(actions_key).copied() |
| 120 | } |
| 121 | |
| 122 | pub fn toggle_open_card(&mut self, actions_key: &str, index: usize) { |
| 123 | if self.open_cards.get(actions_key) == Some(&index) { |
| 124 | self.open_cards.remove(actions_key); |
| 125 | } else { |
| 126 | self.open_cards.insert(actions_key.to_owned(), index); |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | pub fn note(&self, card_key: &str) -> Option<&str> { |
| 131 | self.notes.get(card_key).map(String::as_str) |
| 132 | } |
| 133 | |
| 134 | pub fn set_note(&mut self, card_key: &str, text: String) { |
| 135 | self.notes.insert(card_key.to_owned(), text); |
| 136 | } |
| 137 | |
| 138 | pub fn clear_note(&mut self, card_key: &str) { |
| 139 | self.notes.remove(card_key); |
| 140 | } |
| 141 | |
| 142 | pub fn set_override( |
| 143 | &mut self, |
| 144 | conversation_id: &str, |
| 145 | message_id: &str, |
| 146 | replaced: CardOverride, |
| 147 | ) { |
| 148 | self.overrides.insert( |
| 149 | (conversation_id.to_owned(), message_id.to_owned()), |
| 150 | replaced, |
| 151 | ); |
| 152 | } |
| 153 | |
| 154 | pub fn overrides_for(&self, conversation_id: &str) -> HashMap<String, CardOverride> { |
| 155 | self.overrides |
| 156 | .iter() |
| 157 | .filter(|((conversation, _), _)| conversation == conversation_id) |
| 158 | .map(|((_, message_id), replaced)| (message_id.clone(), replaced.clone())) |
| 159 | .collect() |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | pub fn short_reason(reason: &str) -> String { |
| 164 | reason.chars().take(MAX_REASON_CHARS).collect() |
| 165 | } |
| 166 | |
| 167 | #[cfg(test)] |
| 168 | mod tests { |
| 169 | use super::*; |
| 170 | |
| 171 | #[test] |
| 172 | fn done_clears_but_busy_and_failed_stay() { |
| 173 | let mut state = CardState::default(); |
| 174 | state.set_phase("a", ActionPhase::Done); |
| 175 | state.set_phase("b", ActionPhase::Busy); |
| 176 | state.set_phase("c", ActionPhase::Failed("nope".into())); |
| 177 | for key in ["a", "b", "c"] { |
| 178 | state.clear_done(key); |
| 179 | } |
| 180 | assert_eq!(state.phase("a"), None); |
| 181 | assert!(state.is_busy("b")); |
| 182 | assert_eq!(state.phase("c"), Some(&ActionPhase::Failed("nope".into()))); |
| 183 | } |
| 184 | |
| 185 | #[test] |
| 186 | fn visibility_toggles_from_the_initial_value_or_is_forced() { |
| 187 | let mut state = CardState::default(); |
| 188 | assert!(!state.is_visible("e", false)); |
| 189 | state.toggle_visibility("e", false, None); |
| 190 | assert!(state.is_visible("e", false)); |
| 191 | state.toggle_visibility("e", false, None); |
| 192 | assert!(!state.is_visible("e", false)); |
| 193 | state.toggle_visibility("e", false, Some(true)); |
| 194 | state.toggle_visibility("e", false, Some(true)); |
| 195 | assert!(state.is_visible("e", false)); |
| 196 | } |
| 197 | |
| 198 | #[test] |
| 199 | fn one_show_card_is_open_per_action_set() { |
| 200 | let mut state = CardState::default(); |
| 201 | state.toggle_open_card("set", 1); |
| 202 | assert_eq!(state.open_card("set"), Some(1)); |
| 203 | state.toggle_open_card("set", 2); |
| 204 | assert_eq!(state.open_card("set"), Some(2)); |
| 205 | state.toggle_open_card("set", 2); |
| 206 | assert_eq!(state.open_card("set"), None); |
| 207 | } |
| 208 | |
| 209 | #[test] |
| 210 | fn overrides_are_scoped_to_their_conversation() { |
| 211 | let mut state = CardState::default(); |
| 212 | let replaced = CardOverride { |
| 213 | basis: "[]".into(), |
| 214 | cards: Vec::new(), |
| 215 | }; |
| 216 | state.set_override("c1", "m1", replaced.clone()); |
| 217 | assert_eq!(state.overrides_for("c1").get("m1"), Some(&replaced)); |
| 218 | assert!(state.overrides_for("c2").is_empty()); |
| 219 | } |
| 220 | |
| 221 | #[test] |
| 222 | fn reasons_are_shortened() { |
| 223 | assert_eq!( |
| 224 | short_reason(&"x".repeat(500)).chars().count(), |
| 225 | MAX_REASON_CHARS |
| 226 | ); |
| 227 | } |
| 228 | |
| 229 | #[test] |
| 230 | fn card_keys_differ_by_surface_and_index() { |
| 231 | let message = CardScope::message("c", "m", 2); |
| 232 | assert_eq!(message.card_key(), "m-card-2"); |
| 233 | let dialog = CardScope { |
| 234 | surface: Surface::Dialog, |
| 235 | ..message |
| 236 | }; |
| 237 | assert_eq!(dialog.card_key(), "task-dialog"); |
| 238 | } |
| 239 | } |