daydream

A small matrix web client written in rust
git clone git://archive.git.mtrnord.blog/daydream-mx/daydream.git
Log | Files | Refs | README | LICENSE

login.rs (2830B)


      1 use crate::errors::{Field, MatrixError};
      2 use log::*;
      3 use matrix_sdk::{Client, ClientConfig, Session};
      4 use serde::{Deserialize, Serialize};
      5 use std::convert::TryFrom;
      6 use url::Url;
      7 use wasm_bindgen_futures::spawn_local;
      8 
      9 #[derive(Serialize, Deserialize, Default, Clone, Debug)]
     10 pub struct SessionStore {
     11     pub(crate) access_token: String,
     12     pub(crate) user_id: String,
     13     pub(crate) device_id: String,
     14     pub(crate) homeserver_url: String,
     15 }
     16 
     17 pub fn login(
     18     session: Option<&SessionStore>,
     19     homeserver: Option<&String>,
     20 ) -> Result<Client, MatrixError> {
     21     info!("preparing client");
     22     match session {
     23         Some(session) => restore_client(session),
     24         None => match homeserver {
     25             Some(homeserver) => {
     26                 let homeserver = Url::parse(&homeserver);
     27                 match homeserver {
     28                     Ok(homeserver) => {
     29                         let client_config = ClientConfig::new();
     30                         let client = Client::new_with_config(homeserver, client_config).unwrap();
     31                         Ok(client)
     32                     }
     33                     Err(e) => Err(MatrixError::UrlParseError(e.to_string())),
     34                 }
     35             }
     36             None => Err(MatrixError::MissingFields(Field::Homeserver)),
     37         },
     38     }
     39 }
     40 
     41 fn restore_client(session: &SessionStore) -> Result<Client, MatrixError> {
     42     let homeserver = Url::parse(&session.homeserver_url);
     43     match homeserver {
     44         Ok(homeserver) => {
     45             let client_config = ClientConfig::new();
     46             let client = Client::new_with_config(homeserver, client_config);
     47             match client {
     48                 Ok(client) => {
     49                     info!("got client");
     50                     // Also directly restore Login data
     51                     let session = Session {
     52                         access_token: session.access_token.clone(),
     53                         user_id: matrix_sdk::identifiers::UserId::try_from(
     54                             session.user_id.as_str(),
     55                         )
     56                         .unwrap(),
     57                         device_id: session.device_id.clone().into(),
     58                     };
     59                     info!("before restore");
     60                     let cloned_client = client.clone();
     61                     spawn_local(async move {
     62                         if let Err(e) = cloned_client.restore_login(session).await {
     63                             error!("{}", e);
     64                             // TODO find a way to get this back up in the function tree
     65                         }
     66                     });
     67                     info!("after restore");
     68                     Ok(client)
     69                 }
     70                 Err(e) => Err(MatrixError::SDKError(e.to_string())),
     71             }
     72         }
     73         Err(e) => Err(MatrixError::UrlParseError(e.to_string())),
     74     }
     75 }