diff --git a/Cargo.lock b/Cargo.lock index 30a7cc7..e6decbc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1855,9 +1855,9 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" [[package]] name = "subtle" -version = "2.5.0" +version = "2.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" +checksum = "6bdef32e8150c2a081110b42772ffe7d7c9032b606bc226c8260fd97e0976601" [[package]] name = "syn" diff --git a/Cargo.toml b/Cargo.toml index 0d8b05e..71e8782 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,7 +13,7 @@ log = "^0.4" megalodon = "^0.11" oauth1-request = "^0.6" regex = "1.10.2" -reqwest = "^0.11" +reqwest = { version = "0.11.22", features = ["json"] } rusqlite = "^0.27" serde = { version = "^1.0", features = ["derive"] } tokio = { version = "^1.33", features = ["rt-multi-thread", "macros"] } diff --git a/src/lib.rs b/src/lib.rs index f003d2f..77efeef 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -38,7 +38,7 @@ pub async fn run(config: &Config) { }; // if we wanted to cut toot in half, now would be the right time to do so // treating medias (nothing for now) - let _tweet_id = post_tweet(&tweet_content, &[]) + let _tweet_id = post_tweet(&config.twitter, &tweet_content, &[]) .await .unwrap_or_else(|e| panic!("Cannot Tweet {}: {}", toot.id, e)); return; diff --git a/src/state.rs b/src/state.rs index 5339d90..5deabfc 100644 --- a/src/state.rs +++ b/src/state.rs @@ -15,7 +15,7 @@ pub fn read_state( conn: &Connection, s: Option, ) -> Result, Box> { - debug!("Reading tweet_id {:?}", s); + debug!("Reading toot_id {:?}", s); let query: String = match s { Some(i) => format!("SELECT * FROM tweet_to_toot WHERE toot_id = {i}"), None => "SELECT * FROM tweet_to_toot ORDER BY toot_id DESC LIMIT 1".to_string(), @@ -49,7 +49,10 @@ pub fn write_state(conn: &Connection, t: TweetToToot) -> Result<(), Box Result<(), Box> { - debug!("Initializing DB for Scootaloo"); + debug!( + "{}", + format!("Initializing DB for {}", env!("CARGO_PKG_NAME")) + ); let conn = Connection::open(d)?; conn.execute( diff --git a/src/twitter.rs b/src/twitter.rs index 1670426..8d1901c 100644 --- a/src/twitter.rs +++ b/src/twitter.rs @@ -1,12 +1,31 @@ +use crate::config::TwitterConfig; +use oauth1_request::Token; +use reqwest::Client; use serde::{Deserialize, Serialize}; use std::error::Error; +/// I don’t know, don’t ask me +#[derive(oauth1_request::Request)] +struct EmptyRequest {} + #[derive(Serialize, Debug)] -pub struct Tweet {} +pub struct Tweet { + pub text: String, +} #[derive(Deserialize, Debug)] pub struct TweetResponse {} +/// This function returns the OAuth1 Token object from TwitterConfig +fn get_token(config: &TwitterConfig) -> Token { + oauth1_request::Token::from_parts( + config.consumer_key.to_string(), + config.consumer_secret.to_string(), + config.oauth_token.to_string(), + config.oauth_token_secret.to_string(), + ) +} + /// This function uploads media from Mastodon to Twitter and returns the media id from Twitter #[allow(dead_code)] pub async fn upload_media(_u: &str) -> Result> { @@ -14,7 +33,31 @@ pub async fn upload_media(_u: &str) -> Result> { } /// This posts Tweets with all the associated medias -#[allow(dead_code)] -pub async fn post_tweet(_content: &str, _medias: &[u64]) -> Result> { +pub async fn post_tweet( + config: &TwitterConfig, + content: &str, + _medias: &[u64], +) -> Result> { + let uri = "https://api.twitter.com/2/tweets"; + let token = get_token(config); + let empty_request = EmptyRequest {}; // Why? Because fuck you, that’s why! + let tweet = Tweet { + text: content.to_string(), + }; + + let client = Client::new(); + + let res = client + .post(uri) + .header( + "Authorization", + oauth1_request::post(uri, &empty_request, &token, oauth1_request::HMAC_SHA1), + ) + .json(&tweet) + .send() + .await?; + + println!("{:?}", res.text().await.unwrap()); + Ok(0) }