Added register feature back with reqwest
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m13s Details
Build image / build (push) Successful in 27m27s Details

This commit is contained in:
Alie 2025-01-07 16:55:45 +01:00
parent 64a5ebca2f
commit e9d332487d
1 changed files with 47 additions and 5 deletions

View File

@ -79,7 +79,7 @@ async fn main() -> DynResult<()> {
.build() .build()
.unwrap(); .unwrap();
let config: Config = get_config(); let config: Config = get_config();
let creds = get_mastodon_data(&config).await; let creds = get_mastodon_data(&client, &config).await;
match get_next_url(&config).await { match get_next_url(&config).await {
Ok(image) => match image { Ok(image) => match image {
@ -330,17 +330,59 @@ struct MastodonData {
token: String, token: String,
} }
async fn get_mastodon_data(_config: &Config) -> MastodonData { async fn get_mastodon_data(client: &Client, config: &Config) -> MastodonData {
match parse_mastodon_data(CREDENTIALS_FILENAME) { match parse_mastodon_data(CREDENTIALS_FILENAME) {
Ok(config) => config, Ok(config) => config,
Err(err) => { Err(err) => {
log::error!("Credentials file parsing unsuccesful: {}", err); log::error!("Credentials file parsing unsuccesful: {}", err);
log::info!("Please check the working directory to find a new Credentials file"); register(client, config).await;
exit(1); exit(1);
} }
} }
} }
#[derive(Deserialize)]
struct AppRegister {
client_id: String,
client_secret: String,
}
async fn register(client: &Client, config: &Config) {
let request = vec![
("client_name", config.bot.name.as_str()),
("redirect_uris", "urn:ietf:wg:oauth:2.0:oob"),
("scopes", "write"),
];
let app: AppRegister = client
.post(format!("{}/api/v1/apps", config.bot.instance))
.form(&request)
.send()
.await
.expect("Error sending request to instance on app register")
.json()
.await
.expect("Error parsing app register response");
println!("Please enter into the following url to authrise:");
println!("{}/oauth/authorize?client_id={}&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&scope=write", config.bot.instance, app.client_id);
let mut token = String::new();
std::io::stdin()
.read_line(&mut token)
.expect("Error reading stdin");
let toml = toml::to_string(&MastodonData {
base: config.bot.instance.clone(),
client_id: app.client_id,
client_secret: app.client_secret,
redirect: "urn:ietf:wg:oauth:2.0:oob".to_string(),
token,
})
.expect("Failed to create credentials file");
std::fs::write(CREDENTIALS_FILENAME, toml).expect("Failed to write credentials file");
}
fn parse_mastodon_data(filename: &str) -> DynResult<MastodonData> { fn parse_mastodon_data(filename: &str) -> DynResult<MastodonData> {
let toml_file = std::fs::read_to_string(filename)?; //.expect("No config file, consider getting the original one and modifing it"); let toml_file = std::fs::read_to_string(filename)?; //.expect("No config file, consider getting the original one and modifing it");
Ok(toml::from_str(&toml_file)?) //("Malformed config file, check the original one for reference") Ok(toml::from_str(&toml_file)?) //("Malformed config file, check the original one for reference")
@ -478,7 +520,7 @@ mod tests {
.build() .build()
.unwrap(); .unwrap();
let config = get_config(); let config = get_config();
let creds = get_mastodon_data(&config).await; let creds = get_mastodon_data(&client, &config).await;
let msg = "Test!".to_string(); let msg = "Test!".to_string();
let status = post(&client, &creds, &config, &msg, Visibility::direct) let status = post(&client, &creds, &config, &msg, Visibility::direct)
@ -499,7 +541,7 @@ mod tests {
.build() .build()
.unwrap(); .unwrap();
let config = get_config(); let config = get_config();
let creds = get_mastodon_data(&config).await; let creds = get_mastodon_data(&client, &config).await;
let status = post_image( let status = post_image(
&client, &client,