Browse Source

Starting to retrieve favourites

master
Julio Biason 3 years ago
parent
commit
4d0044900d
  1. 29
      src/commands/mod.rs
  2. 17
      src/config/config.rs
  3. 50
      src/main.rs

29
src/commands/mod.rs

@ -29,6 +29,7 @@ use self::errors::CommandError;
use crate::config::config::AccountConfig;
use crate::config::config::Config;
use crate::config::Configurable;
use crate::storage::data::Data;
use crate::storage::markdown::config::MarkdownConfig;
type CommandResult = Result<(), CommandError>;
@ -146,15 +147,35 @@ fn fetch_all() -> CommandResult {
let mut config = Config::open()?;
for (name, mut account_config) in config {
log::debug!("Fetching new items from {:?}", name);
fetch_account(&mut account_config)?;
let new_top_favourite = fetch_account(&mut account_config)?;
// XXX implement
config.set_new_favourite(new_top_favourite);
}
config.save()?;
Ok(())
}
fn fetch_account(account: &mut AccountConfig) -> CommandResult {
// Do the whole for into favourites here; after finding one favourite, do the dance below
if let Some(markdown_config) = account.markdown.as_ref() {
log::debug!("Markdown set to download to {:?}", markdown_config.path);
// XXX before anything, we could check if there is any storage enabled.
// XXX we could create a list of storages, so after retrieving the toot
// and converting to our format, we just go through this list and call
// `.save()` in each.
let top = account.top_favourite();
let mut most_recent: Option<String> = None;
let client = Mastodon::from(account.mastodon());
for toot in client.favourites()?.items_iter() {
if toot.id == top {
break;
}
if most_recent.is_none() {
most_recent = Some((&toot.id).into());
}
let conversion = Data::from(&toot);
// XXX storage here
// storage.save(&conversion)
}
Ok(())
}

17
src/config/config.rs

@ -38,13 +38,26 @@ struct Favourite {
/// Account configuration
#[derive(Serialize, Deserialize, Debug)]
pub struct AccountConfig {
favourite: Option<Favourite>,
pub favourite: Option<Favourite>,
mastodon: Data,
pub markdown: Option<MarkdownConfig>,
markdown: Option<MarkdownConfig>,
// joplin: Option<JoplinConfig>,
// org: Option<OrgConfig>,
}
impl AccountConfig {
pub fn top_favourite(&self) -> String {
match self.favourite {
Some(favourite) => favourite.last.into(),
None => "0".into(),
}
}
pub fn mastodon(&self) -> Data {
self.mastodon.clone()
}
}
/// The main configuration
#[derive(Serialize, Deserialize, Debug)]
pub struct Config(HashMap<String, AccountConfig>);

50
src/main.rs

@ -29,53 +29,3 @@ fn main() {
Err(e) => println!("Error: {:?}", e),
}
}
// Retrieve favourites
// fn fetch_favourites() {
// let config = match config::AccountConfig::get() {
// Ok(config) => config,
// Err(e) => {
// log::debug!("Configuration error: {:?}", e);
// let data = connect_to_mastodon();
// config::AccountConfig::from(data)
// }
// };
// let top = config.favourite.last.to_string();
// log::debug!("Last favourite seen: {}", top);
// let storage: Box<dyn Storage> = match (&config.joplin, &config.org) {
// (Some(joplin), _) => Box::new(Joplin::new_from_config(&joplin)),
// (None, Some(org)) => Box::new(Org::new_from_config(&org)),
// (None, None) => Box::new(Filesystem::new()),
// };
// let client = Mastodon::from(config.mastodon.clone());
// let most_recent_favourite = client
// .favourites()
// .unwrap()
// .items_iter()
// .take_while(|record| {
// println!("Current ID: {} (last favourite: {})", record.id, top);
// record.id != top
// })
// .map(|record| {
// log::debug!("Incoming record: {:?}", record);
// let conversion = Data::from(&record);
// log::debug!("Converted record: {:?}", conversion);
// storage.save(&conversion);
// record
// })
// .fold(None, {
// |first, current| -> Option<String> {
// if first.is_some() {
// first
// } else {
// Some(current.id)
// }
// }
// });
// if let Some(new_favourite) = most_recent_favourite {
// config.save(&new_favourite);
// }
// }

Loading…
Cancel
Save