notedeck

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

actionbar.rs (3598B)


      1 use crate::{
      2     column::Columns,
      3     route::{Route, Router},
      4     timeline::{TimelineCache, TimelineKind},
      5 };
      6 
      7 use enostr::{NoteId, RelayPool};
      8 use nostrdb::{Ndb, NoteKey, Transaction};
      9 use notedeck::{NoteCache, UnknownIds};
     10 use tracing::error;
     11 
     12 #[derive(Debug, Eq, PartialEq, Clone)]
     13 pub enum NoteAction {
     14     Reply(NoteId),
     15     Quote(NoteId),
     16     OpenTimeline(TimelineKind),
     17 }
     18 
     19 pub struct NewNotes {
     20     pub id: TimelineKind,
     21     pub notes: Vec<NoteKey>,
     22 }
     23 
     24 pub enum TimelineOpenResult {
     25     NewNotes(NewNotes),
     26 }
     27 
     28 impl NoteAction {
     29     #[allow(clippy::too_many_arguments)]
     30     pub fn execute(
     31         &self,
     32         ndb: &Ndb,
     33         router: &mut Router<Route>,
     34         timeline_cache: &mut TimelineCache,
     35         note_cache: &mut NoteCache,
     36         pool: &mut RelayPool,
     37         txn: &Transaction,
     38     ) -> Option<TimelineOpenResult> {
     39         match self {
     40             NoteAction::Reply(note_id) => {
     41                 router.route_to(Route::reply(*note_id));
     42                 None
     43             }
     44 
     45             NoteAction::OpenTimeline(kind) => {
     46                 router.route_to(Route::Timeline(kind.to_owned()));
     47                 timeline_cache.open(ndb, note_cache, txn, pool, kind)
     48             }
     49 
     50             NoteAction::Quote(note_id) => {
     51                 router.route_to(Route::quote(*note_id));
     52                 None
     53             }
     54         }
     55     }
     56 
     57     /// Execute the NoteAction and process the TimelineOpenResult
     58     #[allow(clippy::too_many_arguments)]
     59     pub fn execute_and_process_result(
     60         &self,
     61         ndb: &Ndb,
     62         columns: &mut Columns,
     63         col: usize,
     64         timeline_cache: &mut TimelineCache,
     65         note_cache: &mut NoteCache,
     66         pool: &mut RelayPool,
     67         txn: &Transaction,
     68         unknown_ids: &mut UnknownIds,
     69     ) {
     70         let router = columns.column_mut(col).router_mut();
     71         if let Some(br) = self.execute(ndb, router, timeline_cache, note_cache, pool, txn) {
     72             br.process(ndb, note_cache, txn, timeline_cache, unknown_ids);
     73         }
     74     }
     75 }
     76 
     77 impl TimelineOpenResult {
     78     pub fn new_notes(notes: Vec<NoteKey>, id: TimelineKind) -> Self {
     79         Self::NewNotes(NewNotes::new(notes, id))
     80     }
     81 
     82     pub fn process(
     83         &self,
     84         ndb: &Ndb,
     85         note_cache: &mut NoteCache,
     86         txn: &Transaction,
     87         storage: &mut TimelineCache,
     88         unknown_ids: &mut UnknownIds,
     89     ) {
     90         match self {
     91             // update the thread for next render if we have new notes
     92             TimelineOpenResult::NewNotes(new_notes) => {
     93                 new_notes.process(storage, ndb, txn, unknown_ids, note_cache);
     94             }
     95         }
     96     }
     97 }
     98 
     99 impl NewNotes {
    100     pub fn new(notes: Vec<NoteKey>, id: TimelineKind) -> Self {
    101         NewNotes { notes, id }
    102     }
    103 
    104     /// Simple helper for processing a NewThreadNotes result. It simply
    105     /// inserts/merges the notes into the corresponding timeline cache
    106     pub fn process(
    107         &self,
    108         timeline_cache: &mut TimelineCache,
    109         ndb: &Ndb,
    110         txn: &Transaction,
    111         unknown_ids: &mut UnknownIds,
    112         note_cache: &mut NoteCache,
    113     ) {
    114         let reversed = matches!(&self.id, TimelineKind::Thread(_));
    115 
    116         let timeline = if let Some(profile) = timeline_cache.timelines.get_mut(&self.id) {
    117             profile
    118         } else {
    119             error!("NewNotes: could not get timeline for key {}", self.id);
    120             return;
    121         };
    122 
    123         if let Err(err) = timeline.insert(&self.notes, ndb, txn, unknown_ids, note_cache, reversed)
    124         {
    125             error!("error inserting notes into profile timeline: {err}")
    126         }
    127     }
    128 }