Skip to content

Commit

Permalink
implement auto-login
Browse files Browse the repository at this point in the history
  • Loading branch information
Septias committed Mar 24, 2021
1 parent 9dacfec commit 83cbe02
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions Backend/src/tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,16 @@ use crate::{
IdSize,
};
use futures::future::join_all;
use itertools::Itertools;
use lazy_static::lazy_static;
use log::info;
use ron::de::from_bytes;
use ron::ser::{to_string_pretty, PrettyConfig};
use scraper::{ElementRef, Selector};
use serde::{Deserialize, Serialize};
use std::{
path::PathBuf,
sync::{Arc, Mutex},
};
use tokio::{
fs::File,
io::{AsyncReadExt, AsyncWriteExt},
sync::mpsc::{self, UnboundedReceiver, UnboundedSender},
task::{self, JoinHandle},
};
use std::{convert::TryInto, path::PathBuf, sync::{Arc, Mutex}};
use tokio::{fs::{File, read_to_string}, io::{AsyncReadExt, AsyncWriteExt}, sync::mpsc::{self, UnboundedReceiver, UnboundedSender}, task::{self, JoinHandle}};


#[derive(Debug, Deserialize, Serialize, Clone)]
pub struct IlNode {
Expand Down Expand Up @@ -71,7 +65,7 @@ lazy_static! {
}

pub struct ILiasTree {
client: Arc<IliasClient>,
pub client: Arc<IliasClient>,
tree: Option<Arc<Mutex<IlNode>>>,
receiver: Mutex<Option<UnboundedReceiver<Arc<Mutex<IlNode>>>>>,
sender: UnboundedSender<Arc<Mutex<IlNode>>>,
Expand All @@ -82,13 +76,44 @@ impl ILiasTree {
self.tree.as_ref()
}
pub async fn update_ilias(&self) -> Result<(), ClientError> {
update_ilias_tree(

let credentials = if let Err(err) = update_ilias_tree(
self.client.clone(),
self.get_root_node().unwrap().clone(),
self.sender.clone(),
)
.await
.unwrap()?;
.unwrap(){
if let ClientError::NoToken = err {
if let Ok(raw_credenetials) = read_to_string("credentials.txt").await{
let credentials: [String; 2] = raw_credenetials
.split("\n")
.map(|c| c.trim().to_owned())
.collect_vec()
.try_into()
.unwrap();
Ok(Some(credentials))
} else {
Err(err)
}
} else {
Err(err)
}
} else {
Ok(None)
}?;

if let Some(credentials) = credentials {
info!("Auto relogin");
self.client.acquire_token(&credentials).await?;
update_ilias_tree(
self.client.clone(),
self.get_root_node().unwrap().clone(),
self.sender.clone(),
)
.await
.unwrap()?;
}

Ok(())
}
Expand All @@ -114,6 +139,7 @@ impl ILiasTree {
}
}


pub async fn download_files(&self) -> Result<(), anyhow::Error> {
let client = self.client.clone();
let mut receiver = self.receiver.lock().unwrap().take().unwrap();
Expand All @@ -126,10 +152,6 @@ impl ILiasTree {
Ok(())
}

pub fn set_client_token(&self, token: &str) {
self.client.set_token(token);
}

pub async fn save(&self) {
let pretty = PrettyConfig::new()
.with_separate_tuple_members(true)
Expand Down

0 comments on commit 83cbe02

Please sign in to comment.