diff --git a/bot/Cargo.toml b/bot/Cargo.toml index c4abc70..32aefab 100644 --- a/bot/Cargo.toml +++ b/bot/Cargo.toml @@ -15,5 +15,5 @@ anyhow = "1.0.95" serde = { version = "1.0.216", features = ["derive"] } base64 = "0.22.1" serde_json = "1.0.134" -reqwest = { version = "0.12.11", features = ["json"] } +reqwest = { version = "0.12.11", features = ["json", "multipart"] } chrono = "0.4.39" \ No newline at end of file diff --git a/bot/src/main.rs b/bot/src/main.rs index aa37159..74da3dc 100644 --- a/bot/src/main.rs +++ b/bot/src/main.rs @@ -1,8 +1,8 @@ use crate::bluesky::BlueSkyClient; use crate::cli::{CliArgs, Command}; -use crate::mastodon::api::{PartialMediaResponse, PartialPostStatusResponse, PostStatusRequest}; +use crate::mastodon::api::PostStatusRequest; use crate::mastodon::MastodonClient; -use anyhow::{anyhow}; +use anyhow::anyhow; use clap::Parser; use infrastructure::RedisService; use log::{error, info, warn}; diff --git a/bot/src/mastodon.rs b/bot/src/mastodon.rs index 00891e5..c460699 100644 --- a/bot/src/mastodon.rs +++ b/bot/src/mastodon.rs @@ -18,6 +18,7 @@ impl MastodonClient { } } + /// Posts a new status to Mastodon. pub async fn post_status( &mut self, data: T, @@ -25,13 +26,50 @@ impl MastodonClient { where T: Into, { - unimplemented!() + let post_status_request: PostStatusRequest = data.into(); + let json = serde_json::to_string(&post_status_request)?; + + Ok(self + .client + .post("https://mastodon.social/api/v1/statuses") + .header("Content-Type", "application/json") + .header("Authorization", format!("Bearer {}", self.access_token)) + .body(json) + .send() + .await? + .json() + .await?) } + /// Uploads an image to Mastodon. pub async fn upload_media_by_url( &mut self, image_url: &str, ) -> Result { - unimplemented!() + let data: Vec = self + .client + .get(image_url) + .send() + .await? + .bytes() + .await? + .to_vec(); + + let file_part = reqwest::multipart::Part::bytes(data) + .file_name("image.jpg") + .mime_str("image/jpg")?; + + let form = reqwest::multipart::Form::new().part("file", file_part); + + Ok(self + .client + .post("https://mastodon.social/api/v2/media") + .header("Content-Type", "multipart/form-data") + .header("Authorization", format!("Bearer {}", self.access_token)) + .multipart(form) + .send() + .await? + .json() + .await?) } } diff --git a/bot/src/mastodon/api.rs b/bot/src/mastodon/api.rs index 588cdd4..d22946e 100644 --- a/bot/src/mastodon/api.rs +++ b/bot/src/mastodon/api.rs @@ -44,7 +44,6 @@ impl From for PostStatusRequest { // Push the summary if character_budget > 0 { status.push_str(summary.get(0..character_budget).unwrap_or(summary.as_str())); - character_budget -= summary.len() + 2; status.push_str("\n") }