radicle: use `Alias` in `follow`

Ensure that the validated `Alias` type is used for inserting into the
policy database.
This commit is contained in:
Fintan Halpenny 2025-02-07 17:09:41 +00:00
parent 6940ac42be
commit 7de82b5025
3 changed files with 11 additions and 10 deletions

View File

@ -116,7 +116,7 @@ pub fn follow(
Ok(updated) => updated, Ok(updated) => updated,
Err(e) if e.is_connection_err() => { Err(e) if e.is_connection_err() => {
let mut config = profile.policies_mut()?; let mut config = profile.policies_mut()?;
config.follow(&nid, alias.as_deref())? config.follow(&nid, alias.as_ref())?
} }
Err(e) => return Err(e.into()), Err(e) => return Err(e.into()),
}; };

View File

@ -893,7 +893,7 @@ where
Command::Follow(id, alias, resp) => { Command::Follow(id, alias, resp) => {
let seeded = self let seeded = self
.policies .policies
.follow(&id, alias.as_deref()) .follow(&id, alias.as_ref())
.expect("Service::command: error following node"); .expect("Service::command: error following node");
resp.send(seeded).ok(); resp.send(seeded).ok();
} }

View File

@ -117,7 +117,7 @@ impl Store<Write> {
} }
/// Follow a node. /// Follow a node.
pub fn follow(&mut self, id: &NodeId, alias: Option<&str>) -> Result<bool, Error> { pub fn follow(&mut self, id: &NodeId, alias: Option<&Alias>) -> Result<bool, Error> {
let mut stmt = self.db.prepare( let mut stmt = self.db.prepare(
"INSERT INTO `following` (id, alias) "INSERT INTO `following` (id, alias)
VALUES (?1, ?2) VALUES (?1, ?2)
@ -126,7 +126,7 @@ impl Store<Write> {
)?; )?;
stmt.bind((1, id))?; stmt.bind((1, id))?;
stmt.bind((2, alias.unwrap_or_default()))?; stmt.bind((2, alias.map_or("", |alias| alias.as_str())))?;
stmt.next()?; stmt.next()?;
Ok(self.db.change_count() > 0) Ok(self.db.change_count() > 0)
@ -410,10 +410,11 @@ mod test {
fn test_follow_and_unfollow_node() { fn test_follow_and_unfollow_node() {
let id = arbitrary::gen::<NodeId>(1); let id = arbitrary::gen::<NodeId>(1);
let mut db = Store::open(":memory:").unwrap(); let mut db = Store::open(":memory:").unwrap();
let eve = Alias::new("eve");
assert!(db.follow(&id, Some("eve")).unwrap()); assert!(db.follow(&id, Some(&eve)).unwrap());
assert!(db.is_following(&id).unwrap()); assert!(db.is_following(&id).unwrap());
assert!(!db.follow(&id, Some("eve")).unwrap()); assert!(!db.follow(&id, Some(&eve)).unwrap());
assert!(db.unfollow(&id).unwrap()); assert!(db.unfollow(&id).unwrap());
assert!(!db.is_following(&id).unwrap()); assert!(!db.is_following(&id).unwrap());
} }
@ -463,7 +464,7 @@ mod test {
let id = arbitrary::gen::<NodeId>(1); let id = arbitrary::gen::<NodeId>(1);
let mut db = Store::open(":memory:").unwrap(); let mut db = Store::open(":memory:").unwrap();
assert!(db.follow(&id, Some("eve")).unwrap()); assert!(db.follow(&id, Some(&Alias::new("eve"))).unwrap());
assert_eq!( assert_eq!(
db.follow_policy(&id).unwrap().unwrap().alias, db.follow_policy(&id).unwrap().unwrap().alias,
Some(Alias::from_str("eve").unwrap()) Some(Alias::from_str("eve").unwrap())
@ -471,7 +472,7 @@ mod test {
assert!(db.follow(&id, None).unwrap()); assert!(db.follow(&id, None).unwrap());
assert_eq!(db.follow_policy(&id).unwrap().unwrap().alias, None); assert_eq!(db.follow_policy(&id).unwrap().unwrap().alias, None);
assert!(!db.follow(&id, None).unwrap()); assert!(!db.follow(&id, None).unwrap());
assert!(db.follow(&id, Some("alice")).unwrap()); assert!(db.follow(&id, Some(&Alias::new("alice"))).unwrap());
assert_eq!( assert_eq!(
db.follow_policy(&id).unwrap().unwrap().alias, db.follow_policy(&id).unwrap().unwrap().alias,
Some(Alias::new("alice")) Some(Alias::new("alice"))
@ -532,11 +533,11 @@ mod test {
let (long, long_ids) = input.long(); let (long, long_ids) = input.long();
for id in short_ids { for id in short_ids {
db.follow(id, Some(short.as_str())).unwrap(); db.follow(id, Some(short)).unwrap();
} }
for id in long_ids { for id in long_ids {
db.follow(id, Some(long.as_str())).unwrap(); db.follow(id, Some(long)).unwrap();
} }
node::properties::test_reverse_lookup(&db, input) node::properties::test_reverse_lookup(&db, input)