add twitter support

This commit is contained in:
Cadey Ratio 2020-11-04 10:24:26 -05:00
parent 0aec5a3d98
commit 3926969188
6 changed files with 220 additions and 441 deletions

588
backend/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -9,7 +9,7 @@ edition = "2018"
[dependencies]
chrono = { version = "0.4", features = ["serde"] }
color-eyre = "0.5"
egg-mode = "0.15"
twapi-ureq = "0.1.5"
elefren = "0.22"
kankyo = "0.3"
log = "0.4"

View File

@ -7,7 +7,7 @@ use rocket::{
response::Responder,
Data,
Outcome::*,
Response,
Response, State,
};
use rocket_contrib::json::Json;
use std::io::Read;
@ -30,6 +30,14 @@ pub fn token_info(tok: paseto::Token) -> Json<paseto::Token> {
Json(tok)
}
#[post("/tweet", data = "<body>")]
#[instrument(skip(tw), err)]
pub fn tweet(body: StringBody, tw: State<web::twitter::Client>, tok: paseto::Token) -> Result {
tw.tweet(body.unwrap())?;
Ok(())
}
#[derive(Debug)]
pub struct StringBody(String);

View File

@ -43,9 +43,10 @@ fn main() -> Result<()> {
.attach(MainDatabase::fairing())
.attach(SpaceHelmet::default())
.attach(paseto::ed25519_keypair())
.attach(web::discord_webhook::Client::fairing())
.attach(web::pluralkit::Client::fairing())
.attach(web::switchcounter::Client::fairing())
.attach(web::discord_webhook::Client::fairing())
.attach(web::twitter::Client::fairing())
.mount("/metrics", prometheus)
.mount("/", routes![botinfo])
.mount(
@ -56,7 +57,8 @@ fn main() -> Result<()> {
api::switch::list,
api::switch::switch,
api::get_members,
api::token_info
api::token_info,
api::tweet,
],
)
.launch();

View File

@ -1,6 +1,7 @@
pub mod discord_webhook;
pub mod pluralkit;
pub mod switchcounter;
pub mod twitter;
pub type Result<T = ()> = std::result::Result<T, Error>;

View File

@ -0,0 +1,54 @@
use super::{Error, Result};
use rocket::fairing::AdHoc;
use twapi_ureq::*;
pub struct Client {
consumer_token: String,
consumer_secret: String,
api_key: String,
api_secret: String,
}
impl Client {
pub fn fairing() -> AdHoc {
AdHoc::on_attach("Twitter client", |rocket| {
let cfg = rocket.config();
let table = cfg.get_table("twitter").unwrap();
let consumer_token = table["consumer_token"].as_str().unwrap().to_string();
let consumer_secret = table["consumer_secret"].as_str().unwrap().to_string();
let api_key = table["api_key"].as_str().unwrap().to_string();
let api_secret = table["api_secret"].as_str().unwrap().to_string();
Ok(rocket.manage(Client {
consumer_token: consumer_token,
consumer_secret: consumer_secret,
api_key: api_key,
api_secret: api_secret,
}))
})
}
pub fn tweet(&self, body: String) -> Result<()> {
let url = "https://api.twitter.com/1.1/statuses/update.json";
let form_options = vec![("status", body.as_str())];
let resp = v1::post(
url,
&vec![],
&form_options,
&self.consumer_token,
&self.consumer_secret,
&self.api_key,
&self.api_secret,
);
if resp.ok() {
Ok(())
} else {
Err(match resp.synthetic_error() {
Some(why) => Error::UReq(why.to_string()),
None => Error::HttpStatus(resp.status()),
})
}
}
}