notedeck

One damus client to rule them all
git clone git://jb55.com/notedeck
Log | Files | Refs | README | LICENSE

commit e9da25266aea2fd1426b0c145ce8d23a7f3676e4
parent b31bd2470bbecedf48375c7ac98241742fefdb3a
Author: William Casarin <jb55@jb55.com>
Date:   Mon,  1 Jul 2024 18:18:16 -0700

enable nip10 replies

you can now reply to notes

Signed-off-by: William Casarin <jb55@jb55.com>

Diffstat:
MCargo.lock | 2+-
MCargo.toml | 2+-
Msrc/app.rs | 27++++++++++++++++++++++-----
Msrc/post.rs | 74++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--
Msrc/timeline.rs | 3+++
Msrc/ui/note/reply.rs | 47++++++++++++++++++++++++++++++++++++++++-------
6 files changed, 139 insertions(+), 16 deletions(-)

diff --git a/Cargo.lock b/Cargo.lock @@ -1093,7 +1093,7 @@ dependencies = [ [[package]] name = "egui_nav" version = "0.1.0" -source = "git+https://github.com/damus-io/egui-nav?rev=d15bdcedbed93d25f1d45d7b94f35ac25ee0bdc2#d15bdcedbed93d25f1d45d7b94f35ac25ee0bdc2" +source = "git+https://github.com/damus-io/egui-nav?rev=0498cbee12935448478823d855060dc749a0b8b6#0498cbee12935448478823d855060dc749a0b8b6" dependencies = [ "egui", "egui_extras", diff --git a/Cargo.toml b/Cargo.toml @@ -32,7 +32,7 @@ eframe = { version = "0.27.2", default-features = false, features = [ "glow", "w egui_extras = { version = "0.27.2", features = ["all_loaders"] } ehttp = "0.2.0" egui_tabs = { git = "https://github.com/damus-io/egui-tabs", rev = "120971fc43db6ba0b6f194f4bd4a66f7e00a4e22" } -egui_nav = { git = "https://github.com/damus-io/egui-nav", rev = "d15bdcedbed93d25f1d45d7b94f35ac25ee0bdc2" } +egui_nav = { git = "https://github.com/damus-io/egui-nav", rev = "0498cbee12935448478823d855060dc749a0b8b6" } reqwest = { version = "0.12.4", default-features = false, features = [ "rustls-tls-native-roots" ] } image = { version = "0.24", features = ["jpeg", "png", "webp"] } log = "0.4.17" diff --git a/src/app.rs b/src/app.rs @@ -11,6 +11,7 @@ use crate::relay_pool_manager::RelayPoolManager; use crate::route::Route; use crate::timeline; use crate::timeline::{MergeKind, NoteRef, Timeline, ViewFilter}; +use crate::ui::note::PostAction; use crate::ui::{self, AccountSelectionWidget, DesktopGlobalPopup}; use crate::ui::{DesktopSidePanel, RelayView, View}; use crate::Result; @@ -959,29 +960,35 @@ fn render_panel(ctx: &egui::Context, app: &mut Damus, timeline_ind: usize) { fn render_nav(routes: Vec<Route>, timeline_ind: usize, app: &mut Damus, ui: &mut egui::Ui) { let navigating = app.timelines[timeline_ind].navigating; + let returning = app.timelines[timeline_ind].returning; let app_ctx = Rc::new(RefCell::new(app)); let nav_response = Nav::new(routes) .navigating(navigating) + .returning(returning) .title(false) .show(ui, |ui, nav| match nav.top() { Route::Timeline(_n) => { let app = &mut app_ctx.borrow_mut(); timeline::timeline_view(ui, app, timeline_ind); + None } Route::ManageAccount => { ui.label("account management view"); + None } Route::Thread(_key) => { ui.label("thread view"); + None } Route::Relays => { let pool = &mut app_ctx.borrow_mut().pool; let manager = RelayPoolManager::new(pool); RelayView::new(manager).ui(ui); + None } Route::Reply(id) => { @@ -991,27 +998,37 @@ fn render_nav(routes: Vec<Route>, timeline_ind: usize, app: &mut Damus, ui: &mut txn } else { ui.label("Reply to unknown note"); - return; + return None; }; let note = if let Ok(note) = app.ndb.get_note_by_id(&txn, id.bytes()) { note } else { ui.label("Reply to unknown note"); - return; + return None; }; let id = egui::Id::new(("post", timeline_ind, note.key().unwrap())); - egui::ScrollArea::vertical().show(ui, |ui| { + let response = egui::ScrollArea::vertical().show(ui, |ui| { ui::PostReplyView::new(&mut app, &note) .id_source(id) - .show(ui); + .show(ui) }); + + Some(response) } }); + if let Some(reply_response) = nav_response.inner { + if let Some(PostAction::Post(_np)) = reply_response.inner.action { + app_ctx.borrow_mut().timelines[timeline_ind].returning = true; + } + } + if let Some(NavAction::Returned) = nav_response.action { - app_ctx.borrow_mut().timelines[timeline_ind].routes.pop(); + let mut app = app_ctx.borrow_mut(); + app.timelines[timeline_ind].routes.pop(); + app.timelines[timeline_ind].returning = false; } else if let Some(NavAction::Navigated) = nav_response.action { app_ctx.borrow_mut().timelines[timeline_ind].navigating = false; } diff --git a/src/post.rs b/src/post.rs @@ -1,4 +1,5 @@ -use nostrdb::NoteBuilder; +use nostrdb::{Note, NoteBuilder, NoteReply}; +use std::collections::HashSet; pub struct NewPost { pub content: String, @@ -6,7 +7,7 @@ pub struct NewPost { } impl NewPost { - pub fn to_note(&self, seckey: &[u8; 32]) -> nostrdb::Note { + pub fn to_note(&self, seckey: &[u8; 32]) -> Note { NoteBuilder::new() .kind(1) .content(&self.content) @@ -14,4 +15,73 @@ impl NewPost { .build() .expect("note should be ok") } + + pub fn to_reply(&self, seckey: &[u8; 32], replying_to: &Note) -> Note { + let builder = NoteBuilder::new().kind(1).content(&self.content); + + let nip10 = NoteReply::new(replying_to.tags()); + + let mut builder = if let Some(root) = nip10.root() { + builder + .start_tag() + .tag_str("e") + .tag_str(&hex::encode(root.id)) + .tag_str("") + .tag_str("root") + .start_tag() + .tag_str("e") + .tag_str(&hex::encode(replying_to.id())) + .tag_str("") + .tag_str("reply") + .sign(seckey) + } else { + // we're replying to a post that isn't in a thread, + // just add a single reply-to-root tag + builder + .start_tag() + .tag_str("e") + .tag_str(&hex::encode(replying_to.id())) + .tag_str("") + .tag_str("root") + .sign(seckey) + }; + + let mut seen_p: HashSet<&[u8; 32]> = HashSet::new(); + + builder = builder + .start_tag() + .tag_str("p") + .tag_str(&hex::encode(replying_to.pubkey())); + + seen_p.insert(replying_to.pubkey()); + + for tag in replying_to.tags() { + if tag.count() < 2 { + continue; + } + + if tag.get_unchecked(0).variant().str() != Some("p") { + continue; + } + + let id = if let Some(id) = tag.get_unchecked(1).variant().id() { + id + } else { + continue; + }; + + if seen_p.contains(id) { + continue; + } + + seen_p.insert(id); + + builder = builder.start_tag().tag_str("p").tag_str(&hex::encode(id)); + } + + builder + .sign(seckey) + .build() + .expect("expected build to work") + } } diff --git a/src/timeline.rs b/src/timeline.rs @@ -129,6 +129,7 @@ pub struct Timeline { pub selected_view: i32, pub routes: Vec<Route>, pub navigating: bool, + pub returning: bool, /// Our nostrdb subscription pub subscription: Option<Subscription>, @@ -143,9 +144,11 @@ impl Timeline { let selected_view = 0; let routes = vec![Route::Timeline("Timeline".to_string())]; let navigating = false; + let returning = false; Timeline { navigating, + returning, filter, views, subscription, diff --git a/src/ui/note/reply.rs b/src/ui/note/reply.rs @@ -1,5 +1,7 @@ use crate::draft::DraftSource; +use crate::ui::note::{PostAction, PostResponse}; use crate::{ui, Damus}; +use tracing::info; pub struct PostReplyView<'a> { app: &'a mut Damus, @@ -27,7 +29,7 @@ impl<'a> PostReplyView<'a> { .unwrap_or_else(|| egui::Id::new("post-reply-view")) } - pub fn show(&mut self, ui: &mut egui::Ui) { + pub fn show(&mut self, ui: &mut egui::Ui) -> PostResponse { ui.vertical(|ui| { let avail_rect = ui.available_rect_before_wrap(); @@ -49,21 +51,49 @@ impl<'a> PostReplyView<'a> { .show(ui); }); + let id = self.id(); + let replying_to = self.note.id(); + let draft_source = DraftSource::Reply(replying_to); let poster = self .app .account_manager .get_selected_account_index() .unwrap_or(0); - - let replying_to = self.note.pubkey(); let rect_before_post = ui.min_rect(); - - let id = self.id(); - let draft_source = DraftSource::Reply(replying_to); let post_response = ui::PostView::new(self.app, draft_source, poster) .id_source(id) .ui(self.note.txn().unwrap(), ui); + if self + .app + .account_manager + .get_selected_account() + .map_or(false, |a| a.secret_key.is_some()) + { + if let Some(action) = &post_response.action { + match action { + PostAction::Post(np) => { + let seckey = self + .app + .account_manager + .get_account(poster) + .unwrap() + .secret_key + .as_ref() + .unwrap() + .to_secret_bytes(); + + let note = np.to_reply(&seckey, self.note); + + let raw_msg = format!("[\"EVENT\",{}]", note.json().unwrap()); + info!("sending {}", raw_msg); + self.app.pool.send(&enostr::ClientMessage::raw(raw_msg)); + self.app.drafts.clear(DraftSource::Reply(replying_to)); + } + } + } + } + // // reply line // @@ -103,6 +133,9 @@ impl<'a> PostReplyView<'a> { rect.y_range(), ui.visuals().widgets.noninteractive.bg_stroke, ); - }); + + post_response + }) + .inner } }