In addition to the mandatory and recommended fields, extra custom fields may be added to an activity. Let's look at a more expressive example:
# Create a bit more complex activity
activity_data = {:actor => 'User:1', :verb => 'run', :object => 'Exercise:42',
:course => {:name => 'Golden Gate park', :distance => 10},
:participants => ['Thierry', 'Tommaso'],
:started_at => DateTime.now(),
:foreign_id => 'run:1',
:location => {:type => 'point', :coordinates => [37.769722,-122.476944] }
}
activity_response = user_feed_1.add_activity(activity_data)
// Create a bit more complex activity
activity = {'actor': 'User:1', 'verb': 'run', 'object': 'Exercise:42',
'course': {'name': 'Golden Gate park', 'distance': 10},
'participants': ['Thierry', 'Tommaso'],
'started_at': new Date(),
'foreign_id': 'run:1',
'location': {'type': 'point', 'coordinates': [37.769722,-122.476944] }
};
user1.addActivity(activity)
.then(function(data) { /* on success */ })
.catch(function(reason) { /* on failure */ });
import datetime
# Create a bit more complex activity
activity_data = {'actor': 'User:1', 'verb': 'run', 'object': 'Exercise:42',
'course': {'name': 'Golden Gate park', 'distance': 10},
'participants': ['Thierry', 'Tommaso'],
'started_at': datetime.datetime.utcnow(),
'foreign_id': 'run:1',
'location': {'type': 'point', 'coordinates': [37.769722,-122.476944] }
}
user_feed_1.add_activity(activity_data)
// Create a bit more complex activity
$now = new DateTime('now');
$data = [
'actor' => 'User:1',
'verb' => 'run',
'object' => 1,
'course' => ['name'=> 'Golden Gate park', 'distance'=> 10],
'participants' => ['Thierry', 'Tommaso'],
'started_at' => $now,
'foreign_id' => 'run:1',
'location' => [
'type'=> 'point',
'coordinates'=> [
37.769722, -122.476944,
],
],
];
$userFeed1->addActivity($data);
// Create a bit more complex activity
Activity activity = Activity.builder()
.actor("User:1")
.verb("run")
.object("Exercise:42")
.foreignID("run:1")
.extra(new ImmutableMap.Builder<String, Object>()
.put("course", new ImmutableMap.Builder<String, Object>()
.put("name", "Golden Gate park")
.put("distance", 10)
.build())
.put("participants", new String[]{
"Thierry",
"Tommaso",
})
.put("started_at", LocalDateTime.now())
.put("location", new ImmutableMap.Builder<String, Object>()
.put("type", "point")
.put("coordinates", new double[]{37.769722, -122.476944})
.build())
.build())
.build();
userFeed.addActivity(activity).join();
// Create a bit more complex activity
activity := stream.Activity{
Actor: "User:1",
Verb: "run",
Object: "Exercise:42",
ForeignID: "run:1",
Extra: map[string]interface{}{
"course": map[string]interface{}{
"name": "Golden Gate park",
"distance": 10,
},
"participants": []string{
"Thierry",
"Tommaso",
},
"started_at": time.Now(),
},
}
_, err := userFeed.AddActivity(activity)
if err != nil {
panic(err)
}
// Create a custom Activity class.
final class ExerciseActivity: Activity {
private enum CodingKeys: String, CodingKey {
case course
case participants
case startDate = "started_at"
}
var course: Course
var participants: [String] = []
var startDate: Date = Date()
init(actor: String,
verb: String,
object: String,
course: Course,
participants: [String],
startDate: Date = Date()) {
super.init(actor: actor, verb: verb, object: object)
self.course = course
self.participants = participants
self.startDate = startDate
}
required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
course = try container.decode(Course.self, forKey: .course)
participants = try container.decode([String].self, forKey: .participants)
startDate = try container.decode(Date.self, forKey: .startDate)
try super.init(from: decoder)
}
override public func encode(to encoder: Encoder) throws {
var container = encoder.container(keyedBy: CodingKeys.self)
try container.encode(course, forKey: .course)
try container.encode(participants, forKey: .participants)
try container.encode(startDate, forKey: .startDate)
try super.encode(to: encoder)
}
}
struct Course: Codable {
let name: String
let distance: Float
}
let exerciseActivity = ExerciseActivity(actor: "User:1",
verb: "run",
object: "Exercise:42",
course: Course(name: "Golden Gate Park", distance: 10),
participants: ["Thierry", "Tommaso"])
// Add the exercise activity to the `user1` feed.
user1.add(exerciseActivity) { result in
print(result)
}
// Get a list of exercise activities.
user1.get(typeOf: ExerciseActivity.self) { result in
print(result)
}
// Create a bit more complex activity
var activity = new Activity("User:1", "run", "Exercise:42")
{
ForeignId = "run:1"
};
var course = new Dictionary<string, object>();
course["name"] = "Shevlin Park";
course["distance"] = 10;
var participants = new string[] { "Thierry", "Tommaso" };
var location = new Dictionary<string, object>();
location.Add("type", "point");
location.Add("coordinates", new float[] {37.769722F, -122.476944F});
activity.SetData("location", location);
activity.SetData("course", course);
activity.SetData("participants", participants);
For performance reasons activities are limited in size (128KB) and must not contain blob/binary data (eg. base64 encoded images). Use references and identifiers to facilitate Activity enrichment by your backend or client.
{
id:"ef696c12-69ab-11e4-8080-80003644b625",
actor:"User:1",
course:{
distance:10,
name:"Golden Gate Park"
},
object:"Exercise:42",
participants:[
"Thierry",
"Tommaso"
],
started_at:"2014-11-11T15:06:16+01:00",
target:null,
time:"2014-11-11T14:06:30.494",
verb:"run"
}
{
id:"ef696c12-69ab-11e4-8080-80003644b625",
actor:"User:1",
course:{
distance:10,
name:"Golden Gate Park"
},
object:"Exercise:42",
participants:[
"Thierry",
"Tommaso"
],
started_at:"2014-11-11T15:06:16+01:00",
target:null,
time:"2014-11-11T14:06:30.494",
verb:"run"
}
{
id:"ef696c12-69ab-11e4-8080-80003644b625",
actor:"User:1",
course:{
distance:10,
name:"Golden Gate Park"
},
object:"Exercise:42",
participants:[
"Thierry",
"Tommaso"
],
started_at:"2014-11-11T15:06:16+01:00",
target:null,
time:"2014-11-11T14:06:30.494",
verb:"run"
}
{
"id": "ef696c12-69ab-11e4-8080-80003644b625",
"actor": "User:1",
"course": {
"distance":10,
"name":"Golden Gate Park"
},
"object": "Exercise:42",
"participants": [
"Thierry",
"Tommaso"
],
"started_at": "2014-11-11T15:06:16+01:00",
"target": null,
"time": "2014-11-11T14:06:30.494",
"verb": "run"
}
{
id:"ef696c12-69ab-11e4-8080-80003644b625",
actor:"User:1",
course:{
distance:10,
name:"Golden Gate Park"
},
object:"Exercise:42",
participants:[
"Thierry",
"Tommaso"
],
started_at:"2014-11-11T15:06:16+01:00",
target:null,
time:"2014-11-11T14:06:30.494",
verb:"run"
}
{
id:"ef696c12-69ab-11e4-8080-80003644b625",
actor:"User:1",
course:{
distance:10,
name:"Golden Gate Park"
},
object:"Exercise:42",
participants:[
"Thierry",
"Tommaso"
],
started_at:"2014-11-11T15:06:16+01:00",
target:null,
time:"2014-11-11T14:06:30.494",
verb:"run"
}
{
id:"ef696c12-69ab-11e4-8080-80003644b625",
actor:"User:1",
course:{
distance:10,
name:"Golden Gate Park"
},
object:"Exercise:42",
participants:[
"Thierry",
"Tommaso"
],
started_at:"2014-11-11T15:06:16+01:00",
target:null,
time:"2014-11-11T14:06:30.494",
verb:"run"
}