Increase compatibility with broken clients (e.g. gitea)

This commit is contained in:
lemonsh 2022-07-23 23:55:18 +02:00
parent 8c898caabd
commit ba1165bd1f
2 changed files with 32 additions and 22 deletions

View file

@ -49,6 +49,7 @@ where
};
let body_bytes = to_bytes(req.into_body()).await?;
let body = String::from_utf8_lossy(&body_bytes);
tracing::debug!("received request: {}", body);
let response = parser::textify(&body, &webhook)?;
(ctx.sendmsg)(channel.to_string(), response)?;
let resp = Response::builder()

View file

@ -6,7 +6,7 @@ use std::fmt::Write;
struct WebhookData {
content: Option<String>,
username: Option<String>,
embeds: Vec<Embed>
embeds: Vec<Embed>,
}
#[derive(Deserialize)]
@ -20,33 +20,36 @@ struct Embed {
thumbnail: Option<UrlObject>,
video: Option<UrlObject>,
author: Option<EmbedAuthor>,
fields: Vec<EmbedField>,
fields: Option<Vec<EmbedField>>,
}
#[derive(Deserialize)]
struct UrlObject {
url: String
url: String,
}
#[derive(Deserialize)]
struct EmbedAuthor {
name: String
name: String,
}
#[derive(Deserialize)]
struct EmbedFooter {
text: String
text: String,
}
#[derive(Deserialize)]
struct EmbedField {
name: String,
value: String
value: String,
}
pub fn textify(json: &str, webhook_name: &str) -> anyhow::Result<String> {
let wh: WebhookData = serde_json::from_str(json)?;
let mut buf = format!("-- [Webhook: {}]\r\n", wh.username.as_deref().unwrap_or(webhook_name));
let mut buf = format!(
"-- [Webhook: {}]\r\n",
wh.username.as_deref().unwrap_or(webhook_name)
);
if let Some(content) = wh.content {
let content = content.trim().truncate_ellipse(450);
@ -55,43 +58,49 @@ pub fn textify(json: &str, webhook_name: &str) -> anyhow::Result<String> {
}
}
for embed in wh.embeds {
write!(&mut buf, "-> {}\r\n", embed.title.as_deref().unwrap_or("Embed"))?;
if let Some(description) = embed.description {
write!(
&mut buf,
"-> {}\r\n",
embed.title.as_deref().unwrap_or("Embed")
)?;
if let Some(description) = embed.description.filter(|v| !v.is_empty()) {
let description = description.trim().truncate_ellipse(450);
for line in description.lines() {
write!(&mut buf, " {}\r\n", line)?;
}
}
for field in embed.fields {
write!(&mut buf, " + {}\r\n", field.name)?;
let value = field.value.trim().truncate_ellipse(450);
for line in value.lines() {
write!(&mut buf, " {}\r\n", line)?;
if let Some(fields) = embed.fields {
for field in fields {
write!(&mut buf, " + {}\r\n", field.name)?;
let value = field.value.trim().truncate_ellipse(450);
for line in value.lines() {
write!(&mut buf, " {}\r\n", line)?;
}
}
}
if let Some(url) = embed.url {
if let Some(url) = embed.url.filter(|v| !v.is_empty()) {
write!(&mut buf, " url: {}\r\n", url)?;
}
if let Some(image) = embed.image {
if let Some(image) = embed.image.filter(|v| !v.url.is_empty()) {
write!(&mut buf, " img: {}\r\n", image.url)?;
}
if let Some(thumbnail) = embed.thumbnail {
if let Some(thumbnail) = embed.thumbnail.filter(|v| !v.url.is_empty()) {
write!(&mut buf, " thumb: {}\r\n", thumbnail.url)?;
}
if let Some(video) = embed.video {
if let Some(video) = embed.video.filter(|v| !v.url.is_empty()) {
write!(&mut buf, " vid: {}\r\n", video.url)?;
}
if let Some(author) = embed.author {
if let Some(author) = embed.author.filter(|v| !v.name.is_empty()) {
write!(&mut buf, " by: {}\r\n", author.name)?;
}
if let Some(footer) = embed.footer {
if let Some(footer) = embed.footer.filter(|v| !v.text.is_empty()) {
write!(&mut buf, " - {}\r\n", footer.text)?;
}
if let Some(timestamp) = embed.timestamp {
if let Some(timestamp) = embed.timestamp.filter(|v| !v.is_empty()) {
write!(&mut buf, " - {}\r\n", timestamp)?;
}
}
buf.push_str("-- end of webhook");
Ok(buf)
}
}