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 }}
password: ${{ secrets.DOCKER_PASS }}
- name: Build and push
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6
uses: docker/build-push-action@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
with:
platforms: linux/amd64,linux/arm64
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
[dependencies]
tokio = { version = "=1.43.0", features = ["macros", "rt-multi-thread"] }
reqwest = { version = "=0.12.12", features = ["json", "multipart", "stream"] }
serde = { version = "=1.0.217", features = ["derive"] }
tokio = { version = "=1.42.0", features = ["macros", "rt-multi-thread"] }
reqwest = { version = "=0.11.27", features = ["json"] }
serde = "=1.0.217"
toml = "=0.8.19"
log = "=0.4.25"
log = "=0.4.22"
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
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
WORKDIR /app
COPY Cargo.toml Cargo.toml

View File

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

View File

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

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 std::io::Cursor;
use std::process::exit;
@ -27,7 +29,6 @@ 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>>;
@ -65,7 +66,7 @@ struct Errors {
retry: u8,
}
#[tokio::main]
#[tokio::main] // requires `features = ["mt"]
async fn main() -> DynResult<()> {
stderrlog::new()
.module(module_path!())
@ -74,22 +75,17 @@ 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 creds = get_mastodon_data(&client, &config).await;
let mastodon = get_account(&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(&client, &creds, &config, &image.url, Visibility::public).await
while let Err(err) = post_image(&mastodon, &image.url, &config, Visibility::Public).await
{
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;
if retry >= config.errors.retry {
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?;
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);
std::thread::sleep(Duration::new(1, 0));
async_std::task::sleep(Duration::new(1, 0)).await;
retry += 1;
if retry >= config.errors.retry {
log::error!("Max ammount of retries reached on update bio");
@ -112,19 +108,17 @@ async fn main() -> DynResult<()> {
None => {
let mut retry: u8 = 0;
while let Err(err) = post(
&client,
&creds,
&config,
&mastodon,
&format!(
"{} {}",
&config.errors.maintainers, &config.errors.out_of_images
),
Visibility::direct,
Visibility::Direct,
)
.await
{
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;
if retry >= config.errors.retry {
log::error!("Max ammount of retries reached on post");
@ -136,11 +130,9 @@ async fn main() -> DynResult<()> {
Err(err) => {
log::error!("Cannot get next image: {}", err);
match post(
&client,
&creds,
&config,
&mastodon,
&format!("{} {}", &config.errors.maintainers, &err.to_string()),
Visibility::direct,
Visibility::Direct,
)
.await
{
@ -174,9 +166,24 @@ 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)?;
Ok(toml::from_str(&toml_file)?)
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);
}
}
}
}
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<()> {
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(),
@ -246,37 +250,36 @@ async fn set_url_as_posted(config: &Config, image: &Image) -> DynResult<()> {
}
async fn post_image(
client: &Client,
creds: &MastodonData,
config: &Config,
account: &Mastodon,
url: &String,
config: &Config,
visibility: Visibility,
) -> 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?;
) -> 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
log::info!("Image status posted: {}", url);
Ok(status)
}
async fn update_bio(config: &Config, creds: &MastodonData) -> DynResult<()> {
let images: ImagesWrap =
reqwest::get(format!("{}/images?status=available", config.backend.url))
async fn update_bio(account: &Mastodon, config: &Config) -> 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),
@ -287,7 +290,7 @@ async fn update_bio(config: &Config, creds: &MastodonData) -> DynResult<()> {
"{}/api/v1/accounts/update_credentials",
config.bot.instance
))
.bearer_auth(&creds.token)
.bearer_auth(&account.data.token)
.json(&account_update)
.send()
.await?;
@ -295,20 +298,12 @@ async fn update_bio(config: &Config, creds: &MastodonData) -> DynResult<()> {
Ok(())
}
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?;
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");
log::info!("Text status posted: {}", msg);
Ok(post)
}
@ -321,188 +316,27 @@ async fn fetch_url(url: &String, file_name: &String) -> DynResult<()> {
Ok(())
}
#[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)
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()
.await?;
let mastodon = cli::authenticate(registration).await?;
Ok(client
.post(format!("{}/api/v2/media", &config.bot.instance))
.bearer_auth(&creds.token)
.multipart(image)
.send()
.await?
.json()
.await?)
// Save app data for using on the next run.
masto_toml::to_file(&mastodon.data, "mastodon-data.toml")?;
Ok(mastodon)
}
#[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.png";
const TMPTESTDIR: &str = "/tmp/botimage";
const TEST_URL: &str = "https://2.gravatar.com/avatar/be8eb8426d68e4beb50790647eda6f6b";
#[tokio::test]
@ -515,57 +349,45 @@ 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 creds = get_mastodon_data(&client, &config).await;
let account = get_account(&config).await;
let msg = "Test!".to_string();
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)
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()
.await
.unwrap();
account.delete_status(&status.id).await.unwrap();
response.unwrap();
assert_eq!(response.status(), StatusCode::OK)
}
#[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 creds = get_mastodon_data(&client, &config).await;
let account = get_account(&config).await;
let status = post_image(
&client,
&creds,
&config,
&TEST_URL.to_string(),
Visibility::direct,
)
let status = post_image(&account, &TEST_URL.to_string(), &config, Visibility::Direct)
.await
.unwrap();
let response = get_status(&client, &creds, &config, &status.id)
.await
.unwrap();
delete_status(&client, &creds, &config, &status.id)
.await
.unwrap();
let response = account.get_status(&status.id).await.unwrap();
account.delete_status(&status.id).await.unwrap();
let attachment = &response.media_attachments[0];
let response = client
.get(&attachment.url)
.bearer_auth(&creds.token)
.get(attachment.url.clone().unwrap())
.bearer_auth(&account.data.token)
.send()
.await
.unwrap();
@ -580,8 +402,7 @@ 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()
@ -598,12 +419,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()
@ -612,15 +433,13 @@ 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(),