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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
/*
 * This file is part of ActivityStreams Types.
 *
 * Copyright © 2018 Riley Trautman
 *
 * ActivityStreams Types 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 Types 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with ActivityStreams Types.  If not, see <http://www.gnu.org/licenses/>.
 */

//! Namespace for properties of standard object types
//!
//! To use these properties in your own types, you can flatten them into your struct with serde:
//!
//! ```rust
//! extern crate activitystreams_traits;
//! extern crate activitystreams_types;
//! extern crate serde;
//! #[macro_use]
//! extern crate serde_derive;
//!
//! use activitystreams_traits::Object;
//! use activitystreams_types::object::properties::ObjectProperties;
//!
//! #[derive(Clone, Debug, Serialize, Deserialize)]
//! #[serde(rename_all = "camelCase")]
//! pub struct MyObject {
//!     #[serde(rename = "type")]
//!     pub kind: String,
//!
//!     /// Define a require property for the MyObject type
//!     pub my_property: String,
//!
//!     #[serde(flatten)]
//!     pub object_properties: ObjectProperties,
//! }
//!
//! impl Object for MyObject {}
//! #
//! # fn main() {}
//! ```

use activitystreams_traits::{Collection, Error, Link, Object, Result};
use chrono::{offset::Utc, DateTime};
use mime;
use serde_json;

use object::Image;

/// Alias chrono::DateTime<Utc> for use in derive macros
pub type UtcTime = DateTime<Utc>;

/// Define all the properties of the Object base type as described by the Activity Streams
/// vocabulary.
///
/// In addition to having a global identifier (expressed as an absolute IRI using the id property)
/// and an "object type" (expressed using the type property), all instances of the Object type share
/// a common set of properties normatively defined by the Activity Vocabulary.
///
/// This struct does not provide an optional `type` property, if you are implementing your own
/// object type, you must supply your own type. This crate's provided object types all supply their
/// own `type` properties as Unit Structs with custom serde behaviour.
///
/// All properties are optional (including the id and type).
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]
#[serde(rename_all = "camelCase")]
pub struct ObjectProperties {
    // TODO: IRI type
    /// Provides the globally unique identifier for an Object or Link.
    ///
    /// The `id` property is expressed as an absolute IRI in the spec, but for now is represented
    /// as a string.
    ///
    /// - Range: `anyUri`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String), functional)]
    pub id: Option<serde_json::Value>,

    /// Identifies a resource attached or related to an object that potentially requires special
    /// handling.
    ///
    /// The intent is to provide a model that is at least semantically similar to attachments in
    /// email.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub attachment: Option<serde_json::Value>,

    /// Identifies one or more entities to which this object is attributed.
    ///
    /// The attributed entities might not be Actors. For instance, an object might be attributed to
    /// the completion of another activity.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub attributed_to: Option<serde_json::Value>,

    /// Identifies one or more entities that represent the total population of entities for which
    /// the object can considered to be relevant.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub audience: Option<serde_json::Value>,

    // TODO: rdf:langString
    /// The content or textual representation of the Object encoded as a JSON string.
    ///
    /// By default, the value of content is HTML. The mediaType property can be used in the object
    /// to indicate a different content type.
    ///
    /// The content MAY be expressed using multiple language-tagged values.
    ///
    /// - Range: `xsd:string` | `rdf:langString`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String))]
    pub content: Option<serde_json::Value>,

    /// Identifies the context within which the object exists or an activity was performed.
    ///
    /// The notion of "context" used is intentionally vague. The intended function is to serve as a
    /// means of grouping objects and activities that share a common originating context or purpose.
    /// An example could be all activities relating to a common project or event.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none", rename = "@context")]
    #[activitystreams(ab(Object, Link))]
    pub context: Option<serde_json::Value>,

    // TODO: rdf:langString
    /// A simple, human-readable, plain-text name for the object.
    ///
    /// HTML markup MUST NOT be included. The name MAY be expressed using multiple language-tagged
    /// values.
    ///
    /// - Range: `xsd:string` | `rdf:langString`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String))]
    pub name: Option<serde_json::Value>,

