Handle image attachments, refactor more
This commit is contained in:
parent
01335b6929
commit
c90495a985
6 changed files with 334 additions and 111 deletions
81
src/format.rs
Normal file
81
src/format.rs
Normal file
|
@ -0,0 +1,81 @@
|
|||
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")
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue