matrix-sdk: Add a convenience method to get our own devices.

master
Damir Jelić 2020-09-05 18:04:15 +02:00
parent 217543ef38
commit 6c7dbb814b
2 changed files with 66 additions and 0 deletions

View File

@ -41,6 +41,7 @@ use matrix_sdk_base::{CryptoStoreError, OutgoingRequests, ToDeviceRequest};
use matrix_sdk_common::{
api::r0::{
account::register,
device::get_devices,
directory::{get_public_rooms, get_public_rooms_filtered},
membership::{
ban_user, forget_room,
@ -1111,6 +1112,35 @@ impl Client {
self.send(request).await
}
/// Get information of all our own devices.
///
/// # Examples
///
/// ```no_run
/// # use matrix_sdk::{Client, SyncSettings};
/// # use futures::executor::block_on;
/// # use url::Url;
/// # use std::convert::TryFrom;
/// # block_on(async {
/// # let homeserver = Url::parse("http://localhost:8080").unwrap();
/// # let mut client = Client::new(homeserver).unwrap();
/// let response = client.devices().await.expect("Can't get devices from server");
///
/// for device in response.devices {
/// println!(
/// "Device: {} {}",
/// device.device_id,
/// device.display_name.as_deref().unwrap_or("")
/// );
/// }
/// # });
/// ```
pub async fn devices(&self) -> Result<get_devices::Response> {
let request = get_devices::Request {};
self.send(request).await
}
/// Synchronize the client's state with the latest state on the server.
///
/// If a `StateStore` is provided and this is the initial sync state will
@ -1548,6 +1578,18 @@ mod test {
assert!(logged_in, "Client should be logged in");
}
#[tokio::test]
async fn devices() {
let client = logged_in_client().await;
let _m = mock("GET", "/_matrix/client/r0/devices")
.with_status(200)
.with_body(test_json::DEVICES.to_string())
.create();
assert!(client.devices().await.is_ok());
}
#[tokio::test]
async fn test_join_leave_room() {
let homeserver = Url::from_str(&mockito::server_url()).unwrap();

View File

@ -4,6 +4,9 @@
//! When running `cargo publish` no external folders are allowed so all the
//! test data needs to be contained within this crate.
use lazy_static::lazy_static;
use serde_json::{json, Value as JsonValue};
pub mod events;
pub mod sync;
@ -14,3 +17,24 @@ pub use events::{
ROOM_ID, ROOM_MESSAGES, TYPING,
};
pub use sync::{DEFAULT_SYNC_SUMMARY, INVITE_SYNC, LEAVE_SYNC, LEAVE_SYNC_EVENT, MORE_SYNC, SYNC};
lazy_static! {
pub static ref DEVICES: JsonValue = json!({
"devices": [
{
"device_id": "BNYQQWUMXO",
"display_name": "Client 1",
"last_seen_ip": "-",
"last_seen_ts": 1596117733037u64,
"user_id": "@example:localhost"
},
{
"device_id": "LEBKSEUSNR",
"display_name": "Client 2",
"last_seen_ip": "-",
"last_seen_ts": 1599057006985u64,
"user_id": "@example:localhost"
}
]
});
}