activitypub-federation-rust/examples/local_federation/utils.rs

14 lines
531 B
Rust
Raw Permalink Normal View History

2022-11-25 01:23:07 +05:30
use rand::{distributions::Alphanumeric, thread_rng, Rng};
use url::{ParseError, Url};
/// Just generate random url as object id. In a real project, you probably want to use
/// an url which contains the database id for easy retrieval (or store the random id in db).
2023-03-08 03:31:36 +05:30
pub fn generate_object_id(domain: &str) -> Result<Url, ParseError> {
2022-11-25 01:23:07 +05:30
let id: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
2023-03-08 03:31:36 +05:30
Url::parse(&format!("http://{}/objects/{}", domain, id))
2022-11-25 01:23:07 +05:30
}