media: render images on posts

Signed-off-by: William Casarin <jb55@jb55.com>
This commit is contained in:
William Casarin
2024-02-11 13:58:38 -08:00
parent 4ae3a701b8
commit 72a53c0192

View File

@@ -428,6 +428,8 @@ impl Damus {
cc.egui_ctx cc.egui_ctx
.set_pixels_per_point(cc.egui_ctx.pixels_per_point() + 0.2); .set_pixels_per_point(cc.egui_ctx.pixels_per_point() + 0.2);
egui_extras::install_image_loaders(&cc.egui_ctx);
let mut config = Config::new(); let mut config = Config::new();
config.set_ingester_threads(2); config.set_ingester_threads(2);
Self { Self {
@@ -605,46 +607,60 @@ fn render_note_contents(
puffin::profile_function!(); puffin::profile_function!();
let blocks = damus.ndb.get_blocks_by_key(txn, note_key)?; let blocks = damus.ndb.get_blocks_by_key(txn, note_key)?;
let mut images: Vec<String> = vec![];
for block in blocks.iter(note) { ui.horizontal_wrapped(|ui| {
match block.blocktype() { ui.spacing_mut().item_spacing.x = 0.0;
BlockType::MentionBech32 => {
ui.colored_label(PURPLE, "@"); for block in blocks.iter(note) {
match block.as_mention().unwrap() { match block.blocktype() {
Mention::Pubkey(npub) => { BlockType::MentionBech32 => {
let profile = damus.ndb.get_profile_by_pubkey(txn, npub.pubkey()).ok(); ui.colored_label(PURPLE, "@");
if let Some(name) = profile.as_ref().and_then(|p| get_profile_name(p)) { match block.as_mention().unwrap() {
ui.colored_label(PURPLE, name); Mention::Pubkey(npub) => {
} else { let profile = damus.ndb.get_profile_by_pubkey(txn, npub.pubkey()).ok();
ui.colored_label(PURPLE, "nostrich"); if let Some(name) = profile.as_ref().and_then(|p| get_profile_name(p)) {
ui.colored_label(PURPLE, name);
} else {
ui.colored_label(PURPLE, "nostrich");
}
}
_ => {
ui.colored_label(PURPLE, block.as_str());
} }
} }
_ => { }
ui.colored_label(PURPLE, block.as_str());
BlockType::Hashtag => {
ui.colored_label(PURPLE, "#");
ui.colored_label(PURPLE, block.as_str());
}
BlockType::Url => {
let url = block.as_str().to_lowercase();
if url.ends_with("png") || url.ends_with("jpg") {
images.push(url);
} else {
ui.add(Hyperlink::from_label_and_url(
RichText::new(block.as_str()).color(PURPLE),
block.as_str(),
));
} }
} }
}
BlockType::Hashtag => { BlockType::Text => {
ui.colored_label(PURPLE, "#"); ui.weak(block.as_str());
ui.colored_label(PURPLE, block.as_str()); }
}
BlockType::Url => { _ => {
ui.add(Hyperlink::from_label_and_url( ui.colored_label(PURPLE, block.as_str());
RichText::new(block.as_str()).color(PURPLE), }
block.as_str(),
));
}
BlockType::Text => {
ui.weak(block.as_str());
}
_ => {
ui.colored_label(PURPLE, block.as_str());
} }
} }
});
for image in images {
ui.image(image);
} }
Ok(()) Ok(())
@@ -672,12 +688,9 @@ fn render_note(ui: &mut egui::Ui, damus: &mut Damus, note_key: NoteKey) -> Resul
ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| { ui.with_layout(egui::Layout::top_down(egui::Align::LEFT), |ui| {
render_username(ui, profile.as_ref().ok(), note.pubkey()); render_username(ui, profile.as_ref().ok(), note.pubkey());
ui.horizontal_wrapped(|ui| { if let Err(_err) = render_note_contents(ui, damus, &txn, &note, note_key) {
ui.spacing_mut().item_spacing.x = 0.0; warn!("could not render note contents for note {:?}", note_key)
if let Err(_err) = render_note_contents(ui, damus, &txn, &note, note_key) { }
warn!("could not render note contents for note {:?}", note_key)
}
});
}) })
}); });
}); });