Compare commits

..

5 commits

Author SHA1 Message Date
754b2a0f3d Revert Object trait id() ref 2026-04-25 22:40:47 -07:00
Felix Ableitner
588f431266 Version 0.7.0-beta.11 2026-04-24 11:31:10 +02:00
Hong Minhee (洪 民憙)
838dd9e501
Add a public-aware deserializer for recipient URLs (#165)
* Accept Public aliases in URL deserializer

Update deserialize_one_or_many to deserialize recipient URL fields while
accepting `Public` and `as:Public` as aliases for the canonical
ActivityStreams public URL.

Add focused tests for single and array inputs, and verify that unrelated
string fields such as `content` are left unchanged.

https://github.com/LemmyNet/lemmy/issues/6465

* Deduplicate deserialized recipients

Drop repeated recipient URLs after deserialization so equivalent public
aliases such as `Public`, `as:Public`, and the canonical public URL do
not produce duplicate entries.

Update the helper documentation and tests to match the deduplicated
result.
2026-04-24 11:25:06 +02:00
Felix Ableitner
279d29d350 Version 0.7.0-beta.10 2026-04-15 13:39:03 +02:00
Nutomic
fcb69ebffe
Make IP check public (#164)
* Make IP check public

* change
2026-04-15 13:38:29 +02:00
12 changed files with 129 additions and 34 deletions

2
Cargo.lock generated
View file

@ -4,7 +4,7 @@ version = 4
[[package]]
name = "activitypub_federation"
version = "0.7.0-beta.9"
version = "0.7.0-beta.11"
dependencies = [
"activitystreams-kinds",
"actix-web",

View file

@ -1,6 +1,6 @@
[package]
name = "activitypub_federation"
version = "0.7.0-beta.9"
version = "0.7.0-beta.11"
edition = "2021"
description = "High-level Activitypub framework"
keywords = ["activitypub", "activitystreams", "federation", "fediverse"]

View file

@ -32,10 +32,10 @@ impl Object for SearchableDbObjects {
type Kind = SearchableObjects;
type Error = anyhow::Error;
fn id(&self) -> &Url {
fn id(&self) -> Url {
match self {
SearchableDbObjects::User(p) => &p.federation_id,
SearchableDbObjects::Post(n) => &n.federation_id,
SearchableDbObjects::User(p) => p.federation_id.clone(),
SearchableDbObjects::Post(n) => n.federation_id.clone(),
}
}

View file

@ -69,8 +69,8 @@ impl Object for DbUser {
type Kind = Person;
type Error = Error;
fn id(&self) -> &Url {
self.ap_id.inner()
fn id(&self) -> Url {
self.ap_id.inner().clone()
}
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {

View file

@ -50,8 +50,8 @@ impl Object for DbPost {
type Kind = Note;
type Error = Error;
fn id(&self) -> &Url {
self.ap_id.inner()
fn id(&self) -> Url {
self.ap_id.inner().clone()
}
async fn read_from_id(

View file

@ -134,8 +134,8 @@ impl Object for DbUser {
type Kind = Person;
type Error = Error;
fn id(&self) -> &Url {
self.ap_id.inner()
fn id(&self) -> Url {
self.ap_id.inner().clone()
}
fn last_refreshed_at(&self) -> Option<DateTime<Utc>> {

View file

@ -47,8 +47,8 @@ impl Object for DbPost {
type Kind = Note;
type Error = Error;
fn id(&self) -> &Url {
self.ap_id.inner()
fn id(&self) -> Url {
self.ap_id.inner().clone()
}
async fn read_from_id(

View file

@ -190,7 +190,7 @@ where
// PKey is internally like an Arc<>, so cloning is ok
data.config
.actor_pkey_cache
.try_get_with_by_ref(actor_id, async {
.try_get_with_by_ref(&actor_id, async {
let private_key_pem = actor.private_key_pem().ok_or_else(|| {
Error::Other(format!(
"Actor {actor_id} does not contain a private key for signing"

View file

@ -1,13 +1,22 @@
//! Serde deserialization functions which help to receive differently shaped data
use serde::{Deserialize, Deserializer};
use activitystreams_kinds::public;
use itertools::Itertools;
use serde::{de::Error, Deserialize, Deserializer};
use serde_json::Value;
use url::Url;
/// Deserialize JSON single value or array into Vec.
/// Deserialize JSON single value or array into `Vec<Url>`.
///
/// Useful if your application can handle multiple values for a field, but another federated
/// platform only sends a single one.
///
/// Also accepts common `Public` aliases for recipient fields. Some implementations send `Public`
/// or `as:Public` instead of the canonical `https://www.w3.org/ns/activitystreams#Public` URL
/// in fields such as `to` and `cc`.
///
/// ```
/// # use activitypub_federation::kinds::public;
/// # use activitypub_federation::protocol::helpers::deserialize_one_or_many;
/// # use url::Url;
/// #[derive(serde::Deserialize)]
@ -25,24 +34,39 @@ use serde::{Deserialize, Deserializer};
/// "https://lemmy.ml/u/bob"
/// ]}"#)?;
/// assert_eq!(multiple.to.len(), 2);
/// Ok::<(), anyhow::Error>(())
pub fn deserialize_one_or_many<'de, T, D>(deserializer: D) -> Result<Vec<T>, D::Error>
///
/// let note: Note = serde_json::from_str(r#"{"to": ["Public", "as:Public"]}"#)?;
/// assert_eq!(note.to, vec![public()]);
/// # Ok::<(), anyhow::Error>(())
/// ```
pub fn deserialize_one_or_many<'de, D>(deserializer: D) -> Result<Vec<Url>, D::Error>
where
T: Deserialize<'de>,
D: Deserializer<'de>,
{
#[derive(Deserialize)]
#[serde(untagged)]
enum OneOrMany<T> {
One(T),
Many(Vec<T>),
enum OneOrMany {
Many(Vec<Value>),
One(Value),
}
let result: OneOrMany<T> = Deserialize::deserialize(deserializer)?;
Ok(match result {
OneOrMany::Many(list) => list,
let result: OneOrMany = Deserialize::deserialize(deserializer)?;
let values = match result {
OneOrMany::One(value) => vec![value],
OneOrMany::Many(values) => values,
};
values
.into_iter()
.map(|value| match value {
Value::String(value) if matches!(value.as_str(), "Public" | "as:Public") => {
Ok(public())
}
Value::String(value) => Url::parse(&value).map_err(D::Error::custom),
value => Url::deserialize(value).map_err(D::Error::custom),
})
.collect::<Result<Vec<_>, _>>()
.map(|values| values.into_iter().unique().collect())
}
/// Deserialize JSON single value or single element array into single value.
@ -140,6 +164,11 @@ where
#[cfg(test)]
mod tests {
use super::deserialize_one_or_many;
use activitystreams_kinds::public;
use anyhow::Result;
use serde::Deserialize;
#[test]
fn deserialize_one_multiple_values() {
use crate::protocol::helpers::deserialize_one;
@ -155,4 +184,70 @@ mod tests {
);
assert!(note.is_err());
}
#[test]
fn deserialize_one_or_many_single_public_aliases() -> Result<()> {
use url::Url;
#[derive(Deserialize)]
struct Note {
#[serde(deserialize_with = "deserialize_one_or_many")]
to: Vec<Url>,
}
for alias in ["Public", "as:Public"] {
let note = serde_json::from_str::<Note>(&format!(r#"{{"to": "{alias}"}}"#))?;
assert_eq!(note.to, vec![public()]);
}
Ok(())
}
#[test]
fn deserialize_one_or_many_array() -> Result<()> {
use url::Url;
#[derive(Deserialize)]
struct Note {
#[serde(deserialize_with = "deserialize_one_or_many")]
to: Vec<Url>,
}
let note = serde_json::from_str::<Note>(
r#"{
"to": [
"https://example.com/c/main",
"Public",
"as:Public",
"https://www.w3.org/ns/activitystreams#Public"
]
}"#,
)?;
assert_eq!(
note.to,
vec![Url::parse("https://example.com/c/main")?, public(),]
);
Ok(())
}
#[test]
fn deserialize_one_or_many_leaves_other_strings_unchanged() -> Result<()> {
use url::Url;
#[derive(Deserialize)]
struct Note {
#[serde(deserialize_with = "deserialize_one_or_many")]
to: Vec<Url>,
content: String,
}
let note = serde_json::from_str::<Note>(r#"{"to": "Public", "content": "Public"}"#)?;
assert_eq!(note.to, vec![public()]);
assert_eq!(note.content, "Public");
Ok(())
}
}

View file

@ -30,7 +30,7 @@ where
type Error = E;
/// `id` field of the object
fn id(&self) -> &Url {
fn id(&self) -> Url {
match self {
Either::Left(l) => l.id(),
Either::Right(r) => r.id(),

View file

@ -53,7 +53,7 @@ pub mod tests;
/// type Kind = Note;
/// type Error = anyhow::Error;
///
/// fn id(&self) -> &Url { self.ap_id.inner() }
/// fn id(&self) -> Url { self.ap_id.inner().clone() }
///
/// async fn read_from_id(object_id: Url, data: &Data<Self::DataType>) -> Result<Option<Self>, Self::Error> {
/// // Attempt to read object from local database. Return Ok(None) if not found.
@ -110,7 +110,7 @@ pub trait Object: Sized + Debug {
type Error;
/// `id` field of the object
fn id(&self) -> &Url;
fn id(&self) -> Url;
/// Returns the last time this object was updated.
///
@ -194,8 +194,8 @@ pub trait Object: Sized + Debug {
redirect_remote_object,
};
let id = self.id();
let res = if !data.config.is_local_url(id) {
redirect_remote_object(id)
let res = if !data.config.is_local_url(&id) {
redirect_remote_object(&id)
} else if !self.is_deleted() {
let json = self.into_json(data).await?;
create_http_response(json, federation_context)?

View file

@ -73,8 +73,8 @@ impl Object for DbUser {
type Kind = Person;
type Error = Error;
fn id(&self) -> &Url {
&self.federation_id
fn id(&self) -> Url {
self.federation_id.clone()
}
async fn read_from_id(
@ -179,7 +179,7 @@ impl Object for DbPost {
type Kind = Note;
type Error = Error;
fn id(&self) -> &Url {
fn id(&self) -> Url {
todo!()
}