|
|
|
@ -7,7 +7,11 @@ extern crate smart_default; |
|
|
|
|
#[macro_use] |
|
|
|
|
extern crate serde; |
|
|
|
|
#[macro_use] |
|
|
|
|
use elefren::{helpers::cli, prelude::*}; |
|
|
|
|
use elefren::{ |
|
|
|
|
helpers::cli, |
|
|
|
|
prelude::*, |
|
|
|
|
http_send::HttpSender, |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
use failure::Fallible; |
|
|
|
|
use crate::bootstrap::Config; |
|
|
|
@ -16,32 +20,28 @@ use crate::store::Store; |
|
|
|
|
mod bootstrap; |
|
|
|
|
mod store; |
|
|
|
|
|
|
|
|
|
type EleRegistratered = elefren::registration::Registered<HttpSender>; |
|
|
|
|
|
|
|
|
|
/// Wrapper for the long tuple with Registration state
|
|
|
|
|
#[derive(Serialize,Deserialize,Debug)] |
|
|
|
|
struct ElefrenRegistration { |
|
|
|
|
parts: (String, String, String, String, Scopes, bool), |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const KEY_OAUTH_REGISTRATION: &str = "elefren.registration"; |
|
|
|
|
const KEY_OAUTH_SESSION: &str = "elefren.session"; |
|
|
|
|
|
|
|
|
|
fn main() { |
|
|
|
|
let config : Config = bootstrap::init().expect("error init config"); |
|
|
|
|
|
|
|
|
|
debug!("Loaded config: {:#?}", config); |
|
|
|
|
|
|
|
|
|
let mut store = Store::from_file(&config.store); |
|
|
|
|
store.set_autosave(true); |
|
|
|
|
|
|
|
|
|
debug!("Store: {:?}", store); |
|
|
|
|
|
|
|
|
|
//store.put("foo", vec![1,2,3,4]);
|
|
|
|
|
|
|
|
|
|
let mut v : Vec<u32> = store.get("foo").unwrap(); |
|
|
|
|
v.push(v.last().unwrap()+1); |
|
|
|
|
store.put("foo", v); |
|
|
|
|
let registered = register(&mut store, &config); |
|
|
|
|
|
|
|
|
|
store.save(); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
|
|
|
|
let registration = Registration::new(config.instance) |
|
|
|
|
.client_name("elefren_test") |
|
|
|
|
.build().expect("error register"); |
|
|
|
|
|
|
|
|
|
let mastodon = cli::authenticate(registration).expect("error auth"); |
|
|
|
|
let mastodon = open_session(&mut store, registered); |
|
|
|
|
|
|
|
|
|
println!( |
|
|
|
|
"{:?}", |
|
|
|
@ -49,7 +49,50 @@ fn main() { |
|
|
|
|
.get_home_timeline().expect("error get TL") |
|
|
|
|
.items_iter() |
|
|
|
|
.take(100) |
|
|
|
|
.collect::<Vec<_>>() |
|
|
|
|
.map(|item| { |
|
|
|
|
format!("{}: {}", item.account.acct, item.content) |
|
|
|
|
}) |
|
|
|
|
.collect::<Vec<String>>() |
|
|
|
|
); |
|
|
|
|
*/ |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn register(store : &mut Store, config : &Config) -> EleRegistratered { |
|
|
|
|
match store.get::<ElefrenRegistration>(KEY_OAUTH_REGISTRATION) { |
|
|
|
|
Some(reg) => { |
|
|
|
|
info!("Loaded registration from store"); |
|
|
|
|
|
|
|
|
|
EleRegistratered::from_parts( |
|
|
|
|
// this sucks
|
|
|
|
|
®.parts.0, ®.parts.1, ®.parts.2, ®.parts.3, reg.parts.4, reg.parts.5 |
|
|
|
|
) |
|
|
|
|
} |
|
|
|
|
None => { |
|
|
|
|
info!("Creating a new registration"); |
|
|
|
|
|
|
|
|
|
let registered = Registration::new(&config.instance) |
|
|
|
|
.client_name("manabu") |
|
|
|
|
.build().expect("error register"); |
|
|
|
|
store.put(KEY_OAUTH_REGISTRATION, ElefrenRegistration { parts : registered.clone().into_parts() }); |
|
|
|
|
registered |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
fn open_session(store : &mut Store, registered: EleRegistratered) -> Mastodon<HttpSender> { |
|
|
|
|
match store.get::<elefren::Data>(KEY_OAUTH_SESSION) { |
|
|
|
|
Some(data) => { |
|
|
|
|
info!("Reusing saved authorization."); |
|
|
|
|
|
|
|
|
|
let cli = Mastodon::from(data); |
|
|
|
|
// TODO check if the session is live, somehow
|
|
|
|
|
cli |
|
|
|
|
} |
|
|
|
|
None => { |
|
|
|
|
info!("Creating new authorization."); |
|
|
|
|
|
|
|
|
|
let cli = cli::authenticate(registered).expect("error auth"); |
|
|
|
|
store.put(KEY_OAUTH_SESSION, cli.data.clone()); |
|
|
|
|
cli |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|