Even more Url validation

This commit is contained in:
Mike Dilger 2022-12-26 18:39:12 +13:00
parent ccf969f717
commit 26a6261312
4 changed files with 14 additions and 7 deletions

View File

@ -13,13 +13,15 @@ pub struct DbRelay {
} }
impl DbRelay { impl DbRelay {
pub fn new(url: String) -> DbRelay { pub fn new(url: String) -> Result<DbRelay, Error> {
DbRelay { let _ = Url::new_validated(&url)?;
Ok(DbRelay {
url, url,
success_count: 0, success_count: 0,
failure_count: 0, failure_count: 0,
rank: Some(3), rank: Some(3),
} })
} }
pub async fn fetch(criteria: Option<&str>) -> Result<Vec<DbRelay>, Error> { pub async fn fetch(criteria: Option<&str>) -> Result<Vec<DbRelay>, Error> {
@ -65,6 +67,8 @@ impl DbRelay {
} }
pub async fn insert(relay: DbRelay) -> Result<(), Error> { pub async fn insert(relay: DbRelay) -> Result<(), Error> {
let _ = Url::new_validated(&relay.url)?;
let sql = "INSERT OR IGNORE INTO relay (url, success_count, failure_count, rank) \ let sql = "INSERT OR IGNORE INTO relay (url, success_count, failure_count, rank) \
VALUES (?1, ?2, ?3, ?4)"; VALUES (?1, ?2, ?3, ?4)";

View File

@ -333,7 +333,8 @@ pub async fn follow_key_and_relay(pubkey: String, relay: String) -> Result<DbPer
}; };
// Insert (or ignore) this relay // Insert (or ignore) this relay
DbRelay::insert(DbRelay::new(relay.clone())) let dbrelay = DbRelay::new(relay.clone()).map_err(|e| format!("{}", e))?;
DbRelay::insert(dbrelay)
.await .await
.map_err(|e| format!("{}", e))?; .map_err(|e| format!("{}", e))?;

View File

@ -36,12 +36,14 @@ pub struct Minion {
impl Minion { impl Minion {
pub async fn new(url: Url) -> Result<Minion, Error> { pub async fn new(url: Url) -> Result<Minion, Error> {
let _ = Url::new_validated(&url)?;
let to_overlord = GLOBALS.to_overlord.clone(); let to_overlord = GLOBALS.to_overlord.clone();
let from_overlord = GLOBALS.to_minions.subscribe(); let from_overlord = GLOBALS.to_minions.subscribe();
let dbrelay = match DbRelay::fetch_one(&url).await? { let dbrelay = match DbRelay::fetch_one(&url).await? {
Some(dbrelay) => dbrelay, Some(dbrelay) => dbrelay,
None => { None => {
let dbrelay = DbRelay::new(url.0.clone()); let dbrelay = DbRelay::new(url.0.clone())?;
DbRelay::insert(dbrelay.clone()).await?; DbRelay::insert(dbrelay.clone()).await?;
dbrelay dbrelay
} }

View File

@ -82,7 +82,7 @@ pub async fn process_new_event(
} => { } => {
if let Ok(url) = Url::new_validated(should_be_url) { if let Ok(url) = Url::new_validated(should_be_url) {
// Insert (or ignore) into relays table // Insert (or ignore) into relays table
let dbrelay = DbRelay::new(url.0.clone()); let dbrelay = DbRelay::new(url.0.clone())?;
DbRelay::insert(dbrelay).await?; DbRelay::insert(dbrelay).await?;
} }
} }
@ -93,7 +93,7 @@ pub async fn process_new_event(
} => { } => {
if let Ok(url) = Url::new_validated(should_be_url) { if let Ok(url) = Url::new_validated(should_be_url) {
// Insert (or ignore) into relays table // Insert (or ignore) into relays table
let dbrelay = DbRelay::new(url.0.clone()); let dbrelay = DbRelay::new(url.0.clone())?;
DbRelay::insert(dbrelay).await?; DbRelay::insert(dbrelay).await?;
// upsert person_relay.last_suggested_bytag // upsert person_relay.last_suggested_bytag