-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: NOM-04 support, relay publishing + .well-known
- Loading branch information
Showing
13 changed files
with
186 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
use sqlx::Sqlite; | ||
|
||
pub async fn queue( | ||
conn: impl sqlx::Executor<'_, Database = Sqlite> + Copy, | ||
name: &str, | ||
) -> anyhow::Result<()> { | ||
sqlx::query("INSERT INTO relay_index_queue (name) VALUES (?)") | ||
.bind(name) | ||
.execute(conn) | ||
.await?; | ||
Ok(()) | ||
} | ||
|
||
#[derive(sqlx::FromRow, Debug)] | ||
pub struct Name { | ||
pub name: String, | ||
pub pubkey: String, | ||
pub records: String, | ||
} | ||
|
||
pub async fn fetch_all( | ||
conn: impl sqlx::Executor<'_, Database = Sqlite> + Copy, | ||
) -> anyhow::Result<Vec<Name>> { | ||
let results = sqlx::query_as::<_, Name>( | ||
"SELECT vnr.name, vnr.pubkey, COALESCE(vnr.records, '{}') as records | ||
FROM valid_names_records_vw vnr | ||
JOIN relay_index_queue riq ON vnr.name = riq.name;", | ||
) | ||
.fetch_all(conn) | ||
.await?; | ||
Ok(results) | ||
} | ||
|
||
pub async fn clear(conn: impl sqlx::Executor<'_, Database = Sqlite> + Copy) -> anyhow::Result<()> { | ||
sqlx::query("DELETE FROM relay_index_queue;") | ||
.execute(conn) | ||
.await?; | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
mod event_data; | ||
mod records; | ||
pub mod relay_index; | ||
|
||
pub use event_data::*; | ||
pub use records::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
use std::collections::HashMap; | ||
|
||
use nostr_sdk::{EventBuilder, Keys, Tag}; | ||
use secp256k1::SecretKey; | ||
use serde::Serialize; | ||
use sqlx::SqlitePool; | ||
|
||
use crate::{ | ||
config::Config, | ||
db::{self, relay_index::Name}, | ||
}; | ||
|
||
pub async fn publish(config: &Config, pool: &SqlitePool) -> anyhow::Result<()> { | ||
if !config.publish_index() { | ||
return Ok(()); | ||
} | ||
let sk: SecretKey = config | ||
.secret_key() | ||
.expect("Missing config validation for secret") | ||
.into(); | ||
let keys = Keys::new(sk); | ||
let client = config.nostr_keys_client(&keys).await?; | ||
tracing::info!("Publishing relay index."); | ||
let names = db::relay_index::fetch_all(pool).await?; | ||
|
||
send_event(names, keys, &client).await?; | ||
|
||
db::relay_index::clear(pool).await?; | ||
client.disconnect().await.ok(); | ||
Ok(()) | ||
} | ||
|
||
async fn send_event( | ||
names: Vec<Name>, | ||
keys: Keys, | ||
client: &nostr_sdk::Client, | ||
) -> Result<(), anyhow::Error> { | ||
for name in names { | ||
let records: HashMap<String, String> = serde_json::from_str(&name.records)?; | ||
let content = Content { | ||
name: name.name.clone(), | ||
pubkey: name.pubkey, | ||
records, | ||
}; | ||
let content_serialize = serde_json::to_string(&content)?; | ||
let event = EventBuilder::new( | ||
nostr_sdk::Kind::ParameterizedReplaceable(38301), | ||
content_serialize, | ||
&[Tag::Identifier(name.name.clone())], | ||
) | ||
.to_event(&keys)?; | ||
|
||
if client.send_event(event).await.is_err() { | ||
tracing::error!("Unable to broadcast event during relay index publish"); | ||
} | ||
} | ||
Ok(()) | ||
} | ||
|
||
#[derive(Serialize)] | ||
struct Content { | ||
name: String, | ||
pubkey: String, | ||
records: HashMap<String, String>, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters