Compare commits

..

23 Commits
v1.0.3 ... main

Author SHA1 Message Date
Renovate Bot 926959fa7e chore(deps): update docker/build-push-action digest to ca877d9
Build image / build (pull_request) Successful in 25m17s Details
Unit Tests with docker compose and cargo / unit-test (push) Successful in 1m20s Details
2025-01-24 10:01:17 +00:00
bizcochito 94e3f53d19 Merge pull request 'chore(deps): update docker/build-push-action digest to 67a2d40' (#36) from renovate/docker-build-push-action-digest into main
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m24s Details
Reviewed-on: #36
2025-01-15 19:39:05 +00:00
Renovate Bot e83b20081b chore(deps): update docker/build-push-action digest to 67a2d40
Unit Tests with docker compose and cargo / unit-test (push) Successful in 1m24s Details
Build image / build (pull_request) Successful in 25m25s Details
2025-01-15 13:01:02 +00:00
Renovate Bot 3a7a39d170 fix(deps): update rust crate log to v0.4.25
Unit Tests with docker compose and cargo / unit-test (push) Successful in 1m31s Details
2025-01-14 12:03:01 +00:00
bizcochito 22e2dd92e6 chore: Uptade renovate.json
Unit Tests with docker compose and cargo / unit-test (push) Successful in 1m20s Details
2025-01-12 13:28:20 +00:00
bizcochito cffc5e04e0 Merge pull request 'fix(deps): update rust crate log to v0.4.24' (#34) from renovate/log-0.x into main
Unit Tests with docker compose and cargo / unit-test (push) Successful in 1m14s Details
Reviewed-on: #34
2025-01-11 09:09:54 +00:00
Renovate Bot 1a384cfdfe fix(deps): update rust crate log to v0.4.24
Unit Tests with docker compose and cargo / unit-test (push) Successful in 1m35s Details
2025-01-10 23:02:55 +00:00
bizcochito 27f0b1bfe6 Merge pull request 'chore(deps): update rust docker tag to v1.84.0' (#33) from renovate/rust-1.x into main
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m30s Details
Reviewed-on: #33
2025-01-10 06:45:31 +00:00
Renovate Bot a2aa2654ff chore(deps): update rust docker tag to v1.84.0
Unit Tests with docker compose and cargo / unit-test (push) Successful in 1m40s Details
2025-01-10 01:01:20 +00:00
bizcochito d50c1d7e61 Merge pull request 'chore(deps): update docker/build-push-action digest to b32b51a' (#31) from renovate/docker-build-push-action-digest into main
Unit Tests with docker compose and cargo / unit-test (push) Successful in 1m26s Details
Reviewed-on: #31
2025-01-09 17:57:52 +00:00
bizcochito 9aa7be030a Merge pull request 'fix(deps): update rust crate tokio to v1.43.0' (#32) from renovate/tokio-1.x into main
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m7s Details
Reviewed-on: #32
2025-01-09 16:40:25 +00:00
Renovate Bot f4e463abc1 fix(deps): update rust crate tokio to v1.43.0
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m14s Details
2025-01-08 16:03:26 +00:00
Renovate Bot 5d91a9979e chore(deps): update docker/build-push-action digest to b32b51a
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m42s Details
Build image / build (pull_request) Successful in 23m49s Details
2025-01-08 10:01:37 +00:00
Alie 9b9ddc1d42 cleanup comments
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m19s Details
2025-01-07 17:16:50 +01:00
Alie e9d332487d 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
2025-01-07 16:55:45 +01:00
Alie 64a5ebca2f refactor: deprecated mastodon-async and only make requests with reqwest 2025-01-07 16:06:24 +01:00
Alie 079ad337ca fmt
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m52s Details
Build image / build (push) Successful in 41m5s Details
2025-01-07 11:31:52 +01:00
Alie 34d902cfc8 fmt 2025-01-07 11:28:56 +01:00
Alie 467027fffe fix: remove uneeded dependency 2025-01-07 11:28:49 +01:00
Sugui 197743889d fix: changed visibility to public
Unit Tests with docker compose and cargo / unit-test (push) Successful in 2m51s Details
Build image / build (push) Successful in 43m17s Details
2025-01-07 00:05:12 +01:00
Sugui 6d7d04c7e8 fix: added ca-certificates on Dockerfile layer
Unit Tests with docker compose and cargo / unit-test (push) Has been cancelled Details
Build image / build (push) Successful in 44m33s Details
2025-01-06 22:28:39 +01:00
Sugui 4f2a74e8d2 fix: added user agent for requests
Unit Tests with docker compose and cargo / unit-test (push) Failing after 1m24s Details
Build image / build (push) Successful in 44m7s Details
2025-01-06 20:32:53 +01:00
Alie eefa180a28 change instance and maintainers
Unit Tests with docker compose and cargo / unit-test (push) Failing after 3m19s Details
2025-01-06 20:14:38 +01:00
9 changed files with 671 additions and 1605 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@48aba3b46d1b1fec4febb7c5d0c644b249a11355 # v6
uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6
with:
platforms: linux/amd64,linux/arm64
context: .

View File

@ -1 +0,0 @@
rust 1.83.0

1296
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -1,7 +1,7 @@
# syntax=docker/dockerfile:1
FROM rust:1.83.0-slim-bullseye AS deps
RUN apt update && apt install pkg-config openssl libssl-dev -y
FROM rust:1.84.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
COPY Cargo.lock Cargo.lock
@ -24,7 +24,7 @@ EOF
FROM debian:bullseye-slim AS final
WORKDIR /app
RUN apt update && apt install pkg-config openssl libssl-dev -y
RUN apt update && apt install pkg-config ca-certificates openssl libssl-dev -y
ARG UID=10001
RUN adduser \

View File

@ -1,6 +1,6 @@
[bot]
name = "dev-sleeping-girls-bot"
instance = "https://fedi.absturztau.be" # Changed for testing purposes
instance = "https://social.siesta.cat" # Changed for testing purposes
bio = "Bot who posts images of sleeping girls every 6 hours."
[files]
@ -12,6 +12,6 @@ app = "tester"
secret = "test"
[errors]
maintainers = "@Sugui@awoo.fai.st @MeDueleLaTeta@awoo.fai.st"
maintainers = "@Sugui@owo.cafe @MeDueleLaTeta@owo.cafe"
out_of_images = "me quedé sin chicas"
retry = 10

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

View File

@ -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::from(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::new();
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::new();
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(&config.bot.instance)
.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::new();
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::new();
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::new();
let client = reqwest::Client::builder()
.user_agent("bot")
.build()
.unwrap();
let auth = &Auth {
app: config.backend.app.to_string(),