1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! # ActivityStreams New
//! _A set of Traits and Types that make up the ActivityStreams and ActivityPub specifications_
//!
//! - Find the code on [git.asonix.dog](https://git.asonix.dog/Aardwolf/activitystreams)
//! - Read the docs on [docs.rs](https://docs.rs/activitystreams)
//! - Join the matrix channel at [#activitypub:asonix.dog](https://matrix.to/#/!fAEcHyTUdAaKCzIKCt:asonix.dog?via=asonix.dog&via=matrix.org&via=t2bot.io)
//! - Hit me up on [mastodon](https://asonix.dog/@asonix)
//!
//! ## Usage
//!
//! First, add ActivityStreams to your dependencies
//! ```toml
//! [dependencies]
//! activitystreams = "0.7.0-alpha.4"
//! ```
//!
//! ### Types
//!
//! The project is laid out by Kind => Type
//!
//! So to use an ActivityStreams Video, you'd write
//! ```rust
//! use activitystreams::object::Video;
//! let video = Video::new();
//! ```
//!
//! And to use an ActivityPub profile, you'd write
//! ```rust
//! use activitystreams::object::{ApObject, Profile};
//! let inner = Profile::new();
//! let profile = ApObject::new(inner);
//! ```
//!
//! There's only one kind of Link
//! ```rust
//! use activitystreams::link::Mention;
//! let mention = Mention::new();
//! ```
//!
//! ### Fields
//!
//! Many fields on the provided types are wrapped in `OneOrMany<>` or have a type of `AnyBase`. This
//! is because the activitystreams spec is very open as to what is considered a valid structure.
//!
//! For example, the Object type in ActivityStreams has a `summary` field, which can either be
//! represented as an `xsd:string` or an `rdf:langString`. It also states that the `summary` field
//! is not `functional`, meaning that any number of `xsd:string` or `rdf:langString`, or a
//! combination thereof, can be present. This library represents this as `Option<OneOrMany<AnyString>>`.
//!
//! This resulting type is exactly specific enough to match the following valid ActivityStreams
//! json, without matching any invalid json.
//!
//! With no summary:
//! ```json
//! {}
//! ```
//!
//! With a string summary:
//! ```json
//! {
//!     "summary": "A string"
//! }
//! ```
//!
//! With an rdf langstring
//! ```json
//! {
//!     "summary": {
//!         "@value": "A string",
//!         "@language": "en"
//!     }
//! }
//! ```
//!
//! With multiple values
//! ```json
//! {
//!     "summary": [
//!         {
//!             "@value": "A string",
//!             "@language": "en"
//!         },
//!         "An xsd:string this time"
//!     ]
//! }
//! ```
//!
//! It may seem like interacting with these types might get unweildy, there are some custom methods
//! implemented on the `OneOrMany` type depending on what's inside of it.
//!
//! ```rust,ignore
//! fn from_xsd_string<T>(&mut self, T) -> Self;
//! fn from_rdf_lang_string<T>(&mut self, T) -> Self;
//!
//! fn as_single_xsd_string(&self) -> Option<&str>;
//! fn as_single_rdf_langstring(&self) -> Option<&RdfLangString>;
//!
//! fn single_xsd_string(self) -> Option<String>;
//! fn single_rdf_lang_string(self) -> Option<RdfLangString>;
//!
//! fn add_xsd_string<T>(&mut self, T) -> &mut Self;
//! fn add_rdf_lang_string<T>(&mut self, T) -> &mut Self;
//! ```
//! These methods provide access to setting and fetching uniformly typed data, as well as deleting
//! the data. In the setter methods, the type parameter T is bound by
//! `Into<String>` or `Into<RdfLangString>`. This allows passing values to the method that
//! can be converted into the types, rather than requiring the caller to perform the conversion.
//!
//! Types like `RdfLangString` can be found in the `primitives` module. Unless
//! you're building your own custom types, you shouldn't need to import them yourself. They each
//! implement `FromStr` for parsing and `Display` to convert back to strings, as well as `From` and
//! `Into` or `TryFrom` and `TryInto` for types you might expect them to (e.g.
//! `XsdNonNegativeInteger` implements `From<u64>` and `Into<u64>`).
//!
//! ### Traits
//!
//! Since ActivityStreams is a heirarchical structure of data, it's represented as structs containing
//! other structs. This means that the `context` field, which can be present on any ActivityStreams type,
//! will be located in the innermost struct. In order to avoid writing code like
//! `ap_object.collection.object.base.context = Some(context())`, this library provides traits that are
//! automatically implmeneted for provided types.
//!
//! For example, the `BaseExt` trait provides the following methods for `context`,
//! ```rust,ignore
//! fn context(&self) -> Option<&OneOrMany<AnyBase>>;
//!
//! fn set_context<T>(&mut self, context: T) -> &mut Self
//! where
//!     T: Into<AnyBase>;
//!
//! fn set_many_contexts<I, T>(&mut self, items: I) -> &mut Self
//! where
//!     I: IntoIterator<Item = T>,
//!     T: Into<AnyBase>;
//!
//! fn add_context<T>(&mut self, context: T) -> &mut Self
//! where
//!     T: Into<AnyBase>;
//!
//! fn take_context(&mut self) -> Option<OneOrMany<AnyBase>>;
//! fn delete_context(&mut self) -> &mut Self;
//! ```
//!
//! For fields with more specific bounds, like `id`,
//! ```rust,ignore
//! fn id(&self) -> Option<&Url>;
//! fn set_id(&mut self, Url) -> &mut Self;
//! fn take_id(&self) -> Option<Url>;
//! fn delete_id(&mut self) -> &mut Self;
//! ```
//!
//! The full list of extension traits that implement methods like these on types can be found in the
//! prelude module. By using `use activitystreams::prelude::*;` all of the methods will be
//! implemented for types containing their fields.
//!
//! ### Markers
//!
//! This library provides a number of traits, such as `Object`, `Link`, `Actor`, `Activity`,
//! `Collection`, and `CollectionPage`. The majority of these traits exist solely to "mark" types,
//! meaning they don't provide value, at runtime, but exist to add constraints to generics at
//! compiletime.
//!
//! If you want to make a function that manipulates an Activity, but not a normal object, you could
//! bound the function like so:
//!
//! ```rust
//! use activitystreams::{base::BaseExt, context, markers::Activity, uri};
//!
//! fn manipulator<T, Kind>(mut activity: T) -> Result<(), anyhow::Error>
//! where
//!     T: Activity + BaseExt<Kind>,
//! {
//!     activity
//!         .set_id(uri!("https://example.com"))
//!         .set_context(context());
//!     Ok(())
//! }
//! ```
//!
//! ### Kinds
//!
//! This library has a set of unit structs that serialize and deserialize to strings. This is to
//! enable different ActivityPub Object types to be deserialized into different Named structs.
//! These can be found in `activitystreams::objects::kind`, and similar paths.
//!
//! To build your own Person struct, for example, you could write
//! ```rust
//! use activitystreams::actor::kind::PersonType;
//!
//! #[derive(serde::Deserialize, serde::Serialize)]
//! pub struct MyPerson {
//!     // Do a rename since `type` is not a valid rust field name
//!     #[serde(rename = "type")]
//!     kind: PersonType,
//! }
//! ```
//! And this type would only deserialize for JSON where `"type":"Person"`
//!
//! ## Examples
//!
//! ### Create
//!
//! ```rust
//! use activitystreams::{
//!     context,
//!     object::{ApObject, Video},
//!     prelude::*,
//!     uri,
//! };
//! use chrono::Duration;
//!
//! fn main() -> Result<(), anyhow::Error> {
//!     let mut video = ApObject::new(Video::new());
//!
//!     video
//!         .set_context(context())
//!         .set_id(uri!("https://example.com/@example/lions"))
//!         .set_media_type("video/webm".parse()?)
//!         .set_url(uri!("https://example.com/@example/lions/video.webm"))
//!         .set_summary("A cool video")
//!         .set_duration(Duration::minutes(4) + Duration::seconds(20))
//!         .set_shares(uri!("https://example.com/@example/lions/video.webm#shares"));
//!
//!     println!("Video, {:#?}", video);
//!
//!     let s = serde_json::to_string(&video)?;
//!
//!     println!("json, {}", s);
//!
//!     let v: ApObject<Video> = serde_json::from_str(&s)?;
//!
//!     println!("Video again, {:#?}", v);
//!
//!     Ok(())
//! }
//! ```
//!
//! ### Parse
//!
//! ```rust
//! use activitystreams::{activity::ActorAndObject, prelude::*};
//!
//! #[derive(Clone, Debug, serde::Deserialize, serde::Serialize)]
//! pub enum AcceptedTypes {
//!     Accept,
//!     Announce,
//!     Create,
//!     Delete,
//!     Follow,
//!     Reject,
//!     Update,
//!     Undo,
//! }
//!
//! pub type AcceptedActivity = ActorAndObject<AcceptedTypes>;
//!
//! pub fn handle_activity(activity: AcceptedActivity) -> Result<(), anyhow::Error> {
//!     println!("Actor: {:?}", activity.actor());
//!     println!("Object: {:?}", activity.object());
//!
//!     match activity.kind() {
//!         Some(AcceptedTypes::Accept) => println!("Accept"),
//!         Some(AcceptedTypes::Announce) => println!("Announce"),
//!         Some(AcceptedTypes::Create) => println!("Create"),
//!         Some(AcceptedTypes::Delete) => println!("Delete"),
//!         Some(AcceptedTypes::Follow) => println!("Follow"),
//!         Some(AcceptedTypes::Reject) => println!("Reject"),
//!         Some(AcceptedTypes::Update) => println!("Update"),
//!         Some(AcceptedTypes::Undo) => println!("Undo"),
//!         None => return Err(anyhow::Error::msg("No activity type provided")),
//!     }
//!
//!     Ok(())
//! }
//!
//! static EXAMPLE_JSON: &str = r#"{"actor":"https://asonix.dog/users/asonix","object":"https://asonix.dog/users/asonix/posts/1","type":"Announce"}"#;
//!
//! fn main() -> Result<(), anyhow::Error> {
//!     handle_activity(serde_json::from_str(EXAMPLE_JSON)?)
//! }
//! ```
//!
//! ## 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/](http://www.gnu.org/licenses/).

