diff --git a/src/config/mod.rs b/src/config/mod.rs index f7f1bb9..1075f64 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -8,8 +8,9 @@ use toml; #[derive(Serialize, Deserialize, Debug)] pub struct JoplinConfig { - port: u32, - folder: String, + pub port: u32, + pub folder: String, + pub token: String, } #[derive(Serialize, Deserialize, Debug)] @@ -37,7 +38,6 @@ impl Config { } } - pub fn save(&self, most_recent_favourite: Option) -> () { if let Some(id) = most_recent_favourite { let new_configuration = Config { @@ -46,6 +46,7 @@ impl Config { None => None, Some(x) => Some(JoplinConfig { folder: x.folder.to_string(), + token: x.token.to_string(), port: x.port, }), }, diff --git a/src/main.rs b/src/main.rs index 853f5c4..5c8f2c7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,6 +12,7 @@ fn main() { let config = dbg!(config::Config::get()); let client = dbg!(get_mastodon_connection()); let top = dbg!(config.last_favorite.to_string()); + let _joplin = crate::storage::joplin::validate(&config); let most_recent_favourite = client .favourites() @@ -19,7 +20,7 @@ fn main() { .items_iter() .take_while(|record| record.id != top) .map(|record| { - let storage = dbg!(storage::filesystem::Filesystem::from(&record)); + let storage = dbg!(storage::filesystem::Filesystem::from(dbg!(&record))); storage.open(); storage.save(); record diff --git a/src/storage/joplin.rs b/src/storage/joplin.rs new file mode 100644 index 0000000..71739af --- /dev/null +++ b/src/storage/joplin.rs @@ -0,0 +1,72 @@ +use crate::config::Config; +use crate::config::JoplinConfig; + +use reqwest::Error; +use reqwest::Url; +use serde_derive::Deserialize; + +/// This is the folder structured returned by Joplin. It is here so Reqwests can +/// unjson the data (there are more fields, but these are the only ones we need +/// right now). +#[allow(dead_code)] +#[derive(Deserialize)] +struct Folder { + id: String, + title: String, +} + +/// Connection to Joplin. +pub struct JoplinConnection { + port: u32, + token: String, + folder_id: String, +} + +pub fn validate(config: &Config) -> Option { + if let Some(joplin_config) = &config.joplin { + let folder_id = dbg!(get_folder_id(&joplin_config)); + + if let Some(folder) = folder_id { + Some(JoplinConnection { + port: joplin_config.port, + token: joplin_config.token.to_string(), + folder_id: folder, + }) + } else { + println!("No folder named {}", joplin_config.folder); + None + } + } else { + println!("Joplin not set up"); + None + } +} + +fn build_url(config: &JoplinConfig, resource: &String) -> Url { + let base_url = format!( + "http://localhost:{port}/{resource}?token={token}", + port = config.port, + resource = resource, + token = config.token + ); + let url = Url::parse(&base_url); + url.unwrap() +} + +fn get_folder_id(config: &JoplinConfig) -> Option { + let request = get_folder_list(config); + if let Ok(folders) = request { + for folder in folders { + if folder.title == *config.folder { + return Some(folder.id); + } + } + } + None +} + +fn get_folder_list(config: &JoplinConfig) -> Result, Error> { + let folders: Vec = + reqwest::get(&build_url(config, &String::from("folders")).into_string())?.json()?; + Ok(folders) +} diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 5ec7031..b5f4ae0 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -1,3 +1,4 @@ +pub mod attachment; pub mod filesystem; +pub mod joplin; pub mod storage; -pub mod attachment;