Skip to content

Commit

Permalink
refactor wip
Browse files Browse the repository at this point in the history
Signed-off-by: William Casarin <[email protected]>
  • Loading branch information
jb55 committed Oct 31, 2024
1 parent 9975978 commit 9f98a59
Show file tree
Hide file tree
Showing 19 changed files with 715 additions and 752 deletions.
1 change: 1 addition & 0 deletions enostr/src/relay/pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ impl RelayPool {
}

pub fn unsubscribe(&mut self, subid: String) {
// TODO(jb55): switch to &str
for relay in &mut self.relays {
relay.relay.send(&ClientMessage::close(subid.clone()));
}
Expand Down
77 changes: 47 additions & 30 deletions src/actionbar.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use crate::{
note::NoteRef,
note::{NoteRef, RootNoteId},
notecache::NoteCache,
notes_holder::{NotesHolder, NotesHolderStorage},
route::{Route, Router},
thread::Thread,
timeline::{CachedTimeline, TimelineCache, TimelineCacheKey},
};
use enostr::{NoteId, Pubkey, RelayPool};
use nostrdb::{Ndb, Transaction};
Expand All @@ -22,11 +21,11 @@ pub struct NoteActionResponse {
}

pub struct NewNotes {
pub id: [u8; 32],
pub id: TimelineCacheKey,
pub notes: Vec<NoteRef>,
}

pub enum NotesHolderResult {
pub enum BarResult {
NewNotes(NewNotes),
}

Expand All @@ -41,13 +40,22 @@ fn open_thread(
router: &mut Router<Route>,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
threads: &mut NotesHolderStorage<Thread>,
timeline_cache: &mut TimelineCache,
selected_note: &[u8; 32],
) -> Option<NotesHolderResult> {
router.route_to(Route::thread(NoteId::new(selected_note.to_owned())));
) -> Option<BarResult> {
let root_id_raw =
crate::note::root_note_id_from_selected_id(ndb, note_cache, txn, selected_note);
let root_id = RootNoteId::new_unsafe(root_id_raw);

let root_id = crate::note::root_note_id_from_selected_id(ndb, note_cache, txn, selected_note);
Thread::open(ndb, note_cache, txn, pool, threads, root_id)
router.route_to(Route::thread(root_id.clone()));

timeline_cache.open(
ndb,
note_cache,
txn,
pool,
&TimelineCacheKey::thread(root_id),
)
}

impl BarAction {
Expand All @@ -56,21 +64,27 @@ impl BarAction {
self,
ndb: &Ndb,
router: &mut Router<Route>,
threads: &mut NotesHolderStorage<Thread>,
timeline_cache: &mut TimelineCache,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
txn: &Transaction,
) -> Option<NotesHolderResult> {
) -> Option<BarResult> {
match self {
BarAction::Reply(note_id) => {
router.route_to(Route::reply(note_id));
router.navigating = true;
None
}

BarAction::OpenThread(note_id) => {
open_thread(ndb, txn, router, note_cache, pool, threads, note_id.bytes())
}
BarAction::OpenThread(note_id) => open_thread(
ndb,
txn,
router,
note_cache,
pool,
timeline_cache,
note_id.bytes(),
),

BarAction::Quote(note_id) => {
router.route_to(Route::quote(note_id));
Expand All @@ -85,51 +99,54 @@ impl BarAction {
self,
ndb: &Ndb,
router: &mut Router<Route>,
threads: &mut NotesHolderStorage<Thread>,
timeline_cache: &mut TimelineCache,
note_cache: &mut NoteCache,
pool: &mut RelayPool,
txn: &Transaction,
) {
if let Some(br) = self.execute(ndb, router, threads, note_cache, pool, txn) {
br.process(ndb, note_cache, txn, threads);
if let Some(br) = self.execute(ndb, router, timeline_cache, note_cache, pool, txn) {
br.process(ndb, note_cache, txn, timeline_cache);
}
}
}

impl NotesHolderResult {
pub fn new_notes(notes: Vec<NoteRef>, id: [u8; 32]) -> Self {
NotesHolderResult::NewNotes(NewNotes::new(notes, id))
impl BarResult {
pub fn new_notes(notes: Vec<NoteRef>, id: TimelineCacheKey) -> Self {
Self::NewNotes(NewNotes::new(notes, id))
}

pub fn process<N: NotesHolder>(
pub fn process(
&self,
ndb: &Ndb,
note_cache: &mut NoteCache,
txn: &Transaction,
storage: &mut NotesHolderStorage<N>,
timeline_cache: &mut TimelineCache,
) {
match self {
// update the thread for next render if we have new notes
NotesHolderResult::NewNotes(new_notes) => {
let holder = storage
.notes_holder_mutated(ndb, note_cache, txn, &new_notes.id)
Self::NewNotes(new_notes) => {
let notes = timeline_cache
.notes(ndb, note_cache, txn, &new_notes.id)
.get_ptr();
new_notes.process(holder);
new_notes.process(notes);
}
}
}
}

impl NewNotes {
pub fn new(notes: Vec<NoteRef>, id: [u8; 32]) -> Self {
pub fn new(notes: Vec<NoteRef>, id: TimelineCacheKey) -> Self {
NewNotes { notes, id }
}

/// Simple helper for processing a NewThreadNotes result. It simply
/// inserts/merges the notes into the thread cache
pub fn process<N: NotesHolder>(&self, thread: &mut N) {
pub fn process(&self, thread: &mut CachedTimeline) {
// threads are chronological, ie reversed from reverse-chronological, the default.
let reversed = true;
thread.get_view().insert(&self.notes, reversed);
thread
.timeline()
.get_current_view_mut()
.insert(&self.notes, reversed);
}
}
60 changes: 14 additions & 46 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@ use crate::{
notes_holder::NotesHolderStorage,

Check failure on line 16 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

unresolved imports `crate::notes_holder`, `crate::profile::Profile`, `crate::thread`

Check failure on line 16 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

unresolved imports `crate::notes_holder`, `crate::profile::Profile`, `crate::thread`

Check failure on line 16 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unresolved imports `crate::notes_holder`, `crate::profile::Profile`, `crate::thread`

Check failure on line 16 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

unresolved imports `crate::notes_holder`, `crate::profile::Profile`, `crate::thread`
profile::Profile,
storage::{Directory, FileKeyStorage, KeyStorageType},
subscriptions::{SubKind, Subscriptions},
subscriptions::{Subscriptions, SubKind},
support::Support,
thread::Thread,
timeline::{Timeline, TimelineId, TimelineKind, ViewFilter},
timeline::{Timeline, TimelineCache, TimelineId, TimelineKind, ViewFilter},
ui::{self, DesktopSidePanel},
unknowns::UnknownIds,
view_state::ViewState,
Expand All @@ -43,7 +43,6 @@ use tracing::{debug, error, info, trace, warn};
pub enum DamusState {
Initializing,
Initialized,
NewTimelineSub(TimelineId),
}

/// We derive Deserialize/Serialize so we can persist app state on shutdown.
Expand All @@ -57,13 +56,12 @@ pub struct Damus {
pub view_state: ViewState,
pub unknown_ids: UnknownIds,
pub drafts: Drafts,
pub threads: NotesHolderStorage<Thread>,
pub profiles: NotesHolderStorage<Profile>,
pub timeline_cache: TimelineCache,
pub img_cache: ImageCache,
pub accounts: AccountManager,
pub subscriptions: Subscriptions,
pub app_rect_handler: AppSizeHandler,
pub support: Support,
pub subscriptions: Subscriptions,

frame_history: crate::frame_history::FrameHistory,

Expand Down Expand Up @@ -101,7 +99,7 @@ fn relay_setup(pool: &mut RelayPool, ctx: &egui::Context) {
fn send_initial_timeline_filter(
ndb: &Ndb,
can_since_optimize: bool,
subs: &mut Subscriptions,
subs: &mut RemoteSubscriptions,

Check failure on line 102 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `RemoteSubscriptions` in this scope

Check failure on line 102 in src/app.rs

View workflow job for this annotation

GitHub Actions / Clippy

cannot find type `RemoteSubscriptions` in this scope

Check failure on line 102 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `RemoteSubscriptions` in this scope

Check failure on line 102 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

cannot find type `RemoteSubscriptions` in this scope
pool: &mut RelayPool,
timeline: &mut Timeline,
to: &str,
Expand Down Expand Up @@ -155,6 +153,7 @@ fn send_initial_timeline_filter(
//let sub_id = damus.gen_subid(&SubKind::Initial);
let sub_id = Uuid::new_v4().to_string();
subs.subs.insert(sub_id.clone(), SubKind::Initial);
timeline.subscription.set_remote_subid(sub_id.clone());

Check failure on line 156 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

no method named `set_remote_subid` found for enum `std::option::Option` in the current scope

Check failure on line 156 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

no method named `set_remote_subid` found for enum `std::option::Option` in the current scope

let cmd = ClientMessage::req(sub_id, new_filters);
pool.send_to(&cmd, to);
Expand Down Expand Up @@ -361,7 +360,7 @@ fn setup_initial_timeline(
note_cache: &mut NoteCache,
filters: &[Filter],
) -> Result<()> {
timeline.subscription = Some(ndb.subscribe(filters)?);
timeline.subscription.subscribe_local();

Check failure on line 363 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

no method named `subscribe_local` found for enum `std::option::Option` in the current scope

Check failure on line 363 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

no method named `subscribe_local` found for enum `std::option::Option` in the current scope
let txn = Transaction::new(ndb)?;
debug!(
"querying nostrdb sub {:?} {:?}",
Expand Down Expand Up @@ -477,33 +476,6 @@ fn update_damus(damus: &mut Damus, ctx: &egui::Context) {
.expect("home subscription failed");
}

DamusState::NewTimelineSub(new_timeline_id) => {
info!("adding new timeline {}", new_timeline_id);
setup_new_nostrdb_sub(
&damus.ndb,
&mut damus.note_cache,
&mut damus.columns,
new_timeline_id,
)
.expect("new timeline subscription failed");

if let Some(filter) = {
let timeline = damus
.columns
.find_timeline(new_timeline_id)
.expect("timeline");
match &timeline.filter {
FilterState::Ready(filters) => Some(filters.clone()),
_ => None,
}
} {
let subid = Uuid::new_v4().to_string();
damus.pool.subscribe(subid, filter);

damus.state = DamusState::Initialized;
}
}

DamusState::Initialized => (),
};

Expand Down Expand Up @@ -744,10 +716,9 @@ impl Damus {
pool,
debug,
unknown_ids: UnknownIds::default(),
subscriptions: Subscriptions::default(),
subscriptions: RemoteSubscriptions::default(),

Check failure on line 719 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared type `RemoteSubscriptions`

Check failure on line 719 in src/app.rs

View workflow job for this annotation

GitHub Actions / Test Suite

failed to resolve: use of undeclared type `RemoteSubscriptions`
since_optimize: parsed_args.since_optimize,
threads: NotesHolderStorage::default(),
profiles: NotesHolderStorage::default(),
timeline_cache: TimelineCache::default(),
drafts: Drafts::default(),
state: DamusState::Initializing,
img_cache: ImageCache::new(imgcache_dir.into()),
Expand Down Expand Up @@ -807,10 +778,6 @@ impl Damus {
}
}

pub fn subscribe_new_timeline(&mut self, timeline_id: TimelineId) {
self.state = DamusState::NewTimelineSub(timeline_id);
}

pub fn mock<P: AsRef<Path>>(data_path: P) -> Self {
let mut columns = Columns::new();
let filter = Filter::from_json(include_str!("../queries/global.json")).unwrap();
Expand All @@ -828,10 +795,9 @@ impl Damus {
Self {
debug,
unknown_ids: UnknownIds::default(),
subscriptions: Subscriptions::default(),
subscriptions: RemoteSubscriptions::default(),
since_optimize: true,
threads: NotesHolderStorage::default(),
profiles: NotesHolderStorage::default(),
timeline_cache: TimelineCache::default(),
drafts: Drafts::default(),
state: DamusState::Initializing,
pool: RelayPool::new(),
Expand All @@ -852,6 +818,7 @@ impl Damus {
&mut self.subscriptions.subs
}

/*
pub fn note_cache_mut(&mut self) -> &mut NoteCache {
&mut self.note_cache
}
Expand All @@ -864,13 +831,14 @@ impl Damus {
&self.threads
}
pub fn threads_mut(&mut self) -> &mut NotesHolderStorage<Thread> {
pub fn timeline_cache_mut(&mut self) -> &mut TimelineCache {
&mut self.threads
}
pub fn note_cache(&self) -> &NoteCache {
&self.note_cache
}
*/
}

/*
Expand Down
Loading

0 comments on commit 9f98a59

Please sign in to comment.