2020-07-30 23:22:48 +00:00
|
|
|
use std::{convert::TryFrom, env, process::exit};
|
2020-05-28 14:32:28 +00:00
|
|
|
|
2021-04-05 17:49:55 +00:00
|
|
|
use matrix_sdk::{
|
2021-06-23 10:06:28 +00:00
|
|
|
ruma::{api::client::r0::profile, MxcUri, UserId},
|
2021-04-05 17:49:55 +00:00
|
|
|
Client, Result as MatrixResult,
|
|
|
|
};
|
2021-05-12 10:56:29 +00:00
|
|
|
use url::Url;
|
2020-05-28 14:32:28 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct UserProfile {
|
2021-04-05 17:49:55 +00:00
|
|
|
avatar_url: Option<MxcUri>,
|
2020-05-28 14:32:28 +00:00
|
|
|
displayname: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function calls the GET profile endpoint
|
|
|
|
/// Spec: https://matrix.org/docs/spec/client_server/r0.6.1#get-matrix-client-r0-profile-userid
|
|
|
|
/// Ruma: https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/r0/profile/get_profile/index.html
|
2020-09-12 01:19:22 +00:00
|
|
|
async fn get_profile(client: Client, mxid: &UserId) -> MatrixResult<UserProfile> {
|
2020-05-28 14:32:28 +00:00
|
|
|
// First construct the request you want to make
|
|
|
|
// See https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/index.html for all available Endpoints
|
2020-09-12 01:19:22 +00:00
|
|
|
let request = profile::get_profile::Request::new(mxid);
|
2020-05-28 14:32:28 +00:00
|
|
|
|
|
|
|
// Start the request using matrix_sdk::Client::send
|
2021-02-01 16:15:29 +00:00
|
|
|
let resp = client.send(request, None).await?;
|
2020-05-28 14:32:28 +00:00
|
|
|
|
|
|
|
// Use the response and construct a UserProfile struct.
|
|
|
|
// See https://docs.rs/ruma-client-api/0.9.0/ruma_client_api/r0/profile/get_profile/struct.Response.html
|
|
|
|
// for details on the Response for this Request
|
2021-05-12 15:00:47 +00:00
|
|
|
let user_profile = UserProfile { avatar_url: resp.avatar_url, displayname: resp.displayname };
|
2020-05-28 14:32:28 +00:00
|
|
|
Ok(user_profile)
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn login(
|
|
|
|
homeserver_url: String,
|
2020-08-15 01:09:13 +00:00
|
|
|
username: &str,
|
|
|
|
password: &str,
|
2020-05-28 14:32:28 +00:00
|
|
|
) -> Result<Client, matrix_sdk::Error> {
|
|
|
|
let homeserver_url = Url::parse(&homeserver_url).expect("Couldn't parse the homeserver URL");
|
2021-01-22 09:01:21 +00:00
|
|
|
let client = Client::new(homeserver_url).unwrap();
|
2020-05-28 14:32:28 +00:00
|
|
|
|
2021-05-12 15:00:47 +00:00
|
|
|
client.login(username, password, None, Some("rust-sdk")).await?;
|
2020-05-28 14:32:28 +00:00
|
|
|
|
|
|
|
Ok(client)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[tokio::main]
|
|
|
|
async fn main() -> Result<(), matrix_sdk::Error> {
|
|
|
|
tracing_subscriber::fmt::init();
|
|
|
|
|
|
|
|
let (homeserver_url, username, password) =
|
|
|
|
match (env::args().nth(1), env::args().nth(2), env::args().nth(3)) {
|
|
|
|
(Some(a), Some(b), Some(c)) => (a, b, c),
|
|
|
|
_ => {
|
|
|
|
eprintln!(
|
|
|
|
"Usage: {} <homeserver_url> <mxid> <password>",
|
|
|
|
env::args().next().unwrap()
|
|
|
|
);
|
|
|
|
exit(1)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-08-15 01:09:13 +00:00
|
|
|
let client = login(homeserver_url, &username, &password).await?;
|
2020-05-28 14:32:28 +00:00
|
|
|
|
2020-08-15 01:09:13 +00:00
|
|
|
let user_id = UserId::try_from(username).expect("Couldn't parse the MXID");
|
2020-09-12 01:19:22 +00:00
|
|
|
let profile = get_profile(client, &user_id).await?;
|
2020-05-28 14:32:28 +00:00
|
|
|
println!("{:#?}", profile);
|
|
|
|
Ok(())
|
|
|
|
}
|