#![doc(html_root_url = "https://docs.rs/activitystreams/0.7.0-alpha.4/activitystreams")]

pub mod activity;
pub mod actor;
pub mod base;
pub mod collection;
mod either;
pub mod error;
pub mod link;
mod macros;
pub mod markers;
pub mod object;
pub mod primitives;
pub mod unparsed;

pub extern crate chrono;
pub extern crate mime;
pub extern crate url;

pub fn context() -> url::Url {
    "https://www.w3.org/ns/activitystreams".parse().unwrap()
}

pub fn public() -> url::Url {
    "https://www.w3.org/ns/activitystreams#Public"
        .parse()
        .unwrap()
}

pub fn security() -> url::Url {
    "https://w3id.org/security/v1".parse().unwrap()
}

pub mod prelude {
    //! Extension traits that provide the majority of the helper methods of the crate
    //!
    //! ```rust
    //! # fn main() -> Result<(), anyhow::Error> {
    //! use activitystreams::{
    //!     activity::Create,
    //!     actor::{ApActor, Person},
    //!     context,
    //!     prelude::*,
    //!     public,
    //!     object::{ApObject, Image, Video},
    //!     security,
    //!     uri,
    //! };
    //! use chrono::Duration;
    //!
    //! let mut person = ApActor::new(
    //!     uri!("http://localhost:8080/inbox"),
    //!     Person::new(),
    //! );
    //! person
    //!     .set_outbox(uri!("http:/localhost:8080/outbox"))
    //!     .set_name("Demo Account")
    //!     .set_preferred_username("demo")
    //!     .set_id(uri!("https://localhost:8080/actor"))
    //!     .set_url(uri!("https://localhost:8080/actor"));
    //!
    //! let mut preview = Image::new();
    //!
    //! preview
    //!     .set_url(uri!("https://localhost:8080/preview.png"))
    //!     .set_media_type("image/png".parse()?)
    //!     .set_id(uri!("https://localhostst:8080/preview.png"));
    //!
    //! let mut video = ApObject::new(Video::new());
    //!
    //! video
    //!     .set_id(uri!("http://localhost:8080/video.webm"))
    //!     .set_url(uri!("http://localhost:8080/video.webm"))
    //!     .set_media_type("video/webm".parse()?)
    //!     .set_summary("A cool video")
    //!     .set_preview(preview.into_any_base()?)
    //!     .set_duration(Duration::minutes(4) + Duration::seconds(20))
    //!     .set_shares(uri!("http://localhost:8080/video.webm#shares"));
    //!
    //! let mut activity = Create::new(
    //!     person.into_any_base()?,
    //!     video.into_any_base()?
    //! );
    //!
    //! activity
    //!     .set_many_tos(vec![public()]);
    //! #
    //! # Ok(())
    //! # }
    //! ```

    pub use crate::{
        activity::{
            ActivityExt, ActorAndObjectRefExt, OptOriginRefExt, OptTargetRefExt, OriginRefExt,
            QuestionExt, TargetRefExt,
        },
        actor::ApActorExt,
        base::{BaseExt, ExtendsExt},
        collection::{CollectionExt, CollectionPageExt, OrderedCollectionPageExt},
        link::LinkExt,
        object::{ApObjectExt, ObjectExt, PlaceExt, ProfileExt, RelationshipExt, TombstoneExt},
    };
}