retrix/src/matrix.rs

25 lines
627 B
Rust
Raw Normal View History

2020-11-24 09:12:24 +01:00
use matrix_sdk::{reqwest::Url, Client, Session, SyncSettings};
2020-11-23 17:18:05 +01:00
2020-11-24 09:12:24 +01:00
pub type Error = Box<dyn std::error::Error>;
pub async fn login(
2020-11-23 17:18:05 +01:00
username: &str,
password: &str,
server: &str,
2020-11-24 09:12:24 +01:00
) -> Result<(Client, Session), Error> {
2020-11-23 17:18:05 +01:00
let url = Url::parse(server)?;
let client = Client::new(url)?;
2020-11-24 09:12:24 +01:00
let response = client
2020-11-23 17:18:05 +01:00
.login(username, password, None, Some("retrix"))
.await?;
2020-11-24 09:12:24 +01:00
let session = Session {
access_token: response.access_token,
user_id: response.user_id,
device_id: response.device_id,
};
2020-11-23 17:18:05 +01:00
client.sync(SyncSettings::new()).await;
2020-11-24 09:12:24 +01:00
Ok((client, session))
2020-11-23 17:18:05 +01:00
}