Add constructions and Default for PduBuilder

simplify various RoomMemberEventContent constructions

Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
Jason Volk 2024-10-04 20:25:32 +00:00 committed by strawberry
parent f503ed918c
commit e482c0646f
16 changed files with 279 additions and 564 deletions

View file

@ -1,20 +1,67 @@
use std::{collections::BTreeMap, sync::Arc};
use ruma::{events::TimelineEventType, EventId, MilliSecondsSinceUnixEpoch};
use ruma::{
events::{EventContent, MessageLikeEventType, StateEventType, TimelineEventType},
EventId, MilliSecondsSinceUnixEpoch,
};
use serde::Deserialize;
use serde_json::value::RawValue as RawJsonValue;
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 PduBuilder {
pub struct Builder {
#[serde(rename = "type")]
pub event_type: TimelineEventType,
pub content: Box<RawJsonValue>,
pub unsigned: Option<BTreeMap<String, serde_json::Value>>,
pub unsigned: Option<Unsigned>,
pub state_key: Option<String>,
pub redacts: Option<Arc<EventId>>,
/// For timestamped messaging, should only be used for appservices
///
/// For timestamped messaging, should only be used for appservices.
/// Will be set to current time if None
pub timestamp: Option<MilliSecondsSinceUnixEpoch>,
}
type Unsigned = BTreeMap<String, serde_json::Value>;
impl Builder {
pub fn state<T>(state_key: String, content: &T) -> Self
where
T: EventContent<EventType = StateEventType>,
{
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<T>(content: &T) -> Self
where
T: EventContent<EventType = MessageLikeEventType>,
{
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::<RawJsonValue>::default(),
unsigned: None,
state_key: None,
redacts: None,
timestamp: None,
}
}
}

View file

@ -21,7 +21,10 @@ use serde_json::{
value::{to_raw_value, RawValue as RawJsonValue, Value as JsonValue},
};
pub use self::{builder::PduBuilder, count::PduCount};
pub use self::{
builder::{Builder, Builder as PduBuilder},
count::PduCount,
};
use crate::{err, is_true, warn, Error, Result};
#[derive(Deserialize)]