make PostActionExecutor for code reuse

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-09-17 13:09:07 -04:00
parent de9e0e4ca1
commit 33dcd8ba66
5 changed files with 69 additions and 40 deletions

View File

@@ -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;

View File

@@ -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);
}
}
}
}

View File

@@ -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

View File

@@ -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();
},
);
}
//

View File

@@ -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();
}
}
},
);
}
}