|
1 | 1 | use std::time::{Duration, UNIX_EPOCH};
|
2 | 2 |
|
3 |
| -use matrix_sdk::config::SyncSettings; |
4 |
| -use matrix_sdk_test::{async_test, mocks::mock_encryption_state, test_json, DEFAULT_TEST_ROOM_ID}; |
5 |
| -use ruma::{event_id, time::SystemTime}; |
| 3 | +use futures_util::{pin_mut, StreamExt as _}; |
| 4 | +use js_int::uint; |
| 5 | +use matrix_sdk::{config::SyncSettings, live_location_share::LiveLocationShare}; |
| 6 | +use matrix_sdk_test::{ |
| 7 | + async_test, mocks::mock_encryption_state, sync_timeline_event, test_json, JoinedRoomBuilder, |
| 8 | + SyncResponseBuilder, DEFAULT_TEST_ROOM_ID, |
| 9 | +}; |
| 10 | +use ruma::{event_id, events::location::AssetType, time::SystemTime, MilliSecondsSinceUnixEpoch}; |
6 | 11 | use serde_json::json;
|
7 | 12 | use wiremock::{
|
8 | 13 | matchers::{body_partial_json, header, method, path_regex},
|
@@ -153,3 +158,219 @@ async fn test_send_location_beacon_with_expired_live_share() {
|
153 | 158 |
|
154 | 159 | assert!(response.is_err());
|
155 | 160 | }
|
| 161 | + |
| 162 | +#[async_test] |
| 163 | +async fn test_most_recent_event_in_stream() { |
| 164 | + let (client, server) = logged_in_client_with_server().await; |
| 165 | + |
| 166 | + let mut sync_builder = SyncResponseBuilder::new(); |
| 167 | + |
| 168 | + let current_time = MilliSecondsSinceUnixEpoch::now(); |
| 169 | + let millis_time = current_time |
| 170 | + .to_system_time() |
| 171 | + .unwrap() |
| 172 | + .duration_since(UNIX_EPOCH) |
| 173 | + .expect("Time went backwards") |
| 174 | + .as_millis() as u64; |
| 175 | + |
| 176 | + mock_sync( |
| 177 | + &server, |
| 178 | + json!({ |
| 179 | + "next_batch": "s526_47314_0_7_1_1_1_1_1", |
| 180 | + "rooms": { |
| 181 | + "join": { |
| 182 | + *DEFAULT_TEST_ROOM_ID: { |
| 183 | + "state": { |
| 184 | + "events": [ |
| 185 | + { |
| 186 | + "content": { |
| 187 | + "description": "Live Share", |
| 188 | + "live": true, |
| 189 | + "org.matrix.msc3488.ts": millis_time, |
| 190 | + "timeout": 3000, |
| 191 | + "org.matrix.msc3488.asset": { "type": "m.self" } |
| 192 | + }, |
| 193 | + "event_id": "$15139375514XsgmR:localhost", |
| 194 | + "origin_server_ts": millis_time, |
| 195 | + "sender": "@example:localhost", |
| 196 | + "state_key": "@example:localhost", |
| 197 | + "type": "org.matrix.msc3672.beacon_info", |
| 198 | + "unsigned": { |
| 199 | + "age": 7034220 |
| 200 | + } |
| 201 | + }, |
| 202 | + ] |
| 203 | + } |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + }), |
| 209 | + None, |
| 210 | + ) |
| 211 | + .await; |
| 212 | + let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); |
| 213 | + let _response = client.sync_once(sync_settings.clone()).await.unwrap(); |
| 214 | + server.reset().await; |
| 215 | + |
| 216 | + let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap(); |
| 217 | + |
| 218 | + let observable_live_location_shares = room.observe_live_location_shares(); |
| 219 | + let stream = observable_live_location_shares.subscribe(); |
| 220 | + pin_mut!(stream); |
| 221 | + |
| 222 | + let mut timeline_events = Vec::new(); |
| 223 | + |
| 224 | + for nth in 0..25 { |
| 225 | + timeline_events.push(sync_timeline_event!({ |
| 226 | + "content": { |
| 227 | + "m.relates_to": { |
| 228 | + "event_id": "$15139375514XsgmR:localhost", |
| 229 | + "rel_type": "m.reference" |
| 230 | + }, |
| 231 | + "org.matrix.msc3488.location": { |
| 232 | + "uri": format!("geo:{nth}.9575274619722,12.494122581370175;u={nth}") |
| 233 | + }, |
| 234 | + "org.matrix.msc3488.ts": 1_636_829_458 |
| 235 | + }, |
| 236 | + "event_id": format!("$event_for_stream_{nth}"), |
| 237 | + "origin_server_ts": 1_636_829_458, |
| 238 | + "sender": "@example:localhost", |
| 239 | + "type": "org.matrix.msc3672.beacon", |
| 240 | + "unsigned": { |
| 241 | + "age": 598971 |
| 242 | + } |
| 243 | + })); |
| 244 | + } |
| 245 | + |
| 246 | + sync_builder.add_joined_room( |
| 247 | + JoinedRoomBuilder::new(*DEFAULT_TEST_ROOM_ID).add_timeline_bulk(timeline_events), |
| 248 | + ); |
| 249 | + |
| 250 | + mock_sync(&server, sync_builder.build_json_sync_response(), None).await; |
| 251 | + let _response = client.sync_once(sync_settings.clone()).await.unwrap(); |
| 252 | + server.reset().await; |
| 253 | + |
| 254 | + // Stream should only process the latest beacon event for the user, ignoring any |
| 255 | + // previous events. |
| 256 | + let LiveLocationShare { user_id, last_location, beacon_info } = |
| 257 | + stream.next().await.expect("Another live location was expected"); |
| 258 | + |
| 259 | + assert_eq!(user_id.to_string(), "@example:localhost"); |
| 260 | + |
| 261 | + assert_eq!(last_location.location.uri, "geo:24.9575274619722,12.494122581370175;u=24"); |
| 262 | + |
| 263 | + assert!(last_location.location.description.is_none()); |
| 264 | + assert!(last_location.location.zoom_level.is_none()); |
| 265 | + assert_eq!(last_location.ts, MilliSecondsSinceUnixEpoch(uint!(1_636_829_458))); |
| 266 | + |
| 267 | + let beacon_info = beacon_info.expect("Live location share is missing the beacon_info"); |
| 268 | + |
| 269 | + assert!(beacon_info.live); |
| 270 | + assert!(beacon_info.is_live()); |
| 271 | + assert_eq!(beacon_info.description, Some("Live Share".to_owned())); |
| 272 | + assert_eq!(beacon_info.timeout, Duration::from_millis(3000)); |
| 273 | + assert_eq!(beacon_info.ts, current_time); |
| 274 | + assert_eq!(beacon_info.asset.type_, AssetType::Self_); |
| 275 | +} |
| 276 | + |
| 277 | +#[async_test] |
| 278 | +async fn test_observe_single_live_location_share() { |
| 279 | + let (client, server) = logged_in_client_with_server().await; |
| 280 | + |
| 281 | + let current_time = MilliSecondsSinceUnixEpoch::now(); |
| 282 | + let millis_time = current_time |
| 283 | + .to_system_time() |
| 284 | + .unwrap() |
| 285 | + .duration_since(UNIX_EPOCH) |
| 286 | + .expect("Time went backwards") |
| 287 | + .as_millis() as u64; |
| 288 | + |
| 289 | + mock_sync( |
| 290 | + &server, |
| 291 | + json!({ |
| 292 | + "next_batch": "s526_47314_0_7_1_1_1_1_1", |
| 293 | + "rooms": { |
| 294 | + "join": { |
| 295 | + *DEFAULT_TEST_ROOM_ID: { |
| 296 | + "state": { |
| 297 | + "events": [ |
| 298 | + { |
| 299 | + "content": { |
| 300 | + "description": "Test Live Share", |
| 301 | + "live": true, |
| 302 | + "org.matrix.msc3488.ts": millis_time, |
| 303 | + "timeout": 3000, |
| 304 | + "org.matrix.msc3488.asset": { "type": "m.self" } |
| 305 | + }, |
| 306 | + "event_id": "$test_beacon_info", |
| 307 | + "origin_server_ts": millis_time, |
| 308 | + "sender": "@example:localhost", |
| 309 | + "state_key": "@example:localhost", |
| 310 | + "type": "org.matrix.msc3672.beacon_info", |
| 311 | + } |
| 312 | + ] |
| 313 | + } |
| 314 | + } |
| 315 | + } |
| 316 | + } |
| 317 | + }), |
| 318 | + None, |
| 319 | + ) |
| 320 | + .await; |
| 321 | + |
| 322 | + let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); |
| 323 | + let _response = client.sync_once(sync_settings.clone()).await.unwrap(); |
| 324 | + server.reset().await; |
| 325 | + |
| 326 | + let room = client.get_room(*DEFAULT_TEST_ROOM_ID).unwrap(); |
| 327 | + let observable_live_location_shares = room.observe_live_location_shares(); |
| 328 | + let stream = observable_live_location_shares.subscribe(); |
| 329 | + pin_mut!(stream); |
| 330 | + |
| 331 | + let timeline_event = sync_timeline_event!({ |
| 332 | + "content": { |
| 333 | + "m.relates_to": { |
| 334 | + "event_id": "$test_beacon_info", |
| 335 | + "rel_type": "m.reference" |
| 336 | + }, |
| 337 | + "org.matrix.msc3488.location": { |
| 338 | + "uri": "geo:10.000000,20.000000;u=5" |
| 339 | + }, |
| 340 | + "org.matrix.msc3488.ts": 1_636_829_458 |
| 341 | + }, |
| 342 | + "event_id": "$location_event", |
| 343 | + "origin_server_ts": millis_time, |
| 344 | + "sender": "@example:localhost", |
| 345 | + "type": "org.matrix.msc3672.beacon", |
| 346 | + }); |
| 347 | + |
| 348 | + mock_sync( |
| 349 | + &server, |
| 350 | + SyncResponseBuilder::new() |
| 351 | + .add_joined_room( |
| 352 | + JoinedRoomBuilder::new(*DEFAULT_TEST_ROOM_ID).add_timeline_event(timeline_event), |
| 353 | + ) |
| 354 | + .build_json_sync_response(), |
| 355 | + None, |
| 356 | + ) |
| 357 | + .await; |
| 358 | + |
| 359 | + let _response = client.sync_once(sync_settings.clone()).await.unwrap(); |
| 360 | + server.reset().await; |
| 361 | + |
| 362 | + let LiveLocationShare { user_id, last_location, beacon_info } = |
| 363 | + stream.next().await.expect("Another live location was expected"); |
| 364 | + |
| 365 | + assert_eq!(user_id.to_string(), "@example:localhost"); |
| 366 | + assert_eq!(last_location.location.uri, "geo:10.000000,20.000000;u=5"); |
| 367 | + assert_eq!(last_location.ts, current_time); |
| 368 | + |
| 369 | + let beacon_info = beacon_info.expect("Live location share is missing the beacon_info"); |
| 370 | + |
| 371 | + assert!(beacon_info.live); |
| 372 | + assert!(beacon_info.is_live()); |
| 373 | + assert_eq!(beacon_info.description, Some("Test Live Share".to_owned())); |
| 374 | + assert_eq!(beacon_info.timeout, Duration::from_millis(3000)); |
| 375 | + assert_eq!(beacon_info.ts, current_time); |
| 376 | +} |
0 commit comments