Serialize fediverse configuration

This commit is contained in:
Izzy Swart 2021-07-05 19:41:24 -07:00
parent c51beb2bda
commit c9bf51baee
2 changed files with 10 additions and 9 deletions

View File

@ -14,7 +14,7 @@ pub struct Config {
pub interval_seconds: MinMax,
pub bot_token: String,
pub chat_ref: ChatId,
pub fediverse_oauth_token: String,
pub fediverse_token: Option<mammut::Data>,
pub post_buffer: u32,
}
@ -38,7 +38,7 @@ impl Default for Config {
max: 60 * 90,
},
post_buffer: 5,
fediverse_oauth_token: "".to_owned(),
fediverse_token: None,
bot_token: "".to_owned(),
chat_ref: ChatId::new(0),
}

View File

@ -4,7 +4,7 @@ use async_std::io::stdin;
use futures_timer::Delay;
use mammut::{
apps::{AppBuilder, Scopes},
Registration,
Mastodon, Registration,
};
use rand::Rng;
use telegram_bot::{Api, ChatRef};
@ -48,22 +48,23 @@ async fn main() -> Result<(), Box<dyn Error>> {
let mut registration = Registration::new(cfg.fediverse_base_url.clone());
registration.register(app)?;
let token = if cfg.fediverse_oauth_token.is_empty() {
let mastodon = if let Some(data) = &cfg.fediverse_token {
Mastodon::from_data(data.clone())
} else {
let url = registration.authorise()?;
println!("{}", url);
let mut buffer = String::new();
stdin().read_line(&mut buffer).await?;
cfg.fediverse_oauth_token = buffer.clone();
let mastodon = registration.create_access_token(buffer)?;
cfg.fediverse_token = Some(mastodon.data.clone());
cfg.save(CONFIG_PATH)?;
buffer
} else {
cfg.fediverse_oauth_token.clone()
mastodon
};
let mastodon = registration.create_access_token(token)?;
let publisher = MastodonPublisher::new(mastodon);
let api = Arc::new(Api::new(cfg.bot_token.clone()));