Compare commits

..
Author SHA1 Message Date
renovate 8144c948cb chore(Nix): Updated flake hashes 2026-11-28 17:44:02 +00:00
Renovate Bot a1c0045367 chore(deps): update rust to v1.100.1 2026-11-28 17:44:02 +00:00
4 changed files with 22 additions and 150 deletions

No files matched your search

-1
View File
@@ -1 +0,0 @@
Fix minor Matrix state resolution bugs where some events were incorrectly prioritised. Contributed by @Jade
+1 -1
View File
@@ -12,7 +12,7 @@
target:
target.fromToolchainName {
name = (lib.importTOML "${inputs.self}/rust-toolchain.toml").toolchain.channel;
sha256 = "sha256-MFHoWVcPZKjFrLnQnodCXbUsfZcofGfo9tV9fTTvklg=";
sha256 = "sha256-W3A3rtdDaQW5yOvFr5JVTfngRUCKKUqPCPIW/3aH0nM=";
};
in
{
+1 -1
View File
@@ -10,7 +10,7 @@
[toolchain]
profile = "minimal"
channel = "1.100.0"
channel = "1.100.1"
components = [
# For rust-analyzer
"rust-src",
+20 -147
View File
@@ -20,13 +20,10 @@ use std::{
use futures::{Future, FutureExt, Stream, StreamExt, TryFutureExt, TryStreamExt, future};
use ruma::{
EventId, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId,
EventId, Int, MilliSecondsSinceUnixEpoch, OwnedEventId, OwnedUserId,
events::{
StateEventType, TimelineEventType,
room::{
member::{MembershipState, RoomMemberEventContent},
power_levels::UserPowerLevel,
},
room::member::{MembershipState, RoomMemberEventContent},
},
int,
room_version_rules::{RoomIdFormatVersion, RoomVersionRules, StateResolutionVersion},
@@ -414,7 +411,7 @@ where
.inspect(|(event_id, pl)| {
debug!(
event_id = event_id.as_str(),
power_level = ?pl,
power_level = i64::from(*pl),
"found the power level of an event's sender",
);
})
@@ -442,29 +439,27 @@ where
/// `key_fn` is used as to obtain the power level and age of an event for
/// breaking ties (together with the event ID).
#[tracing::instrument(level = "debug", skip_all)]
pub async fn lexicographical_topological_sort<Id, P, F, Fut, Hasher, S>(
pub async fn lexicographical_topological_sort<Id, F, Fut, Hasher, S>(
graph: &HashMap<Id, HashSet<Id, Hasher>, S>,
key_fn: &F,
) -> Result<Vec<Id>>
where
F: Fn(Id) -> Fut + Sync,
Fut: Future<Output = Result<(P, MilliSecondsSinceUnixEpoch)>> + Send,
Fut: Future<Output = Result<(Int, MilliSecondsSinceUnixEpoch)>> + Send,
Id: Borrow<EventId> + Clone + Eq + Hash + Ord + Send + Sync,
P: Ord + Send,
Hasher: BuildHasher + Default + Clone + Send + Sync,
S: BuildHasher + Clone + Send + Sync,
{
#[derive(PartialEq, Eq)]
struct TieBreaker<'a, Id, P> {
power_level: P,
struct TieBreaker<'a, Id> {
power_level: Int,
origin_server_ts: MilliSecondsSinceUnixEpoch,
event_id: &'a Id,
}
impl<Id, P> Ord for TieBreaker<'_, Id, P>
impl<Id> Ord for TieBreaker<'_, Id>
where
Id: Ord,
P: Ord,
{
fn cmp(&self, other: &Self) -> Ordering {
// NOTE: the power level comparison is "backwards" intentionally.
@@ -482,10 +477,9 @@ where
}
}
impl<Id, P> PartialOrd for TieBreaker<'_, Id, P>
impl<Id> PartialOrd for TieBreaker<'_, Id>
where
Id: Ord,
P: Ord,
{
fn partial_cmp(&self, other: &Self) -> Option<Ordering> { Some(self.cmp(other)) }
}
@@ -575,7 +569,7 @@ async fn get_power_level_for_sender<E, F, Fut>(
event_id: &EventId,
room_version: &RoomVersionRules,
fetch_event: &F,
) -> serde_json::Result<UserPowerLevel>
) -> serde_json::Result<Int>
where
F: Fn(OwnedEventId) -> Fut + Sync,
Fut: Future<Output = Option<E>> + Send,
@@ -584,7 +578,7 @@ where
debug!("fetch event ({event_id}) senders power level");
let Some(event) = fetch_event(event_id.to_owned()).await else {
return Ok(UserPowerLevel::Int(int!(0)));
return Ok(int!(0));
};
let auth_events: Vec<E> = event
@@ -605,7 +599,7 @@ where
.authorization
.explicitly_privilege_room_creators
{
return Ok(UserPowerLevel::Infinite);
return Ok(Int::MAX);
}
let pl = auth_events
@@ -615,17 +609,16 @@ where
let content: PowerLevelsContentFields = match pl {
// With no m.room.power_levels in scope the create event's defaults
// apply, under which the room creator holds 100 and everyone else 0.
| None =>
return Ok(UserPowerLevel::Int(if sender_is_creator { int!(100) } else { int!(0) })),
| None => return Ok(if sender_is_creator { int!(100) } else { int!(0) }),
| Some(ev) => from_json_str(ev.content().get())?,
};
if let Some(&user_level) = content.get_user_power(event.sender()) {
debug!("found {} at power_level {user_level}", event.sender());
return Ok(UserPowerLevel::Int(user_level));
return Ok(user_level);
}
Ok(UserPowerLevel::Int(content.users_default))
Ok(content.users_default)
}
#[derive(Deserialize)]
@@ -973,14 +966,11 @@ where
}
}
// Reserve depth 0 for events with no mainline power-levels ancestor (spec
// position ∞) so they always sort strictly before the oldest mainline
// power-levels event (depth 1).
let mainline_map: HashMap<_, _> = mainline
.iter()
.rev()
.enumerate()
.map(|(idx, eid)| ((*eid).clone(), idx.saturating_add(1)))
.map(|(idx, eid)| ((*eid).clone(), idx))
.collect();
let order_map: HashMap<_, _> = to_sort
@@ -1145,10 +1135,7 @@ mod tests {
MilliSecondsSinceUnixEpoch, OwnedEventId, RoomVersionId,
events::{
StateEventType, TimelineEventType,
room::{
join_rules::{JoinRule, RoomJoinRulesEventContent},
power_levels::UserPowerLevel,
},
room::join_rules::{JoinRule, RoomJoinRulesEventContent},
},
int,
room_version_rules::RoomVersionRules,
@@ -1399,47 +1386,6 @@ mod tests {
);
}
#[tokio::test]
async fn mainline_sort_orders_no_ancestor_before_oldest_power_level() {
let mut events = INITIAL_EVENTS();
let mut with_pl = to_pdu_event(
"WITH_PL",
alice(),
TimelineEventType::RoomTopic,
Some(""),
to_raw_json_value(&json!({ "topic": "with pl" })).unwrap(),
&["CREATE", "IPOWER", "IMA"],
&["IMA"],
);
with_pl.origin_server_ts = uint!(50);
let mut no_pl = to_pdu_event(
"NO_PL",
alice(),
TimelineEventType::RoomTopic,
Some(""),
to_raw_json_value(&json!({ "topic": "no pl" })).unwrap(),
&["CREATE", "IMA"],
&["IMA"],
);
no_pl.origin_server_ts = uint!(100);
let to_sort = [with_pl.event_id.clone(), no_pl.event_id.clone()];
for event in [with_pl, no_pl] {
events.insert(event.event_id.clone(), event);
}
let fetcher = |id: OwnedEventId| {
let events = &events;
async move { events.get(&id).cloned() }
};
let sorted = super::mainline_sort(&to_sort, Some(event_id("IPOWER")), &fetcher)
.await
.unwrap();
assert_eq!(sorted, vec![event_id("NO_PL"), event_id("WITH_PL")]);
}
/// Events at the start of a room have no `m.room.power_levels` in their
/// auth events, so the sender's power for sort ordering comes from the
/// create event instead.
@@ -1480,11 +1426,7 @@ mod tests {
.await
.unwrap();
assert_eq!(
power_level,
UserPowerLevel::Int(int!(100)),
"the creator should sort with their default power"
);
assert_eq!(power_level, int!(100), "the creator should sort with their default power");
}
#[tokio::test]
@@ -1534,83 +1476,14 @@ mod tests {
.await
.unwrap();
assert_eq!(
alice_power,
UserPowerLevel::Int(int!(100)),
"the content creator should sort with creator power"
);
assert_eq!(alice_power, int!(100), "the content creator should sort with creator power");
let bob_power =
super::get_power_level_for_sender(&event_id("IMB"), &RoomVersionRules::V6, &fetcher)
.await
.unwrap();
assert_eq!(
bob_power,
UserPowerLevel::Int(int!(0)),
"the create event's sender is not the creator"
);
}
#[tokio::test]
async fn v12_creator_outranks_max_int_power_level() {
use futures::future::ready;
use ruma::Int;
let mut events = INITIAL_EVENTS();
let max_pl = to_pdu_event(
"MAX_PL",
alice(),
TimelineEventType::RoomPowerLevels,
Some(""),
to_raw_json_value(&json!({
"users": {
bob(): Int::MAX,
},
}))
.unwrap(),
&["CREATE", "IMA"],
&["IMA"],
);
let mut bob_event = to_pdu_event(
"BOB_EV",
bob(),
TimelineEventType::RoomJoinRules,
Some(""),
to_raw_json_value(&json!({ "join_rule": "invite" })).unwrap(),
&["CREATE", "MAX_PL", "IMB"],
&["MAX_PL"],
);
bob_event.origin_server_ts = uint!(50);
let mut alice_event = to_pdu_event(
"ALICE_EV",
alice(),
TimelineEventType::RoomJoinRules,
Some(""),
to_raw_json_value(&json!({ "join_rule": "public" })).unwrap(),
&["CREATE", "MAX_PL", "IMA"],
&["MAX_PL"],
);
alice_event.origin_server_ts = uint!(100);
let conflicted = hashset![bob_event.event_id.clone(), alice_event.event_id.clone()];
let to_sort = vec![bob_event.event_id.clone(), alice_event.event_id.clone()];
for event in [max_pl, bob_event, alice_event] {
events.insert(event.event_id.clone(), event);
}
let fetcher = |id: OwnedEventId| ready(events.get(&id).cloned());
let sorted = super::reverse_topological_power_sort(
to_sort,
&conflicted,
&RoomVersionRules::V12,
&fetcher,
)
.await
.unwrap();
assert_eq!(sorted, vec![event_id("ALICE_EV"), event_id("BOB_EV")]);
assert_eq!(bob_power, int!(0), "the create event's sender is not the creator");
}
async fn test_event_sort() {