add simple mastodon client

This commit is contained in:
Cadey Ratio 2020-11-04 10:54:21 -05:00
parent 073b39520f
commit 79eb929f26
6 changed files with 79 additions and 898 deletions

910
backend/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,7 +10,6 @@ edition = "2018"
chrono = { version = "0.4", features = ["serde"] }
color-eyre = "0.5"
twapi-ureq = "0.1.5"
elefren = "0.22"
kankyo = "0.3"
log = "0.4"
mime = "0.3.0"

View File

@ -38,6 +38,14 @@ pub fn tweet(body: StringBody, tw: State<web::twitter::Client>, tok: paseto::Tok
Ok(())
}
#[post("/toot", data = "<body>")]
#[instrument(skip(ma), err)]
pub fn toot(body: StringBody, ma: State<web::mastodon::Client>, tok: paseto::Token) -> Result {
ma.toot(body.unwrap())?;
Ok(())
}
#[derive(Debug)]
pub struct StringBody(String);

View File

@ -44,6 +44,7 @@ fn main() -> Result<()> {
.attach(SpaceHelmet::default())
.attach(paseto::ed25519_keypair())
.attach(web::discord_webhook::Client::fairing())
.attach(web::mastodon::Client::fairing())
.attach(web::pluralkit::Client::fairing())
.attach(web::switchcounter::Client::fairing())
.attach(web::twitter::Client::fairing())
@ -59,6 +60,7 @@ fn main() -> Result<()> {
api::get_members,
api::token_info,
api::tweet,
api::toot,
],
)
.launch();

View File

@ -0,0 +1,55 @@
use super::{Error, Result};
use rocket::fairing::AdHoc;
pub struct Client {
instance_url: String,
app_id: String,
app_secret: String,
token: String,
account_name: String,
}
impl Client {
pub fn fairing() -> AdHoc {
AdHoc::on_attach("Mastodon client", |rocket| {
let cfg = rocket.config();
let table = cfg.get_table("mastodon").unwrap();
let instance_url = table["instance"].as_str().unwrap().to_string();
let app_id = table["app_id"].as_str().unwrap().to_string();
let app_secret = table["app_secret"].as_str().unwrap().to_string();
let token = table["token"].as_str().unwrap().to_string();
let account_name = table["account"].as_str().unwrap().to_string();
let cli = Client {
instance_url: instance_url,
app_id: app_id,
app_secret: app_secret,
token: token,
account_name: account_name,
};
Ok(rocket.manage(cli))
})
}
pub fn account_name(&self) -> String {
self.account_name.clone()
}
pub fn toot(&self, body: String) -> Result {
let url = &format!("{}/api/v1/statuses", self.instance_url);
let resp = ureq::post(url)
.set("Authorization", &format!("bearer {}", self.token))
.send_form(&[("status", body.as_str())]);
if resp.ok() {
Ok(())
} else {
Err(match resp.synthetic_error() {
Some(why) => Error::UReq(why.to_string()),
None => Error::HttpStatus(resp.status()),
})
}
}
}

View File

@ -1,4 +1,5 @@
pub mod discord_webhook;
pub mod mastodon;
pub mod pluralkit;
pub mod switchcounter;
pub mod twitter;