This commit is contained in:
Astro 2022-12-25 04:02:37 +01:00
parent a1d8227eb0
commit dd31b877a2
2 changed files with 15 additions and 18 deletions

View File

@ -9,19 +9,16 @@ use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
mod stream; mod stream;
fn mangle_account(post: &mut serde_json::Value) { fn mangle_account(post: &mut serde_json::Value) {
match post { if let serde_json::Value::Object(ref mut obj) = post {
serde_json::Value::Object(ref mut obj) => { if let Entry::Occupied(mut account) = obj.entry("account") {
if let Entry::Occupied(mut account) = obj.entry("account") { let id = account.get_mut().get_mut("acct").map(serde_json::Value::take);
let id = account.get_mut().get_mut("acct").map(|id| id.take()); match id {
match id { Some(id) =>
Some(id) => account.insert(id),
account.insert(id), None =>
None => account.remove(),
account.remove(), };
};
}
} }
_ => {}
} }
} }
@ -43,7 +40,7 @@ async fn main() {
let default_debug = "buzz2elastic=info".into(); let default_debug = "buzz2elastic=info".into();
tracing_subscriber::registry() tracing_subscriber::registry()
.with( .with(
tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or_else(|_| default_debug), tracing_subscriber::EnvFilter::try_from_default_env().unwrap_or(default_debug),
) )
.with(tracing_subscriber::fmt::layer()) .with(tracing_subscriber::fmt::layer())
.init(); .init();

View File

@ -7,27 +7,27 @@ use tokio::{
}; };
#[derive(Debug)] #[derive(Debug)]
pub enum StreamError { pub enum Error {
Http(reqwest::Error), Http(reqwest::Error),
HttpStatus(reqwest::StatusCode), HttpStatus(reqwest::StatusCode),
InvalidContentType, InvalidContentType,
} }
async fn run(host: &str) -> Result<impl Stream<Item = String>, StreamError> { async fn run(host: &str) -> Result<impl Stream<Item = String>, Error> {
let url = format!("https://{}/api/v1/streaming/public", host); let url = format!("https://{}/api/v1/streaming/public", host);
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let res = client.get(url) let res = client.get(url)
.timeout(Duration::MAX) .timeout(Duration::MAX)
.send() .send()
.await .await
.map_err(StreamError::Http)?; .map_err(Error::Http)?;
if res.status() != 200 { if res.status() != 200 {
return Err(StreamError::HttpStatus(res.status())); return Err(Error::HttpStatus(res.status()));
} }
let ct = res.headers().get("content-type") let ct = res.headers().get("content-type")
.and_then(|c| c.to_str().ok()); .and_then(|c| c.to_str().ok());
if ct.map_or(true, |ct| ct != "text/event-stream") { if ct.map_or(true, |ct| ct != "text/event-stream") {
return Err(StreamError::InvalidContentType); return Err(Error::InvalidContentType);
} }
let src = res.bytes_stream() let src = res.bytes_stream()