mirror of
https://github.com/mikedilger/gossip.git
synced 2024-09-19 03:33:53 +00:00
Include recommended relay url in replies
This commit is contained in:
parent
cf7a495ba3
commit
c5b844c9d2
@ -1,6 +1,6 @@
|
||||
use crate::error::Error;
|
||||
use crate::globals::GLOBALS;
|
||||
use nostr_types::Url;
|
||||
use nostr_types::{Id, Url};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use tokio::task::spawn_blocking;
|
||||
|
||||
@ -235,4 +235,48 @@ impl DbRelay {
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn recommended_relay_for_reply(reply_to: Id) -> Result<Option<Url>, Error> {
|
||||
// Try to find a relay where the event was seen AND that I post to which
|
||||
// has a rank>1
|
||||
let sql = "SELECT url FROM relay INNER JOIN event_seen ON relay.url=event_seen.relay \
|
||||
WHERE event_seen.event=? AND relay.post=1 AND relay.rank>1";
|
||||
let output: Option<Url> = spawn_blocking(move || {
|
||||
let maybe_db = GLOBALS.db.blocking_lock();
|
||||
let db = maybe_db.as_ref().unwrap();
|
||||
let mut stmt = db.prepare(sql)?;
|
||||
let mut query_result = stmt.query([reply_to.as_hex_string()])?;
|
||||
if let Some(row) = query_result.next()? {
|
||||
let s: String = row.get(0)?;
|
||||
let url = Url::new(&s);
|
||||
Ok::<Option<Url>, Error>(Some(url))
|
||||
} else {
|
||||
Ok::<Option<Url>, Error>(None)
|
||||
}
|
||||
})
|
||||
.await??;
|
||||
|
||||
if output.is_some() {
|
||||
return Ok(output);
|
||||
}
|
||||
|
||||
// Fallback to finding any relay where the event was seen
|
||||
let sql = "SELECT relay FROM event_seen WHERE event=?";
|
||||
let output: Option<Url> = spawn_blocking(move || {
|
||||
let maybe_db = GLOBALS.db.blocking_lock();
|
||||
let db = maybe_db.as_ref().unwrap();
|
||||
let mut stmt = db.prepare(sql)?;
|
||||
let mut query_result = stmt.query([reply_to.as_hex_string()])?;
|
||||
if let Some(row) = query_result.next()? {
|
||||
let s: String = row.get(0)?;
|
||||
let url = Url::new(&s);
|
||||
Ok::<Option<Url>, Error>(Some(url))
|
||||
} else {
|
||||
Ok::<Option<Url>, Error>(None)
|
||||
}
|
||||
})
|
||||
.await??;
|
||||
|
||||
Ok(output)
|
||||
}
|
||||
}
|
||||
|
@ -795,7 +795,7 @@ impl Overlord {
|
||||
kind: EventKind::TextNote,
|
||||
tags: vec![Tag::Event {
|
||||
id: reply_to,
|
||||
recommended_relay_url: None, // FIXME - we should pick a URL shared by who I am replying to and myself
|
||||
recommended_relay_url: DbRelay::recommended_relay_for_reply(reply_to).await?,
|
||||
marker: Some("reply".to_string()),
|
||||
}],
|
||||
content,
|
||||
|
Loading…
Reference in New Issue
Block a user