    /// The date and time describing the actual or expected ending time of the object.
    ///
    /// When used with an Activity object, for instance, the endTime property specifies the moment
    /// the activity concluded or is expected to conclude.
    ///
    /// - Range: `xsd:dateTime`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String, UtcTime), functional)]
    pub end_time: Option<serde_json::Value>,

    /// Identifies the entity (e.g. an application) that generated the object.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub generator: Option<serde_json::Value>,

    /// Indicates an entity that describes an icon for this object.
    ///
    /// The image should have an aspect ratio of one (horizontal) to one (vertical) and should be
    /// suitable for presentation at a small size.
    ///
    /// - Range: `Image` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link), concrete(Image))]
    pub icon: Option<serde_json::Value>,

    /// Indicates an entity that describes an image for this object.
    ///
    /// Unlike the icon property, there are no aspect ratio or display size limitations assumed.
    ///
    /// - Range: `Image` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link), concrete(Image))]
    pub image: Option<serde_json::Value>,

    /// Indicates one or more entities for which this object is considered a response.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub in_reply_to: Option<serde_json::Value>,

    /// Indicates one or more physical or logical locations associated with the object.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub location: Option<serde_json::Value>,

    /// Identifies an entity that provides a preview of this object.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub preview: Option<serde_json::Value>,

    /// The date and time at which the object was published.
    ///
    /// - Range: `xsd:dateTime`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String, UtcTime), functional)]
    pub published: Option<serde_json::Value>,

    /// Identifies a `Collection` containing objects considered to be responses to this object.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Collection), functional)]
    pub replies: Option<serde_json::Value>,

    /// The date and time describing the actual or expected starting time of the object.
    ///
    /// When used with an `Activity` object, for instance, the `start_time` property specifies the
    /// moment the activity began or is scheduled to begin.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String, UtcTime), functional)]
    pub start_time: Option<serde_json::Value>,

    // TODO: rdf:langString
    /// A natural language summarization of the object encoded as HTML.
    ///
    /// Multiple language tagged summaries MAY be provided.
    ///
    /// - Range: `xsd:string` | `rdf:langString`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String))]
    pub summary: Option<serde_json::Value>,

    /// One or more "tags" that have been associated with an objects. A tag can be any kind of
    /// `Object`.
    ///
    /// The key difference between attachment and tag is that the former implies association by
    /// inclusion, while the latter implies associated by reference.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub tag: Option<serde_json::Value>,

    /// The date and time at which the object was updated,
    ///
    /// - Range: `xsd:dateTime`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String, UtcTime), functional)]
    pub updated: Option<serde_json::Value>,

    /// Identifies one or more links to representations of the object.
    ///
    /// - Range: `xsd:anyUri` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String), ab(Link))]
    pub url: Option<serde_json::Value>,

    /// Identifies an entity considered to be part of the public primary audience of an `Object`.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub to: Option<serde_json::Value>,

    /// Identifies an `Object` that is part of the private primary audience of this `Object`.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub bto: Option<serde_json::Value>,

    /// Identifies an `Object` that is part of the public secondary audience of this `Object`.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub cc: Option<serde_json::Value>,

    /// Identifies one or more `Objects` that are part of the private secondary audience of this
    /// `Object`.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object, Link))]
    pub bcc: Option<serde_json::Value>,

    /// When used on an `Object`, identifies the MIME media type of the value of the content
    /// property.
    ///
    /// If not specified, the content property is assumed to contain text/html content.
    ///
    /// - Range: `Mime Media Type`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String), functional)]
    pub media_type: Option<serde_json::Value>,

    // TODO: xsd:duration
    /// When the object describes a time-bound resource, such as an audio or video, a meeting, etc,
    /// the duration property indicates the object's approximate duration.
    ///
    /// The value MUST be expressed as an xsd:duration as defined by
    /// [[xmlschema11-2](https://www.w3.org/TR/xmlschema11-2/)], section
    /// 3.3.6 (e.g. a period of 5 seconds is represented as "PT5S").
    ///
    /// - Range: `xsd:duration`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String), functional)]
    pub duration: Option<serde_json::Value>,
}

