use ruma_common::OwnedEventId;
use ruma_macros::EventContent;
use serde::{Deserialize, Serialize};
use super::{unstable_start::UnstablePollStartContentBlock, validate_selections, PollResponseData};
use crate::relation::Reference;
#[derive(Clone, Debug, Serialize, Deserialize, EventContent)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
#[ruma_event(type = "org.matrix.msc3381.poll.response", kind = MessageLike)]
pub struct UnstablePollResponseEventContent {
#[serde(rename = "org.matrix.msc3381.poll.response")]
pub poll_response: UnstablePollResponseContentBlock,
#[serde(rename = "m.relates_to")]
pub relates_to: Reference,
}
impl UnstablePollResponseEventContent {
pub fn new(answers: Vec<String>, poll_start_id: OwnedEventId) -> Self {
Self {
poll_response: UnstablePollResponseContentBlock::new(answers),
relates_to: Reference::new(poll_start_id),
}
}
}
impl OriginalSyncUnstablePollResponseEvent {
pub fn data(&self) -> PollResponseData<'_> {
PollResponseData {
sender: &self.sender,
origin_server_ts: self.origin_server_ts,
selections: &self.content.poll_response.answers,
}
}
}
impl OriginalUnstablePollResponseEvent {
pub fn data(&self) -> PollResponseData<'_> {
PollResponseData {
sender: &self.sender,
origin_server_ts: self.origin_server_ts,
selections: &self.content.poll_response.answers,
}
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[cfg_attr(not(feature = "unstable-exhaustive-types"), non_exhaustive)]
pub struct UnstablePollResponseContentBlock {
pub answers: Vec<String>,
}
impl UnstablePollResponseContentBlock {
pub fn new(answers: Vec<String>) -> Self {
Self { answers }
}
pub fn validate<'a>(
&'a self,
poll: &UnstablePollStartContentBlock,
) -> Option<impl Iterator<Item = &'a str>> {
let answer_ids = poll.answers.iter().map(|a| a.id.as_str()).collect();
validate_selections(&answer_ids, poll.max_selections, &self.answers)
}
}
impl From<Vec<String>> for UnstablePollResponseContentBlock {
fn from(value: Vec<String>) -> Self {
Self::new(value)
}
}