matrix_sdk_test/sync_builder/bulk.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
use std::ops::Range;
use ruma::{
events::{room::member::MembershipState, AnySyncStateEvent},
serde::Raw,
};
use serde_json::{from_value as from_json_value, json};
/// Create `m.room.member` events in the given range.
///
/// The user IDs are generated as `@user_{idx}:{server}`, with `idx` being the
/// current value in `range`, so providing the same range in several method
/// calls will create events that replace the previous state.
///
/// The event IDs are generated as `$roommember_{batch}_{idx}` so it's important
/// to increment `batch` between method calls to avoid having two events with
/// the same event ID.
///
/// This method can be used as input for room builders with
/// `add_timeline_state_bulk()` or `add_state_bulk()`.
pub fn bulk_room_members<'a>(
batch: usize,
range: Range<usize>,
server: &'a str,
membership: &'a MembershipState,
) -> impl Iterator<Item = Raw<AnySyncStateEvent>> + 'a {
range.map(move |idx| {
let user_id = format!("@user_{idx}:{server}");
let event_id = format!("$roommember_{batch}_{idx}");
let ts = 151800000 + batch * 100 + idx;
from_json_value(json!({
"content": {
"membership": membership,
},
"event_id": event_id,
"origin_server_ts": ts,
"sender": user_id,
"state_key": user_id,
"type": "m.room.member",
}))
.unwrap()
})
}