impl ObjectProperties {
    /// Fetch a typed `Mime` struct from the `media_type` field.
    pub fn media_type(&self) -> Result<mime::Mime> {
        self.media_type_string()
            .and_then(|s| s.parse().map_err(|_| Error::Deserialize))
    }
}

/// Define all the properties of the Location type as described by the Activity Streams vocabulary.
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]
#[serde(rename_all = "camelCase")]
pub struct PlaceProperties {
    /// Indicates the accuracy of position coordinates on a `Place` objects.
    ///
    /// Expressed in properties of percentage. e.g. "94.0" means "94.0% accurate".
    ///
    /// - Range: `xsd:float` [>= 0.0f, <= 100.0f]
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(f64), functional)]
    pub accuracy: Option<serde_json::Value>,

    /// Indicates the altitude of a place. The measurement units is indicated using the units
    /// property.
    ///
    /// If units is not specified, the default is assumed to be "m" indicating meters.
    ///
    /// - Range: `xsd:float`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(f64), functional)]
    pub altitude: Option<serde_json::Value>,

    /// The latitude of a place.
    ///
    /// - Range: `xsd:float`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(f64), functional)]
    pub latitude: Option<serde_json::Value>,

    /// The longitude of a place.
    ///
    /// - Range: `xsd:float`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(f64), functional)]
    pub longitude: Option<serde_json::Value>,

    /// The radius from the given latitude and longitude for a Place.
    ///
    /// The units is expressed by the units property. If units is not specified, the default is
    /// assumed to be "m" indicating "meters".
    ///
    /// - Range: `xsd:float`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(f64), functional)]
    pub radius: Option<serde_json::Value>,

    /// Specifies the measurement units for the radius and altitude properties on a `Place` object.
    ///
    /// If not specified, the default is assumed to be "m" for "meters".
    ///
    /// - Range: `xsd:float`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String), functional)]
    pub units: Option<serde_json::Value>,
}

/// Define all the properties of the Profile type as described by the Activity Streams vocabulary.
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]
#[serde(rename_all = "camelCase")]
pub struct ProfileProperties {
    /// On a `Profile` object, the describes property identifies the object described by the
    /// `Profile`.
    ///
    /// - Range: `Object`
    /// - Functional: true
    #[activitystreams(ab(Object), functional)]
    pub describes: serde_json::Value,
}

/// Define all the properties of the Relationship type as described by the Activity Streams
/// vocabulary.
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]
#[serde(rename_all = "camelCase")]
pub struct RelationshipProperties {
    /// On a `Relationship` object, the subject property identifies one of the connected
    /// individuals.
    ///
    /// For instance, for a `Relationship` object describing "John is related to Sally", subject
    /// would refer to John.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: true
    #[activitystreams(ab(Object, Link), functional)]
    subject: serde_json::Value,

    /// When used within a `Relationship` describes the entity to which the subject is related.
    ///
    /// - Range: `Object` | `Link`
    /// - Functional: false
    #[activitystreams(ab(Object, Link))]
    object: serde_json::Value,

    /// On a `Relationship` object, the relationship property identifies the kind of relationship
    /// that exists between subject and object.
    ///
    /// - Range: `Object`
    /// - Functional: false
    #[activitystreams(ab(Object))]
    relationship: serde_json::Value,
}

/// Define all the properties of the Tombstone type as described by the Activity Streams vocabulary.
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]
#[serde(rename_all = "camelCase")]
pub struct TombstoneProperties {
    /// On a `Tombstone` object, the formerType property identifies the type of the object that was
    /// deleted.
    ///
    /// - Range: `Object`
    /// - Functional: false
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(ab(Object))]
    pub former_type: Option<serde_json::Value>,

    /// On a `Tombstone` object, the deleted property is a timestamp for when the object was
    /// deleted.
    ///
    /// - Range: `xsd:dateTime`
    /// - Functional: true
    #[serde(skip_serializing_if = "Option::is_none")]
    #[activitystreams(concrete(String, UtcTime), functional)]
    pub deleted: Option<serde_json::Value>,
}