Clean up & refactor embed output code
This commit is contained in:
parent
da62c7c2c6
commit
1783ed00e6
1 changed files with 33 additions and 29 deletions
|
@ -127,32 +127,23 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
|
||||||
|
|
||||||
let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36").build().unwrap();
|
let reqwest_client = reqwest::Client::builder().user_agent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/118.0.0.0 Safari/537.36").build().unwrap();
|
||||||
|
|
||||||
for mut url in urls {
|
for url in urls {
|
||||||
if let Ok(req) = reqwest_client.get(url).send().await {
|
if let Ok(req) = reqwest_client.get(url).send().await {
|
||||||
if let Ok(res) = req.text().await {
|
if let Ok(res) = req.text().await {
|
||||||
// beware, dirty HTML parsing code
|
// beware, dirty HTML parsing code
|
||||||
let metadata = parse_metadata(&res);
|
let metadata = parse_metadata(&res);
|
||||||
|
|
||||||
// If we didn't get any metadata set URL to nothing so it won't get repeated
|
// Build and send our message reply
|
||||||
// With no other embed data in the bot's embed message
|
if metadata.is_some() {
|
||||||
if metadata.is_none() {
|
let embed = metadata.unwrap();
|
||||||
url = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
// Build our message reply
|
|
||||||
let embed = metadata
|
|
||||||
.unwrap_or(Embed::new("No metadata found".to_string(), "".to_string()));
|
|
||||||
let bot_reply = RoomMessageEventContent::text_html(
|
let bot_reply = RoomMessageEventContent::text_html(
|
||||||
&embed.title,
|
&embed.title,
|
||||||
format!(
|
format!(
|
||||||
r#"
|
"<blockquote>
|
||||||
<blockquote>
|
<h4>{}</h4>
|
||||||
<h6><a href="{}">{}</a></h6>
|
|
||||||
<h3><strong>{}</strong></h3>
|
|
||||||
<p>{}</p>
|
<p>{}</p>
|
||||||
</blockquote>
|
</blockquote>",
|
||||||
"#,
|
&embed.title, &embed.description
|
||||||
&url, &url, &embed.title, &embed.description
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.make_reply_to(&full_reply_event);
|
.make_reply_to(&full_reply_event);
|
||||||
|
@ -162,11 +153,24 @@ pub async fn embed_handler(event: OriginalSyncRoomMessageEvent, room: Room, clie
|
||||||
if room.send(bot_reply, None).await.is_err() {
|
if room.send(bot_reply, None).await.is_err() {
|
||||||
warn!("Failed to send embed for URL: '{}'", &url);
|
warn!("Failed to send embed for URL: '{}'", &url);
|
||||||
}
|
}
|
||||||
|
// If we didn't get any metadata send a generic "No metadata" response
|
||||||
|
} else {
|
||||||
|
let bot_reply = RoomMessageEventContent::text_html(
|
||||||
|
"Couldn't parse metadata for URL",
|
||||||
|
"<blockquote><h5>Couldn't parse metadata for URL</h5></blockquote>",
|
||||||
|
)
|
||||||
|
.make_reply_to(&full_reply_event);
|
||||||
|
// Send the reply to the room
|
||||||
|
warn!("Sending 'No metadata' embed for URL: '{}'", &url);
|
||||||
|
if room.send(bot_reply, None).await.is_err() {
|
||||||
|
warn!("Failed to send embed for URL: '{}'", &url);
|
||||||
|
}
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
warn!("Failed to parse HTML for URL: '{}'", &url);
|
warn!("Failed to parse HTML for URL: '{}'", &url);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
warn!("Failed to get metadata for '{}'", &url);
|
warn!("Failed to fetch metadata for '{}'", &url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue