#activity-stream #activity-pub

activitystreams-ext

Extension types for the ActivityStreams crate

4 releases

0.1.0-alpha.3 Dec 7, 2022
0.1.0-alpha.2 Jul 26, 2020
0.1.0-alpha.0 Jul 25, 2020

#4 in #activitypub

Download history 42/week @ 2024-01-01 71/week @ 2024-01-08 19/week @ 2024-01-15 46/week @ 2024-01-22 101/week @ 2024-01-29 32/week @ 2024-02-05 12/week @ 2024-02-12 35/week @ 2024-02-19 65/week @ 2024-02-26 27/week @ 2024-03-04 26/week @ 2024-03-11 45/week @ 2024-03-18 77/week @ 2024-03-25 55/week @ 2024-04-01 31/week @ 2024-04-08 22/week @ 2024-04-15

192 downloads per month
Used in ap-relay

GPL-3.0 license

41KB
1K SLoC

ActivityStreams Ext

This crate provides Ext1, Ext2, Ext3, and Ext4 for adding extensions to ActivityStreams types

Usage

First, add ActivityStreams to your dependencies

[dependencies]
activitystreams = "0.7.0-alpha.3"
activitystreams-ext = "0.1.0-alpha.2"

For an example, we'll implement a PublicKey extension and demonstrate usage with Ext1

use activitystreams::{
    actor::{ApActor, Person},
    context,
    prelude::*,
    security,
    unparsed::UnparsedMutExt,
    url::Url,
};
use activitystreams_ext::{Ext1, UnparsedExtension};

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKey {
    public_key: PublicKeyInner,
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
#[serde(rename_all = "camelCase")]
pub struct PublicKeyInner {
    id: Url,
    owner: Url,
    public_key_pem: String,
}

impl<U> UnparsedExtension<U> for PublicKey
where
    U: UnparsedMutExt,
{
    type Error = serde_json::Error;

    fn try_from_unparsed(unparsed_mut: &mut U) -> Result<Self, Self::Error> {
        Ok(PublicKey {
            public_key: unparsed_mut.remove("publicKey")?,
        })
    }

    fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error> {
        unparsed_mut.insert("publicKey", self.public_key)?;
        Ok(())
    }
}

pub type ExtendedPerson = Ext1<ApActor<Person>, PublicKey>;

fn main() -> Result<(), anyhow::Error> {
    let actor = ApActor::new("http://in.box".parse()?, Person::new());

    let mut person = Ext1::new(
        actor,
        PublicKey {
            public_key: PublicKeyInner {
                id: "http://key.id".parse()?,
                owner: "http://owner.id".parse()?,
                public_key_pem: "asdfasdfasdf".to_owned(),
            },
        },
    );

    person.set_context(context()).add_context(security());

    let any_base = person.into_any_base()?;
    println!("any_base: {:#?}", any_base);
    let person = ExtendedPerson::from_any_base(any_base)?;

    println!("person: {:#?}", person);
    Ok(())
}

Contributing

Feel free to open issues for anything you find an issue with. Please note that any contributed code will be licensed under the GPLv3.

License

Copyright © 2020 Riley Trautman

ActivityStreams is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

ActivityStreams is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. This file is part of ActivityStreams.

You should have received a copy of the GNU General Public License along with ActivityStreams. If not, see http://www.gnu.org/licenses/.

Dependencies

~2.5–3.5MB
~64K SLoC