| 1 | use gpui_kit::*; |
| 2 | |
| 3 | use crate::app_state::{AppEvent, AppState}; |
| 4 | use crate::rows::{Delivery, MessageRow}; |
| 5 | |
| 6 | pub const FORWARD_SELECTION_LIMIT: usize = 5; |
| 7 | pub const FORWARD_IMAGE_LIMIT: usize = 5; |
| 8 | pub const FORWARD_LABEL: &str = "Forward"; |
| 9 | pub const THIS_MESSAGE_LABEL: &str = "This message"; |
| 10 | pub const MULTIPLE_MESSAGES_LABEL: &str = "Multiple messages"; |
| 11 | pub const SELECT_MESSAGE_LABEL: &str = "Select message"; |
| 12 | pub const NEXT_LABEL: &str = "Next"; |
| 13 | pub const CANCEL_LABEL: &str = "Cancel"; |
| 14 | pub const LIMIT_REASON: &str = "You can only forward up to 5 messages at a time."; |
| 15 | pub const IMAGE_LIMIT_REASON: &str = "Messages with more than 5 images can't be forwarded."; |
| 16 | pub const UNDELIVERED_REASON: &str = "Undelivered messages can't be forwarded."; |
| 17 | pub const SCHEDULED_REASON: &str = "Scheduled messages can't be forwarded until they're sent."; |
| 18 | pub const DEFAULT_REASON: &str = "This message can't be forwarded."; |
| 19 | |
| 20 | pub fn selected_count_label(count: usize) -> String { |
| 21 | format!("{count} selected") |
| 22 | } |
| 23 | |
| 24 | #[derive(Debug, Clone, PartialEq, Eq)] |
| 25 | pub struct ForwardSelection { |
| 26 | conversation_id: String, |
| 27 | message_ids: Vec<String>, |
| 28 | } |
| 29 | |
| 30 | impl ForwardSelection { |
| 31 | pub fn starting_with(conversation_id: &str, message_id: &str) -> Self { |
| 32 | ForwardSelection { |
| 33 | conversation_id: conversation_id.to_owned(), |
| 34 | message_ids: vec![message_id.to_owned()], |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | pub fn conversation_id(&self) -> &str { |
| 39 | &self.conversation_id |
| 40 | } |
| 41 | |
| 42 | pub fn message_ids(&self) -> &[String] { |
| 43 | &self.message_ids |
| 44 | } |
| 45 | |
| 46 | pub fn contains(&self, message_id: &str) -> bool { |
| 47 | self.message_ids |
| 48 | .iter() |
| 49 | .any(|selected| selected == message_id) |
| 50 | } |
| 51 | |
| 52 | pub fn len(&self) -> usize { |
| 53 | self.message_ids.len() |
| 54 | } |
| 55 | |
| 56 | pub fn is_empty(&self) -> bool { |
| 57 | self.message_ids.is_empty() |
| 58 | } |
| 59 | |
| 60 | pub fn is_full(&self) -> bool { |
| 61 | self.message_ids.len() >= FORWARD_SELECTION_LIMIT |
| 62 | } |
| 63 | |
| 64 | /// Removes a selected message or adds a new one; returns false when the limit refuses it. |
| 65 | pub fn toggle(&mut self, message_id: &str) -> bool { |
| 66 | if let Some(position) = self |
| 67 | .message_ids |
| 68 | .iter() |
| 69 | .position(|selected| selected == message_id) |
| 70 | { |
| 71 | self.message_ids.remove(position); |
| 72 | return true; |
| 73 | } |
| 74 | if self.is_full() { |
| 75 | return false; |
| 76 | } |
| 77 | self.message_ids.push(message_id.to_owned()); |
| 78 | true |
| 79 | } |
| 80 | |
| 81 | /// `rows` in timeline order; the result keeps only the selected rows, oldest first. |
| 82 | pub fn ordered_ids<'a>(&self, rows: impl IntoIterator<Item = &'a MessageRow>) -> Vec<String> { |
| 83 | rows.into_iter() |
| 84 | .filter(|row| self.contains(&row.key)) |
| 85 | .map(|row| row.key.clone()) |
| 86 | .collect() |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// Why `row` cannot be checked now, in Teams' words; `None` when it can. |
| 91 | pub fn block_reason( |
| 92 | row: &MessageRow, |
| 93 | selection: &ForwardSelection, |
| 94 | selected_image_count: usize, |
| 95 | ) -> Option<&'static str> { |
| 96 | if selection.contains(&row.key) { |
| 97 | return None; |
| 98 | } |
| 99 | match row.delivery { |
| 100 | Delivery::Sending | Delivery::Failed(_) => return Some(UNDELIVERED_REASON), |
| 101 | Delivery::Scheduled(_) => return Some(SCHEDULED_REASON), |
| 102 | Delivery::Delivered => {} |
| 103 | } |
| 104 | if row.deleted || row.call.is_some() { |
| 105 | return Some(DEFAULT_REASON); |
| 106 | } |
| 107 | if !row.images.is_empty() && selected_image_count + row.images.len() > FORWARD_IMAGE_LIMIT { |
| 108 | return Some(IMAGE_LIMIT_REASON); |
| 109 | } |
| 110 | if selection.is_full() { |
| 111 | return Some(LIMIT_REASON); |
| 112 | } |
| 113 | None |
| 114 | } |
| 115 | |
| 116 | impl AppState { |
| 117 | pub fn start_forward_selection( |
| 118 | &mut self, |
| 119 | conversation_id: &str, |
| 120 | message_id: &str, |
| 121 | cx: &mut Context<Self>, |
| 122 | ) { |
| 123 | if self.mode.read_only { |
| 124 | self.raise_notice("Read-only mode: message not forwarded".to_owned(), None, cx); |
| 125 | return; |
| 126 | } |
| 127 | self.forward_selection = Some(ForwardSelection::starting_with(conversation_id, message_id)); |
| 128 | cx.emit(AppEvent::ForwardSelection); |
| 129 | cx.notify(); |
| 130 | } |
| 131 | |
| 132 | pub fn toggle_forward_selected(&mut self, message_id: &str, cx: &mut Context<Self>) { |
| 133 | let Some(selection) = self.forward_selection.as_mut() else { |
| 134 | return; |
| 135 | }; |
| 136 | if selection.toggle(message_id) { |
| 137 | cx.notify(); |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | pub fn cancel_forward_selection(&mut self, cx: &mut Context<Self>) { |
| 142 | if self.forward_selection.take().is_some() { |
| 143 | cx.emit(AppEvent::ForwardSelection); |
| 144 | cx.notify(); |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | /// `ordered_ids` are the selected messages, oldest first. The selection stays until the forward is sent. |
| 149 | pub fn forward_selected(&mut self, ordered_ids: &[String], cx: &mut Context<Self>) { |
| 150 | let Some(conversation_id) = self |
| 151 | .forward_selection |
| 152 | .as_ref() |
| 153 | .map(|selection| selection.conversation_id().to_owned()) |
| 154 | else { |
| 155 | return; |
| 156 | }; |
| 157 | if !ordered_ids.is_empty() { |
| 158 | self.request_forward_messages(&conversation_id, ordered_ids, cx); |
| 159 | } |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | #[cfg(test)] |
| 164 | mod tests { |
| 165 | use super::{ |
| 166 | DEFAULT_REASON, Delivery, FORWARD_SELECTION_LIMIT, ForwardSelection, IMAGE_LIMIT_REASON, |
| 167 | LIMIT_REASON, MessageRow, SCHEDULED_REASON, UNDELIVERED_REASON, block_reason, |
| 168 | selected_count_label, |
| 169 | }; |
| 170 | use crate::rows::ScheduledState; |
| 171 | use teams_core::ImageRef; |
| 172 | |
| 173 | fn row(key: &str) -> MessageRow { |
| 174 | crate::pending_rows::pending_row( |
| 175 | key.to_owned(), |
| 176 | "chat".to_owned(), |
| 177 | None, |
| 178 | chrono::Utc::now(), |
| 179 | &crate::scheduled_rows::scheduled_outgoing("<p>hello</p>"), |
| 180 | Delivery::Delivered, |
| 181 | ) |
| 182 | } |
| 183 | |
| 184 | fn image() -> ImageRef { |
| 185 | ImageRef { |
| 186 | id: "image".into(), |
| 187 | url: "https://example.invalid/image.png".into(), |
| 188 | width: None, |
| 189 | height: None, |
| 190 | alt: None, |
| 191 | } |
| 192 | } |
| 193 | |
| 194 | #[test] |
| 195 | fn the_first_message_is_selected_and_toggling_adds_or_removes() { |
| 196 | let mut selection = ForwardSelection::starting_with("chat", "m1"); |
| 197 | assert_eq!(selection.message_ids(), ["m1"]); |
| 198 | assert!(selection.toggle("m2")); |
| 199 | assert!(selection.contains("m2")); |
| 200 | assert!(selection.toggle("m1")); |
| 201 | assert_eq!(selection.message_ids(), ["m2"]); |
| 202 | assert!(selection.toggle("m2")); |
| 203 | assert!(selection.is_empty()); |
| 204 | } |
| 205 | |
| 206 | #[test] |
| 207 | fn a_sixth_message_is_refused() { |
| 208 | let mut selection = ForwardSelection::starting_with("chat", "m1"); |
| 209 | for id in ["m2", "m3", "m4", "m5"] { |
| 210 | assert!(selection.toggle(id)); |
| 211 | } |
| 212 | assert!(selection.is_full()); |
| 213 | assert!(!selection.toggle("m6")); |
| 214 | assert_eq!(selection.len(), FORWARD_SELECTION_LIMIT); |
| 215 | assert_eq!(block_reason(&row("m6"), &selection, 0), Some(LIMIT_REASON)); |
| 216 | assert_eq!(block_reason(&row("m3"), &selection, 0), None); |
| 217 | } |
| 218 | |
| 219 | #[test] |
| 220 | fn the_order_follows_the_timeline_not_the_clicks() { |
| 221 | let mut selection = ForwardSelection::starting_with("chat", "m3"); |
| 222 | selection.toggle("m1"); |
| 223 | let rows = [row("m1"), row("m2"), row("m3")]; |
| 224 | assert_eq!(selection.ordered_ids(rows.iter()), ["m1", "m3"]); |
| 225 | } |
| 226 | |
| 227 | #[test] |
| 228 | fn unsent_scheduled_deleted_and_call_rows_cannot_be_checked() { |
| 229 | let selection = ForwardSelection::starting_with("chat", "m1"); |
| 230 | let mut sending = row("a"); |
| 231 | sending.delivery = Delivery::Sending; |
| 232 | assert_eq!( |
| 233 | block_reason(&sending, &selection, 0), |
| 234 | Some(UNDELIVERED_REASON) |
| 235 | ); |
| 236 | let mut failed = row("b"); |
| 237 | failed.delivery = Delivery::Failed("offline".into()); |
| 238 | assert_eq!( |
| 239 | block_reason(&failed, &selection, 0), |
| 240 | Some(UNDELIVERED_REASON) |
| 241 | ); |
| 242 | let mut scheduled = row("c"); |
| 243 | scheduled.delivery = Delivery::Scheduled(ScheduledState::Waiting); |
| 244 | assert_eq!( |
| 245 | block_reason(&scheduled, &selection, 0), |
| 246 | Some(SCHEDULED_REASON) |
| 247 | ); |
| 248 | let mut deleted = row("d"); |
| 249 | deleted.deleted = true; |
| 250 | assert_eq!(block_reason(&deleted, &selection, 0), Some(DEFAULT_REASON)); |
| 251 | } |
| 252 | |
| 253 | #[test] |
| 254 | fn images_over_the_combined_limit_block_the_row() { |
| 255 | let selection = ForwardSelection::starting_with("chat", "m1"); |
| 256 | let mut pictures = row("p"); |
| 257 | pictures.images = vec![image(), image()]; |
| 258 | assert_eq!(block_reason(&pictures, &selection, 3), None); |
| 259 | assert_eq!( |
| 260 | block_reason(&pictures, &selection, 4), |
| 261 | Some(IMAGE_LIMIT_REASON) |
| 262 | ); |
| 263 | } |
| 264 | |
| 265 | #[test] |
| 266 | fn the_footer_counts_the_selection() { |
| 267 | assert_eq!(selected_count_label(3), "3 selected"); |
| 268 | } |
| 269 | } |