ruma_events/poll/
unstable_response.rs

1//! Types for the `org.matrix.msc3381.poll.response` event, the unstable version of
2//! `m.poll.response`.
3
4use ruma_common::OwnedEventId;
5use ruma_macros::EventContent;
6use serde::{Deserialize, Serialize};
7
8use super::{unstable_start::UnstablePollStartContentBlock, validate_selections, PollResponseData};
9use crate::relation::Reference;
10
11/// The payload for an unstable poll response event.
12///
13/// This is the event content that should be sent for room versions that don't support extensible
14/// events. As of Matrix 1.7, none of the stable room versions (1 through 10) support extensible
15/// events.
16///
17/// To send a poll response event for a room version that supports extensible events, use
18/// [`PollResponseEventContent`].
19///
20/// [`PollResponseEventContent`]: super::response::PollResponseEventContent
21#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
22#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
23#[ruma_event(type = "org.matrix.msc3381.poll.response", kind = MessageLike)]
24pub struct UnstablePollResponseEventContent {
25    /// The response's content.
26    #[serde(rename = "org.matrix.msc3381.poll.response")]
27    pub poll_response: UnstablePollResponseContentBlock,
28
29    /// Information about the poll start event this responds to.
30    #[serde(rename = "m.relates_to")]
31    pub relates_to: Reference,
32}
33
34impl UnstablePollResponseEventContent {
35    /// Creates a new `UnstablePollResponseEventContent` that responds to the given poll start event
36    /// ID, with the given answers.
37    pub fn new(answers: Vec<String>, poll_start_id: OwnedEventId) -> Self {
38        Self {
39            poll_response: UnstablePollResponseContentBlock::new(answers),
40            relates_to: Reference::new(poll_start_id),
41        }
42    }
43}
44
45impl OriginalSyncUnstablePollResponseEvent {
46    /// Get the data from this response necessary to compile poll results.
47    pub fn data(&self) -> PollResponseData<'_> {
48        PollResponseData {
49            sender: &self.sender,
50            origin_server_ts: self.origin_server_ts,
51            selections: &self.content.poll_response.answers,
52        }
53    }
54}
55
56impl OriginalUnstablePollResponseEvent {
57    /// Get the data from this response necessary to compile poll results.
58    pub fn data(&self) -> PollResponseData<'_> {
59        PollResponseData {
60            sender: &self.sender,
61            origin_server_ts: self.origin_server_ts,
62            selections: &self.content.poll_response.answers,
63        }
64    }
65}
66
67/// An unstable block for poll response content.
68#[derive(Clone, Debug, Serialize, Deserialize)]
69#[cfg_attr(not(ruma_unstable_exhaustive_types), non_exhaustive)]
70pub struct UnstablePollResponseContentBlock {
71    /// The selected answers for the response.
72    pub answers: Vec<String>,
73}
74
75impl UnstablePollResponseContentBlock {
76    /// Creates a new `UnstablePollResponseContentBlock` with the given answers.
77    pub fn new(answers: Vec<String>) -> Self {
78        Self { answers }
79    }
80
81    /// Validate these selections against the given `UnstablePollStartContentBlock`.
82    ///
83    /// Returns the list of valid selections in this `UnstablePollResponseContentBlock`, or `None`
84    /// if there is no valid selection.
85    pub fn validate<'a>(
86        &'a self,
87        poll: &UnstablePollStartContentBlock,
88    ) -> Option<impl Iterator<Item = &'a str>> {
89        let answer_ids = poll.answers.iter().map(|a| a.id.as_str()).collect();
90        validate_selections(&answer_ids, poll.max_selections, &self.answers)
91    }
92}
93
94impl From<Vec<String>> for UnstablePollResponseContentBlock {
95    fn from(value: Vec<String>) -> Self {
96        Self::new(value)
97    }
98}