matrix_sdk_ui/timeline/event_item/content/
polls.rsuse std::collections::HashMap;
use ruma::{
events::poll::{
compile_unstable_poll_results,
start::PollKind,
unstable_response::UnstablePollResponseEventContent,
unstable_start::{
NewUnstablePollStartEventContent, NewUnstablePollStartEventContentWithoutRelation,
UnstablePollStartContentBlock,
},
PollResponseData,
},
MilliSecondsSinceUnixEpoch, OwnedUserId, UserId,
};
#[derive(Clone, Debug)]
pub struct PollState {
pub(in crate::timeline) start_event_content: NewUnstablePollStartEventContent,
pub(in crate::timeline) response_data: Vec<ResponseData>,
pub(in crate::timeline) end_event_timestamp: Option<MilliSecondsSinceUnixEpoch>,
pub(in crate::timeline) has_been_edited: bool,
}
#[derive(Clone, Debug)]
pub(in crate::timeline) struct ResponseData {
pub sender: OwnedUserId,
pub timestamp: MilliSecondsSinceUnixEpoch,
pub answers: Vec<String>,
}
impl PollState {
pub(crate) fn new(
content: NewUnstablePollStartEventContent,
edit: Option<NewUnstablePollStartEventContentWithoutRelation>,
) -> Self {
let mut ret = Self {
start_event_content: content,
response_data: vec![],
end_event_timestamp: None,
has_been_edited: false,
};
if let Some(edit) = edit {
ret = ret.edit(edit).unwrap();
}
ret
}
pub(crate) fn edit(
&self,
replacement: NewUnstablePollStartEventContentWithoutRelation,
) -> Option<Self> {
if self.end_event_timestamp.is_none() {
let mut clone = self.clone();
clone.start_event_content.poll_start = replacement.poll_start;
clone.start_event_content.text = replacement.text;
clone.has_been_edited = true;
Some(clone)
} else {
None
}
}
pub(crate) fn add_response(
&self,
sender: &UserId,
timestamp: MilliSecondsSinceUnixEpoch,
content: &UnstablePollResponseEventContent,
) -> Self {
let mut clone = self.clone();
clone.response_data.push(ResponseData {
sender: sender.to_owned(),
timestamp,
answers: content.poll_response.answers.clone(),
});
clone
}
pub(crate) fn end(&self, timestamp: MilliSecondsSinceUnixEpoch) -> Result<Self, ()> {
if self.end_event_timestamp.is_none() {
let mut clone = self.clone();
clone.end_event_timestamp = Some(timestamp);
Ok(clone)
} else {
Err(())
}
}
pub fn fallback_text(&self) -> Option<String> {
self.start_event_content.text.clone()
}
pub fn results(&self) -> PollResult {
let results = compile_unstable_poll_results(
&self.start_event_content.poll_start,
self.response_data.iter().map(|response_data| PollResponseData {
sender: &response_data.sender,
origin_server_ts: response_data.timestamp,
selections: &response_data.answers,
}),
self.end_event_timestamp,
);
PollResult {
question: self.start_event_content.poll_start.question.text.clone(),
kind: self.start_event_content.poll_start.kind.clone(),
max_selections: self.start_event_content.poll_start.max_selections.into(),
answers: self
.start_event_content
.poll_start
.answers
.iter()
.map(|i| PollResultAnswer { id: i.id.clone(), text: i.text.clone() })
.collect(),
votes: results
.iter()
.map(|i| ((*i.0).to_owned(), i.1.iter().map(|i| i.to_string()).collect()))
.collect(),
end_time: self.end_event_timestamp.map(|millis| millis.0.into()),
has_been_edited: self.has_been_edited,
}
}
pub fn is_edit(&self) -> bool {
self.has_been_edited
}
}
impl From<PollState> for NewUnstablePollStartEventContent {
fn from(value: PollState) -> Self {
let content = UnstablePollStartContentBlock::new(
value.start_event_content.poll_start.question.text.clone(),
value.start_event_content.poll_start.answers.clone(),
);
if let Some(text) = value.fallback_text() {
NewUnstablePollStartEventContent::plain_text(text, content)
} else {
NewUnstablePollStartEventContent::new(content)
}
}
}
#[derive(Debug)]
pub struct PollResult {
pub question: String,
pub kind: PollKind,
pub max_selections: u64,
pub answers: Vec<PollResultAnswer>,
pub votes: HashMap<String, Vec<String>>,
pub end_time: Option<u64>,
pub has_been_edited: bool,
}
#[derive(Debug)]
pub struct PollResultAnswer {
pub id: String,
pub text: String,
}