|
|
|
@ -1,6 +1,4 @@
|
|
|
|
|
use mastodon_async::entities::visibility::Visibility;
|
|
|
|
|
use mastodon_async::helpers::{cli, toml as masto_toml};
|
|
|
|
|
use mastodon_async::prelude::*;
|
|
|
|
|
use reqwest::Client;
|
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
use std::io::Cursor;
|
|
|
|
|
use std::process::exit;
|
|
|
|
@ -29,6 +27,7 @@ struct AccountUpdate {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const CONFIG_FILENAME: &str = "config.toml";
|
|
|
|
|
const CREDENTIALS_FILENAME: &str = "mastodon-data.toml";
|
|
|
|
|
|
|
|
|
|
type DynResult<T> = Result<T, Box<dyn std::error::Error>>;
|
|
|
|
|
|
|
|
|
@ -66,7 +65,7 @@ struct Errors {
|
|
|
|
|
retry: u8,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::main] // requires `features = ["mt"]
|
|
|
|
|
#[tokio::main]
|
|
|
|
|
async fn main() -> DynResult<()> {
|
|
|
|
|
stderrlog::new()
|
|
|
|
|
.module(module_path!())
|
|
|
|
@ -75,17 +74,22 @@ async fn main() -> DynResult<()> {
|
|
|
|
|
.timestamp(stderrlog::Timestamp::Second)
|
|
|
|
|
.init()?;
|
|
|
|
|
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.user_agent("bot")
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let config: Config = get_config();
|
|
|
|
|
let mastodon = get_account(&config).await;
|
|
|
|
|
let creds = get_mastodon_data(&client, &config).await;
|
|
|
|
|
|
|
|
|
|
match get_next_url(&config).await {
|
|
|
|
|
Ok(image) => match image {
|
|
|
|
|
Some(image) => {
|
|
|
|
|
let mut retry: u8 = 0;
|
|
|
|
|
while let Err(err) = post_image(&mastodon, &image.url, &config, Visibility::Unlisted).await
|
|
|
|
|
while let Err(err) =
|
|
|
|
|
post_image(&client, &creds, &config, &image.url, Visibility::public).await
|
|
|
|
|
{
|
|
|
|
|
log::warn!("Cannot post image, retry: {}, {}", retry, err);
|
|
|
|
|
async_std::task::sleep(Duration::new(1, 0)).await;
|
|
|
|
|
std::thread::sleep(Duration::new(1, 0));
|
|
|
|
|
retry += 1;
|
|
|
|
|
if retry >= config.errors.retry {
|
|
|
|
|
log::error!("Max ammount of retries reached on post_image");
|
|
|
|
@ -95,9 +99,9 @@ async fn main() -> DynResult<()> {
|
|
|
|
|
}
|
|
|
|
|
set_url_as_posted(&config, &image).await?;
|
|
|
|
|
let mut retry: u8 = 0;
|
|
|
|
|
while let Err(err) = update_bio(&mastodon, &config).await {
|
|
|
|
|
while let Err(err) = update_bio(&config, &creds).await {
|
|
|
|
|
log::warn!("Cannot update bio, retry: {}, {}", retry, err);
|
|
|
|
|
async_std::task::sleep(Duration::new(1, 0)).await;
|
|
|
|
|
std::thread::sleep(Duration::new(1, 0));
|
|
|
|
|
retry += 1;
|
|
|
|
|
if retry >= config.errors.retry {
|
|
|
|
|
log::error!("Max ammount of retries reached on update bio");
|
|
|
|
@ -108,17 +112,19 @@ async fn main() -> DynResult<()> {
|
|
|
|
|
None => {
|
|
|
|
|
let mut retry: u8 = 0;
|
|
|
|
|
while let Err(err) = post(
|
|
|
|
|
&mastodon,
|
|
|
|
|
&client,
|
|
|
|
|
&creds,
|
|
|
|
|
&config,
|
|
|
|
|
&format!(
|
|
|
|
|
"{} {}",
|
|
|
|
|
&config.errors.maintainers, &config.errors.out_of_images
|
|
|
|
|
),
|
|
|
|
|
Visibility::Direct,
|
|
|
|
|
Visibility::direct,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
|
log::warn!("Cannot post, retry: {}, {}", retry, err);
|
|
|
|
|
async_std::task::sleep(Duration::new(1, 0)).await;
|
|
|
|
|
std::thread::sleep(Duration::new(1, 0));
|
|
|
|
|
retry += 1;
|
|
|
|
|
if retry >= config.errors.retry {
|
|
|
|
|
log::error!("Max ammount of retries reached on post");
|
|
|
|
@ -130,9 +136,11 @@ async fn main() -> DynResult<()> {
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("Cannot get next image: {}", err);
|
|
|
|
|
match post(
|
|
|
|
|
&mastodon,
|
|
|
|
|
&client,
|
|
|
|
|
&creds,
|
|
|
|
|
&config,
|
|
|
|
|
&format!("{} {}", &config.errors.maintainers, &err.to_string()),
|
|
|
|
|
Visibility::Direct,
|
|
|
|
|
Visibility::direct,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
{
|
|
|
|
@ -166,24 +174,9 @@ fn generate_config() -> DynResult<()> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Parses the given filename to a config struct
|
|
|
|
|
fn parse_config(filename: &str) -> DynResult<Config> {
|
|
|
|
|
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")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_account(config: &Config) -> Mastodon {
|
|
|
|
|
if let Ok(data) = masto_toml::from_file("mastodon-data.toml") {
|
|
|
|
|
Mastodon::new(reqwest::Client::builder().user_agent("bot").build().unwrap(), data)
|
|
|
|
|
} else {
|
|
|
|
|
match register(config).await {
|
|
|
|
|
Ok(account) => account,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("Api registation unsuccesful: {}", err);
|
|
|
|
|
exit(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
let toml_file = std::fs::read_to_string(filename)?;
|
|
|
|
|
Ok(toml::from_str(&toml_file)?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_next_url(config: &Config) -> DynResult<Option<Image>> {
|
|
|
|
@ -216,7 +209,10 @@ struct Token {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn set_url_as_posted(config: &Config, image: &Image) -> DynResult<()> {
|
|
|
|
|
let client = reqwest::Client::builder().user_agent("bot").build().unwrap();
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.user_agent("bot")
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let auth = &Auth {
|
|
|
|
|
app: config.backend.app.to_string(),
|
|
|
|
@ -250,36 +246,37 @@ async fn set_url_as_posted(config: &Config, image: &Image) -> DynResult<()> {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn post_image(
|
|
|
|
|
account: &Mastodon,
|
|
|
|
|
url: &String,
|
|
|
|
|
client: &Client,
|
|
|
|
|
creds: &MastodonData,
|
|
|
|
|
config: &Config,
|
|
|
|
|
url: &String,
|
|
|
|
|
visibility: Visibility,
|
|
|
|
|
) -> DynResult<Status> {
|
|
|
|
|
fetch_url(url, &config.files.tempfile).await?; //.expect("Error fetching url");
|
|
|
|
|
let attachment = account
|
|
|
|
|
.media(&config.files.tempfile, Some(url.to_string()))
|
|
|
|
|
.await?; //.expect("Attachment upload error");
|
|
|
|
|
let attachment = account
|
|
|
|
|
.wait_for_processing(attachment, Default::default())
|
|
|
|
|
.await?; //.expect("Attachment processing error");
|
|
|
|
|
let status = StatusBuilder::new()
|
|
|
|
|
.media_ids(&[attachment.id])
|
|
|
|
|
.visibility(visibility)
|
|
|
|
|
.sensitive(true)
|
|
|
|
|
.build()?; //.expect("Could not build status"); // we should retry
|
|
|
|
|
let status = account.new_status(status).await?; //.expect("Error generating status"); // we should retry or delete last url in posted
|
|
|
|
|
) -> DynResult<Post> {
|
|
|
|
|
fetch_url(url, &config.files.tempfile).await?;
|
|
|
|
|
let media_id = upload_media(&client, &creds, &config, url).await?.id;
|
|
|
|
|
let post = PostForm {
|
|
|
|
|
media_ids: vec![media_id],
|
|
|
|
|
sensitive: true,
|
|
|
|
|
status: String::new(),
|
|
|
|
|
visibility,
|
|
|
|
|
};
|
|
|
|
|
let status = post_status(&client, &creds, &config, post).await?;
|
|
|
|
|
log::info!("Image status posted: {}", url);
|
|
|
|
|
Ok(status)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> {
|
|
|
|
|
let images: ImagesWrap = reqwest::get(format!("{}/images?status=available", config.backend.url))
|
|
|
|
|
async fn update_bio(config: &Config, creds: &MastodonData) -> DynResult<()> {
|
|
|
|
|
let images: ImagesWrap =
|
|
|
|
|
reqwest::get(format!("{}/images?status=available", config.backend.url))
|
|
|
|
|
.await?
|
|
|
|
|
.json()
|
|
|
|
|
.await?;
|
|
|
|
|
|
|
|
|
|
let remaining = images.images.len();
|
|
|
|
|
let client = reqwest::Client::builder().user_agent("bot").build().unwrap();
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.user_agent("bot")
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let account_update = AccountUpdate {
|
|
|
|
|
note: format!("{}\n\n{} new images remaining", config.bot.bio, remaining),
|
|
|
|
@ -290,7 +287,7 @@ async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> {
|
|
|
|
|
"{}/api/v1/accounts/update_credentials",
|
|
|
|
|
config.bot.instance
|
|
|
|
|
))
|
|
|
|
|
.bearer_auth(&account.data.token)
|
|
|
|
|
.bearer_auth(&creds.token)
|
|
|
|
|
.json(&account_update)
|
|
|
|
|
.send()
|
|
|
|
|
.await?;
|
|
|
|
@ -298,12 +295,20 @@ async fn update_bio(account: &Mastodon, config: &Config) -> DynResult<()> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn post(account: &Mastodon, msg: &str, visibility: Visibility) -> DynResult<Status> {
|
|
|
|
|
let status = StatusBuilder::new()
|
|
|
|
|
.visibility(visibility)
|
|
|
|
|
.status(msg)
|
|
|
|
|
.build()?; //.expect("Error building error status");
|
|
|
|
|
let post = account.new_status(status).await?; //.expect("Error posting error status");
|
|
|
|
|
async fn post(
|
|
|
|
|
client: &Client,
|
|
|
|
|
creds: &MastodonData,
|
|
|
|
|
config: &Config,
|
|
|
|
|
msg: &str,
|
|
|
|
|
visibility: Visibility,
|
|
|
|
|
) -> DynResult<Post> {
|
|
|
|
|
let post = PostForm {
|
|
|
|
|
status: msg.to_owned(),
|
|
|
|
|
sensitive: false,
|
|
|
|
|
visibility,
|
|
|
|
|
media_ids: vec![],
|
|
|
|
|
};
|
|
|
|
|
let post = post_status(client, creds, config, post).await?;
|
|
|
|
|
log::info!("Text status posted: {}", msg);
|
|
|
|
|
Ok(post)
|
|
|
|
|
}
|
|
|
|
@ -316,27 +321,188 @@ async fn fetch_url(url: &String, file_name: &String) -> DynResult<()> {
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn register(config: &Config) -> DynResult<Mastodon> {
|
|
|
|
|
let registration = Registration::new_with_client(&config.bot.instance, reqwest::Client::builder().user_agent("bot").build().unwrap())
|
|
|
|
|
.client_name(&config.bot.name)
|
|
|
|
|
.scopes(Scopes::all())
|
|
|
|
|
.build()
|
|
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
|
struct MastodonData {
|
|
|
|
|
base: String,
|
|
|
|
|
client_id: String,
|
|
|
|
|
client_secret: String,
|
|
|
|
|
redirect: String,
|
|
|
|
|
token: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn get_mastodon_data(client: &Client, config: &Config) -> MastodonData {
|
|
|
|
|
match parse_mastodon_data(CREDENTIALS_FILENAME) {
|
|
|
|
|
Ok(config) => config,
|
|
|
|
|
Err(err) => {
|
|
|
|
|
log::error!("Credentials file parsing unsuccesful: {}", err);
|
|
|
|
|
register(client, config).await;
|
|
|
|
|
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> {
|
|
|
|
|
let toml_file = std::fs::read_to_string(filename)?;
|
|
|
|
|
Ok(toml::from_str(&toml_file)?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
struct Post {
|
|
|
|
|
id: String,
|
|
|
|
|
media_attachments: Vec<Attachment>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
#[allow(dead_code)]
|
|
|
|
|
struct Attachment {
|
|
|
|
|
url: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
|
#[allow(dead_code, non_camel_case_types)]
|
|
|
|
|
enum Visibility {
|
|
|
|
|
public,
|
|
|
|
|
unlisted,
|
|
|
|
|
private,
|
|
|
|
|
direct,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Serialize, Debug)]
|
|
|
|
|
struct PostForm {
|
|
|
|
|
status: String,
|
|
|
|
|
sensitive: bool,
|
|
|
|
|
visibility: Visibility,
|
|
|
|
|
media_ids: Vec<String>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn post_status(
|
|
|
|
|
client: &Client,
|
|
|
|
|
creds: &MastodonData,
|
|
|
|
|
config: &Config,
|
|
|
|
|
post: PostForm,
|
|
|
|
|
) -> DynResult<Post> {
|
|
|
|
|
Ok(client
|
|
|
|
|
.post(format!("{}/api/v1/statuses", &config.bot.instance))
|
|
|
|
|
.bearer_auth(&creds.token)
|
|
|
|
|
.header("Idempotency-Key", &post.status)
|
|
|
|
|
.json(&post)
|
|
|
|
|
.send()
|
|
|
|
|
.await?
|
|
|
|
|
.json()
|
|
|
|
|
.await?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[derive(Deserialize)]
|
|
|
|
|
struct MediaAttachment {
|
|
|
|
|
id: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn upload_media(
|
|
|
|
|
client: &Client,
|
|
|
|
|
creds: &MastodonData,
|
|
|
|
|
config: &Config,
|
|
|
|
|
url: &String,
|
|
|
|
|
) -> DynResult<MediaAttachment> {
|
|
|
|
|
let image = reqwest::multipart::Form::new()
|
|
|
|
|
.text("description", url.to_string())
|
|
|
|
|
.file("file", &config.files.tempfile)
|
|
|
|
|
.await?;
|
|
|
|
|
let mastodon = cli::authenticate(registration).await?;
|
|
|
|
|
|
|
|
|
|
// Save app data for using on the next run.
|
|
|
|
|
masto_toml::to_file(&mastodon.data, "mastodon-data.toml")?;
|
|
|
|
|
|
|
|
|
|
Ok(mastodon)
|
|
|
|
|
Ok(client
|
|
|
|
|
.post(format!("{}/api/v2/media", &config.bot.instance))
|
|
|
|
|
.bearer_auth(&creds.token)
|
|
|
|
|
.multipart(image)
|
|
|
|
|
.send()
|
|
|
|
|
.await?
|
|
|
|
|
.json()
|
|
|
|
|
.await?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
|
mod tests {
|
|
|
|
|
|
|
|
|
|
async fn get_status(
|
|
|
|
|
client: &Client,
|
|
|
|
|
creds: &MastodonData,
|
|
|
|
|
config: &Config,
|
|
|
|
|
post_id: &String,
|
|
|
|
|
) -> DynResult<Post> {
|
|
|
|
|
Ok(client
|
|
|
|
|
.get(format!(
|
|
|
|
|
"{}/api/v1/statuses/{}",
|
|
|
|
|
&config.bot.instance, post_id
|
|
|
|
|
))
|
|
|
|
|
.bearer_auth(&creds.token)
|
|
|
|
|
.send()
|
|
|
|
|
.await?
|
|
|
|
|
.json()
|
|
|
|
|
.await?)
|
|
|
|
|
}
|
|
|
|
|
async fn delete_status(
|
|
|
|
|
client: &Client,
|
|
|
|
|
creds: &MastodonData,
|
|
|
|
|
config: &Config,
|
|
|
|
|
post_id: &String,
|
|
|
|
|
) -> DynResult<Post> {
|
|
|
|
|
Ok(client
|
|
|
|
|
.delete(format!(
|
|
|
|
|
"{}/api/v1/statuses/{}",
|
|
|
|
|
&config.bot.instance, post_id
|
|
|
|
|
))
|
|
|
|
|
.bearer_auth(&creds.token)
|
|
|
|
|
.send()
|
|
|
|
|
.await?
|
|
|
|
|
.json()
|
|
|
|
|
.await?)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
use reqwest::StatusCode;
|
|
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
const TMPTESTDIR: &str = "/tmp/botimage";
|
|
|
|
|
const TMPTESTDIR: &str = "/tmp/botimage.png";
|
|
|
|
|
const TEST_URL: &str = "https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b";
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
@ -349,45 +515,57 @@ mod tests {
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn post_should_post() {
|
|
|
|
|
let client = reqwest::Client::builder().user_agent("bot").build().unwrap();
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.user_agent("bot")
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let config = get_config();
|
|
|
|
|
let account = get_account(&config).await;
|
|
|
|
|
let creds = get_mastodon_data(&client, &config).await;
|
|
|
|
|
let msg = "Test!".to_string();
|
|
|
|
|
|
|
|
|
|
let status = post(&account, &msg, Visibility::Direct).await.unwrap();
|
|
|
|
|
let response = client
|
|
|
|
|
.get(format!(
|
|
|
|
|
"{}/api/v1/statuses/{}",
|
|
|
|
|
&config.bot.instance,
|
|
|
|
|
&status.id.to_string()
|
|
|
|
|
))
|
|
|
|
|
.bearer_auth(&account.data.token)
|
|
|
|
|
.send()
|
|
|
|
|
let status = post(&client, &creds, &config, &msg, Visibility::direct)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
let response = get_status(&client, &creds, &config, &status.id).await;
|
|
|
|
|
delete_status(&client, &creds, &config, &status.id)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
account.delete_status(&status.id).await.unwrap();
|
|
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::OK)
|
|
|
|
|
response.unwrap();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
|
|
|
|
async fn post_image_works() {
|
|
|
|
|
let client = reqwest::Client::builder().user_agent("bot").build().unwrap();
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.user_agent("bot")
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
let config = get_config();
|
|
|
|
|
let account = get_account(&config).await;
|
|
|
|
|
let creds = get_mastodon_data(&client, &config).await;
|
|
|
|
|
|
|
|
|
|
let status = post_image(&account, &TEST_URL.to_string(), &config, Visibility::Direct)
|
|
|
|
|
let status = post_image(
|
|
|
|
|
&client,
|
|
|
|
|
&creds,
|
|
|
|
|
&config,
|
|
|
|
|
&TEST_URL.to_string(),
|
|
|
|
|
Visibility::direct,
|
|
|
|
|
)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let response = account.get_status(&status.id).await.unwrap();
|
|
|
|
|
let response = get_status(&client, &creds, &config, &status.id)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
delete_status(&client, &creds, &config, &status.id)
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
account.delete_status(&status.id).await.unwrap();
|
|
|
|
|
let attachment = &response.media_attachments[0];
|
|
|
|
|
|
|
|
|
|
let response = client
|
|
|
|
|
.get(attachment.url.clone().unwrap())
|
|
|
|
|
.bearer_auth(&account.data.token)
|
|
|
|
|
.get(&attachment.url)
|
|
|
|
|
.bearer_auth(&creds.token)
|
|
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
.unwrap();
|
|
|
|
@ -402,7 +580,8 @@ mod tests {
|
|
|
|
|
let expected = insert_image(&config, IMAGE).await.unwrap();
|
|
|
|
|
|
|
|
|
|
set_url_as_posted(&config, &expected).await.unwrap();
|
|
|
|
|
let image: ImageWrap = reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
|
|
|
|
|
let image: ImageWrap =
|
|
|
|
|
reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.json()
|
|
|
|
@ -419,12 +598,12 @@ mod tests {
|
|
|
|
|
const IMAGE: &str = "https://picsum.photos/id/1";
|
|
|
|
|
let expected = insert_image(&config, IMAGE).await.unwrap();
|
|
|
|
|
|
|
|
|
|
// Get test url
|
|
|
|
|
let image = get_next_url(&config).await.unwrap().unwrap();
|
|
|
|
|
assert_eq!(image.url, IMAGE);
|
|
|
|
|
|
|
|
|
|
set_url_as_posted(&config, &expected).await.unwrap();
|
|
|
|
|
let image: ImageWrap = reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
|
|
|
|
|
let image: ImageWrap =
|
|
|
|
|
reqwest::get(format!("{}/images/{}", config.backend.url, expected._id))
|
|
|
|
|
.await
|
|
|
|
|
.unwrap()
|
|
|
|
|
.json()
|
|
|
|
@ -433,13 +612,15 @@ mod tests {
|
|
|
|
|
let image = image.image;
|
|
|
|
|
assert_eq!(image.status, "consumed");
|
|
|
|
|
|
|
|
|
|
// Test that now it does not get it
|
|
|
|
|
let image = get_next_url(&config).await.unwrap();
|
|
|
|
|
assert_eq!(image, None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async fn insert_image(config: &Config, url: &str) -> DynResult<Image> {
|
|
|
|
|
let client = reqwest::Client::builder().user_agent("bot").build().unwrap();
|
|
|
|
|
let client = reqwest::Client::builder()
|
|
|
|
|
.user_agent("bot")
|
|
|
|
|
.build()
|
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
|
|
let auth = &Auth {
|
|
|
|
|
app: config.backend.app.to_string(),
|
|
|
|
|