Skip to content

Commit c9f810c

Browse files
committed
client-api: Add the unstable MSC4222 /sync state_after names.
Accept the org.matrix.msc4222.use_state_after query parameter and add an AfterUnstable state variant serializing under org.matrix.msc4222.state_after, for clients predating the Matrix 1.16 stabilization.
1 parent 236d1ed commit c9f810c

1 file changed

Lines changed: 58 additions & 2 deletions

File tree

  • crates/ruma-client-api/src/sync/sync_events

crates/ruma-client-api/src/sync/sync_events/v3.rs

Lines changed: 58 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,18 @@ pub struct Request {
8282
#[serde(default, skip_serializing_if = "ruma_common::serde::is_default")]
8383
#[ruma_api(query)]
8484
pub use_state_after: bool,
85+
86+
/// The unstable name for [`use_state_after`], used before MSC4222 was stabilized in
87+
/// Matrix 1.16.
88+
///
89+
/// [`use_state_after`]: Self::use_state_after
90+
#[serde(
91+
default,
92+
rename = "org.matrix.msc4222.use_state_after",
93+
skip_serializing_if = "ruma_common::serde::is_default"
94+
)]
95+
#[ruma_api(query)]
96+
pub use_state_after_unstable: bool,
8597
}
8698

8799
/// Response type for the `sync` endpoint.
@@ -435,6 +447,15 @@ pub enum State {
435447
/// To get this variant, `use_state_after` must be set to `true` in the [`Request`].
436448
#[serde(rename = "state_after")]
437449
After(StateEvents),
450+
451+
/// The same as [`After`](Self::After), serialized under the unstable name from MSC4222
452+
/// before its stabilization in Matrix 1.16.
453+
///
454+
/// Emitted to clients that opted in with [`use_state_after_unstable`].
455+
///
456+
/// [`use_state_after_unstable`]: Request::use_state_after_unstable
457+
#[serde(rename = "org.matrix.msc4222.state_after")]
458+
AfterUnstable(StateEvents),
438459
}
439460

440461
impl State {
@@ -446,8 +467,8 @@ impl State {
446467
/// Returns true if there are no state updates.
447468
pub fn is_empty(&self) -> bool {
448469
match self {
449-
Self::Before(state) => state.is_empty(),
450-
Self::After(state) => state.is_empty(),
470+
Self::Before(state) | Self::After(state) | Self::AfterUnstable(state) =>
471+
state.is_empty(),
451472
}
452473
}
453474
}
@@ -762,6 +783,7 @@ mod client_tests {
762783
set_presence: PresenceState::Offline,
763784
timeout: Some(Duration::from_millis(30000)),
764785
use_state_after: true,
786+
use_state_after_unstable: true,
765787
}
766788
.try_into_http_request(
767789
"https://homeserver.tld",
@@ -780,6 +802,7 @@ mod client_tests {
780802
assert!(query.contains("set_presence=offline"));
781803
assert!(query.contains("timeout=30000"));
782804
assert!(query.contains("use_state_after=true"));
805+
assert!(query.contains("org.matrix.msc4222.use_state_after=true"));
783806
}
784807

785808
#[test]
@@ -1304,4 +1327,37 @@ mod server_tests {
13041327
})
13051328
);
13061329
}
1330+
1331+
#[test]
1332+
fn serialize_response_state_after_unstable() {
1333+
let joined_room_id = owned_room_id!("!joined:localhost");
1334+
let event = sync_state_event();
1335+
1336+
let mut response = Response::new("aaa".to_owned());
1337+
1338+
let mut joined_room = JoinedRoom::new();
1339+
joined_room.state = State::AfterUnstable(vec![event.clone()].into());
1340+
response.rooms.join.insert(joined_room_id.clone(), joined_room);
1341+
1342+
let http_response = response.try_into_http_response::<Vec<u8>>().unwrap();
1343+
1344+
assert_eq!(
1345+
from_json_slice::<JsonValue>(http_response.body()).unwrap(),
1346+
json!({
1347+
"next_batch": "aaa",
1348+
"rooms": {
1349+
"join": {
1350+
joined_room_id: {
1351+
"account_data": { "events": [] },
1352+
"org.matrix.msc4222.state_after": {
1353+
"events": [
1354+
event,
1355+
],
1356+
},
1357+
},
1358+
},
1359+
},
1360+
})
1361+
);
1362+
}
13071363
}

0 commit comments

Comments
 (0)