mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-19 19:46:50 +00:00
Events::get_local()
This commit is contained in:
parent
287de1c955
commit
ea6de6deec
@ -1,5 +1,8 @@
|
|||||||
|
use crate::error::Error;
|
||||||
|
use crate::globals::GLOBALS;
|
||||||
use dashmap::{DashMap, DashSet};
|
use dashmap::{DashMap, DashSet};
|
||||||
use nostr_types::{Event, Id};
|
use nostr_types::{Event, Id};
|
||||||
|
use tokio::task;
|
||||||
|
|
||||||
pub struct Events {
|
pub struct Events {
|
||||||
events: DashMap<Id, Event>,
|
events: DashMap<Id, Event>,
|
||||||
@ -27,6 +30,35 @@ impl Events {
|
|||||||
self.events.get(id).map(|e| e.value().to_owned())
|
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 {
|
pub fn is_new(&self, id: &Id) -> bool {
|
||||||
self.new_events.contains(id)
|
self.new_events.contains(id)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user