82 lines
2.2 KiB
Rust
82 lines
2.2 KiB
Rust
use anyhow::Result;
|
|
use chrono::Local;
|
|
use html2md::parse_html;
|
|
use log::debug;
|
|
use megalodon::{
|
|
entities::{Account, Attachment, Status},
|
|
response::Response,
|
|
Megalodon,
|
|
};
|
|
use rustyline::DefaultEditor;
|
|
use url::Url;
|
|
use url_open::UrlOpen;
|
|
|
|
pub(super) async fn format_status(
|
|
client: &Box<dyn Megalodon + Send + Sync>,
|
|
account: &Account,
|
|
status: &Status,
|
|
) -> Result<String> {
|
|
let ancestor = format!(
|
|
"{}
|
|
> {}{}",
|
|
status.created_at.with_timezone(&Local).format("%H:%M"),
|
|
parse_html(&status.content).trim(),
|
|
format_attachments(1, &status.media_attachments)
|
|
);
|
|
let Response { json, .. } = client.get_status_context(status.id.clone(), None).await?;
|
|
let thread = json
|
|
.descendants
|
|
.into_iter()
|
|
.filter(|s| {
|
|
s.in_reply_to_account_id == Some(account.id.clone()) && s.account.id == account.id
|
|
})
|
|
.map(|status| {
|
|
format!(
|
|
">
|
|
> {}
|
|
>> {}{}",
|
|
status.created_at.with_timezone(&Local).format("%H:%M"),
|
|
parse_html(&status.content).trim(),
|
|
format_attachments(2, &status.media_attachments)
|
|
)
|
|
})
|
|
.collect::<Vec<String>>()
|
|
.join("\n");
|
|
Ok(format!(
|
|
"{}{}",
|
|
ancestor,
|
|
if !thread.is_empty() {
|
|
thread
|
|
} else {
|
|
String::default()
|
|
}
|
|
))
|
|
}
|
|
|
|
fn format_attachments(depth: usize, attachments: &[Attachment]) -> String {
|
|
let prefix = vec![">"; depth].join("");
|
|
debug!("Attachments {:?}", attachments);
|
|
if attachments.is_empty() {
|
|
String::default()
|
|
} else {
|
|
let mut editor = DefaultEditor::new().unwrap();
|
|
attachments
|
|
.iter()
|
|
.map(|a| {
|
|
Url::parse(&a.url).unwrap().open();
|
|
let src = if let Ok(line) = editor.readline("Filename: ") {
|
|
line
|
|
} else {
|
|
a.url.clone()
|
|
};
|
|
format!(
|
|
"
|
|
{} <img src=\"{}\" />",
|
|
prefix, src
|
|
)
|
|
})
|
|
.collect::<Vec<String>>()
|
|
.join("\n")
|
|
}
|
|
}
|