From 76fa15f248f892a82c8ae56de27dca7f957b4167 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Fri, 3 Sep 2021 20:51:20 +0200 Subject: [PATCH] state-res: Rename Event::kind to event_type --- .../ruma-state-res/benches/state_res_bench.rs | 16 ++++---- crates/ruma-state-res/src/event_auth.rs | 40 +++++++++++-------- crates/ruma-state-res/src/lib.rs | 12 +++--- crates/ruma-state-res/src/state_event.rs | 2 +- crates/ruma-state-res/tests/event_auth.rs | 4 +- crates/ruma-state-res/tests/event_sorting.rs | 2 +- .../ruma-state-res/tests/res_with_auth_ids.rs | 4 +- crates/ruma-state-res/tests/state_res.rs | 6 +-- crates/ruma-state-res/tests/utils.rs | 14 +++---- 9 files changed, 54 insertions(+), 46 deletions(-) diff --git a/crates/ruma-state-res/benches/state_res_bench.rs b/crates/ruma-state-res/benches/state_res_bench.rs index 7c8cbcf1..6b9dde19 100644 --- a/crates/ruma-state-res/benches/state_res_bench.rs +++ b/crates/ruma-state-res/benches/state_res_bench.rs @@ -101,7 +101,7 @@ fn resolve_deeper_event_set(c: &mut Criterion) { inner.get(&event_id("PA")).unwrap(), ] .iter() - .map(|ev| ((ev.kind(), ev.state_key().unwrap()), ev.event_id().clone())) + .map(|ev| ((ev.event_type(), ev.state_key().unwrap()), ev.event_id().clone())) .collect::>(); let state_set_b = [ @@ -114,7 +114,7 @@ fn resolve_deeper_event_set(c: &mut Criterion) { inner.get(&event_id("PA")).unwrap(), ] .iter() - .map(|ev| ((ev.kind(), ev.state_key().unwrap()), ev.event_id().clone())) + .map(|ev| ((ev.event_type(), ev.state_key().unwrap()), ev.event_id().clone())) .collect::>(); b.iter(|| { @@ -289,17 +289,17 @@ impl TestStore { let state_at_bob = [&create_event, &alice_mem, &join_rules, &bob_mem] .iter() - .map(|e| ((e.kind(), e.state_key().unwrap()), e.event_id().clone())) + .map(|e| ((e.event_type(), e.state_key().unwrap()), e.event_id().clone())) .collect::>(); let state_at_charlie = [&create_event, &alice_mem, &join_rules, &charlie_mem] .iter() - .map(|e| ((e.kind(), e.state_key().unwrap()), e.event_id().clone())) + .map(|e| ((e.event_type(), e.state_key().unwrap()), e.event_id().clone())) .collect::>(); let expected = [&create_event, &alice_mem, &join_rules, &bob_mem, &charlie_mem] .iter() - .map(|e| ((e.kind(), e.state_key().unwrap()), e.event_id().clone())) + .map(|e| ((e.event_type(), e.state_key().unwrap()), e.event_id().clone())) .collect::>(); (state_at_bob, state_at_charlie, expected) @@ -539,8 +539,8 @@ pub mod event { self.sender() } - fn kind(&self) -> EventType { - self.kind() + fn event_type(&self) -> EventType { + self.event_type() } fn content(&self) -> serde_json::Value { @@ -682,7 +682,7 @@ pub mod event { _ => unreachable!("new PDU version"), } } - pub fn kind(&self) -> EventType { + pub fn event_type(&self) -> EventType { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.kind.clone(), Pdu::RoomV3Pdu(ev) => ev.kind.clone(), diff --git a/crates/ruma-state-res/src/event_auth.rs b/crates/ruma-state-res/src/event_auth.rs index 0bd7fdae..c9e507ed 100644 --- a/crates/ruma-state-res/src/event_auth.rs +++ b/crates/ruma-state-res/src/event_auth.rs @@ -95,7 +95,11 @@ where E: Event, F: Fn(&EventType, &str) -> Option>, { - info!("auth_check beginning for {} ({})", incoming_event.event_id(), incoming_event.kind()); + info!( + "auth_check beginning for {} ({})", + incoming_event.event_id(), + incoming_event.event_type() + ); // [synapse] check that all the events are in the same room as `incoming_event` @@ -108,7 +112,7 @@ where // Implementation of https://matrix.org/docs/spec/rooms/v1#authorization-rules // // 1. If type is m.room.create: - if incoming_event.kind() == EventType::RoomCreate { + if incoming_event.event_type() == EventType::RoomCreate { info!("start m.room.create check"); // If it has any previous events, reject @@ -180,7 +184,9 @@ where // [synapse] checks for federation here // 4. If type is m.room.aliases - if incoming_event.kind() == EventType::RoomAliases && room_version.special_case_aliases_auth { + if incoming_event.event_type() == EventType::RoomAliases + && room_version.special_case_aliases_auth + { info!("starting m.room.aliases check"); // If sender's domain doesn't matches state_key, reject @@ -193,7 +199,7 @@ where return Ok(true); } - if incoming_event.kind() == EventType::RoomMember { + if incoming_event.event_type() == EventType::RoomMember { info!("starting m.room.member check"); let state_key = match incoming_event.state_key() { None => { @@ -243,7 +249,7 @@ where // Allow if and only if sender's current power level is greater than // or equal to the invite level - if incoming_event.kind() == EventType::RoomThirdPartyInvite + if incoming_event.event_type() == EventType::RoomThirdPartyInvite && !can_send_invite(incoming_event, &fetch_state)? { warn!("sender's cannot send invites in this room"); @@ -257,7 +263,7 @@ where return Ok(false); } - if incoming_event.kind() == EventType::RoomPowerLevels { + if incoming_event.event_type() == EventType::RoomPowerLevels { info!("starting m.room.power_levels check"); if let Some(required_pwr_lvl) = @@ -282,7 +288,7 @@ where // power levels. if room_version.extra_redaction_checks - && incoming_event.kind() == EventType::RoomRedaction + && incoming_event.event_type() == EventType::RoomRedaction && !check_redaction(room_version, incoming_event, &fetch_state)? { return Ok(false); @@ -373,7 +379,7 @@ where } if let Some(prev) = prev_event { - if prev.kind() == EventType::RoomCreate && prev.prev_events().is_empty() { + if prev.event_type() == EventType::RoomCreate && prev.prev_events().is_empty() { return Ok(true); } } @@ -526,7 +532,8 @@ where { let ple = fetch_state(&EventType::RoomPowerLevels, ""); - let event_type_power_level = get_send_level(&event.kind(), event.state_key(), ple.as_ref()); + let event_type_power_level = + get_send_level(&event.event_type(), event.state_key(), ple.as_ref()); let user_level = get_user_power_level(event.sender(), fetch_state); debug!("{} ev_type {} usr {}", event.event_id(), event_type_power_level, user_level); @@ -555,13 +562,14 @@ where F: Fn(&EventType, &str) -> Option>, { let power_event_state_key = power_event.state_key().expect("power events have state keys"); - let current_state = - if let Some(current_state) = fetch_state(&power_event.kind(), &power_event_state_key) { - current_state - } else { - // If there is no previous m.room.power_levels event in the room, allow - return Some(true); - }; + let current_state = if let Some(current_state) = + fetch_state(&power_event.event_type(), &power_event_state_key) + { + current_state + } else { + // If there is no previous m.room.power_levels event in the room, allow + return Some(true); + }; // If users key in content is not a dictionary with keys that are valid user IDs // with values that are integers (or a string that is an integer), reject. diff --git a/crates/ruma-state-res/src/lib.rs b/crates/ruma-state-res/src/lib.rs index 3aff2ae5..5812d771 100644 --- a/crates/ruma-state-res/src/lib.rs +++ b/crates/ruma-state-res/src/lib.rs @@ -411,7 +411,7 @@ where // related to soft-failing auth_events.insert( ( - ev.kind(), + ev.event_type(), ev.state_key().ok_or_else(|| { Error::InvalidPdu("State event had no state key".to_owned()) })?, @@ -424,7 +424,7 @@ where } for key in auth_types_for_event( - &event.kind(), + &event.event_type(), event.sender(), Some(state_key.clone()), event.content(), @@ -445,7 +445,7 @@ where // The key for this is (eventType + a state_key of the signed token not sender) so // search for it let current_third_party = auth_events.iter().find_map(|(_, pdu)| { - (pdu.kind() == EventType::RoomThirdPartyInvite).then(|| { + (pdu.event_type() == EventType::RoomThirdPartyInvite).then(|| { // TODO no clone, auth_events is borrowed while moved pdu.clone() }) @@ -459,7 +459,7 @@ where |ty, key| auth_events.get(&(ty.clone(), key.to_owned())).cloned(), )? { // add event to resolved state map - resolved_state.insert((event.kind(), state_key), event_id.clone()); + resolved_state.insert((event.event_type(), state_key), event_id.clone()); } else { // synapse passes here on AuthError. We do not add this event to resolved_state. warn!("event {} failed the authentication check", event_id); @@ -623,11 +623,11 @@ where } pub fn is_type_and_key(ev: &Arc, ev_type: EventType, state_key: &str) -> bool { - ev.kind() == ev_type && ev.state_key().as_deref() == Some(state_key) + ev.event_type() == ev_type && ev.state_key().as_deref() == Some(state_key) } pub fn is_power_event(event: &Arc) -> bool { - match event.kind() { + match event.event_type() { EventType::RoomPowerLevels | EventType::RoomJoinRules | EventType::RoomCreate => { event.state_key() == Some("".into()) } diff --git a/crates/ruma-state-res/src/state_event.rs b/crates/ruma-state-res/src/state_event.rs index 85da0855..40c3b9ec 100644 --- a/crates/ruma-state-res/src/state_event.rs +++ b/crates/ruma-state-res/src/state_event.rs @@ -21,7 +21,7 @@ pub trait Event { fn origin_server_ts(&self) -> MilliSecondsSinceUnixEpoch; /// The kind of event. - fn kind(&self) -> EventType; + fn event_type(&self) -> EventType; /// The `UserId` of this PDU. fn content(&self) -> serde_json::Value; diff --git a/crates/ruma-state-res/tests/event_auth.rs b/crates/ruma-state-res/tests/event_auth.rs index e613f230..ca11f70f 100644 --- a/crates/ruma-state-res/tests/event_auth.rs +++ b/crates/ruma-state-res/tests/event_auth.rs @@ -14,7 +14,7 @@ fn test_ban_pass() { let auth_events = events .values() - .map(|ev| ((ev.kind(), ev.state_key()), Arc::clone(ev))) + .map(|ev| ((ev.event_type(), ev.state_key()), Arc::clone(ev))) .collect::>(); let requester = to_pdu_event( @@ -46,7 +46,7 @@ fn test_ban_fail() { let auth_events = events .values() - .map(|ev| ((ev.kind(), ev.state_key()), Arc::clone(ev))) + .map(|ev| ((ev.event_type(), ev.state_key()), Arc::clone(ev))) .collect::>(); let requester = to_pdu_event( diff --git a/crates/ruma-state-res/tests/event_sorting.rs b/crates/ruma-state-res/tests/event_sorting.rs index 21c09aad..da122789 100644 --- a/crates/ruma-state-res/tests/event_sorting.rs +++ b/crates/ruma-state-res/tests/event_sorting.rs @@ -15,7 +15,7 @@ fn test_event_sort() { let event_map = events .values() - .map(|ev| ((ev.kind(), ev.state_key()), ev.clone())) + .map(|ev| ((ev.event_type(), ev.state_key()), ev.clone())) .collect::>(); let auth_chain = HashSet::new(); diff --git a/crates/ruma-state-res/tests/res_with_auth_ids.rs b/crates/ruma-state-res/tests/res_with_auth_ids.rs index 14c93859..b58a48e3 100644 --- a/crates/ruma-state-res/tests/res_with_auth_ids.rs +++ b/crates/ruma-state-res/tests/res_with_auth_ids.rs @@ -47,7 +47,7 @@ fn ban_with_auth_chains2() { inner.get(&event_id("PA")).unwrap(), ] .iter() - .map(|ev| ((ev.kind(), ev.state_key()), ev.event_id().clone())) + .map(|ev| ((ev.event_type(), ev.state_key()), ev.event_id().clone())) .collect::>(); let state_set_b = [ @@ -60,7 +60,7 @@ fn ban_with_auth_chains2() { inner.get(&event_id("PA")).unwrap(), ] .iter() - .map(|ev| ((ev.kind(), ev.state_key()), ev.event_id().clone())) + .map(|ev| ((ev.event_type(), ev.state_key()), ev.event_id().clone())) .collect::>(); let ev_map: EventMap> = store.0.clone(); diff --git a/crates/ruma-state-res/tests/state_res.rs b/crates/ruma-state-res/tests/state_res.rs index 18fd99d3..7a1d4be6 100644 --- a/crates/ruma-state-res/tests/state_res.rs +++ b/crates/ruma-state-res/tests/state_res.rs @@ -364,17 +364,17 @@ impl TestStore { let state_at_bob = [&create_event, &alice_mem, &join_rules, &bob_mem] .iter() - .map(|e| ((e.kind(), e.state_key()), e.event_id().clone())) + .map(|e| ((e.event_type(), e.state_key()), e.event_id().clone())) .collect::>(); let state_at_charlie = [&create_event, &alice_mem, &join_rules, &charlie_mem] .iter() - .map(|e| ((e.kind(), e.state_key()), e.event_id().clone())) + .map(|e| ((e.event_type(), e.state_key()), e.event_id().clone())) .collect::>(); let expected = [&create_event, &alice_mem, &join_rules, &bob_mem, &charlie_mem] .iter() - .map(|e| ((e.kind(), e.state_key()), e.event_id().clone())) + .map(|e| ((e.event_type(), e.state_key()), e.event_id().clone())) .collect::>(); (state_at_bob, state_at_charlie, expected) diff --git a/crates/ruma-state-res/tests/utils.rs b/crates/ruma-state-res/tests/utils.rs index 390c0da0..b662034c 100644 --- a/crates/ruma-state-res/tests/utils.rs +++ b/crates/ruma-state-res/tests/utils.rs @@ -133,12 +133,12 @@ pub fn do_check( let mut state_after = state_before.clone(); - let ty = fake_event.kind(); + let ty = fake_event.event_type(); let key = fake_event.state_key(); state_after.insert((ty, key), event_id.clone()); let auth_types = auth_types_for_event( - &fake_event.kind(), + &fake_event.event_type(), fake_event.sender(), Some(fake_event.state_key()), fake_event.content(), @@ -158,7 +158,7 @@ pub fn do_check( let event = to_pdu_event( e.event_id().as_str(), e.sender().clone(), - e.kind().clone(), + e.event_type().clone(), Some(&e.state_key()), e.content(), &auth_events, @@ -183,7 +183,7 @@ pub fn do_check( ) }); - let key = (ev.kind(), ev.state_key()); + let key = (ev.event_type(), ev.state_key()); expected_state.insert(key, node); } @@ -512,8 +512,8 @@ pub mod event { fn sender(&self) -> &UserId { self.sender() } - fn kind(&self) -> EventType { - self.kind() + fn event_type(&self) -> EventType { + self.event_type() } fn content(&self) -> serde_json::Value { @@ -648,7 +648,7 @@ pub mod event { _ => unreachable!("new PDU version"), } } - pub fn kind(&self) -> EventType { + pub fn event_type(&self) -> EventType { match &self.rest { Pdu::RoomV1Pdu(ev) => ev.kind.clone(), Pdu::RoomV3Pdu(ev) => ev.kind.clone(),