From 33dcd8ba667430e337644c969261dc1d3b8d44f3 Mon Sep 17 00:00:00 2001 From: kernelkind Date: Tue, 17 Sep 2024 13:09:07 -0400 Subject: [PATCH] make PostActionExecutor for code reuse Signed-off-by: kernelkind --- src/lib.rs | 1 + src/post_action_executor.rs | 29 +++++++++++++++++++++++++++++ src/ui/note/quote_repost.rs | 30 +++++++++++++++--------------- src/ui/note/reply.rs | 26 ++++++++++++-------------- src/ui/timeline.rs | 23 ++++++++++++----------- 5 files changed, 69 insertions(+), 40 deletions(-) create mode 100644 src/post_action_executor.rs diff --git a/src/lib.rs b/src/lib.rs index 2362a9d..22610e4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -25,6 +25,7 @@ mod nav; mod note; mod notecache; mod post; +mod post_action_executor; mod profile; pub mod relay_pool_manager; mod result; diff --git a/src/post_action_executor.rs b/src/post_action_executor.rs new file mode 100644 index 0000000..e26c109 --- /dev/null +++ b/src/post_action_executor.rs @@ -0,0 +1,29 @@ +use enostr::{FilledKeypair, RelayPool}; +use nostrdb::Note; +use tracing::info; + +use crate::{draft::Drafts, post::NewPost, ui::note::PostAction}; + +pub struct PostActionExecutor {} + +impl PostActionExecutor { + pub fn execute<'a>( + poster: &FilledKeypair<'_>, + action: &'a PostAction, + pool: &mut RelayPool, + drafts: &mut Drafts, + get_note: impl Fn(&'a NewPost, &[u8; 32]) -> Note<'a>, + clear_draft: impl Fn(&mut Drafts), + ) { + match action { + PostAction::Post(np) => { + let note = get_note(np, &poster.secret_key.to_secret_bytes()); + + let raw_msg = format!("[\"EVENT\",{}]", note.json().unwrap()); + info!("sending {}", raw_msg); + pool.send(&enostr::ClientMessage::raw(raw_msg)); + clear_draft(drafts); + } + } + } +} diff --git a/src/ui/note/quote_repost.rs b/src/ui/note/quote_repost.rs index 7add1bd..8b483e2 100644 --- a/src/ui/note/quote_repost.rs +++ b/src/ui/note/quote_repost.rs @@ -1,10 +1,12 @@ use enostr::{FilledKeypair, RelayPool}; use nostrdb::Ndb; -use tracing::info; -use crate::{draft::Drafts, imgcache::ImageCache, notecache::NoteCache, ui}; +use crate::{ + draft::Drafts, imgcache::ImageCache, notecache::NoteCache, + post_action_executor::PostActionExecutor, ui, +}; -use super::{PostAction, PostResponse}; +use super::PostResponse; pub struct QuoteRepostView<'a> { ndb: &'a Ndb, @@ -59,18 +61,16 @@ impl<'a> QuoteRepostView<'a> { }; if let Some(action) = &post_response.action { - match action { - PostAction::Post(np) => { - let seckey = self.poster.secret_key.to_secret_bytes(); - - let note = np.to_quote(&seckey, self.quoting_note); - - let raw_msg = format!("[\"EVENT\",{}]", note.json().unwrap()); - info!("sending {}", raw_msg); - self.pool.send(&enostr::ClientMessage::raw(raw_msg)); - self.drafts.quote_mut(quoting_note_id).clear(); - } - } + PostActionExecutor::execute( + &self.poster, + action, + self.pool, + self.drafts, + |np, seckey| np.to_quote(seckey, self.quoting_note), + |drafts| { + drafts.quote_mut(quoting_note_id).clear(); + }, + ); } post_response diff --git a/src/ui/note/reply.rs b/src/ui/note/reply.rs index aec7d44..e50efbc 100644 --- a/src/ui/note/reply.rs +++ b/src/ui/note/reply.rs @@ -1,11 +1,11 @@ use crate::draft::Drafts; use crate::imgcache::ImageCache; use crate::notecache::NoteCache; +use crate::post_action_executor::PostActionExecutor; use crate::ui; -use crate::ui::note::{PostAction, PostResponse}; +use crate::ui::note::PostResponse; use enostr::{FilledKeypair, RelayPool}; use nostrdb::Ndb; -use tracing::info; pub struct PostReplyView<'a> { ndb: &'a Ndb, @@ -93,18 +93,16 @@ impl<'a> PostReplyView<'a> { }; if let Some(action) = &post_response.action { - match action { - PostAction::Post(np) => { - let seckey = self.poster.secret_key.to_secret_bytes(); - - let note = np.to_reply(&seckey, self.note); - - let raw_msg = format!("[\"EVENT\",{}]", note.json().unwrap()); - info!("sending {}", raw_msg); - self.pool.send(&enostr::ClientMessage::raw(raw_msg)); - self.drafts.reply_mut(replying_to).clear(); - } - } + PostActionExecutor::execute( + &self.poster, + action, + self.pool, + self.drafts, + |np, seckey| np.to_reply(seckey, self.note), + |drafts| { + drafts.reply_mut(replying_to).clear(); + }, + ); } // diff --git a/src/ui/timeline.rs b/src/ui/timeline.rs index 81f3aa5..cb17281 100644 --- a/src/ui/timeline.rs +++ b/src/ui/timeline.rs @@ -1,13 +1,14 @@ +use crate::post_action_executor::PostActionExecutor; use crate::{ actionbar::BarAction, column::Columns, draft::Drafts, imgcache::ImageCache, - notecache::NoteCache, timeline::TimelineId, ui, ui::note::PostAction, + notecache::NoteCache, timeline::TimelineId, ui, }; use egui::containers::scroll_area::ScrollBarVisibility; use egui::{Direction, Layout}; use egui_tabs::TabColor; use enostr::{FilledKeypair, RelayPool}; use nostrdb::{Ndb, Transaction}; -use tracing::{debug, error, info, warn}; +use tracing::{debug, error, warn}; pub struct TimelineView<'a> { timeline_id: TimelineId, @@ -189,16 +190,16 @@ pub fn postbox_view<'a>( .ui(&txn, ui); if let Some(action) = response.action { - match action { - PostAction::Post(np) => { - let seckey = key.secret_key.to_secret_bytes(); - let note = np.to_note(&seckey); - let raw_msg = format!("[\"EVENT\",{}]", note.json().unwrap()); - info!("sending {}", raw_msg); - pool.send(&enostr::ClientMessage::raw(raw_msg)); + PostActionExecutor::execute( + &key, + &action, + pool, + drafts, + |np, seckey| np.to_note(seckey), + |drafts| { drafts.compose_mut().clear(); - } - } + }, + ); } }