Expand description

Various small Utils

Session

The easiest way to use the Session struct is to use it like this:

use matrix_sdk::Session as SDKSession;

if let Some(session) = Session::load(config.session_path.parse().unwrap()) {
    info!("Starting relogin");

    let session = SDKSession {
        access_token: session.access_token,
        device_id: session.device_id.into(),
        user_id: matrix_sdk::identifiers::UserId::try_from(session.user_id.as_str()).unwrap(),
    };

    if let Err(e) = client.restore_login(session).await {
        error!("{}", e);
    };
    info!("Finished relogin");
} else {
    info!("Starting login");
    let login_response = client
        .login(
            &config.mxid,
            &config.password,
            None,
            Some(&"timetracking-bot".to_string()),
        )
        .await;
    match login_response {
        Ok(login_response) => {
           info!("Session: {:#?}", login_response);
           let session = Session {
                homeserver: client.homeserver().to_string(),
                user_id: login_response.user_id.to_string(),
                access_token: login_response.access_token,
                device_id: login_response.device_id.into(),
            };
            session.save(config.session_path.parse().unwrap())?;
        }
        Err(e) => error!("Error while login: {}", e),
    }
    info!("Finished login");
}

This first checks if there is a session already existing and uses it to relogin using the known session data.

If not it creates and saves the Session struct. Allowing for a relogin on the next start.

Structs

Informations needed to keep track about a session