Browse Source

Validate if Joplin is set up and working

master
Julio Biason 5 years ago
parent
commit
2f7c4cde65
  1. 7
      src/config/mod.rs
  2. 3
      src/main.rs
  3. 72
      src/storage/joplin.rs
  4. 3
      src/storage/mod.rs

7
src/config/mod.rs

@ -8,8 +8,9 @@ use toml;
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
pub struct JoplinConfig { pub struct JoplinConfig {
port: u32, pub port: u32,
folder: String, pub folder: String,
pub token: String,
} }
#[derive(Serialize, Deserialize, Debug)] #[derive(Serialize, Deserialize, Debug)]
@ -37,7 +38,6 @@ impl Config {
} }
} }
pub fn save(&self, most_recent_favourite: Option<String>) -> () { pub fn save(&self, most_recent_favourite: Option<String>) -> () {
if let Some(id) = most_recent_favourite { if let Some(id) = most_recent_favourite {
let new_configuration = Config { let new_configuration = Config {
@ -46,6 +46,7 @@ impl Config {
None => None, None => None,
Some(x) => Some(JoplinConfig { Some(x) => Some(JoplinConfig {
folder: x.folder.to_string(), folder: x.folder.to_string(),
token: x.token.to_string(),
port: x.port, port: x.port,
}), }),
}, },

3
src/main.rs

@ -12,6 +12,7 @@ fn main() {
let config = dbg!(config::Config::get()); let config = dbg!(config::Config::get());
let client = dbg!(get_mastodon_connection()); let client = dbg!(get_mastodon_connection());
let top = dbg!(config.last_favorite.to_string()); let top = dbg!(config.last_favorite.to_string());
let _joplin = crate::storage::joplin::validate(&config);
let most_recent_favourite = client let most_recent_favourite = client
.favourites() .favourites()
@ -19,7 +20,7 @@ fn main() {
.items_iter() .items_iter()
.take_while(|record| record.id != top) .take_while(|record| record.id != top)
.map(|record| { .map(|record| {
let storage = dbg!(storage::filesystem::Filesystem::from(&record)); let storage = dbg!(storage::filesystem::Filesystem::from(dbg!(&record)));
storage.open(); storage.open();
storage.save(); storage.save();
record record

72
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<JoplinConnection> {
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<String> {
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<Vec<Folder>, Error> {
let folders: Vec<Folder> =
reqwest::get(&build_url(config, &String::from("folders")).into_string())?.json()?;
Ok(folders)
}

3
src/storage/mod.rs

@ -1,3 +1,4 @@
pub mod attachment;
pub mod filesystem; pub mod filesystem;
pub mod joplin;
pub mod storage; pub mod storage;
pub mod attachment;

Loading…
Cancel
Save