use crate::config::CONFIG;
use html2md::parse_html;
use megalodon::entities::{attachment::AttachmentType, Status, StatusVisibility};
use nanohtml2text::html2text;
use ureq::json;
pub fn log(message: Status) -> Result<(), &'static str> {
let logger = &CONFIG.get().unwrap().logger;
if logger.stdout.enable {
log::debug!("{:?}", message);
println!("==========");
println!(
"Name: {} ({})",
message.account.display_name, message.account.acct
);
println!("Content:");
println!(
"{}",
message
.plain_content
.unwrap_or_else(|| html2text(&message.content))
);
println!("URL: {}", message.uri);
}
if logger.discord.enable {
let json = if message.visibility == StatusVisibility::Private
|| message.visibility == StatusVisibility::Direct
|| logger.discord.use_embed
{
let mut images = vec![];
for attachment in message
.media_attachments
.iter()
.filter(|x| matches!(x.r#type, AttachmentType::Image))
.take(10)
{
images.push(json!({
"url": attachment.url,
}));
}
let mut embeds = vec![];
embeds.push(json!({
"description": parse_html(&message.content).replace("[https://", "[").replace("\\#", "#").replace("\\_", "_"), "url": message.uri,
"timestamp": message.created_at.to_rfc3339(),
"title": message.uri,
"image": images.get(0).unwrap_or(&json!({})),
}));
for image in images.into_iter().skip(1) {
embeds.push(json!({
"url": message.uri,
"image": image,
}));
}
ureq::json!({
"username": message.account.display_name,
"avatar_url": message.account.avatar,
"embeds": embeds,
})
} else {
ureq::json!({
"content": message.uri,
})
};
if ureq::post(&logger.discord.webhook).send_json(json).is_err() {
return Err("* Something happend executing Webhook.");
}
}
Ok(())
}