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

17 lines
589 B
Rust
Raw Normal View History

2024-09-12 01:10:18 -07:00
use std::str::FromStr;
use activitypub_federation::url::Url;
2022-11-24 11:53:07 -08:00
use rand::{distributions::Alphanumeric, thread_rng, Rng};
2024-09-12 01:10:18 -07:00
use url::ParseError;
2022-11-24 11:53:07 -08:00
/// 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-07 14:01:36 -08:00
pub fn generate_object_id(domain: &str) -> Result<Url, ParseError> {
2022-11-24 11:53:07 -08:00
let id: String = thread_rng()
.sample_iter(&Alphanumeric)
.take(7)
.map(char::from)
.collect();
2024-09-12 01:10:18 -07:00
Url::from_str(&format!("http://{}/objects/{}", domain, id))
2022-11-24 11:53:07 -08:00
}