mirror of
https://git.asonix.dog/asonix/activitystreams.git
synced 2024-09-28 23:02:01 +00:00
Compare commits
No commits in common. "main" and "activitystreams-v0.7.0-alpha.22" have entirely different histories.
main
...
activityst
9 changed files with 89 additions and 178 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -2,5 +2,3 @@
|
|||
**/*.rs.bk
|
||||
Cargo.lock
|
||||
/*/target
|
||||
/.direnv
|
||||
/.envrc
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
[package]
|
||||
name = "activitystreams"
|
||||
description = "A set of core types and traits for activitystreams data"
|
||||
version = "0.7.0-alpha.25"
|
||||
version = "0.7.0-alpha.22"
|
||||
license = "GPL-3.0"
|
||||
authors = ["asonix <asonix@asonix.dog>"]
|
||||
repository = "https://git.asonix.dog/asonix/activitystreams"
|
||||
|
|
61
flake.lock
61
flake.lock
|
@ -1,61 +0,0 @@
|
|||
{
|
||||
"nodes": {
|
||||
"flake-utils": {
|
||||
"inputs": {
|
||||
"systems": "systems"
|
||||
},
|
||||
"locked": {
|
||||
"lastModified": 1687709756,
|
||||
"narHash": "sha256-Y5wKlQSkgEK2weWdOu4J3riRd+kV/VCgHsqLNTTWQ/0=",
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"rev": "dbabf0ca0c0c4bce6ea5eaf65af5cb694d2082c7",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "numtide",
|
||||
"repo": "flake-utils",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1688590700,
|
||||
"narHash": "sha256-ZF055rIUP89cVwiLpG5xkJzx00gEuuGFF60Bs/LM3wc=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "f292b4964cb71f9dfbbd30dc9f511d6165cd109b",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "NixOS",
|
||||
"ref": "nixos-unstable",
|
||||
"repo": "nixpkgs",
|
||||
"type": "github"
|
||||
}
|
||||
},
|
||||
"root": {
|
||||
"inputs": {
|
||||
"flake-utils": "flake-utils",
|
||||
"nixpkgs": "nixpkgs"
|
||||
}
|
||||
},
|
||||
"systems": {
|
||||
"locked": {
|
||||
"lastModified": 1681028828,
|
||||
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
"owner": "nix-systems",
|
||||
"repo": "default",
|
||||
"type": "github"
|
||||
}
|
||||
}
|
||||
},
|
||||
"root": "root",
|
||||
"version": 7
|
||||
}
|
25
flake.nix
25
flake.nix
|
@ -1,25 +0,0 @@
|
|||
{
|
||||
description = "activitystreams";
|
||||
|
||||
inputs = {
|
||||
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
|
||||
flake-utils.url = "github:numtide/flake-utils";
|
||||
};
|
||||
|
||||
outputs = { self, nixpkgs, flake-utils }:
|
||||
flake-utils.lib.eachDefaultSystem (system:
|
||||
let
|
||||
pkgs = import nixpkgs {
|
||||
inherit system;
|
||||
};
|
||||
in
|
||||
{
|
||||
packages.default = pkgs.hello;
|
||||
|
||||
devShell = with pkgs; mkShell {
|
||||
nativeBuildInputs = [ cargo cargo-outdated cargo-zigbuild clippy gcc protobuf rust-analyzer rustc rustfmt ];
|
||||
|
||||
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
||||
};
|
||||
});
|
||||
}
|
|
@ -438,7 +438,7 @@ pub trait AsActivityActorExt: AsActivityActor {
|
|||
let actor = self.actor_unchecked();
|
||||
|
||||
for any_base in actor {
|
||||
let id = any_base.id().ok_or(CheckError(None))?;
|
||||
let id = any_base.id().ok_or(CheckError)?;
|
||||
self.check_authority(id)?;
|
||||
}
|
||||
|
||||
|
@ -571,7 +571,7 @@ pub trait AsActivityObjectExt: AsActivityObject {
|
|||
let object = self.object_unchecked();
|
||||
|
||||
for any_base in object {
|
||||
let id = any_base.id().ok_or(CheckError(None))?;
|
||||
let id = any_base.id().ok_or(CheckError)?;
|
||||
self.check_authority(id)?;
|
||||
}
|
||||
|
||||
|
|
132
src/actor.rs
132
src/actor.rs
|
@ -21,8 +21,6 @@
|
|||
//! # Ok(())
|
||||
//! # }
|
||||
//! ```
|
||||
use std::collections::HashMap;
|
||||
|
||||
use crate::{
|
||||
base::{AsBase, Base, Extends},
|
||||
checked::CheckError,
|
||||
|
@ -32,23 +30,12 @@ use crate::{
|
|||
primitives::OneOrMany,
|
||||
unparsed::{Unparsed, UnparsedMut, UnparsedMutExt},
|
||||
};
|
||||
use iri_string::{components::AuthorityComponents, types::IriString};
|
||||
use iri_string::types::IriString;
|
||||
|
||||
pub use activitystreams_kinds::actor as kind;
|
||||
|
||||
use self::kind::*;
|
||||
|
||||
fn check_opt(
|
||||
iri: &IriString,
|
||||
authority_components: Option<&AuthorityComponents>,
|
||||
) -> Result<(), CheckError> {
|
||||
if iri.authority_components().as_ref() == authority_components {
|
||||
Ok(())
|
||||
} else {
|
||||
Err(CheckError(Some(iri.clone())))
|
||||
}
|
||||
}
|
||||
|
||||
/// Implementation trait for deriving ActivityPub Actor methods for a type
|
||||
///
|
||||
/// Any type implementing AsObject will automatically gain methods provided by ApActorExt
|
||||
|
@ -825,7 +812,7 @@ pub trait ApActorExt: AsApActor {
|
|||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
fn endpoints<'a>(&'a self) -> Result<Option<&'a Endpoints<IriString>>, CheckError>
|
||||
fn endpoints<'a>(&'a self) -> Result<Option<Endpoints<&'a IriString>>, CheckError>
|
||||
where
|
||||
Self: BaseExt,
|
||||
Self::Inner: 'a,
|
||||
|
@ -833,40 +820,36 @@ pub trait ApActorExt: AsApActor {
|
|||
if let Some(endpoints) = self.endpoints_unchecked() {
|
||||
let authority_opt = self.id_unchecked().and_then(|id| id.authority_components());
|
||||
|
||||
endpoints
|
||||
let mut any_failed = false;
|
||||
|
||||
any_failed |= endpoints
|
||||
.proxy_url
|
||||
.as_ref()
|
||||
.map(|i| check_opt(i, authority_opt.as_ref()))
|
||||
.transpose()?;
|
||||
endpoints
|
||||
.map(|u| u.authority_components() != authority_opt)
|
||||
.unwrap_or(false);
|
||||
any_failed |= endpoints
|
||||
.oauth_authorization_endpoint
|
||||
.as_ref()
|
||||
.map(|i| check_opt(i, authority_opt.as_ref()))
|
||||
.transpose()?;
|
||||
endpoints
|
||||
.map(|u| u.authority_components() != authority_opt)
|
||||
.unwrap_or(false);
|
||||
any_failed |= endpoints
|
||||
.oauth_token_endpoint
|
||||
.as_ref()
|
||||
.map(|i| check_opt(i, authority_opt.as_ref()))
|
||||
.transpose()?;
|
||||
endpoints
|
||||
.map(|u| u.authority_components() != authority_opt)
|
||||
.unwrap_or(false);
|
||||
any_failed |= endpoints
|
||||
.provide_client_key
|
||||
.as_ref()
|
||||
.map(|i| check_opt(i, authority_opt.as_ref()))
|
||||
.transpose()?;
|
||||
endpoints
|
||||
.map(|u| u.authority_components() != authority_opt)
|
||||
.unwrap_or(false);
|
||||
any_failed |= endpoints
|
||||
.sign_client_key
|
||||
.as_ref()
|
||||
.map(|i| check_opt(i, authority_opt.as_ref()))
|
||||
.transpose()?;
|
||||
endpoints
|
||||
.map(|u| u.authority_components() != authority_opt)
|
||||
.unwrap_or(false);
|
||||
any_failed |= endpoints
|
||||
.shared_inbox
|
||||
.as_ref()
|
||||
.map(|i| check_opt(i, authority_opt.as_ref()))
|
||||
.transpose()?;
|
||||
endpoints
|
||||
.nonstandard
|
||||
.values()
|
||||
.try_for_each(|v| check_opt(v, authority_opt.as_ref()))?;
|
||||
.map(|u| u.authority_components() != authority_opt)
|
||||
.unwrap_or(false);
|
||||
|
||||
if any_failed {
|
||||
return Err(CheckError);
|
||||
}
|
||||
|
||||
return Ok(Some(endpoints));
|
||||
}
|
||||
|
@ -885,11 +868,11 @@ pub trait ApActorExt: AsApActor {
|
|||
/// println!("{:?}", endpoints);
|
||||
/// }
|
||||
/// ```
|
||||
fn endpoints_unchecked<'a>(&'a self) -> Option<&'a Endpoints<IriString>>
|
||||
fn endpoints_unchecked<'a>(&'a self) -> Option<Endpoints<&'a IriString>>
|
||||
where
|
||||
Self::Inner: 'a,
|
||||
{
|
||||
self.ap_actor_ref().endpoints.as_ref()
|
||||
self.ap_actor_ref().endpoints.as_ref().map(|e| e.as_ref())
|
||||
}
|
||||
|
||||
/// Mutably fetch the endpoints for the current actor
|
||||
|
@ -906,11 +889,11 @@ pub trait ApActorExt: AsApActor {
|
|||
/// println!("{:?}", endpoints);
|
||||
/// }
|
||||
/// ```
|
||||
fn endpoints_mut<'a>(&'a mut self) -> Option<&'a mut Endpoints<IriString>>
|
||||
fn endpoints_mut<'a>(&'a mut self) -> Option<Endpoints<&'a mut IriString>>
|
||||
where
|
||||
Self::Inner: 'a,
|
||||
{
|
||||
self.ap_actor_mut().endpoints.as_mut()
|
||||
self.ap_actor_mut().endpoints.as_mut().map(|e| e.as_mut())
|
||||
}
|
||||
|
||||
/// Set the endpoints for the current actor
|
||||
|
@ -1151,14 +1134,6 @@ pub struct Endpoints<T> {
|
|||
/// - Functional: true
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
pub shared_inbox: Option<T>,
|
||||
|
||||
/// Any nonstandard endpoints present in the endpoints record end up here.
|
||||
///
|
||||
/// Software like Pleroma and Akkoma provide additional URLs here and extending Endpoints
|
||||
/// specifically the way objects are normally extended isn't possible for nested structs like
|
||||
/// this.
|
||||
#[serde(flatten)]
|
||||
pub nonstandard: HashMap<String, T>,
|
||||
}
|
||||
|
||||
/// A simple type to create an Actor out of any Object
|
||||
|
@ -1345,6 +1320,47 @@ impl<Inner> ApActor<Inner> {
|
|||
}
|
||||
|
||||
impl<T> Endpoints<T> {
|
||||
/// Borrow the current Endpoints struct
|
||||
///
|
||||
/// ```rust
|
||||
/// # fn main() -> Result<(), anyhow::Error> {
|
||||
/// use activitystreams::{actor::Endpoints, iri};
|
||||
/// use iri_string::types::IriString;
|
||||
///
|
||||
/// let uri = iri!("https://example.com");
|
||||
///
|
||||
/// let endpoints: Endpoints<IriString> = Endpoints {
|
||||
/// shared_inbox: Some(uri.clone()),
|
||||
/// ..Default::default()
|
||||
/// };
|
||||
///
|
||||
/// assert_eq!(endpoints.as_ref().shared_inbox, Some(&uri));
|
||||
/// # Ok(())
|
||||
/// # }
|
||||
/// ```
|
||||
pub fn as_ref(&self) -> Endpoints<&T> {
|
||||
Endpoints {
|
||||
proxy_url: self.proxy_url.as_ref(),
|
||||
oauth_authorization_endpoint: self.oauth_authorization_endpoint.as_ref(),
|
||||
oauth_token_endpoint: self.oauth_token_endpoint.as_ref(),
|
||||
provide_client_key: self.provide_client_key.as_ref(),
|
||||
sign_client_key: self.sign_client_key.as_ref(),
|
||||
shared_inbox: self.shared_inbox.as_ref(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Mutably borrow the endpoints struct
|
||||
pub fn as_mut(&mut self) -> Endpoints<&mut T> {
|
||||
Endpoints {
|
||||
proxy_url: self.proxy_url.as_mut(),
|
||||
oauth_authorization_endpoint: self.oauth_authorization_endpoint.as_mut(),
|
||||
oauth_token_endpoint: self.oauth_token_endpoint.as_mut(),
|
||||
provide_client_key: self.provide_client_key.as_mut(),
|
||||
sign_client_key: self.sign_client_key.as_mut(),
|
||||
shared_inbox: self.shared_inbox.as_mut(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Map the URLs in Endpoints from T to U
|
||||
///
|
||||
/// ```rust
|
||||
|
@ -1374,11 +1390,6 @@ impl<T> Endpoints<T> {
|
|||
provide_client_key: self.provide_client_key.map(f),
|
||||
sign_client_key: self.sign_client_key.map(f),
|
||||
shared_inbox: self.shared_inbox.map(f),
|
||||
nonstandard: self
|
||||
.nonstandard
|
||||
.into_iter()
|
||||
.map(|(k, v)| (k, (f)(v)))
|
||||
.collect(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1392,7 +1403,6 @@ impl<T> Default for Endpoints<T> {
|
|||
provide_client_key: None,
|
||||
sign_client_key: None,
|
||||
shared_inbox: None,
|
||||
nonstandard: HashMap::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -263,7 +263,7 @@ pub trait BaseExt: AsBase {
|
|||
let authority = self
|
||||
.id_unchecked()
|
||||
.and_then(|id| id.authority_components())
|
||||
.ok_or_else(|| CheckError(Some(iri.as_ref().to_owned())))?;
|
||||
.ok_or(CheckError)?;
|
||||
check(iri, authority.host(), authority.port())
|
||||
}
|
||||
|
||||
|
@ -290,7 +290,7 @@ pub trait BaseExt: AsBase {
|
|||
if authority.host() == host && authority.port() == port {
|
||||
Some(Ok(id))
|
||||
} else {
|
||||
Some(Err(CheckError(Some(id.clone()))))
|
||||
Some(Err(CheckError))
|
||||
}
|
||||
})
|
||||
.transpose()
|
||||
|
@ -342,7 +342,7 @@ pub trait BaseExt: AsBase {
|
|||
if authority.host() == host && authority.port() == port {
|
||||
Some(Ok(id))
|
||||
} else {
|
||||
Some(Err(CheckError(Some(id.clone()))))
|
||||
Some(Err(CheckError))
|
||||
}
|
||||
})
|
||||
.transpose()
|
||||
|
|
|
@ -1,15 +1,11 @@
|
|||
use iri_string::types::{IriStr, IriString};
|
||||
use iri_string::types::IriStr;
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct CheckError(pub(crate) Option<IriString>);
|
||||
pub struct CheckError;
|
||||
|
||||
impl std::fmt::Display for CheckError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
if let Some(iri) = &self.0 {
|
||||
write!(f, "IRI failed host and port check: {}", iri)
|
||||
} else {
|
||||
write!(f, "IRI missing")
|
||||
}
|
||||
write!(f, "IRI failed host and port check")
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -20,13 +16,10 @@ pub(crate) fn check<T: AsRef<IriStr>>(
|
|||
host: &str,
|
||||
port: Option<&str>,
|
||||
) -> Result<T, CheckError> {
|
||||
let authority = iri
|
||||
.as_ref()
|
||||
.authority_components()
|
||||
.ok_or_else(|| CheckError(Some(iri.as_ref().to_owned())))?;
|
||||
let authority = iri.as_ref().authority_components().ok_or(CheckError)?;
|
||||
|
||||
if authority.host() != host || authority.port() != port {
|
||||
return Err(CheckError(Some(iri.as_ref().to_owned())));
|
||||
return Err(CheckError);
|
||||
}
|
||||
|
||||
Ok(iri)
|
||||
|
|
|
@ -222,16 +222,7 @@ impl From<&str> for Unit {
|
|||
|
||||
/// A list of units of length that represent valid units for certain ActivityStreams objects
|
||||
#[derive(
|
||||
Clone,
|
||||
Debug,
|
||||
Default,
|
||||
Eq,
|
||||
Hash,
|
||||
Ord,
|
||||
PartialEq,
|
||||
PartialOrd,
|
||||
serde::Deserialize,
|
||||
serde::Serialize,
|
||||
Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, serde::Deserialize, serde::Serialize,
|
||||
)]
|
||||
#[serde(untagged)]
|
||||
enum Length {
|
||||
|
@ -247,7 +238,6 @@ enum Length {
|
|||
#[serde(rename = "km")]
|
||||
Kilometers,
|
||||
|
||||
#[default]
|
||||
#[serde(rename = "m")]
|
||||
Meters,
|
||||
}
|
||||
|
@ -286,6 +276,12 @@ impl Length {
|
|||
}
|
||||
}
|
||||
|
||||
impl Default for Length {
|
||||
fn default() -> Self {
|
||||
Length::Meters
|
||||
}
|
||||
}
|
||||
|
||||
impl std::str::FromStr for Length {
|
||||
type Err = LengthError;
|
||||
|
||||
|
|
Loading…
Reference in a new issue