Implementing Activity Streams
An activity comprises a number of elements such as the person creating
the activity, the object being acted upon, and the target of the
activity. For instance, consider the following sentence:
Mary added to a new photo into her collection.
If we break this sentence down by Activity Stream standards, we
can describe the following items:
- Mary
The actor of the activity. We may
create an actor
object for Mary
that links back to her profile or provides additional information
about her.
- photo
The object of the activity. The photo
may be a structure that contains media information such as a URI,
width, and height.
- collection
The target of the activity. This target
may be composed of many different objects such as photos and
videos. The collection can be expanded with additional information
or objects.
The Activity Streams specification goes beyond defining just a few
simple constructs, attempting to build out a comprehensive model for
developers and companies to structure their updates and activities
according to the data they contain:
{
"items" : [
{
"verb": "post",
"published": "2011-02-18T16:26:43Z",
"generator": {
"url": "http://example.com/activities-app"
},
"provider": {
"url": "http://providersite.com/activity-stream"
},
"title": "Mary added a new photo to her album.",
"actor": {
"url": "http://providersite.com/mary",
"objectType": "person",
"id": "tag:provider.com,20110218,162643:mary",
"image": {
"url": "http://providersite.com/mary/image",
"width": 125,
"height": 125
},
"displayName": "Mary Smith"
},
"object" : {
"url": "http://providersite.com/mary/album/my_place.jpg",
"objectType": "http://activitystrea.ms/schema/1.0/photo",
"id": "tag:provider.com,20110218,162643:my_place",
"image": {
"url": "http://providersite.com/mary/album/my_place_thumb.jpg",
"width": 100,
"height": 100
}
},
"target": {
"url": "http://targetsite.com/mary/album/",
"objectType": "http://activitystrea.ms/schema/1.0/photo-album",
"id": "tag:example.org,20110218,162643:album4323",
"displayName": "Mary's Photo Album",
"image": {
"url": "http://providersite.com/mary/album/thumbnail.jpg",
"width": 100,
"height": 100
}
}
}
]
}
This basic example shows how an activity (or stream of activities)
may be presented on a platform. It contains our major activity sections
and provides an extensive amount of information about each individual
source.
Table 10-7 shows
the main fields we can set within an activity.
Table 10-7. Main settable sections within an activity
Property | Type | Description |
---|
actor
| object | A single object containing the person or entity that
performed the activity. Each activity must contain one actor property. |
content (optional)
| string | A human-readable HTML string that describes the
activity. Thumbnail images may also be included in the
markup. |
generator (optional)
| object | An object that describes the application or provider
that generated the activity. |
icon (optional)
| string | An
IRI (Internationalized Resource Identifier)
that identifies an image that represents the
activity that is produced. The image should have an aspect ratio of height equal
to width (e.g., 20×20 square) and should be legible at small
sizes (i.e., for use as a small icon associated with the
activity). |
id (optional)
| string | A permanent, universally unique identifier for the
activity in the form of an absolute IRI. In the case where an
id is not present, the URL
may be used as a less reliable identifier. |
object
| object | The primary object that is being described in an
activity. For example, in the sentence, “Erik just purchased a
new phone on Amazon,” the object of the activity is the phone.
If this field is not present, the object may be inferred from
the context of the activity. |
published
| date-time | The date and time at which the activity occurred. This
may be different from the time that it was published on the
associated site—for example, if there was a posting delay. This
field must be present in an activity. |
provider (optional)
| object | An object that describes the application or provider
that published the activity. This may not be the same as the
source that created the activity, but should be a single object
in either case. |
target (optional)
| object | The object that is the target of the activity. For
instance, if the activity is “Erik just added a new item to his
Amazon wish list,” the target of the activity is the “Amazon
wish list.” This should be a single direct target, not an
indirect target source. |
title (optional)
| string | A human-readable string that may contain HTML and
that is used as a title or headline for the
activity. |
updated (optional)
| date-time | If an activity has been modified, this field should
include the date and time at which the modification
occurred. |
url (optional)
| string | An IRI that links to an HTML representation of the
activity. |
verb
| string | The verb string that describes the type of action in
the activity and is required for each activity posted. Relative
string representations other than a simple name (e.g., post ) are not allowed. |
These are the main, top-level items that we can set. Some of them
have specific types that may also be set, such as string or date-time;
however, many of them have a type set to object. This means that the
item in question (e.g., actor
or
target
) represents a larger topic
that may be expanded. Each item in Table 10-7 that has a type
set to object may have alternate attributes defined by the JSON Activity Streams specification, as you can see in the
following example of an object comprising the actor
element:
"actor": {
"url": "http://providersite.com/mary",
"objectType": "person",
"id": "tag:provider.com,20110218,162643:mary",
"image": {
"url": "http://providersite.com/mary/image",
"width": 125,
"height": 125
},
"displayName": "Mary Smith"
},
Table 10-8 provides more
details on the optional JSON attributes that may comprise an
object.
Table 10-8. Optional object attributes
Attribute | Type | Description |
---|
attachments
| array of objects | One or more objects that are related or relevant to the
activity being generated (similar to the concept of attaching
files in an email message). |
author
| object | An entity representing the person who created or authored
the object. The entity is itself represented as an object, and
an object may include a single author object. The person
who authored the object may differ from the
person who published the
object. |
content
| string | A description of the object, provided in natural
languages. This field may introduce HTML elements, including
image representations such as thumbnails. |
displayName
| string | A human-readable, plain-text name for the object.
This display name must not contain HTML markup. |
downstreamDuplicates
| array of
strings | One or more absolute IRIs that reference duplicate
objects in the activity. These may be duplicates in different
systems (e.g., if the provider has multiple, separate places
that display activities) and may prompt the implementer to
launch an object deduplication effort. |
id
| string | Provides a permanent and universally unique identifier for
the object. The id should be
presented in the form of an IRI. If an id is not present in the object, the
consumer of the object may use the URL as a less reliable
identifier. |
image
| media link | A human-consumable visual representation of the
object. (The media link type will be described further
shortly.) |
objectType
| string | A string displaying the type of object that is being
presented. This may be either the short form of the type or the
full IRI representation of the type. |
published
| date-time | The date and time when the object was
published. |
summary
| string | A human-readable string containing a summary of the
object. This string may include HTML markup and
images. |
updated
| date-time | If an update to an object is published, this field
should include the date and time when the update
occurred. |
upstreamDuplicates
| array of
strings | Much like downstreamDuplicates , this contains
objects that are duplicates of the current object. Instead of
duplicates that already exist, however, these are objects that
the provider knowingly duplicates after posting with a new
id . |
url
| string | A string that contains an IRI pointing to an
HTML-based page that provides more details about the object. For
instance, if the object is a Facebook page, url would point to it. |
Now we have a clearer definition of what an object is. All of the
attributes listed in Table 10-8 break
down into individual media types, such as string or additional objects.
But there is also a new type introduced in the table—media link.
A media link type defines an object that is geared toward
presenting image, audio, and video content. For example, if we return to
our previous actor
object, the
image
element contains the media link
object that displays a photo of our actor:
"actor": {
"url": "http://providersite.com/mary",
"objectType": "person",
"id": "tag:provider.com,20110218,162643:mary",
"image": {
"url": "http://providersite.com/mary/image",
"width": 125,
"height": 125
},
"displayName": "Mary Smith"
},
You can define any element that has a type of media link by using
a series of predefined attributes in the Activity Streams specification,
as listed in Table 10-9.
Table 10-9. Media link object attributes
Attribute | Type | Description |
---|
duration (optional)
| int | The duration of the media object in seconds. This
should be included where it makes sense (e.g., video and audio
files). |
height (optional)
| int | The height of the media object in pixels. Height
should be included where it makes sense (e.g., videos or
images). |
url
| string | The IRI of the media link resource. This must be
included within a media link object. |
width (optional)
| int | The width of the media object in pixels. Width should
be included where it makes sense (e.g., videos or
images). |
All of these elements define a base-level activity via the
Activity Streams JSON specification. Implementers of the specification
will work from this foundation to define their applications and extend
it with their own verbs, object types, and additional elements beyond
the defaults shown here.