use std::{collections::BTreeMap, sync::Arc}; use ruma::{ events::{EventContent, MessageLikeEventType, StateEventType, TimelineEventType}, EventId, MilliSecondsSinceUnixEpoch, }; use serde::Deserialize; use serde_json::value::{to_raw_value, RawValue as RawJsonValue}; /// Build the start of a PDU in order to add it to the Database. #[derive(Debug, Deserialize)] pub struct Builder { #[serde(rename = "type")] pub event_type: TimelineEventType, pub content: Box, pub unsigned: Option, pub state_key: Option, pub redacts: Option>, /// For timestamped messaging, should only be used for appservices. /// Will be set to current time if None pub timestamp: Option, } type Unsigned = BTreeMap; impl Builder { pub fn state(state_key: String, content: &T) -> Self where T: EventContent, { Self { event_type: content.event_type().into(), content: to_raw_value(content).expect("Builder failed to serialize state event content to RawValue"), state_key: Some(state_key), ..Self::default() } } pub fn timeline(content: &T) -> Self where T: EventContent, { Self { event_type: content.event_type().into(), content: to_raw_value(content).expect("Builder failed to serialize timeline event content to RawValue"), ..Self::default() } } } impl Default for Builder { fn default() -> Self { Self { event_type: "m.room.message".into(), content: Box::::default(), unsigned: None, state_key: None, redacts: None, timestamp: None, } } }