diff --git a/docs/03_federating_users.md b/docs/03_federating_users.md index 7882af1..39ecb3b 100644 --- a/docs/03_federating_users.md +++ b/docs/03_federating_users.md @@ -40,7 +40,7 @@ Based on this we can define the following minimal struct to (de)serialize a `Per # use activitypub_federation::fetch::object_id::ObjectId; # use serde::{Deserialize, Serialize}; # use activitystreams_kinds::actor::PersonType; -# use url::Url; +# use activitypub_federation::url::Url; # use activitypub_federation::traits::tests::DbUser; #[derive(Deserialize, Serialize)] @@ -64,7 +64,7 @@ pub struct Person { Besides we also need a second struct to represent the data which gets stored in our local database (for example PostgreSQL). This is necessary because the data format used by SQL is very different from that used by that from Activitypub. It is organized by an integer primary key instead of a link id. Nested structs are complicated to represent and easier if flattened. Some fields like `type` don't need to be stored at all. On the other hand, the database contains fields which can't be federated, such as the private key and a boolean indicating if the item is local or remote. ```rust -# use url::Url; +# use activitypub_federation::url::Url; # use chrono::{DateTime, Utc}; pub struct DbUser { diff --git a/docs/08_receiving_activities.md b/docs/08_receiving_activities.md index 7e0cd03..c54096d 100644 --- a/docs/08_receiving_activities.md +++ b/docs/08_receiving_activities.md @@ -4,7 +4,7 @@ Activitypub propagates actions across servers using `Activities`. For this each ``` # use serde::{Deserialize, Serialize}; -# use url::Url; +# use activitypub_federation::url::Url; # use anyhow::Error; # use async_trait::async_trait; # use activitypub_federation::fetch::object_id::ObjectId; @@ -62,7 +62,7 @@ Next its time to setup the actual HTTP handler for the inbox. For this we first # use activitypub_federation::traits::ActivityHandler; # use activitypub_federation::traits::tests::{DbConnection, DbUser, Follow}; # use serde::{Deserialize, Serialize}; -# use url::Url; +# use activitypub_federation::url::Url; #[derive(Deserialize, Serialize, Debug)] #[serde(untagged)] diff --git a/docs/09_sending_activities.md b/docs/09_sending_activities.md index d5efe3a..3ddd6c1 100644 --- a/docs/09_sending_activities.md +++ b/docs/09_sending_activities.md @@ -22,7 +22,7 @@ let activity = Follow { actor: ObjectId::parse("https://lemmy.ml/u/nutomic")?, object: recipient.federation_id.clone().into(), kind: Default::default(), - id: "https://lemmy.ml/activities/321".try_into()? + id: "https://lemmy.ml/activities/321".parse()? }; let inboxes = vec![recipient.shared_inbox_or_inbox()]; @@ -62,7 +62,7 @@ let activity = Follow { actor: ObjectId::parse("https://lemmy.ml/u/nutomic")?, object: recipient.federation_id.clone().into(), kind: Default::default(), - id: "https://lemmy.ml/activities/321".try_into()? + id: "https://lemmy.ml/activities/321".parse()? }; let inboxes = vec![recipient.shared_inbox_or_inbox()]; diff --git a/docs/10_fetching_objects_with_unknown_type.md b/docs/10_fetching_objects_with_unknown_type.md index 96392d4..0ca84a4 100644 --- a/docs/10_fetching_objects_with_unknown_type.md +++ b/docs/10_fetching_objects_with_unknown_type.md @@ -10,7 +10,7 @@ It is sometimes necessary to fetch from a URL, but we don't know the exact type # use serde::{Deserialize, Serialize}; # use activitypub_federation::traits::tests::DbConnection; # use activitypub_federation::config::Data; -# use url::Url; +# use activitypub_federation::url::Url; # use activitypub_federation::traits::tests::{Person, Note}; #[derive(Debug)] diff --git a/src/config.rs b/src/config.rs index 2e23c41..e998a97 100644 --- a/src/config.rs +++ b/src/config.rs @@ -243,7 +243,7 @@ impl Deref for FederationConfig { /// /// ``` /// # use async_trait::async_trait; -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// # use activitypub_federation::config::UrlVerifier; /// # use activitypub_federation::error::Error; /// # #[derive(Clone)] @@ -260,7 +260,7 @@ impl Deref for FederationConfig { /// impl UrlVerifier for Verifier { /// async fn verify(&self, url: &Url) -> Result<(), Error> { /// let blocklist = get_blocklist(&self.db_connection).await; -/// let domain = url.domain().unwrap().to_string(); +/// let domain = url.domain().to_string(); /// if blocklist.contains(&domain) { /// Err(Error::Other("Domain is blocked".into())) /// } else { @@ -365,8 +365,6 @@ mod test { let config = config().await; assert!(config.is_local_url(&Url::from_str("http://example.com")?)); assert!(!config.is_local_url(&Url::from_str("http://other.com")?)); - // ensure that missing domain doesnt cause crash - assert!(!config.is_local_url(&Url::from_str("http://127.0.0.1")?)); Ok(()) } diff --git a/src/fetch/webfinger.rs b/src/fetch/webfinger.rs index b61b72b..4bc011a 100644 --- a/src/fetch/webfinger.rs +++ b/src/fetch/webfinger.rs @@ -143,10 +143,10 @@ where /// of discovery. /// /// ``` -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// # use activitypub_federation::fetch::webfinger::build_webfinger_response; /// let subject = "acct:nutomic@lemmy.ml".to_string(); -/// let url = Url::from_str("https://lemmy.ml/u/nutomic")?; +/// let url = "https://lemmy.ml/u/nutomic".parse()?; /// build_webfinger_response(subject, url); /// # Ok::<(), anyhow::Error>(()) /// ``` @@ -162,11 +162,11 @@ pub fn build_webfinger_response(subject: String, url: Url) -> Webfinger { /// will be empty. /// /// ``` -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// # use activitypub_federation::fetch::webfinger::build_webfinger_response_with_type; /// let subject = "acct:nutomic@lemmy.ml".to_string(); -/// let user = Url::from_str("https://lemmy.ml/u/nutomic")?; -/// let group = Url::from_str("https://lemmy.ml/c/asklemmy")?; +/// let user = "https://lemmy.ml/u/nutomic".parse()?; +/// let group = "https://lemmy.ml/c/asklemmy".parse()?; /// build_webfinger_response_with_type(subject, vec![ /// (user, Some("Person")), /// (group, Some("Group"))]); diff --git a/src/protocol/helpers.rs b/src/protocol/helpers.rs index 4410bf6..6fae72f 100644 --- a/src/protocol/helpers.rs +++ b/src/protocol/helpers.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Deserializer}; /// /// ``` /// # use activitypub_federation::protocol::helpers::deserialize_one_or_many; -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// #[derive(serde::Deserialize)] /// struct Note { /// #[serde(deserialize_with = "deserialize_one_or_many")] @@ -52,7 +52,7 @@ where /// /// ``` /// # use activitypub_federation::protocol::helpers::deserialize_one; -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// #[derive(serde::Deserialize)] /// struct Note { /// #[serde(deserialize_with = "deserialize_one")] @@ -88,7 +88,7 @@ where /// /// ``` /// # use activitypub_federation::protocol::helpers::deserialize_skip_error; -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// #[derive(serde::Deserialize)] /// struct Note { /// content: String, diff --git a/src/protocol/verification.rs b/src/protocol/verification.rs index e747ec8..7b9d1d4 100644 --- a/src/protocol/verification.rs +++ b/src/protocol/verification.rs @@ -5,12 +5,12 @@ use crate::{error::Error, url::Url}; /// Check that both urls have the same domain. If not, return UrlVerificationError. /// /// ``` -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// # use activitypub_federation::protocol::verification::verify_domains_match; -/// let a = Url::from_str("https://example.com/abc")?; -/// let b = Url::from_str("https://sample.net/abc")?; +/// let a = "https://example.com/abc".parse()?; +/// let b = "https://sample.net/abc".parse()?; /// assert!(verify_domains_match(&a, &b).is_err()); -/// # Ok::<(), Url::from_strError>(()) +/// # Ok::<(), url::ParseError>(()) /// ``` pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), Error> { if a.domain() != b.domain() { @@ -22,12 +22,12 @@ pub fn verify_domains_match(a: &Url, b: &Url) -> Result<(), Error> { /// Check that both urls are identical. If not, return UrlVerificationError. /// /// ``` -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// # use activitypub_federation::protocol::verification::verify_urls_match; -/// let a = Url::from_str("https://example.com/abc")?; -/// let b = Url::from_str("https://example.com/123")?; +/// let a = "https://example.com/abc".parse()?; +/// let b = "https://example.com/123".parse()?; /// assert!(verify_urls_match(&a, &b).is_err()); -/// # Ok::<(), Url::from_strError>(()) +/// # Ok::<(), url::ParseError>(()) /// ``` pub fn verify_urls_match(a: &Url, b: &Url) -> Result<(), Error> { if a != b { diff --git a/src/traits.rs b/src/traits.rs index c42f5ca..6e78cef 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -12,7 +12,7 @@ use std::{fmt::Debug, ops::Deref}; /// # use activitystreams_kinds::{object::NoteType, public}; /// # use chrono::{Local, DateTime, Utc}; /// # use serde::{Deserialize, Serialize}; -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// # use activitypub_federation::protocol::{public_key::PublicKey, helpers::deserialize_one_or_many}; /// # use activitypub_federation::config::Data; /// # use activitypub_federation::fetch::object_id::ObjectId; @@ -61,7 +61,7 @@ use std::{fmt::Debug, ops::Deref}; /// kind: Default::default(), /// id: self.ap_id.clone().into(), /// attributed_to: self.creator, -/// to: vec![public()], +/// to: vec![public().try_into()?], /// content: self.text, /// }) /// } @@ -161,7 +161,7 @@ pub trait Object: Sized + Debug { /// /// ``` /// # use activitystreams_kinds::activity::FollowType; -/// # use crate::url::Url; +/// # use activitypub_federation::url::Url; /// # use activitypub_federation::fetch::object_id::ObjectId; /// # use activitypub_federation::config::Data; /// # use activitypub_federation::traits::ActivityHandler; diff --git a/src/url.rs b/src/url.rs index 0423f06..f3b3166 100644 --- a/src/url.rs +++ b/src/url.rs @@ -62,3 +62,21 @@ impl FromStr for Url { Ok(Url(url)) } } + +#[cfg(test)] +#[allow(clippy::unwrap_used)] +mod test { + use super::*; + use crate::error::Error; + use std::str::FromStr; + + #[tokio::test] + async fn test_url() -> Result<(), Error> { + assert!(Url::from_str("http://example.com").is_ok()); + assert!(Url::try_from(url::Url::from_str("http://example.com")?).is_ok()); + + assert!(Url::from_str("http://127.0.0.1").is_err()); + assert!(Url::try_from(url::Url::from_str("http://127.0.0.1")?).is_err()); + Ok(()) + } +}