-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3771 from torrybr/feat/send-beacon
sdk: basic support for sending location beacons
- Loading branch information
Showing
5 changed files
with
215 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
use std::time::{Duration, UNIX_EPOCH}; | ||
|
||
use matrix_sdk::{config::SyncSettings, instant::SystemTime}; | ||
use matrix_sdk_test::{async_test, test_json, DEFAULT_TEST_ROOM_ID}; | ||
use ruma::event_id; | ||
use serde_json::json; | ||
use wiremock::{ | ||
matchers::{body_partial_json, header, method, path_regex}, | ||
Mock, ResponseTemplate, | ||
}; | ||
|
||
use crate::{logged_in_client_with_server, mock_encryption_state, mock_sync}; | ||
#[async_test] | ||
async fn test_send_location_beacon() { | ||
let (client, server) = logged_in_client_with_server().await; | ||
|
||
// Validate request body and response, partial body matching due to | ||
// auto-generated `org.matrix.msc3488.ts`. | ||
Mock::given(method("PUT")) | ||
.and(path_regex(r"^/_matrix/client/r0/rooms/.*/send/org.matrix.msc3672.beacon/.*")) | ||
.and(header("authorization", "Bearer 1234")) | ||
.and(body_partial_json(json!({ | ||
"m.relates_to": { | ||
"event_id": "$15139375514XsgmR:localhost", | ||
"rel_type": "m.reference" | ||
}, | ||
"org.matrix.msc3488.location": { | ||
"uri": "geo:48.8588448,2.2943506" | ||
} | ||
}))) | ||
.respond_with(ResponseTemplate::new(200).set_body_json(&*test_json::EVENT_ID)) | ||
.mount(&server) | ||
.await; | ||
|
||
let current_timestamp = | ||
SystemTime::now().duration_since(UNIX_EPOCH).expect("Time went backwards").as_millis() | ||
as u64; | ||
|
||
mock_sync( | ||
&server, | ||
json!({ | ||
"next_batch": "s526_47314_0_7_1_1_1_1_1", | ||
"rooms": { | ||
"join": { | ||
*DEFAULT_TEST_ROOM_ID: { | ||
"state": { | ||
"events": [ | ||
{ | ||
"content": { | ||
"description": "Live Share", | ||
"live": true, | ||
"org.matrix.msc3488.ts": current_timestamp, | ||
"timeout": 600_000, | ||
"org.matrix.msc3488.asset": { "type": "m.self" } | ||
}, | ||
"event_id": "$15139375514XsgmR:localhost", | ||
"origin_server_ts": 1_636_829_458, | ||
"sender": "@example:localhost", | ||
"state_key": "@example:localhost", | ||
"type": "org.matrix.msc3672.beacon_info", | ||
"unsigned": { | ||
"age": 7034220 | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
}), | ||
None, | ||
) | ||
.await; | ||
|
||
mock_encryption_state(&server, false).await; | ||
|
||
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); | ||
|
||
client.sync_once(sync_settings).await.unwrap(); | ||
|
||
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap(); | ||
|
||
let response = room.send_location_beacon("geo:48.8588448,2.2943506".to_owned()).await.unwrap(); | ||
|
||
assert_eq!(event_id!("$h29iv0s8:example.com"), response.event_id) | ||
} | ||
|
||
#[async_test] | ||
async fn test_send_location_beacon_fails_without_starting_live_share() { | ||
let (client, server) = logged_in_client_with_server().await; | ||
|
||
mock_sync(&server, &*test_json::SYNC, None).await; | ||
|
||
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); | ||
client.sync_once(sync_settings).await.unwrap(); | ||
|
||
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap(); | ||
|
||
let response = room.send_location_beacon("geo:48.8588448,2.2943506".to_owned()).await; | ||
|
||
assert!(response.is_err()); | ||
} | ||
|
||
#[async_test] | ||
async fn test_send_location_beacon_with_expired_live_share() { | ||
let (client, server) = logged_in_client_with_server().await; | ||
|
||
mock_sync( | ||
&server, | ||
json!({ | ||
"next_batch": "s526_47314_0_7_1_1_1_1_1", | ||
"rooms": { | ||
"join": { | ||
*DEFAULT_TEST_ROOM_ID: { | ||
"state": { | ||
"events": [ | ||
{ | ||
"content": { | ||
"description": "Live Share", | ||
"live": false, | ||
"org.matrix.msc3488.ts": 1_636_829_458, | ||
"timeout": 3000, | ||
"org.matrix.msc3488.asset": { "type": "m.self" } | ||
}, | ||
"event_id": "$15139375514XsgmR:localhost", | ||
"origin_server_ts": 1_636_829_458, | ||
"sender": "@example:localhost", | ||
"state_key": "@example:localhost", | ||
"type": "org.matrix.msc3672.beacon_info", | ||
"unsigned": { | ||
"age": 7034220 | ||
} | ||
}, | ||
] | ||
} | ||
} | ||
} | ||
} | ||
|
||
}), | ||
None, | ||
) | ||
.await; | ||
|
||
let sync_settings = SyncSettings::new().timeout(Duration::from_millis(3000)); | ||
|
||
client.sync_once(sync_settings).await.unwrap(); | ||
|
||
let room = client.get_room(&DEFAULT_TEST_ROOM_ID).unwrap(); | ||
|
||
let response = room.send_location_beacon("geo:48.8588448,2.2943506".to_owned()).await; | ||
|
||
assert!(response.is_err()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
mod attachment; | ||
mod beacon; | ||
mod common; | ||
mod joined; | ||
mod left; | ||
|