Events::get_local()

This commit is contained in:
Mike Dilger 2023-01-14 11:26:42 +13:00
parent 287de1c955
commit ea6de6deec

View File

@ -1,5 +1,8 @@
use crate::error::Error;
use crate::globals::GLOBALS;
use dashmap::{DashMap, DashSet};
use nostr_types::{Event, Id};
use tokio::task;
pub struct Events {
events: DashMap<Id, Event>,
@ -27,6 +30,35 @@ impl Events {
self.events.get(id).map(|e| e.value().to_owned())
}
/// Get the event from memory, and also try the database
#[allow(dead_code)]
pub async fn get_local(&self, id: Id) -> Result<Option<Event>, Error> {
if let Some(e) = self.get(&id) {
return Ok(Some(e));
}
if let Some(event) = task::spawn_blocking(move || {
let maybe_db = GLOBALS.db.blocking_lock();
let db = maybe_db.as_ref().unwrap();
let mut stmt = db.prepare("SELECT raw FROM event WHERE id=?")?;
stmt.raw_bind_parameter(1, id.as_hex_string())?;
let mut rows = stmt.raw_query();
if let Some(row) = rows.next()? {
let s: String = row.get(0)?;
Ok(Some(serde_json::from_str(&s)?))
} else {
Ok::<Option<Event>, Error>(None)
}
})
.await??
{
self.insert(event.clone());
Ok(Some(event))
} else {
Ok(None)
}
}
pub fn is_new(&self, id: &Id) -> bool {
self.new_events.contains(id)
}