route.rs (4999B)
1 use crate::{ 2 account_manager::AccountManager, 3 column::Columns, 4 draft::Drafts, 5 imgcache::ImageCache, 6 notecache::NoteCache, 7 post_action_executor::PostActionExecutor, 8 thread::Threads, 9 timeline::TimelineId, 10 ui::{ 11 self, 12 note::{post::PostResponse, QuoteRepostView}, 13 }, 14 }; 15 16 use enostr::{NoteId, RelayPool}; 17 use nostrdb::{Ndb, Transaction}; 18 19 #[derive(Debug, Eq, PartialEq, Clone, Copy)] 20 pub enum TimelineRoute { 21 Timeline(TimelineId), 22 Thread(NoteId), 23 Reply(NoteId), 24 Quote(NoteId), 25 } 26 27 pub enum TimelineRouteResponse { 28 Post(PostResponse), 29 } 30 31 impl TimelineRouteResponse { 32 pub fn post(post: PostResponse) -> Self { 33 TimelineRouteResponse::Post(post) 34 } 35 } 36 37 #[allow(clippy::too_many_arguments)] 38 pub fn render_timeline_route( 39 ndb: &Ndb, 40 columns: &mut Columns, 41 pool: &mut RelayPool, 42 drafts: &mut Drafts, 43 img_cache: &mut ImageCache, 44 note_cache: &mut NoteCache, 45 threads: &mut Threads, 46 accounts: &mut AccountManager, 47 route: TimelineRoute, 48 col: usize, 49 show_postbox: bool, 50 textmode: bool, 51 ui: &mut egui::Ui, 52 ) -> Option<TimelineRouteResponse> { 53 match route { 54 TimelineRoute::Timeline(timeline_id) => { 55 if show_postbox { 56 let kp = accounts.selected_or_first_nsec()?; 57 let draft = drafts.compose_mut(); 58 let response = 59 ui::timeline::postbox_view(ndb, kp, draft, img_cache, note_cache, ui); 60 61 if let Some(action) = response.action { 62 PostActionExecutor::execute(kp, &action, pool, draft, |np, seckey| { 63 np.to_note(seckey) 64 }); 65 } 66 } 67 68 if let Some(bar_action) = 69 ui::TimelineView::new(timeline_id, columns, ndb, note_cache, img_cache, textmode) 70 .ui(ui) 71 { 72 let txn = Transaction::new(ndb).expect("txn"); 73 let router = columns.columns_mut()[col].router_mut(); 74 75 bar_action.execute_and_process_result(ndb, router, threads, note_cache, pool, &txn); 76 } 77 78 None 79 } 80 81 TimelineRoute::Thread(id) => { 82 if let Some(bar_action) = 83 ui::ThreadView::new(threads, ndb, note_cache, img_cache, id.bytes(), textmode) 84 .id_source(egui::Id::new(("threadscroll", col))) 85 .ui(ui) 86 { 87 let txn = Transaction::new(ndb).expect("txn"); 88 let router = columns.columns_mut()[col].router_mut(); 89 bar_action.execute_and_process_result(ndb, router, threads, note_cache, pool, &txn); 90 } 91 92 None 93 } 94 95 TimelineRoute::Reply(id) => { 96 let txn = if let Ok(txn) = Transaction::new(ndb) { 97 txn 98 } else { 99 ui.label("Reply to unknown note"); 100 return None; 101 }; 102 103 let note = if let Ok(note) = ndb.get_note_by_id(&txn, id.bytes()) { 104 note 105 } else { 106 ui.label("Reply to unknown note"); 107 return None; 108 }; 109 110 let id = egui::Id::new(("post", col, note.key().unwrap())); 111 let poster = accounts.selected_or_first_nsec()?; 112 let draft = drafts.reply_mut(note.id()); 113 114 let response = egui::ScrollArea::vertical().show(ui, |ui| { 115 ui::PostReplyView::new(ndb, poster, draft, note_cache, img_cache, ¬e) 116 .id_source(id) 117 .show(ui) 118 }); 119 120 if let Some(action) = &response.inner.action { 121 PostActionExecutor::execute(poster, action, pool, draft, |np, seckey| { 122 np.to_reply(seckey, ¬e) 123 }); 124 } 125 126 Some(TimelineRouteResponse::post(response.inner)) 127 } 128 129 TimelineRoute::Quote(id) => { 130 let txn = Transaction::new(ndb).expect("txn"); 131 132 let note = if let Ok(note) = ndb.get_note_by_id(&txn, id.bytes()) { 133 note 134 } else { 135 ui.label("Quote of unknown note"); 136 return None; 137 }; 138 139 let id = egui::Id::new(("post", col, note.key().unwrap())); 140 141 let poster = accounts.selected_or_first_nsec()?; 142 let draft = drafts.quote_mut(note.id()); 143 144 let response = egui::ScrollArea::vertical().show(ui, |ui| { 145 QuoteRepostView::new(ndb, poster, note_cache, img_cache, draft, ¬e) 146 .id_source(id) 147 .show(ui) 148 }); 149 150 if let Some(action) = &response.inner.action { 151 PostActionExecutor::execute(poster, action, pool, draft, |np, seckey| { 152 np.to_quote(seckey, ¬e) 153 }); 154 } 155 Some(TimelineRouteResponse::post(response.inner)) 156 } 157 } 158 }