Skip to content

Commit

Permalink
squash: use MuteFun instead of passing account data around
Browse files Browse the repository at this point in the history
Instead of passing a reference to the accounts Muted struct pass a
reference to a MuteFun which is called on each note and returns bool.
  • Loading branch information
ksedgwic committed Nov 21, 2024
1 parent 8e160d1 commit e1afc1b
Show file tree
Hide file tree
Showing 14 changed files with 88 additions and 96 deletions.
26 changes: 16 additions & 10 deletions src/accounts/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use std::cmp::Ordering;
use std::collections::{BTreeMap, BTreeSet};
use std::sync::Arc;

use url::Url;
use uuid::Uuid;

use enostr::{ClientMessage, FilledKeypair, FullKeypair, Keypair, RelayPool};
use nostrdb::{Filter, Ndb, NoteKey, Subscription, Transaction};
use nostrdb::{Filter, Ndb, Note, NoteKey, Subscription, Transaction};

use crate::{
column::Columns,
Expand Down Expand Up @@ -123,7 +124,7 @@ pub struct AccountMutedData {
filter: Filter,
subid: String,
sub: Option<Subscription>,
muted: Muted,
muted: Arc<Muted>,
}

impl AccountMutedData {
Expand Down Expand Up @@ -162,7 +163,7 @@ impl AccountMutedData {
filter,
subid,
sub: Some(ndbsub),
muted,
muted: Arc::new(muted),
}
}

Expand Down Expand Up @@ -416,12 +417,17 @@ impl Accounts {
self.key_store.select_key(None);
}

pub fn maybe_mutes(&self) -> Option<&Muted> {
self.currently_selected_account
.and_then(|index| self.accounts.get(index))
.map(|account| account.pubkey.bytes())
.and_then(|pubkey| self.account_data.get(pubkey))
.map(|ad| &ad.muted.muted)
pub fn mutefun(&self) -> Box<dyn Fn(&Note) -> bool> {
if let Some(index) = self.currently_selected_account {
if let Some(account) = self.accounts.get(index) {
let pubkey = account.pubkey.bytes();
if let Some(account_data) = self.account_data.get(pubkey) {
let muted = Arc::clone(&account_data.muted.muted);
return Box::new(move |note: &Note| muted.is_muted(note));
}
}
}
Box::new(|_: &Note| false)
}

pub fn send_initial_filters(&mut self, pool: &mut RelayPool, relay_url: &str) {
Expand Down Expand Up @@ -494,7 +500,7 @@ impl Accounts {
let txn = Transaction::new(ndb).expect("txn");
let muted = AccountMutedData::harvest_nip51_muted(ndb, &txn, &nks);
debug!("pubkey {}: updated muted {:#?}", hex::encode(pubkey), muted);
data.muted.muted = muted;
data.muted.muted = Arc::new(muted);
changed = true;
}
}
Expand Down
20 changes: 10 additions & 10 deletions src/actionbar.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
muted::Muted,
muted::MuteFun,
note::NoteRef,
notecache::NoteCache,
notes_holder::{NotesHolder, NotesHolderStorage},
Expand Down Expand Up @@ -44,12 +44,12 @@ fn open_thread(
pool: &mut RelayPool,
threads: &mut NotesHolderStorage<Thread>,
selected_note: &[u8; 32],
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Option<NotesHolderResult> {
router.route_to(Route::thread(NoteId::new(selected_note.to_owned())));

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, maybe_mutes)
Thread::open(ndb, note_cache, txn, pool, threads, root_id, is_muted)
}

impl BarAction {
Expand All @@ -62,7 +62,7 @@ impl BarAction {
note_cache: &mut NoteCache,
pool: &mut RelayPool,
txn: &Transaction,
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Option<NotesHolderResult> {
match self {
BarAction::Reply(note_id) => {
Expand All @@ -79,7 +79,7 @@ impl BarAction {
pool,
threads,
note_id.bytes(),
maybe_mutes,
is_muted,
),

BarAction::Quote(note_id) => {
Expand All @@ -99,10 +99,10 @@ impl BarAction {
note_cache: &mut NoteCache,
pool: &mut RelayPool,
txn: &Transaction,
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) {
if let Some(br) = self.execute(ndb, router, threads, note_cache, pool, txn, maybe_mutes) {
br.process(ndb, note_cache, txn, threads, maybe_mutes);
if let Some(br) = self.execute(ndb, router, threads, note_cache, pool, txn, is_muted) {
br.process(ndb, note_cache, txn, threads, is_muted);
}
}
}
Expand All @@ -118,13 +118,13 @@ impl NotesHolderResult {
note_cache: &mut NoteCache,
txn: &Transaction,
storage: &mut NotesHolderStorage<N>,
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) {
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, maybe_mutes)
.notes_holder_mutated(ndb, note_cache, txn, &new_notes.id, is_muted)
.get_ptr();
new_notes.process(holder);
}
Expand Down
6 changes: 3 additions & 3 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ fn try_process_event(damus: &mut Damus, ctx: &egui::Context) -> Result<()> {
&mut damus.pool,
&mut damus.note_cache,
timeline,
damus.accounts.maybe_mutes(),
&damus.accounts.mutefun(),
)
};

Expand All @@ -162,7 +162,7 @@ fn try_process_event(damus: &mut Damus, ctx: &egui::Context) -> Result<()> {
&txn,
&mut damus.unknown_ids,
&mut damus.note_cache,
damus.accounts.maybe_mutes(),
&damus.accounts.mutefun(),
) {
error!("poll_notes_into_view: {err}");
}
Expand Down Expand Up @@ -211,7 +211,7 @@ fn update_damus(damus: &mut Damus, ctx: &egui::Context) {
&damus.ndb,
&mut damus.note_cache,
&mut damus.columns,
damus.accounts.maybe_mutes(),
&damus.accounts.mutefun(),
) {
warn!("update_damus init: {err}");
}
Expand Down
9 changes: 3 additions & 6 deletions src/multi_subscriber.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use nostrdb::{Ndb, Note, Transaction};
use tracing::{debug, error, info};
use uuid::Uuid;

use crate::{filter::UnifiedSubscription, muted::Muted, note::NoteRef, Error};
use crate::{filter::UnifiedSubscription, muted::MuteFun, note::NoteRef, Error};

pub struct MultiSubscriber {
filters: Vec<Filter>,
Expand Down Expand Up @@ -109,7 +109,7 @@ impl MultiSubscriber {
&mut self,
ndb: &Ndb,
txn: &Transaction,
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Result<Vec<NoteRef>, Error> {
let sub = self.sub.as_ref().ok_or(Error::no_active_sub())?;
let new_note_keys = ndb.poll_for_notes(sub.local, 500);
Expand All @@ -128,10 +128,7 @@ impl MultiSubscriber {
continue;
};

if maybe_mutes
.map(|mutes| mutes.is_muted(&note))
.unwrap_or(false)
{
if is_muted(&note) {
continue;
}

Expand Down
2 changes: 2 additions & 0 deletions src/muted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::collections::BTreeSet;

use tracing::debug;

pub type MuteFun = dyn Fn(&Note) -> bool;

#[derive(Default)]
pub struct Muted {
// TODO - implement private mutes
Expand Down
10 changes: 5 additions & 5 deletions src/nav.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> Option<Rend
&mut app.threads,
col,
ui,
app.accounts.maybe_mutes(),
&app.accounts.mutefun(),
),
Route::Support => {
SupportView::new(&mut app.support).show(ui);
Expand Down Expand Up @@ -180,14 +180,14 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> Option<Rend
&mut app.pool,
&mut app.profiles,
pubkey.bytes(),
app.accounts.maybe_mutes(),
&app.accounts.mutefun(),
) {
res.process(
&app.ndb,
&mut app.note_cache,
&txn,
&mut app.profiles,
app.accounts.maybe_mutes(),
&app.accounts.mutefun(),
);
}
}
Expand All @@ -213,7 +213,7 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> Option<Rend
&mut app.threads,
&mut app.pool,
root_id,
app.accounts.maybe_mutes(),
&app.accounts.mutefun(),
);
}

Expand All @@ -225,7 +225,7 @@ pub fn render_nav(col: usize, app: &mut Damus, ui: &mut egui::Ui) -> Option<Rend
&mut app.profiles,
&mut app.pool,
pubkey.bytes(),
app.accounts.maybe_mutes(),
&app.accounts.mutefun(),
);
}
resp = Some(RenderNavResponse::ColumnChanged)
Expand Down
20 changes: 10 additions & 10 deletions src/notes_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use nostrdb::{Ndb, Transaction};
use tracing::{debug, info, warn};

use crate::{
actionbar::NotesHolderResult, multi_subscriber::MultiSubscriber, muted::Muted, note::NoteRef,
actionbar::NotesHolderResult, multi_subscriber::MultiSubscriber, muted::MuteFun, note::NoteRef,
notecache::NoteCache, timeline::TimelineTab, unknowns::NoteRefsUnkIdAction, Error, Result,
};

Expand Down Expand Up @@ -55,7 +55,7 @@ impl<M: NotesHolder> NotesHolderStorage<M> {
note_cache: &mut NoteCache,
txn: &Transaction,
id: &[u8; 32],
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Vitality<'a, M> {
// we can't use the naive hashmap entry API here because lookups
// require a copy, wait until we have a raw entry api. We could
Expand Down Expand Up @@ -89,7 +89,7 @@ impl<M: NotesHolder> NotesHolderStorage<M> {

self.id_to_object.insert(
id.to_owned(),
M::new_notes_holder(txn, ndb, note_cache, id, M::filters(id), notes, maybe_mutes),
M::new_notes_holder(txn, ndb, note_cache, id, M::filters(id), notes, is_muted),
);
Vitality::Fresh(self.id_to_object.get_mut(id).unwrap())
}
Expand All @@ -108,19 +108,19 @@ pub trait NotesHolder {
id: &[u8; 32],
filters: Vec<Filter>,
notes: Vec<NoteRef>,
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Self;

#[must_use = "process_action must be handled in the Ok(action) case"]
fn poll_notes_into_view(
&mut self,
txn: &Transaction,
ndb: &Ndb,
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Result<NoteRefsUnkIdAction> {
if let Some(multi_subscriber) = self.get_multi_subscriber() {
let reversed = true;
let note_refs: Vec<NoteRef> = multi_subscriber.poll_for_notes(ndb, txn, maybe_mutes)?;
let note_refs: Vec<NoteRef> = multi_subscriber.poll_for_notes(ndb, txn, is_muted)?;
self.get_view().insert(&note_refs, reversed);
Ok(NoteRefsUnkIdAction::new(note_refs))
} else {
Expand Down Expand Up @@ -159,10 +159,10 @@ pub trait NotesHolder {
notes_holder_storage: &mut NotesHolderStorage<M>,
pool: &mut RelayPool,
id: &[u8; 32],
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) {
let notes_holder = notes_holder_storage
.notes_holder_mutated(ndb, note_cache, txn, id, maybe_mutes)
.notes_holder_mutated(ndb, note_cache, txn, id, is_muted)
.get_ptr();

if let Some(multi_subscriber) = notes_holder.get_multi_subscriber() {
Expand All @@ -177,9 +177,9 @@ pub trait NotesHolder {
pool: &mut RelayPool,
storage: &mut NotesHolderStorage<M>,
id: &[u8; 32],
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Option<NotesHolderResult> {
let vitality = storage.notes_holder_mutated(ndb, note_cache, txn, id, maybe_mutes);
let vitality = storage.notes_holder_mutated(ndb, note_cache, txn, id, is_muted);

let (holder, result) = match vitality {
Vitality::Stale(holder) => {
Expand Down
10 changes: 5 additions & 5 deletions src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use nostrdb::{FilterBuilder, Ndb, ProfileRecord, Transaction};
use crate::{
filter::{self, FilterState},
multi_subscriber::MultiSubscriber,
muted::Muted,
muted::MuteFun,
note::NoteRef,
notecache::NoteCache,
notes_holder::NotesHolder,
Expand Down Expand Up @@ -62,12 +62,12 @@ impl Profile {
source: PubkeySource,
filters: Vec<Filter>,
notes: Vec<NoteRef>,
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Self {
let mut timeline =
Timeline::new(TimelineKind::profile(source), FilterState::ready(filters));

copy_notes_into_timeline(&mut timeline, txn, ndb, note_cache, notes, maybe_mutes);
copy_notes_into_timeline(&mut timeline, txn, ndb, note_cache, notes, is_muted);

Profile {
timeline,
Expand Down Expand Up @@ -113,7 +113,7 @@ impl NotesHolder for Profile {
id: &[u8; 32],
filters: Vec<Filter>,
notes: Vec<NoteRef>,
maybe_mutes: Option<&Muted>,
is_muted: &MuteFun,
) -> Self {
Profile::new(
txn,
Expand All @@ -122,7 +122,7 @@ impl NotesHolder for Profile {
PubkeySource::Explicit(Pubkey::new(*id)),
filters,
notes,
maybe_mutes,
is_muted,
)
}

Expand Down
4 changes: 2 additions & 2 deletions src/thread.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
multi_subscriber::MultiSubscriber,
muted::Muted,
muted::MuteFun,
note::NoteRef,
notecache::NoteCache,
notes_holder::NotesHolder,
Expand Down Expand Up @@ -75,7 +75,7 @@ impl NotesHolder for Thread {
_: &[u8; 32],
_: Vec<Filter>,
notes: Vec<NoteRef>,
_: Option<&Muted>,
_: &MuteFun,
) -> Self {
Thread::new(notes)
}
Expand Down
Loading

0 comments on commit e1afc1b

Please sign in to comment.