diff --git a/matrix_sdk/src/client.rs b/matrix_sdk/src/client.rs index 47227b47..d104f7ab 100644 --- a/matrix_sdk/src/client.rs +++ b/matrix_sdk/src/client.rs @@ -100,7 +100,7 @@ use ruma::{ api::{ client::{ r0::{ - account::register, + account::{register, whoami}, device::{delete_devices, get_devices}, directory::{get_public_rooms, get_public_rooms_filtered}, filter::{create_filter::Request as FilterUploadRequest, FilterDefinition}, @@ -2686,6 +2686,12 @@ impl Client { Ok(()) } + + /// Gets information about the owner of a given access token. + pub async fn whoami(&self) -> Result { + let request = whoami::Request::new(); + self.send(request, None).await + } } #[cfg(test)] @@ -3862,4 +3868,19 @@ mod test { .is_ok()); m.assert(); } + + #[tokio::test] + async fn whoami() { + let client = logged_in_client().await; + + let _m = mock("GET", "/_matrix/client/r0/account/whoami") + .with_status(200) + .with_body(test_json::WHOAMI.to_string()) + .match_header("authorization", "Bearer 1234") + .create(); + + let user_id = user_id!("@joe:example.org"); + + assert_eq!(client.whoami().await.unwrap().user_id, user_id); + } } diff --git a/matrix_sdk_test/src/test_json/mod.rs b/matrix_sdk_test/src/test_json/mod.rs index 64b628f6..c0ab33e3 100644 --- a/matrix_sdk_test/src/test_json/mod.rs +++ b/matrix_sdk_test/src/test_json/mod.rs @@ -68,3 +68,9 @@ lazy_static! { } }); } + +lazy_static! { + pub static ref WHOAMI: JsonValue = json!({ + "user_id": "@joe:example.org" + }); +}