first real functional version

This commit is contained in:
VC
2023-11-08 15:41:27 +01:00
parent bf70072376
commit b3ba8637f8
5 changed files with 55 additions and 9 deletions

View File

@@ -1,12 +1,31 @@
use crate::config::TwitterConfig;
use oauth1_request::Token;
use reqwest::Client;
use serde::{Deserialize, Serialize};
use std::error::Error;
/// I dont know, dont 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<u64, Box<dyn Error>> {
@@ -14,7 +33,31 @@ pub async fn upload_media(_u: &str) -> Result<u64, Box<dyn Error>> {
}
/// This posts Tweets with all the associated medias
#[allow(dead_code)]
pub async fn post_tweet(_content: &str, _medias: &[u64]) -> Result<u64, Box<dyn Error>> {
pub async fn post_tweet(
config: &TwitterConfig,
content: &str,
_medias: &[u64],
) -> Result<u64, Box<dyn Error>> {
let uri = "https://api.twitter.com/2/tweets";
let token = get_token(config);
let empty_request = EmptyRequest {}; // Why? Because fuck you, thats 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)
}