Skip to content

Commit

Permalink
storage: Simplify to Storage::get_replaceable_event() which handles p…
Browse files Browse the repository at this point in the history
…aramters
  • Loading branch information
mikedilger committed Nov 14, 2023
1 parent 39c1afa commit e44d4db
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 44 deletions.
10 changes: 5 additions & 5 deletions gossip-bin/src/ui/feed/note/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -305,11 +305,11 @@ pub(super) fn render_parameterized_event_link(
let nam = format!("[{:?}: {}]", event_addr.kind, event_addr.d);
//let nam = format!("nostr:{}", event_addr.as_bech32_string());
if ui.link(&nam).clicked() {
if let Ok(Some(prevent)) = GLOBALS.storage.get_parameterized_replaceable_event(
event_addr.kind,
event_addr.author,
&event_addr.d,
) {
if let Ok(Some(prevent)) =
GLOBALS
.storage
.get_replaceable_event(event_addr.kind, event_addr.author, &event_addr.d)
{
app.set_page(Page::Feed(FeedKind::Thread {
id: prevent.id,
referenced_by: referenced_by_id,
Expand Down
7 changes: 4 additions & 3 deletions gossip-lib/src/overlord/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2314,9 +2314,10 @@ impl Overlord {

// Load the latest PersonList event from the database
let event = {
if let Some(event) = GLOBALS
.storage
.get_replaceable_event(list.event_kind(), my_pubkey)?
if let Some(event) =
GLOBALS
.storage
.get_replaceable_event(list.event_kind(), my_pubkey, "")?
{
event.clone()
} else {
Expand Down
16 changes: 9 additions & 7 deletions gossip-lib/src/people.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ impl People {
};

for (person_list, _) in PersonList::all_lists() {
if let Ok(Some(event)) = GLOBALS
.storage
.get_replaceable_event(person_list.event_kind(), pk)
if let Ok(Some(event)) =
GLOBALS
.storage
.get_replaceable_event(person_list.event_kind(), pk, "")
{
self.latest_person_list_event_data.insert(
person_list,
Expand Down Expand Up @@ -644,10 +645,11 @@ impl People {

let content = {
if kind == EventKind::ContactList {
match GLOBALS
.storage
.get_replaceable_event(EventKind::ContactList, my_pubkey)?
{
match GLOBALS.storage.get_replaceable_event(
EventKind::ContactList,
my_pubkey,
"",
)? {
Some(c) => c.content,
None => "".to_owned(),
}
Expand Down
2 changes: 1 addition & 1 deletion gossip-lib/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ pub async fn process_new_event(
NostrBech32::EventAddr(mut ea) => {
if let Ok(None) = GLOBALS
.storage
.get_parameterized_replaceable_event(ea.kind, ea.author, &ea.d)
.get_replaceable_event(ea.kind, ea.author, &ea.d)
{
// Add the seen_on relay
if let Some(seen_on_url) = seen_on.as_ref() {
Expand Down
49 changes: 21 additions & 28 deletions gossip-lib/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1246,45 +1246,38 @@ impl Storage {
Ok(true)
}

/// Get the matching replaceable event
/// Get the matching replaceable event (possibly parameterized)
/// TBD: optimize this by storing better event indexes
pub fn get_replaceable_event(
&self,
kind: EventKind,
pubkey: PublicKey,
mut parameter: &str,
) -> Result<Option<Event>, Error> {
if kind.is_replaceable() {
parameter = "";
} else if !kind.is_parameterized_replaceable() {
return Err(ErrorKind::General("Event kind is not replaceable".to_owned()).into());
}

Ok(self
.find_events(&[kind], &[pubkey], None, |_| true, true)?
.find_events(
&[kind],
&[pubkey],
None, // any time
|e| {
if !parameter.is_empty() {
e.parameter().as_deref() == Some(parameter)
} else {
true
}
},
true, // sorted in reverse time order
)?
.first()
.cloned())
}

/// Get the matching parameterized replaceable event
/// TBD: use forthcoming event_tags index
pub fn get_parameterized_replaceable_event(
&self,
kind: EventKind,
pubkey: PublicKey,
parameter: &str,
) -> Result<Option<Event>, Error> {
if !kind.is_parameterized_replaceable() {
return Err(
ErrorKind::General("Kind is not parameterized replaceable.".to_owned()).into(),
);
}

let mut events = self.find_events(
&[kind],
&[pubkey],
None, // any time
|e| e.parameter().as_deref() == Some(parameter),
true, // sorted in reverse time order
)?;

let maybe_event = events.drain(..).take(1).next();
Ok(maybe_event)
}

/// Replace the matching parameterized event with the given event if it is parameterized
/// replaceable and is newer.
pub fn replace_parameterized_event<'a>(
Expand Down

0 comments on commit e44d4db

Please sign in to comment.