Compare commits

..

No commits in common. "main" and "v1.0.6" have entirely different histories.
main ... v1.0.6

7 changed files with 1437 additions and 848 deletions

View File

@ -43,7 +43,7 @@ jobs:
username: ${{ secrets.DOCKER_USER }} username: ${{ secrets.DOCKER_USER }}
password: ${{ secrets.DOCKER_PASS }} password: ${{ secrets.DOCKER_PASS }}
- name: Build and push - name: Build and push
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6 uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
with: with:
platforms: linux/amd64,linux/arm64 platforms: linux/amd64,linux/arm64
context: . context: .

1312
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -6,9 +6,14 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
tokio = { version = "=1.43.0", features = ["macros", "rt-multi-thread"] } tokio = { version = "=1.42.0", features = ["macros", "rt-multi-thread"] }
reqwest = { version = "=0.12.12", features = ["json", "multipart", "stream"] } reqwest = { version = "=0.11.27", features = ["json"] }
serde = { version = "=1.0.217", features = ["derive"] } serde = "=1.0.217"
toml = "=0.8.19" toml = "=0.8.19"
log = "=0.4.25" log = "=0.4.22"
stderrlog = "=0.6.0" stderrlog = "=0.6.0"
async-std = "=1.13.0"
[dependencies.mastodon-async]
version = "=1.3.2"
features = ["toml", "mt"]

View File

@ -1,6 +1,6 @@
# syntax=docker/dockerfile:1 # syntax=docker/dockerfile:1
FROM rust:1.84.0-slim-bullseye AS deps FROM rust:1.83.0-slim-bullseye AS deps
RUN apt update && apt install pkg-config ca-certificates openssl libssl-dev -y RUN apt update && apt install pkg-config ca-certificates openssl libssl-dev -y
WORKDIR /app WORKDIR /app
COPY Cargo.toml Cargo.toml COPY Cargo.toml Cargo.toml

View File

@ -1,10 +1,10 @@
db = new Mongo().getDB("bot"); db = new Mongo().getDB("bot");
db.createCollection("authorizations"); db.createCollection('authorizations');
db.authorizations.insert([ db.authorizations.insert([
{ {
app: "tester", app: "tester",
secret: "test", secret: "test",
}, }
]); ]);

View File

@ -1,6 +1,5 @@
{ {
"$schema": "https://docs.renovatebot.com/renovate-schema.json", "$schema": "https://docs.renovatebot.com/renovate-schema.json",
"extends": ["config:recommended", "helpers:pinGitHubActionDigests"], "extends": ["config:recommended", "helpers:pinGitHubActionDigests"],
"rangeStrategy": "pin", "rangeStrategy": "pin"
"automerge": true
} }

View File

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