From c9bf51baee3a8174421580101a6a4cc95510bc08 Mon Sep 17 00:00:00 2001 From: Izzy Swart Date: Mon, 5 Jul 2021 19:41:24 -0700 Subject: [PATCH] Serialize fediverse configuration --- src/config.rs | 4 ++-- src/main.rs | 15 ++++++++------- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/config.rs b/src/config.rs index 4ad369b..47c2baa 100644 --- a/src/config.rs +++ b/src/config.rs @@ -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, 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), } diff --git a/src/main.rs b/src/main.rs index f477705..f389a41 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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> { 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()));