Commit 9e08b53e18
Unregistered key
Layout: unified · split
src/message/poll.rs +76 −21
| @@ -28,6 +28,8 @@ use matrix_sdk::ruma::{EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedU | ||
| 28 | 28 | |
| 29 | 29 | use crate::message::{MessageKey, MessageTimeStamp}; |
| 30 | 30 | |
| 31 | const PLAINTEXT_FALLBACK: &str = "(Answer is missing plain text description)"; | |
| 32 | ||
| 31 | 33 | /// Indicates where an [EventId] lives in the poll. |
| 32 | 34 | #[derive(Clone, Debug)] |
| 33 | 35 | pub enum PollEventLocation { |
| @@ -57,7 +59,9 @@ fn body_cow<'a>( | ||
| 57 | 59 | |
| 58 | 60 | let show_user_count = ended.is_some() || *kind == PollKind::Disclosed; |
| 59 | 61 | for (id, users) in results { |
| 60 | let (answer_text, selection_char) = answers.get_mut(id).unwrap(); | |
| 62 | let Some((answer_text, selection_char)) = answers.get_mut(id) else { | |
| 63 | continue; | |
| 64 | }; | |
| 61 | 65 | |
| 62 | 66 | if users.contains(own_user_id) { |
| 63 | 67 | *selection_char = 'X'; |
| @@ -94,7 +98,7 @@ pub struct Poll { | ||
| 94 | 98 | pub start: PollStartEventContent, |
| 95 | 99 | pub replacements: BTreeMap<MessageKey, PollStartEventContentWithoutRelation>, |
| 96 | 100 | |
| 97 | pub responeses: BTreeMap<MessageKey, OriginalPollResponseEvent>, | |
| 101 | pub responses: BTreeMap<MessageKey, OriginalPollResponseEvent>, | |
| 98 | 102 | |
| 99 | 103 | pub end: Option<OriginalMessageLikeEvent<PollEndEventContent>>, |
| 100 | 104 | } |
| @@ -111,7 +115,7 @@ impl Poll { | ||
| 111 | 115 | own_user_id, |
| 112 | 116 | start, |
| 113 | 117 | replacements: unloaded.replacements, |
| 114 | responeses: unloaded.responeses, | |
| 118 | responses: unloaded.responses, | |
| 115 | 119 | end: unloaded.end, |
| 116 | 120 | } |
| 117 | 121 | } |
| @@ -127,7 +131,7 @@ impl Poll { | ||
| 127 | 131 | |
| 128 | 132 | fn results(&self) -> impl IntoIterator<Item = (&str, BTreeSet<&UserId>)> { |
| 129 | 133 | let poll = self.content(); |
| 130 | let responses = self.responeses.values().map(OriginalPollResponseEvent::data); | |
| 134 | let responses = self.responses.values().map(OriginalPollResponseEvent::data); | |
| 131 | 135 | let end_timestamp = self.end.as_ref().map(|end| end.origin_server_ts); |
| 132 | 136 | |
| 133 | 137 | poll::compile_poll_results(poll, responses, end_timestamp) |
| @@ -169,7 +173,7 @@ impl Poll { | ||
| 169 | 173 | self.replacements.remove(key); |
| 170 | 174 | }, |
| 171 | 175 | PollEventLocation::Response(key) => { |
| 172 | self.responeses.remove(key); | |
| 176 | self.responses.remove(key); | |
| 173 | 177 | }, |
| 174 | 178 | PollEventLocation::End => self.end = None, |
| 175 | 179 | } |
| @@ -183,7 +187,7 @@ impl Poll { | ||
| 183 | 187 | id: ev.event_id.clone().into(), |
| 184 | 188 | }; |
| 185 | 189 | |
| 186 | self.responeses.insert(key.clone(), ev); | |
| 190 | self.responses.insert(key.clone(), ev); | |
| 187 | 191 | |
| 188 | 192 | PollEventLocation::Response(key) |
| 189 | 193 | }, |
| @@ -198,10 +202,10 @@ impl Poll { | ||
| 198 | 202 | let start = self.content(); |
| 199 | 203 | |
| 200 | 204 | let question = start.question.text.find_plain().unwrap_or("Question not found"); |
| 201 | let answers = start | |
| 202 | .answers | |
| 203 | .iter() | |
| 204 | .filter_map(|answer| answer.text.find_plain().map(|text| (answer.id.as_str(), text))); | |
| 205 | let answers = start.answers.iter().map(|answer| { | |
| 206 | // Only show the plaintext representation for now: | |
| 207 | (answer.id.as_str(), answer.text.find_plain().unwrap_or(PLAINTEXT_FALLBACK)) | |
| 208 | }); | |
| 205 | 209 | let results = self.results(); |
| 206 | 210 | let ended = self.end.as_ref().map(|ev| ev.origin_server_ts); |
| 207 | 211 | |
| @@ -214,7 +218,7 @@ impl Poll { | ||
| 214 | 218 | pub struct UnloadedPoll { |
| 215 | 219 | pub replacements: BTreeMap<MessageKey, PollStartEventContentWithoutRelation>, |
| 216 | 220 | |
| 217 | pub responeses: BTreeMap<MessageKey, OriginalPollResponseEvent>, | |
| 221 | pub responses: BTreeMap<MessageKey, OriginalPollResponseEvent>, | |
| 218 | 222 | |
| 219 | 223 | pub end: Option<OriginalMessageLikeEvent<PollEndEventContent>>, |
| 220 | 224 | } |
| @@ -226,7 +230,7 @@ impl UnloadedPoll { | ||
| 226 | 230 | self.replacements.remove(key); |
| 227 | 231 | }, |
| 228 | 232 | PollEventLocation::Response(key) => { |
| 229 | self.responeses.remove(key); | |
| 233 | self.responses.remove(key); | |
| 230 | 234 | }, |
| 231 | 235 | PollEventLocation::End => self.end = None, |
| 232 | 236 | } |
| @@ -240,7 +244,7 @@ impl UnloadedPoll { | ||
| 240 | 244 | id: ev.event_id.clone().into(), |
| 241 | 245 | }; |
| 242 | 246 | |
| 243 | self.responeses.insert(key.clone(), ev); | |
| 247 | self.responses.insert(key.clone(), ev); | |
| 244 | 248 | |
| 245 | 249 | PollEventLocation::Response(key) |
| 246 | 250 | }, |
| @@ -295,7 +299,7 @@ pub struct UnstablePoll { | ||
| 295 | 299 | pub start: NewUnstablePollStartEventContent, |
| 296 | 300 | pub replacements: BTreeMap<MessageKey, ReplacementUnstablePollStartEventContent>, |
| 297 | 301 | |
| 298 | pub responeses: BTreeMap<MessageKey, OriginalUnstablePollResponseEvent>, | |
| 302 | pub responses: BTreeMap<MessageKey, OriginalUnstablePollResponseEvent>, | |
| 299 | 303 | |
| 300 | 304 | pub end: Option<OriginalMessageLikeEvent<UnstablePollEndEventContent>>, |
| 301 | 305 | } |
| @@ -312,7 +316,7 @@ impl UnstablePoll { | ||
| 312 | 316 | own_user_id, |
| 313 | 317 | start, |
| 314 | 318 | replacements: unloaded.replacements, |
| 315 | responeses: unloaded.responeses, | |
| 319 | responses: unloaded.responses, | |
| 316 | 320 | end: unloaded.end, |
| 317 | 321 | } |
| 318 | 322 | } |
| @@ -328,7 +332,7 @@ impl UnstablePoll { | ||
| 328 | 332 | |
| 329 | 333 | fn results(&self) -> impl IntoIterator<Item = (&str, BTreeSet<&UserId>)> { |
| 330 | 334 | let poll = self.content(); |
| 331 | let responses = self.responeses.values().map(OriginalUnstablePollResponseEvent::data); | |
| 335 | let responses = self.responses.values().map(OriginalUnstablePollResponseEvent::data); | |
| 332 | 336 | let end_timestamp = self.end.as_ref().map(|end| end.origin_server_ts); |
| 333 | 337 | |
| 334 | 338 | poll::compile_unstable_poll_results(poll, responses, end_timestamp) |
| @@ -372,7 +376,7 @@ impl UnstablePoll { | ||
| 372 | 376 | self.replacements.remove(key); |
| 373 | 377 | }, |
| 374 | 378 | PollEventLocation::Response(key) => { |
| 375 | self.responeses.remove(key); | |
| 379 | self.responses.remove(key); | |
| 376 | 380 | }, |
| 377 | 381 | PollEventLocation::End => self.end = None, |
| 378 | 382 | } |
| @@ -386,7 +390,7 @@ impl UnstablePoll { | ||
| 386 | 390 | id: ev.event_id.clone().into(), |
| 387 | 391 | }; |
| 388 | 392 | |
| 389 | self.responeses.insert(key.clone(), ev); | |
| 393 | self.responses.insert(key.clone(), ev); | |
| 390 | 394 | |
| 391 | 395 | PollEventLocation::Response(key) |
| 392 | 396 | }, |
| @@ -417,7 +421,7 @@ impl UnstablePoll { | ||
| 417 | 421 | pub struct UnloadedUnstablePoll { |
| 418 | 422 | pub replacements: BTreeMap<MessageKey, ReplacementUnstablePollStartEventContent>, |
| 419 | 423 | |
| 420 | pub responeses: BTreeMap<MessageKey, OriginalUnstablePollResponseEvent>, | |
| 424 | pub responses: BTreeMap<MessageKey, OriginalUnstablePollResponseEvent>, | |
| 421 | 425 | |
| 422 | 426 | pub end: Option<OriginalMessageLikeEvent<UnstablePollEndEventContent>>, |
| 423 | 427 | } |
| @@ -429,7 +433,7 @@ impl UnloadedUnstablePoll { | ||
| 429 | 433 | self.replacements.remove(key); |
| 430 | 434 | }, |
| 431 | 435 | PollEventLocation::Response(key) => { |
| 432 | self.responeses.remove(key); | |
| 436 | self.responses.remove(key); | |
| 433 | 437 | }, |
| 434 | 438 | PollEventLocation::End => self.end = None, |
| 435 | 439 | } |
| @@ -443,7 +447,7 @@ impl UnloadedUnstablePoll { | ||
| 443 | 447 | id: ev.event_id.clone().into(), |
| 444 | 448 | }; |
| 445 | 449 | |
| 446 | self.responeses.insert(key.clone(), ev); | |
| 450 | self.responses.insert(key.clone(), ev); | |
| 447 | 451 | |
| 448 | 452 | PollEventLocation::Response(key) |
| 449 | 453 | }, |
| @@ -489,3 +493,54 @@ impl From<OriginalUnstablePollEndEvent> for UnstablePollRelation { | ||
| 489 | 493 | Self::End(value) |
| 490 | 494 | } |
| 491 | 495 | } |
| 496 | ||
| 497 | #[cfg(test)] | |
| 498 | mod tests { | |
| 499 | use super::*; | |
| 500 | ||
| 501 | use matrix_sdk::ruma::events::message::{TextContentBlock, TextRepresentation}; | |
| 502 | use matrix_sdk::ruma::events::poll::start::{PollAnswer, PollAnswers}; | |
| 503 | use matrix_sdk::ruma::{owned_event_id, owned_user_id}; | |
| 504 | ||
| 505 | fn poll_from(answers: Vec<PollAnswer>) -> Poll { | |
| 506 | let answers = PollAnswers::try_from(answers).unwrap(); | |
| 507 | let block = PollContentBlock::new(TextContentBlock::plain("Pick one"), answers); | |
| 508 | let start = PollStartEventContent::new(TextContentBlock::plain("Pick one"), block); | |
| 509 | ||
| 510 | Poll::new( | |
| 511 | owned_event_id!("$poll:example.com"), | |
| 512 | owned_user_id!("@user:example.com"), | |
| 513 | start, | |
| 514 | UnloadedPoll::default(), | |
| 515 | ) | |
| 516 | } | |
| 517 | ||
| 518 | #[test] | |
| 519 | fn test_body_cow_answer_html() { | |
| 520 | let html_only = TextContentBlock::from(vec![TextRepresentation::html("<b>Yes</b>")]); | |
| 521 | let answer1 = PollAnswer::new("a1".to_owned(), html_only); | |
| 522 | let answer2 = PollAnswer::new("a2".to_owned(), TextContentBlock::plain("No")); | |
| 523 | let poll = poll_from(vec![answer1, answer2]); | |
| 524 | ||
| 525 | // Verify that we show a fallback message when we don't show HTML answers: | |
| 526 | let body = poll.body_cow(); | |
| 527 | assert!(body.contains("Poll: Pick one"), "{body:?}"); | |
| 528 | assert!(body.contains(PLAINTEXT_FALLBACK), "{body:?}"); | |
| 529 | assert!(body.contains("No"), "{body:?}"); | |
| 530 | assert!(!body.contains("Yes"), "{body:?}"); | |
| 531 | assert_eq!(body.lines().count(), 3, "{body:?}"); | |
| 532 | } | |
| 533 | ||
| 534 | #[test] | |
| 535 | fn test_body_cow_answer_plaintext_only() { | |
| 536 | let answer1 = PollAnswer::new("a1".to_owned(), TextContentBlock::plain("Yes")); | |
| 537 | let answer2 = PollAnswer::new("a2".to_owned(), TextContentBlock::plain("No")); | |
| 538 | let poll = poll_from(vec![answer1, answer2]); | |
| 539 | ||
| 540 | let body = poll.body_cow(); | |
| 541 | assert!(body.contains("Poll: Pick one"), "{body:?}"); | |
| 542 | assert!(body.contains("Yes"), "{body:?}"); | |
| 543 | assert!(body.contains("No"), "{body:?}"); | |
| 544 | assert!(!body.contains(PLAINTEXT_FALLBACK), "{body:?}"); | |
| 545 | } | |
| 546 | } | |