Reorganize feed module

This commit is contained in:
Mike Dilger 2023-06-16 08:12:05 +12:00
parent 6a19ee8fe9
commit cabd0aa7e5
4 changed files with 114 additions and 108 deletions

View File

@ -6,12 +6,16 @@ use eframe::egui;
use egui::{Context, Frame, RichText, ScrollArea, Ui, Vec2};
use nostr_types::Id;
pub use note::Notes;
mod notedata;
mod notes;
pub use notes::Notes;
mod note;
pub use note::NoteRenderData;
pub(super) mod post;
struct FeedNoteParams {
id: Id,
indent: usize,

View File

@ -1,11 +1,10 @@
mod content;
mod notedata;
pub use notedata::Notes;
pub use super::Notes;
use std::cell::RefCell;
use std::rc::Rc;
use notedata::{NoteData, RepostType};
use super::notedata::{NoteData, RepostType};
use super::FeedNoteParams;
use crate::comms::ToOverlordMessage;

View File

@ -6,7 +6,6 @@ use nostr_types::{
ContentSegment, Event, EventDelegation, EventKind, Id, NostrBech32, PublicKeyHex,
ShatteredContent, Tag,
};
use std::{cell::RefCell, collections::HashMap, rc::Rc};
#[derive(PartialEq)]
pub(super) enum RepostType {
@ -191,106 +190,3 @@ impl NoteData {
self.self_already_reacted = self_already_reacted;
}
}
/// a 'note' is a processed event
pub struct Notes {
notes: HashMap<Id, Rc<RefCell<NoteData>>>,
}
impl Notes {
pub fn new() -> Notes {
Notes {
notes: HashMap::new(),
}
}
/*
/// Drop NoteData objects that do not have a
/// correlated event in the event cache
pub(super) fn cache_invalidate_missing_events(&mut self) {
self.notes.retain(|id,_| GLOBALS.events.contains_key(id));
}
*/
/// Drop NoteData for a specific note
pub(super) fn cache_invalidate_note(&mut self, id: &Id) {
self.notes.remove(id);
}
/// Drop all NoteData for a given person
pub(in crate::ui) fn cache_invalidate_person(&mut self, pubkey: &PublicKeyHex) {
self.notes
.retain(|_, note| note.borrow().author.pubkey != *pubkey);
}
pub(super) fn try_update_and_get(&mut self, id: &Id) -> Option<Rc<RefCell<NoteData>>> {
if self.notes.contains_key(id) {
// get a mutable reference to update reactions, then give it back
if let Some(pair) = self.notes.get(id) {
if let Ok(mut mut_ref) = pair.try_borrow_mut() {
mut_ref.update_reactions();
}
}
// return from cache
return self._try_get_and_borrow(id);
} else {
// otherwise try to create new and add to cache
if let Some(event) = GLOBALS.events.get(id) {
let note = NoteData::new(event);
// add to cache
let ref_note = Rc::new(RefCell::new(note));
self.notes.insert(*id, ref_note);
return self._try_get_and_borrow(id);
} else {
// send a worker to try and load it from the database
// if it's in the db it will go into the cache and be
// available on a future UI update
let id_copy = id.to_owned();
tokio::spawn(async move {
if let Err(e) = GLOBALS.events.get_local(id_copy).await {
tracing::error!("{}", e);
}
});
}
}
None
}
/*
pub(super) fn try_get(&mut self, id: &Id) -> Option<Rc<RefCell<NoteData>>> {
if self.notes.contains_key(id) {
// return from cache
return self._try_get_and_borrow(id)
} else {
// otherwise try to create new and add to cache
if let Some(event) = GLOBALS.events.get(id) {
if let Some(note) = NoteData::new(event) {
// add to cache
let ref_note = Rc::new(RefCell::new(note));
self.notes.insert(*id, ref_note);
return self._try_get_and_borrow(id);
}
} else {
// send a worker to try and load it from the database
// if it's in the db it will go into the cache and be
// available on the next UI update
let id_copy = id.to_owned();
tokio::spawn(async move {
if let Err(e) = GLOBALS.events.get_local(id_copy).await {
tracing::error!("{}", e);
}
});
}
}
None
}
*/
fn _try_get_and_borrow(&self, id: &Id) -> Option<Rc<RefCell<NoteData>>> {
if let Some(value) = self.notes.get(id) {
return Some(value.clone());
}
None
}
}

107
src/ui/feed/notes.rs Normal file
View File

@ -0,0 +1,107 @@
use crate::globals::GLOBALS;
use super::notedata::NoteData;
use nostr_types::{Id, PublicKeyHex};
use std::{cell::RefCell, collections::HashMap, rc::Rc};
/// a 'note' is a processed event
pub struct Notes {
notes: HashMap<Id, Rc<RefCell<NoteData>>>,
}
impl Notes {
pub fn new() -> Notes {
Notes {
notes: HashMap::new(),
}
}
/*
/// Drop NoteData objects that do not have a
/// correlated event in the event cache
pub(super) fn cache_invalidate_missing_events(&mut self) {
self.notes.retain(|id,_| GLOBALS.events.contains_key(id));
}
*/
/// Drop NoteData for a specific note
pub(super) fn cache_invalidate_note(&mut self, id: &Id) {
self.notes.remove(id);
}
/// Drop all NoteData for a given person
pub(in crate::ui) fn cache_invalidate_person(&mut self, pubkey: &PublicKeyHex) {
self.notes
.retain(|_, note| note.borrow().author.pubkey != *pubkey);
}
pub(super) fn try_update_and_get(&mut self, id: &Id) -> Option<Rc<RefCell<NoteData>>> {
if self.notes.contains_key(id) {
// get a mutable reference to update reactions, then give it back
if let Some(pair) = self.notes.get(id) {
if let Ok(mut mut_ref) = pair.try_borrow_mut() {
mut_ref.update_reactions();
}
}
// return from cache
return self._try_get_and_borrow(id);
} else {
// otherwise try to create new and add to cache
if let Some(event) = GLOBALS.events.get(id) {
let note = NoteData::new(event);
// add to cache
let ref_note = Rc::new(RefCell::new(note));
self.notes.insert(*id, ref_note);
return self._try_get_and_borrow(id);
} else {
// send a worker to try and load it from the database
// if it's in the db it will go into the cache and be
// available on a future UI update
let id_copy = id.to_owned();
tokio::spawn(async move {
if let Err(e) = GLOBALS.events.get_local(id_copy).await {
tracing::error!("{}", e);
}
});
}
}
None
}
/*
pub(super) fn try_get(&mut self, id: &Id) -> Option<Rc<RefCell<NoteData>>> {
if self.notes.contains_key(id) {
// return from cache
return self._try_get_and_borrow(id)
} else {
// otherwise try to create new and add to cache
if let Some(event) = GLOBALS.events.get(id) {
if let Some(note) = NoteData::new(event) {
// add to cache
let ref_note = Rc::new(RefCell::new(note));
self.notes.insert(*id, ref_note);
return self._try_get_and_borrow(id);
}
} else {
// send a worker to try and load it from the database
// if it's in the db it will go into the cache and be
// available on the next UI update
let id_copy = id.to_owned();
tokio::spawn(async move {
if let Err(e) = GLOBALS.events.get_local(id_copy).await {
tracing::error!("{}", e);
}
});
}
}
None
}
*/
fn _try_get_and_borrow(&self, id: &Id) -> Option<Rc<RefCell<NoteData>>> {
if let Some(value) = self.notes.get(id) {
return Some(value.clone());
}
None
}
}