notedeck

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

note.rs (1629B)


      1 use crate::Damus;
      2 use nostrdb::{NoteKey, QueryResult, Transaction};
      3 use std::cmp::Ordering;
      4 
      5 #[derive(Debug, Eq, PartialEq, Copy, Clone)]
      6 pub struct NoteRef {
      7     pub key: NoteKey,
      8     pub created_at: u64,
      9 }
     10 
     11 impl NoteRef {
     12     pub fn new(key: NoteKey, created_at: u64) -> Self {
     13         NoteRef { key, created_at }
     14     }
     15 
     16     pub fn from_query_result(qr: QueryResult<'_>) -> Self {
     17         NoteRef {
     18             key: qr.note_key,
     19             created_at: qr.note.created_at(),
     20         }
     21     }
     22 }
     23 
     24 impl Ord for NoteRef {
     25     fn cmp(&self, other: &Self) -> Ordering {
     26         match self.created_at.cmp(&other.created_at) {
     27             Ordering::Equal => self.key.cmp(&other.key),
     28             Ordering::Less => Ordering::Greater,
     29             Ordering::Greater => Ordering::Less,
     30         }
     31     }
     32 }
     33 
     34 impl PartialOrd for NoteRef {
     35     fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
     36         Some(self.cmp(other))
     37     }
     38 }
     39 
     40 pub fn root_note_id_from_selected_id<'a>(
     41     app: &mut Damus,
     42     txn: &'a Transaction,
     43     selected_note_id: &'a [u8; 32],
     44 ) -> &'a [u8; 32] {
     45     let selected_note_key = if let Ok(key) = app
     46         .ndb
     47         .get_notekey_by_id(txn, selected_note_id)
     48         .map(NoteKey::new)
     49     {
     50         key
     51     } else {
     52         return selected_note_id;
     53     };
     54 
     55     let note = if let Ok(note) = app.ndb.get_note_by_key(txn, selected_note_key) {
     56         note
     57     } else {
     58         return selected_note_id;
     59     };
     60 
     61     app.note_cache_mut()
     62         .cached_note_or_insert(selected_note_key, &note)
     63         .reply
     64         .borrow(note.tags())
     65         .root()
     66         .map_or_else(|| selected_note_id, |nr| nr.id)
     67 }