notedeck

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

unknowns.rs (2007B)


      1 use crate::{column::Columns, Result};
      2 use nostrdb::{Ndb, NoteKey, Transaction};
      3 use notedeck::{CachedNote, NoteCache, UnknownIds};
      4 use tracing::error;
      5 
      6 pub fn update_from_columns(
      7     txn: &Transaction,
      8     unknown_ids: &mut UnknownIds,
      9     columns: &Columns,
     10     ndb: &Ndb,
     11     note_cache: &mut NoteCache,
     12 ) -> bool {
     13     let before = unknown_ids.ids().len();
     14     if let Err(e) = get_unknown_ids(txn, unknown_ids, columns, ndb, note_cache) {
     15         error!("UnknownIds::update {e}");
     16     }
     17     let after = unknown_ids.ids().len();
     18 
     19     if before != after {
     20         unknown_ids.mark_updated();
     21         true
     22     } else {
     23         false
     24     }
     25 }
     26 
     27 pub fn get_unknown_ids(
     28     txn: &Transaction,
     29     unknown_ids: &mut UnknownIds,
     30     columns: &Columns,
     31     ndb: &Ndb,
     32     note_cache: &mut NoteCache,
     33 ) -> Result<()> {
     34     #[cfg(feature = "profiling")]
     35     puffin::profile_function!();
     36 
     37     let mut new_cached_notes: Vec<(NoteKey, CachedNote)> = vec![];
     38 
     39     for timeline in columns.timelines() {
     40         for noteref in timeline.all_or_any_notes() {
     41             let note = ndb.get_note_by_key(txn, noteref.key)?;
     42             let note_key = note.key().unwrap();
     43             let cached_note = note_cache.cached_note(noteref.key);
     44             let cached_note = if let Some(cn) = cached_note {
     45                 cn.clone()
     46             } else {
     47                 let new_cached_note = CachedNote::new(&note);
     48                 new_cached_notes.push((note_key, new_cached_note.clone()));
     49                 new_cached_note
     50             };
     51 
     52             let _ = notedeck::get_unknown_note_ids(
     53                 ndb,
     54                 &cached_note,
     55                 txn,
     56                 &note,
     57                 unknown_ids.ids_mut(),
     58             );
     59         }
     60     }
     61 
     62     // This is mainly done to avoid the double mutable borrow that would happen
     63     // if we tried to update the note_cache mutably in the loop above
     64     for (note_key, note) in new_cached_notes {
     65         note_cache.cache_mut().insert(note_key, note);
     66     }
     67 
     68     Ok(())
     69 }