2024-12-28 00:57:02 +00:00
|
|
|
use std::collections::BTreeMap;
|
2024-07-03 21:05:24 +00:00
|
|
|
|
2024-10-04 20:25:32 +00:00
|
|
|
use ruma::{
|
|
|
|
events::{EventContent, MessageLikeEventType, StateEventType, TimelineEventType},
|
2024-12-28 00:57:02 +00:00
|
|
|
MilliSecondsSinceUnixEpoch, OwnedEventId,
|
2024-10-04 20:25:32 +00:00
|
|
|
};
|
2024-07-03 21:05:24 +00:00
|
|
|
use serde::Deserialize;
|
2024-10-04 20:25:32 +00:00
|
|
|
use serde_json::value::{to_raw_value, RawValue as RawJsonValue};
|
2024-07-03 21:05:24 +00:00
|
|
|
|
2025-02-08 00:16:37 +00:00
|
|
|
use super::StateKey;
|
|
|
|
|
2024-07-03 21:05:24 +00:00
|
|
|
/// Build the start of a PDU in order to add it to the Database.
|
|
|
|
#[derive(Debug, Deserialize)]
|
2024-10-04 20:25:32 +00:00
|
|
|
pub struct Builder {
|
2024-07-03 21:05:24 +00:00
|
|
|
#[serde(rename = "type")]
|
|
|
|
pub event_type: TimelineEventType,
|
2024-10-04 20:25:32 +00:00
|
|
|
|
2024-07-03 21:05:24 +00:00
|
|
|
pub content: Box<RawJsonValue>,
|
2024-10-04 20:25:32 +00:00
|
|
|
|
|
|
|
pub unsigned: Option<Unsigned>,
|
|
|
|
|
2025-02-08 00:16:37 +00:00
|
|
|
pub state_key: Option<StateKey>,
|
2024-10-04 20:25:32 +00:00
|
|
|
|
2024-12-28 00:57:02 +00:00
|
|
|
pub redacts: Option<OwnedEventId>,
|
2024-10-04 20:25:32 +00:00
|
|
|
|
|
|
|
/// For timestamped messaging, should only be used for appservices.
|
2024-08-14 20:01:34 -04:00
|
|
|
/// Will be set to current time if None
|
|
|
|
pub timestamp: Option<MilliSecondsSinceUnixEpoch>,
|
2024-07-03 21:05:24 +00:00
|
|
|
}
|
2024-10-04 20:25:32 +00:00
|
|
|
|
|
|
|
type Unsigned = BTreeMap<String, serde_json::Value>;
|
|
|
|
|
|
|
|
impl Builder {
|
2025-02-08 00:16:37 +00:00
|
|
|
pub fn state<S, T>(state_key: S, content: &T) -> Self
|
2024-10-04 20:25:32 +00:00
|
|
|
where
|
|
|
|
T: EventContent<EventType = StateEventType>,
|
2025-02-08 00:16:37 +00:00
|
|
|
S: Into<StateKey>,
|
2024-10-04 20:25:32 +00:00
|
|
|
{
|
|
|
|
Self {
|
|
|
|
event_type: content.event_type().into(),
|
2024-12-15 00:05:47 -05:00
|
|
|
content: to_raw_value(content)
|
|
|
|
.expect("Builder failed to serialize state event content to RawValue"),
|
2025-02-08 00:16:37 +00:00
|
|
|
state_key: Some(state_key.into()),
|
2024-10-04 20:25:32 +00:00
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn timeline<T>(content: &T) -> Self
|
|
|
|
where
|
|
|
|
T: EventContent<EventType = MessageLikeEventType>,
|
|
|
|
{
|
|
|
|
Self {
|
|
|
|
event_type: content.event_type().into(),
|
2024-12-15 00:05:47 -05:00
|
|
|
content: to_raw_value(content)
|
|
|
|
.expect("Builder failed to serialize timeline event content to RawValue"),
|
2024-10-04 20:25:32 +00:00
|
|
|
..Self::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Builder {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
event_type: "m.room.message".into(),
|
|
|
|
content: Box::<RawJsonValue>::default(),
|
|
|
|
unsigned: None,
|
|
|
|
state_key: None,
|
|
|
|
redacts: None,
|
|
|
|
timestamp: None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|