mirror of
https://forgejo.ellis.link/continuwuation/continuwuity.git
synced 2025-10-18 19:31:59 +00:00
Toward abstracting Pdu into trait Event.
Co-authored-by: Jade Ellis <jade@ellis.link> Signed-off-by: Jason Volk <jason@zemos.net>
This commit is contained in:
parent
3d0360bcd6
commit
116f85360f
41 changed files with 842 additions and 886 deletions
|
@ -13,7 +13,6 @@ use ruma::{
|
|||
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, RoomVersionId, Signatures, UserId,
|
||||
events::{
|
||||
StateEventType, TimelineEventType,
|
||||
pdu::{EventHash, Pdu, RoomV3Pdu},
|
||||
room::{
|
||||
join_rules::{JoinRule, RoomJoinRulesEventContent},
|
||||
member::{MembershipState, RoomMemberEventContent},
|
||||
|
@ -26,8 +25,10 @@ use serde_json::{
|
|||
value::{RawValue as RawJsonValue, to_raw_value as to_raw_json_value},
|
||||
};
|
||||
|
||||
use self::event::PduEvent;
|
||||
use crate::state_res::{self as state_res, Error, Event, Result, StateMap};
|
||||
use crate::{
|
||||
matrix::{Event, Pdu, pdu::EventHash},
|
||||
state_res::{self as state_res, Error, Result, StateMap},
|
||||
};
|
||||
|
||||
static SERVER_TIMESTAMP: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
|
@ -60,7 +61,7 @@ fn resolution_shallow_auth_chain(c: &mut test::Bencher) {
|
|||
c.iter(|| async {
|
||||
let ev_map = store.0.clone();
|
||||
let state_sets = [&state_at_bob, &state_at_charlie];
|
||||
let fetch = |id: OwnedEventId| ready(ev_map.get(&id).clone());
|
||||
let fetch = |id: OwnedEventId| ready(ev_map.get(&id).map(ToOwned::to_owned));
|
||||
let exists = |id: OwnedEventId| ready(ev_map.get(&id).is_some());
|
||||
let auth_chain_sets: Vec<HashSet<_>> = state_sets
|
||||
.iter()
|
||||
|
@ -142,7 +143,7 @@ fn resolve_deeper_event_set(c: &mut test::Bencher) {
|
|||
})
|
||||
.collect();
|
||||
|
||||
let fetch = |id: OwnedEventId| ready(inner.get(&id).clone());
|
||||
let fetch = |id: OwnedEventId| ready(inner.get(&id).map(ToOwned::to_owned));
|
||||
let exists = |id: OwnedEventId| ready(inner.get(&id).is_some());
|
||||
let _ = match state_res::resolve(
|
||||
&RoomVersionId::V6,
|
||||
|
@ -246,7 +247,7 @@ impl<E: Event + Clone> TestStore<E> {
|
|||
}
|
||||
}
|
||||
|
||||
impl TestStore<PduEvent> {
|
||||
impl TestStore<Pdu> {
|
||||
#[allow(clippy::type_complexity)]
|
||||
fn set_up(
|
||||
&mut self,
|
||||
|
@ -380,7 +381,7 @@ fn to_pdu_event<S>(
|
|||
content: Box<RawJsonValue>,
|
||||
auth_events: &[S],
|
||||
prev_events: &[S],
|
||||
) -> PduEvent
|
||||
) -> Pdu
|
||||
where
|
||||
S: AsRef<str>,
|
||||
{
|
||||
|
@ -403,30 +404,28 @@ where
|
|||
.map(event_id)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let state_key = state_key.map(ToOwned::to_owned);
|
||||
PduEvent {
|
||||
Pdu {
|
||||
event_id: id.try_into().unwrap(),
|
||||
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
|
||||
room_id: room_id().to_owned(),
|
||||
sender: sender.to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
|
||||
state_key,
|
||||
kind: ev_type,
|
||||
content,
|
||||
redacts: None,
|
||||
unsigned: btreemap! {},
|
||||
auth_events,
|
||||
prev_events,
|
||||
depth: uint!(0),
|
||||
hashes: EventHash::new(String::new()),
|
||||
signatures: Signatures::new(),
|
||||
}),
|
||||
room_id: room_id().to_owned(),
|
||||
sender: sender.to_owned(),
|
||||
origin_server_ts: ts.try_into().unwrap(),
|
||||
state_key: state_key.map(Into::into),
|
||||
kind: ev_type,
|
||||
content,
|
||||
origin: None,
|
||||
redacts: None,
|
||||
unsigned: None,
|
||||
auth_events,
|
||||
prev_events,
|
||||
depth: uint!(0),
|
||||
hashes: EventHash { sha256: String::new() },
|
||||
signatures: None,
|
||||
}
|
||||
}
|
||||
|
||||
// all graphs start with these input events
|
||||
#[allow(non_snake_case)]
|
||||
fn INITIAL_EVENTS() -> HashMap<OwnedEventId, PduEvent> {
|
||||
fn INITIAL_EVENTS() -> HashMap<OwnedEventId, Pdu> {
|
||||
vec![
|
||||
to_pdu_event::<&EventId>(
|
||||
"CREATE",
|
||||
|
@ -508,7 +507,7 @@ fn INITIAL_EVENTS() -> HashMap<OwnedEventId, PduEvent> {
|
|||
|
||||
// all graphs start with these input events
|
||||
#[allow(non_snake_case)]
|
||||
fn BAN_STATE_SET() -> HashMap<OwnedEventId, PduEvent> {
|
||||
fn BAN_STATE_SET() -> HashMap<OwnedEventId, Pdu> {
|
||||
vec![
|
||||
to_pdu_event(
|
||||
"PA",
|
||||
|
@ -551,119 +550,3 @@ fn BAN_STATE_SET() -> HashMap<OwnedEventId, PduEvent> {
|
|||
.map(|ev| (ev.event_id().to_owned(), ev))
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Convenience trait for adding event type plus state key to state maps.
|
||||
trait EventTypeExt {
|
||||
fn with_state_key(self, state_key: impl Into<String>) -> (StateEventType, String);
|
||||
}
|
||||
|
||||
impl EventTypeExt for &TimelineEventType {
|
||||
fn with_state_key(self, state_key: impl Into<String>) -> (StateEventType, String) {
|
||||
(self.to_string().into(), state_key.into())
|
||||
}
|
||||
}
|
||||
|
||||
mod event {
|
||||
use ruma::{
|
||||
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, UserId,
|
||||
events::{TimelineEventType, pdu::Pdu},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::value::RawValue as RawJsonValue;
|
||||
|
||||
use super::Event;
|
||||
|
||||
impl Event for PduEvent {
|
||||
fn event_id(&self) -> &EventId { &self.event_id }
|
||||
|
||||
fn room_id(&self) -> &RoomId {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => &ev.room_id,
|
||||
| Pdu::RoomV3Pdu(ev) => &ev.room_id,
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn sender(&self) -> &UserId {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => &ev.sender,
|
||||
| Pdu::RoomV3Pdu(ev) => &ev.sender,
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn event_type(&self) -> &TimelineEventType {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => &ev.kind,
|
||||
| Pdu::RoomV3Pdu(ev) => &ev.kind,
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn content(&self) -> &RawJsonValue {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => &ev.content,
|
||||
| Pdu::RoomV3Pdu(ev) => &ev.content,
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => ev.origin_server_ts,
|
||||
| Pdu::RoomV3Pdu(ev) => ev.origin_server_ts,
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn state_key(&self) -> Option<&str> {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => ev.state_key.as_deref(),
|
||||
| Pdu::RoomV3Pdu(ev) => ev.state_key.as_deref(),
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn prev_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + Send + '_> {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) =>
|
||||
Box::new(ev.prev_events.iter().map(|(id, _)| id.as_ref())),
|
||||
| Pdu::RoomV3Pdu(ev) => Box::new(ev.prev_events.iter().map(AsRef::as_ref)),
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + Send + '_> {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) =>
|
||||
Box::new(ev.auth_events.iter().map(|(id, _)| id.as_ref())),
|
||||
| Pdu::RoomV3Pdu(ev) => Box::new(ev.auth_events.iter().map(AsRef::as_ref)),
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn redacts(&self) -> Option<&EventId> {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => ev.redacts.as_deref(),
|
||||
| Pdu::RoomV3Pdu(ev) => ev.redacts.as_deref(),
|
||||
#[cfg(not(feature = "unstable-exhaustive-types"))]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
pub(crate) struct PduEvent {
|
||||
pub(crate) event_id: OwnedEventId,
|
||||
#[serde(flatten)]
|
||||
pub(crate) rest: Pdu,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -136,17 +136,17 @@ pub fn auth_types_for_event(
|
|||
event_id = incoming_event.event_id().as_str(),
|
||||
)
|
||||
)]
|
||||
pub async fn auth_check<F, Fut, Fetched, Incoming>(
|
||||
pub async fn auth_check<E, F, Fut>(
|
||||
room_version: &RoomVersion,
|
||||
incoming_event: &Incoming,
|
||||
current_third_party_invite: Option<&Incoming>,
|
||||
incoming_event: &E,
|
||||
current_third_party_invite: Option<&E>,
|
||||
fetch_state: F,
|
||||
) -> Result<bool, Error>
|
||||
where
|
||||
F: Fn(&StateEventType, &str) -> Fut + Send,
|
||||
Fut: Future<Output = Option<Fetched>> + Send,
|
||||
Fetched: Event + Send,
|
||||
Incoming: Event + Send + Sync,
|
||||
Fut: Future<Output = Option<E>> + Send,
|
||||
E: Event + Send + Sync,
|
||||
for<'a> &'a E: Event + Send,
|
||||
{
|
||||
debug!(
|
||||
event_id = format!("{}", incoming_event.event_id()),
|
||||
|
@ -514,20 +514,24 @@ where
|
|||
/// event and the current State.
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
#[allow(clippy::cognitive_complexity)]
|
||||
fn valid_membership_change(
|
||||
fn valid_membership_change<E>(
|
||||
room_version: &RoomVersion,
|
||||
target_user: &UserId,
|
||||
target_user_membership_event: Option<&impl Event>,
|
||||
target_user_membership_event: Option<&E>,
|
||||
sender: &UserId,
|
||||
sender_membership_event: Option<&impl Event>,
|
||||
current_event: impl Event,
|
||||
current_third_party_invite: Option<&impl Event>,
|
||||
power_levels_event: Option<&impl Event>,
|
||||
join_rules_event: Option<&impl Event>,
|
||||
sender_membership_event: Option<&E>,
|
||||
current_event: &E,
|
||||
current_third_party_invite: Option<&E>,
|
||||
power_levels_event: Option<&E>,
|
||||
join_rules_event: Option<&E>,
|
||||
user_for_join_auth: Option<&UserId>,
|
||||
user_for_join_auth_membership: &MembershipState,
|
||||
create_room: &impl Event,
|
||||
) -> Result<bool> {
|
||||
create_room: &E,
|
||||
) -> Result<bool>
|
||||
where
|
||||
E: Event + Send + Sync,
|
||||
for<'a> &'a E: Event + Send,
|
||||
{
|
||||
#[derive(Deserialize)]
|
||||
struct GetThirdPartyInvite {
|
||||
third_party_invite: Option<Raw<ThirdPartyInvite>>,
|
||||
|
@ -820,7 +824,7 @@ fn valid_membership_change(
|
|||
///
|
||||
/// Does the event have the correct userId as its state_key if it's not the ""
|
||||
/// state_key.
|
||||
fn can_send_event(event: impl Event, ple: Option<impl Event>, user_level: Int) -> bool {
|
||||
fn can_send_event(event: &impl Event, ple: Option<&impl Event>, user_level: Int) -> bool {
|
||||
let event_type_power_level = get_send_level(event.event_type(), event.state_key(), ple);
|
||||
|
||||
debug!(
|
||||
|
@ -846,8 +850,8 @@ fn can_send_event(event: impl Event, ple: Option<impl Event>, user_level: Int) -
|
|||
/// Confirm that the event sender has the required power levels.
|
||||
fn check_power_levels(
|
||||
room_version: &RoomVersion,
|
||||
power_event: impl Event,
|
||||
previous_power_event: Option<impl Event>,
|
||||
power_event: &impl Event,
|
||||
previous_power_event: Option<&impl Event>,
|
||||
user_level: Int,
|
||||
) -> Option<bool> {
|
||||
match power_event.state_key() {
|
||||
|
@ -1010,7 +1014,7 @@ fn get_deserialize_levels(
|
|||
/// given event.
|
||||
fn check_redaction(
|
||||
_room_version: &RoomVersion,
|
||||
redaction_event: impl Event,
|
||||
redaction_event: &impl Event,
|
||||
user_level: Int,
|
||||
redact_level: Int,
|
||||
) -> Result<bool> {
|
||||
|
@ -1039,7 +1043,7 @@ fn check_redaction(
|
|||
fn get_send_level(
|
||||
e_type: &TimelineEventType,
|
||||
state_key: Option<&str>,
|
||||
power_lvl: Option<impl Event>,
|
||||
power_lvl: Option<&impl Event>,
|
||||
) -> Int {
|
||||
power_lvl
|
||||
.and_then(|ple| {
|
||||
|
@ -1062,7 +1066,7 @@ fn verify_third_party_invite(
|
|||
target_user: Option<&UserId>,
|
||||
sender: &UserId,
|
||||
tp_id: &ThirdPartyInvite,
|
||||
current_third_party_invite: Option<impl Event>,
|
||||
current_third_party_invite: Option<&impl Event>,
|
||||
) -> bool {
|
||||
// 1. Check for user being banned happens before this is called
|
||||
// checking for mxid and token keys is done by ruma when deserializing
|
||||
|
@ -1128,12 +1132,15 @@ mod tests {
|
|||
};
|
||||
use serde_json::value::to_raw_value as to_raw_json_value;
|
||||
|
||||
use crate::state_res::{
|
||||
Event, EventTypeExt, RoomVersion, StateMap,
|
||||
event_auth::valid_membership_change,
|
||||
test_utils::{
|
||||
INITIAL_EVENTS, INITIAL_EVENTS_CREATE_ROOM, PduEvent, alice, charlie, ella, event_id,
|
||||
member_content_ban, member_content_join, room_id, to_pdu_event,
|
||||
use crate::{
|
||||
matrix::{Event, EventTypeExt, Pdu as PduEvent},
|
||||
state_res::{
|
||||
RoomVersion, StateMap,
|
||||
event_auth::valid_membership_change,
|
||||
test_utils::{
|
||||
INITIAL_EVENTS, INITIAL_EVENTS_CREATE_ROOM, alice, charlie, ella, event_id,
|
||||
member_content_ban, member_content_join, room_id, to_pdu_event,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ pub use self::{
|
|||
};
|
||||
use crate::{
|
||||
debug, debug_error,
|
||||
matrix::{event::Event, pdu::StateKey},
|
||||
matrix::{Event, StateKey},
|
||||
trace,
|
||||
utils::stream::{BroadbandExt, IterStream, ReadyExt, TryBroadbandExt, WidebandExt},
|
||||
warn,
|
||||
|
@ -90,7 +90,7 @@ where
|
|||
SetIter: Iterator<Item = &'a StateMap<OwnedEventId>> + Clone + Send,
|
||||
Hasher: BuildHasher + Send + Sync,
|
||||
E: Event + Clone + Send + Sync,
|
||||
for<'b> &'b E: Send,
|
||||
for<'b> &'b E: Event + Send,
|
||||
{
|
||||
debug!("State resolution starting");
|
||||
|
||||
|
@ -522,6 +522,7 @@ where
|
|||
Fut: Future<Output = Option<E>> + Send,
|
||||
S: Stream<Item = &'a EventId> + Send + 'a,
|
||||
E: Event + Clone + Send + Sync,
|
||||
for<'b> &'b E: Event + Send,
|
||||
{
|
||||
debug!("starting iterative auth check");
|
||||
|
||||
|
@ -552,7 +553,7 @@ where
|
|||
|
||||
let auth_events = &auth_events;
|
||||
let mut resolved_state = unconflicted_state;
|
||||
for event in &events_to_check {
|
||||
for event in events_to_check {
|
||||
let state_key = event
|
||||
.state_key()
|
||||
.ok_or_else(|| Error::InvalidPdu("State event had no state key".to_owned()))?;
|
||||
|
@ -607,11 +608,15 @@ where
|
|||
});
|
||||
|
||||
let fetch_state = |ty: &StateEventType, key: &str| {
|
||||
future::ready(auth_state.get(&ty.with_state_key(key)))
|
||||
future::ready(
|
||||
auth_state
|
||||
.get(&ty.with_state_key(key))
|
||||
.map(ToOwned::to_owned),
|
||||
)
|
||||
};
|
||||
|
||||
let auth_result =
|
||||
auth_check(room_version, &event, current_third_party.as_ref(), fetch_state).await;
|
||||
auth_check(room_version, &event, current_third_party, fetch_state).await;
|
||||
|
||||
match auth_result {
|
||||
| Ok(true) => {
|
||||
|
@ -794,11 +799,11 @@ where
|
|||
}
|
||||
}
|
||||
|
||||
fn is_type_and_key(ev: impl Event, ev_type: &TimelineEventType, state_key: &str) -> bool {
|
||||
fn is_type_and_key(ev: &impl Event, ev_type: &TimelineEventType, state_key: &str) -> bool {
|
||||
ev.event_type() == ev_type && ev.state_key() == Some(state_key)
|
||||
}
|
||||
|
||||
fn is_power_event(event: impl Event) -> bool {
|
||||
fn is_power_event(event: &impl Event) -> bool {
|
||||
match event.event_type() {
|
||||
| TimelineEventType::RoomPowerLevels
|
||||
| TimelineEventType::RoomJoinRules
|
||||
|
@ -859,15 +864,19 @@ mod tests {
|
|||
use serde_json::{json, value::to_raw_value as to_raw_json_value};
|
||||
|
||||
use super::{
|
||||
Event, EventTypeExt, StateMap, is_power_event,
|
||||
StateMap, is_power_event,
|
||||
room_version::RoomVersion,
|
||||
test_utils::{
|
||||
INITIAL_EVENTS, PduEvent, TestStore, alice, bob, charlie, do_check, ella, event_id,
|
||||
INITIAL_EVENTS, TestStore, alice, bob, charlie, do_check, ella, event_id,
|
||||
member_content_ban, member_content_join, room_id, to_init_pdu_event, to_pdu_event,
|
||||
zara,
|
||||
},
|
||||
};
|
||||
use crate::{debug, utils::stream::IterStream};
|
||||
use crate::{
|
||||
debug,
|
||||
matrix::{Event, EventTypeExt, Pdu as PduEvent},
|
||||
utils::stream::IterStream,
|
||||
};
|
||||
|
||||
async fn test_event_sort() {
|
||||
use futures::future::ready;
|
||||
|
|
|
@ -10,7 +10,6 @@ use ruma::{
|
|||
UserId, event_id,
|
||||
events::{
|
||||
TimelineEventType,
|
||||
pdu::{EventHash, Pdu, RoomV3Pdu},
|
||||
room::{
|
||||
join_rules::{JoinRule, RoomJoinRulesEventContent},
|
||||
member::{MembershipState, RoomMemberEventContent},
|
||||
|
@ -23,17 +22,16 @@ use serde_json::{
|
|||
value::{RawValue as RawJsonValue, to_raw_value as to_raw_json_value},
|
||||
};
|
||||
|
||||
pub(crate) use self::event::PduEvent;
|
||||
use super::auth_types_for_event;
|
||||
use crate::{
|
||||
Result, info,
|
||||
matrix::{Event, EventTypeExt, StateMap},
|
||||
matrix::{Event, EventTypeExt, Pdu, StateMap, pdu::EventHash},
|
||||
};
|
||||
|
||||
static SERVER_TIMESTAMP: AtomicU64 = AtomicU64::new(0);
|
||||
|
||||
pub(crate) async fn do_check(
|
||||
events: &[PduEvent],
|
||||
events: &[Pdu],
|
||||
edges: Vec<Vec<OwnedEventId>>,
|
||||
expected_state_ids: Vec<OwnedEventId>,
|
||||
) {
|
||||
|
@ -81,8 +79,8 @@ pub(crate) async fn do_check(
|
|||
}
|
||||
}
|
||||
|
||||
// event_id -> PduEvent
|
||||
let mut event_map: HashMap<OwnedEventId, PduEvent> = HashMap::new();
|
||||
// event_id -> Pdu
|
||||
let mut event_map: HashMap<OwnedEventId, Pdu> = HashMap::new();
|
||||
// event_id -> StateMap<OwnedEventId>
|
||||
let mut state_at_event: HashMap<OwnedEventId, StateMap<OwnedEventId>> = HashMap::new();
|
||||
|
||||
|
@ -265,7 +263,7 @@ impl<E: Event + Clone> TestStore<E> {
|
|||
|
||||
// A StateStore implementation for testing
|
||||
#[allow(clippy::type_complexity)]
|
||||
impl TestStore<PduEvent> {
|
||||
impl TestStore<Pdu> {
|
||||
pub(crate) fn set_up(
|
||||
&mut self,
|
||||
) -> (StateMap<OwnedEventId>, StateMap<OwnedEventId>, StateMap<OwnedEventId>) {
|
||||
|
@ -390,7 +388,7 @@ pub(crate) fn to_init_pdu_event(
|
|||
ev_type: TimelineEventType,
|
||||
state_key: Option<&str>,
|
||||
content: Box<RawJsonValue>,
|
||||
) -> PduEvent {
|
||||
) -> Pdu {
|
||||
let ts = SERVER_TIMESTAMP.fetch_add(1, SeqCst);
|
||||
let id = if id.contains('$') {
|
||||
id.to_owned()
|
||||
|
@ -398,24 +396,22 @@ pub(crate) fn to_init_pdu_event(
|
|||
format!("${id}:foo")
|
||||
};
|
||||
|
||||
let state_key = state_key.map(ToOwned::to_owned);
|
||||
PduEvent {
|
||||
Pdu {
|
||||
event_id: id.try_into().unwrap(),
|
||||
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
|
||||
room_id: room_id().to_owned(),
|
||||
sender: sender.to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
|
||||
state_key,
|
||||
kind: ev_type,
|
||||
content,
|
||||
redacts: None,
|
||||
unsigned: BTreeMap::new(),
|
||||
auth_events: vec![],
|
||||
prev_events: vec![],
|
||||
depth: uint!(0),
|
||||
hashes: EventHash::new("".to_owned()),
|
||||
signatures: ServerSignatures::default(),
|
||||
}),
|
||||
room_id: room_id().to_owned(),
|
||||
sender: sender.to_owned(),
|
||||
origin_server_ts: ts.try_into().unwrap(),
|
||||
state_key: state_key.map(Into::into),
|
||||
kind: ev_type,
|
||||
content,
|
||||
origin: None,
|
||||
redacts: None,
|
||||
unsigned: None,
|
||||
auth_events: vec![],
|
||||
prev_events: vec![],
|
||||
depth: uint!(0),
|
||||
hashes: EventHash { sha256: "".to_owned() },
|
||||
signatures: None,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -427,7 +423,7 @@ pub(crate) fn to_pdu_event<S>(
|
|||
content: Box<RawJsonValue>,
|
||||
auth_events: &[S],
|
||||
prev_events: &[S],
|
||||
) -> PduEvent
|
||||
) -> Pdu
|
||||
where
|
||||
S: AsRef<str>,
|
||||
{
|
||||
|
@ -448,30 +444,28 @@ where
|
|||
.map(event_id)
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let state_key = state_key.map(ToOwned::to_owned);
|
||||
PduEvent {
|
||||
Pdu {
|
||||
event_id: id.try_into().unwrap(),
|
||||
rest: Pdu::RoomV3Pdu(RoomV3Pdu {
|
||||
room_id: room_id().to_owned(),
|
||||
sender: sender.to_owned(),
|
||||
origin_server_ts: MilliSecondsSinceUnixEpoch(ts.try_into().unwrap()),
|
||||
state_key,
|
||||
kind: ev_type,
|
||||
content,
|
||||
redacts: None,
|
||||
unsigned: BTreeMap::new(),
|
||||
auth_events,
|
||||
prev_events,
|
||||
depth: uint!(0),
|
||||
hashes: EventHash::new("".to_owned()),
|
||||
signatures: ServerSignatures::default(),
|
||||
}),
|
||||
room_id: room_id().to_owned(),
|
||||
sender: sender.to_owned(),
|
||||
origin_server_ts: ts.try_into().unwrap(),
|
||||
state_key: state_key.map(Into::into),
|
||||
kind: ev_type,
|
||||
content,
|
||||
origin: None,
|
||||
redacts: None,
|
||||
unsigned: None,
|
||||
auth_events,
|
||||
prev_events,
|
||||
depth: uint!(0),
|
||||
hashes: EventHash { sha256: "".to_owned() },
|
||||
signatures: None,
|
||||
}
|
||||
}
|
||||
|
||||
// all graphs start with these input events
|
||||
#[allow(non_snake_case)]
|
||||
pub(crate) fn INITIAL_EVENTS() -> HashMap<OwnedEventId, PduEvent> {
|
||||
pub(crate) fn INITIAL_EVENTS() -> HashMap<OwnedEventId, Pdu> {
|
||||
vec![
|
||||
to_pdu_event::<&EventId>(
|
||||
"CREATE",
|
||||
|
@ -553,7 +547,7 @@ pub(crate) fn INITIAL_EVENTS() -> HashMap<OwnedEventId, PduEvent> {
|
|||
|
||||
// all graphs start with these input events
|
||||
#[allow(non_snake_case)]
|
||||
pub(crate) fn INITIAL_EVENTS_CREATE_ROOM() -> HashMap<OwnedEventId, PduEvent> {
|
||||
pub(crate) fn INITIAL_EVENTS_CREATE_ROOM() -> HashMap<OwnedEventId, Pdu> {
|
||||
vec![to_pdu_event::<&EventId>(
|
||||
"CREATE",
|
||||
alice(),
|
||||
|
@ -575,111 +569,3 @@ pub(crate) fn INITIAL_EDGES() -> Vec<OwnedEventId> {
|
|||
.map(event_id)
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
pub(crate) mod event {
|
||||
use ruma::{
|
||||
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, RoomId, UserId,
|
||||
events::{TimelineEventType, pdu::Pdu},
|
||||
};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::value::RawValue as RawJsonValue;
|
||||
|
||||
use crate::Event;
|
||||
|
||||
impl Event for PduEvent {
|
||||
fn event_id(&self) -> &EventId { &self.event_id }
|
||||
|
||||
fn room_id(&self) -> &RoomId {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => &ev.room_id,
|
||||
| Pdu::RoomV3Pdu(ev) => &ev.room_id,
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn sender(&self) -> &UserId {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => &ev.sender,
|
||||
| Pdu::RoomV3Pdu(ev) => &ev.sender,
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn event_type(&self) -> &TimelineEventType {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => &ev.kind,
|
||||
| Pdu::RoomV3Pdu(ev) => &ev.kind,
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn content(&self) -> &RawJsonValue {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => &ev.content,
|
||||
| Pdu::RoomV3Pdu(ev) => &ev.content,
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => ev.origin_server_ts,
|
||||
| Pdu::RoomV3Pdu(ev) => ev.origin_server_ts,
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn state_key(&self) -> Option<&str> {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => ev.state_key.as_deref(),
|
||||
| Pdu::RoomV3Pdu(ev) => ev.state_key.as_deref(),
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(refining_impl_trait)]
|
||||
fn prev_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + Send + '_> {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) =>
|
||||
Box::new(ev.prev_events.iter().map(|(id, _)| id.as_ref())),
|
||||
| Pdu::RoomV3Pdu(ev) => Box::new(ev.prev_events.iter().map(AsRef::as_ref)),
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(refining_impl_trait)]
|
||||
fn auth_events(&self) -> Box<dyn DoubleEndedIterator<Item = &EventId> + Send + '_> {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) =>
|
||||
Box::new(ev.auth_events.iter().map(|(id, _)| id.as_ref())),
|
||||
| Pdu::RoomV3Pdu(ev) => Box::new(ev.auth_events.iter().map(AsRef::as_ref)),
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
|
||||
fn redacts(&self) -> Option<&EventId> {
|
||||
match &self.rest {
|
||||
| Pdu::RoomV1Pdu(ev) => ev.redacts.as_deref(),
|
||||
| Pdu::RoomV3Pdu(ev) => ev.redacts.as_deref(),
|
||||
#[allow(unreachable_patterns)]
|
||||
| _ => unreachable!("new PDU version"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)]
|
||||
#[allow(clippy::exhaustive_structs)]
|
||||
pub(crate) struct PduEvent {
|
||||
pub(crate) event_id: OwnedEventId,
|
||||
#[serde(flatten)]
|
||||
pub(crate) rest: Pdu,
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue