more fixes

This commit is contained in:
Felix Ableitner 2024-09-12 11:03:19 +02:00
parent b6e461a51b
commit d066cc9e13
10 changed files with 46 additions and 30 deletions

View file

@ -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 {

View file

@ -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)]

View file

@ -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()];

View file

@ -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)]

View file

@ -243,7 +243,7 @@ impl<T: Clone> Deref for FederationConfig<T> {
///
/// ```
/// # 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<T: Clone> Deref for FederationConfig<T> {
/// 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(())
}

View file

@ -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"))]);

View file

@ -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,

View file

@ -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 {

View file

@ -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;

View file

@ -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(())
}
}