From fa1a1851db07de8e6d7ca488fb344c5a4856be40 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 3 Jun 2013 08:55:00 -0400 Subject: [PATCH 01/20] Add an ID to registered service --- classes/User.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/classes/User.php b/classes/User.php index 32a04238c2..377a22e16d 100644 --- a/classes/User.php +++ b/classes/User.php @@ -1190,11 +1190,13 @@ class User extends Managed_DataObject $service->type = "service"; $service->displayName = common_config('site', 'name'); $service->url = common_root_url(); + $service->id = $service->url; $act = new Activity(); $act->actor = ActivityObject::fromProfile($profile); $act->verb = ActivityVerb::JOIN; + $act->objects[] = $service; $act->id = TagURI::mint('user:register:%d', From 8e3bd04d987aad7afe9d44e921befbfc5f9539a1 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Mon, 3 Jun 2013 09:10:56 -0400 Subject: [PATCH 02/20] Only a single object in activitystrea.ms JSON output --- lib/activity.php | 45 ++++++++++++++++++++++----------------------- 1 file changed, 22 insertions(+), 23 deletions(-) diff --git a/lib/activity.php b/lib/activity.php index e352baf639..3b7a4d2aaa 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -374,8 +374,27 @@ class Activity $activity['id'] = $this->id; // object - if ($this->verb == ActivityVerb::POST && count($this->objects) == 1) { - $activity['object'] = $this->objects[0]->asArray(); + + if (count($this->objects) > 1) { + common_log(LOG_WARNING, "Ignoring extra objects in JSON output for activity " . $this->id); + } else if (count($this->objects == 0)) { + common_log(LOG_ERR, "Can't save " . $this->id); + } else { + $object = $this->objects[0]; + + if ($object instanceof Activity) { + // Sharing a post activity is more like sharing the original object + if ($this->verb == 'share' && $object->verb == 'post') { + // XXX: Here's one for the obfuscation record books + $object = $object->object; + } + } + + $activity['object'] = $object->asArray(); + + if ($object instanceof Activity) { + $activity['object']['objectType'] = 'activity'; + } // Context stuff. For now I'm just sticking most of it // in a property called "context" @@ -385,14 +404,6 @@ class Activity if (!empty($this->context->location)) { $loc = $this->context->location; - // GeoJSON - - $activity['geopoint'] = array( - 'type' => 'Point', - 'coordinates' => array($loc->lat, $loc->lon), - 'deprecated' => true, - ); - $activity['location'] = array( 'objectType' => 'place', 'position' => sprintf("%+02.5F%+03.5F/", $loc->lat, $loc->lon), @@ -452,24 +463,12 @@ class Activity if ($enclosure->title) { $attachments[]['displayName'] = $enclosure->title; } - } + } } if (!empty($attachments)) { $activity['object']['attachments'] = $attachments; } - - } else { - $activity['object'] = array(); - foreach($this->objects as $object) { - $oa = $object->asArray(); - if ($object instanceof Activity) { - // throw in a type - // XXX: hackety-hack - $oa['objectType'] = 'activity'; - } - $activity['object'][] = $oa; - } } // published From 04f6e4ce7b8775ba305b539c155a752905979d07 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 15:20:00 -0400 Subject: [PATCH 03/20] Better registrationActivity --- classes/User.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/classes/User.php b/classes/User.php index 377a22e16d..7533110fea 100644 --- a/classes/User.php +++ b/classes/User.php @@ -1187,10 +1187,10 @@ class User extends Managed_DataObject $service = new ActivityObject(); - $service->type = "service"; - $service->displayName = common_config('site', 'name'); - $service->url = common_root_url(); - $service->id = $service->url; + $service->type = "service"; + $service->title = common_config('site', 'name'); + $service->link = common_root_url(); + $service->id = $service->link; $act = new Activity(); From 2ad5aece55fed8fb4242961bcb301ad33ae7ca38 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 15:20:21 -0400 Subject: [PATCH 04/20] Better handling of multiple objects --- lib/activity.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/activity.php b/lib/activity.php index 3b7a4d2aaa..592c56bcbd 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -375,11 +375,12 @@ class Activity // object - if (count($this->objects) > 1) { - common_log(LOG_WARNING, "Ignoring extra objects in JSON output for activity " . $this->id); - } else if (count($this->objects == 0)) { + if (count($this->objects) == 0) { common_log(LOG_ERR, "Can't save " . $this->id); } else { + if (count($this->objects) > 1) { + common_log(LOG_WARNING, "Ignoring " . (count($this->objects) - 1) . " extra objects in JSON output for activity " . $this->id); + } $object = $this->objects[0]; if ($object instanceof Activity) { From 795a4a02ba1dea9eb41a46511fc8b796053a86e2 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 16:29:47 -0400 Subject: [PATCH 05/20] Add the service type for activity objects --- lib/activityobject.php | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/activityobject.php b/lib/activityobject.php index 4c11be8597..f1c708db2e 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -69,6 +69,7 @@ class ActivityObject const COMMENT = 'http://activitystrea.ms/schema/1.0/comment'; // ^^^^^^^^^^ tea! const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity'; + const SERVICE = 'http://activitystrea.ms/schema/1.0/service'; // Atom elements we snarf From b493f3839c77d386aafcd19207fe91be4537dce6 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 16:30:40 -0400 Subject: [PATCH 06/20] Use better type, title for service --- classes/User.php | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/classes/User.php b/classes/User.php index 7533110fea..8d21d2bc19 100644 --- a/classes/User.php +++ b/classes/User.php @@ -1187,7 +1187,7 @@ class User extends Managed_DataObject $service = new ActivityObject(); - $service->type = "service"; + $service->type = ActivityObject::SERVICE; $service->title = common_config('site', 'name'); $service->link = common_root_url(); $service->id = $service->link; @@ -1207,9 +1207,8 @@ class User extends Managed_DataObject $act->title = _("Register"); $act->content = sprintf(_('%1$s joined %2$s.'), - $profile->getBestName(), - $service->displayName); - + $profile->getBestName(), + $service->title); return $act; } } From 879a6f9ce773d955b043d5a8a832e8a505d5dad3 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 17:00:51 -0400 Subject: [PATCH 07/20] Slightly better ActivityStreams JSON output --- lib/activity.php | 92 +++++++++++++++++++++++++----------------- lib/activityobject.php | 9 +++++ lib/activityverb.php | 9 +++++ 3 files changed, 74 insertions(+), 36 deletions(-) diff --git a/lib/activity.php b/lib/activity.php index 592c56bcbd..b8c9426268 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -397,37 +397,6 @@ class Activity $activity['object']['objectType'] = 'activity'; } - // Context stuff. For now I'm just sticking most of it - // in a property called "context" - - if (!empty($this->context)) { - - if (!empty($this->context->location)) { - $loc = $this->context->location; - - $activity['location'] = array( - 'objectType' => 'place', - 'position' => sprintf("%+02.5F%+03.5F/", $loc->lat, $loc->lon), - 'lat' => $loc->lat, - 'lon' => $loc->lon - ); - - $name = $loc->getName(); - - if ($name) { - $activity['location']['displayName'] = $name; - } - - $url = $loc->getURL(); - - if ($url) { - $activity['location']['url'] = $url; - } - } - - $activity['to'] = $this->context->getToArray(); - $activity['context'] = $this->context->asArray(); - } // Instead of adding enclosures as an extension to JSON // Activities, it seems like we should be using the @@ -471,6 +440,51 @@ class Activity $activity['object']['attachments'] = $attachments; } } + + // Context stuff. + + if (!empty($this->context)) { + + if (!empty($this->context->location)) { + $loc = $this->context->location; + + $activity['location'] = array( + 'objectType' => 'place', + 'position' => sprintf("%+02.5F%+03.5F/", $loc->lat, $loc->lon), + 'lat' => $loc->lat, + 'lon' => $loc->lon + ); + + $name = $loc->getName(); + + if ($name) { + $activity['location']['displayName'] = $name; + } + + $url = $loc->getURL(); + + if ($url) { + $activity['location']['url'] = $url; + } + } + + $activity['to'] = $this->context->getToArray(); + + $ctxarr = $this->context->asArray(); + + if (array_key_exists('inReplyTo', $ctxarr)) { + $activity['object']['inReplyTo'] = $ctxarr['inReplyTo']; + unset($ctxarr['inReplyTo']); + } + + if (!array_key_exists('status_net', $activity)) { + $activity['status_net'] = array(); + } + + foreach ($ctxarr as $key => $value) { + $activity['status_net'][$key] = $value; + } + } // published $activity['published'] = self::iso8601Date($this->time); @@ -496,10 +510,8 @@ class Activity // eceived a remote notice? Probably not. // verb - // - // We can probably use the whole schema URL here but probably the - // relative simple name is easier to parse - $activity['verb'] = substr($this->verb, strrpos($this->verb, '/') + 1); + + $activity['verb'] = ActivityVerb::canonical($this->verb); // url $activity['url'] = $this->id; @@ -527,7 +539,15 @@ class Activity foreach ($this->extra as $e) { list($objectName, $props, $txt) = $e; if (!empty($objectName)) { - $activity[$objectName] = $props; + $parts = explode(":", $objectName); + if (count($parts) == 2 && $parts[0] == "statusnet") { + if (!array_key_exists('status_net', $activity)) { + $activity['status_net'] = array(); + } + $activity['status_net'][$parts[1]] = $props; + } else { + $activity[$objectName] = $props; + } } } diff --git a/lib/activityobject.php b/lib/activityobject.php index f1c708db2e..13592ad32b 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -778,4 +778,13 @@ class ActivityObject } return array_filter($object); } + + static function canonicalType($type) { + $ns = 'http://activitystrea.ms/schema/1.0/'; + if (substr($type, 0, mb_strlen($ns)) == $ns) { + return substr($type, mb_strlen($ns)); + } else { + return $type; + } + } } diff --git a/lib/activityverb.php b/lib/activityverb.php index 5ee68f2880..513605b620 100644 --- a/lib/activityverb.php +++ b/lib/activityverb.php @@ -63,4 +63,13 @@ class ActivityVerb // For simple profile-update pings; no content to share. const UPDATE_PROFILE = 'http://ostatus.org/schema/1.0/update-profile'; + + static function canonical($verb) { + $ns = 'http://activitystrea.ms/schema/1.0/'; + if (substr($verb, 0, mb_strlen($ns)) == $ns) { + return substr($verb, mb_strlen($ns)); + } else { + return $verb; + } + } } From f66fedaac8d592bce9ae87a45cb8c18ffb62b054 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 17:12:28 -0400 Subject: [PATCH 08/20] Use status_net, portablecontacts_net namespaces --- lib/activityobject.php | 27 ++++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/lib/activityobject.php b/lib/activityobject.php index 13592ad32b..3f623ebcb9 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -722,7 +722,11 @@ class ActivityObject $avatarMediaLinks[] = $avatar->asArray(); } - $object['avatarLinks'] = $avatarMediaLinks; // extension + if (!array_key_exists('status_net', $object)) { + $object['status_net'] = array(); + } + + $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension // image if (!empty($imgLink)) { @@ -734,8 +738,8 @@ class ActivityObject // // We can probably use the whole schema URL here but probably the // relative simple name is easier to parse - // @fixme this breaks extension URIs - $object['objectType'] = substr($this->type, strrpos($this->type, '/') + 1); + + $object['objectType'] = ActivityObject::canonicalType($this->type); // summary $object['summary'] = $this->summary; @@ -756,9 +760,21 @@ class ActivityObject // @fixme these may collide with XML extensions // @fixme multiple tags of same name will overwrite each other // @fixme text content from XML extensions will be lost + foreach ($this->extra as $e) { list($objectName, $props, $txt) = $e; - $object[$objectName] = $props; + if (!empty($objectName)) { + $parts = explode(":", $objectName); + if (count($parts) == 2 && $parts[0] == "statusnet") { + if (!array_key_exists('status_net', $object)) { + $object['status_net'] = array(); + } + $object['status_net'][$parts[1]] = $props; + } else { + $object[$objectName] = $props; + } + $object[$objectName] = $props; + } } if (!empty($this->geopoint)) { @@ -772,8 +788,9 @@ class ActivityObject } if (!empty($this->poco)) { - $object['contact'] = array_filter($this->poco->asArray()); + $object['portablecontacts_net'] = array_filter($this->poco->asArray()); } + Event::handle('EndActivityObjectOutputJson', array($this, &$object)); } return array_filter($object); From a2dd5dfef3741d9987ec09f259c22f474b53ebcc Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 17:15:43 -0400 Subject: [PATCH 09/20] Remove duplicate of extensions --- lib/activityobject.php | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/activityobject.php b/lib/activityobject.php index 3f623ebcb9..8ae2e6e977 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -773,7 +773,6 @@ class ActivityObject } else { $object[$objectName] = $props; } - $object[$objectName] = $props; } } From 759754555d39c0cbef3b6c6bc821b1b53df5bd9d Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 17:22:51 -0400 Subject: [PATCH 10/20] Change geopoint to location --- lib/activityobject.php | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/lib/activityobject.php b/lib/activityobject.php index 8ae2e6e977..9640f025b2 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -778,12 +778,29 @@ class ActivityObject if (!empty($this->geopoint)) { - list($lat, $long) = explode(' ', $this->geopoint); + list($lat, $lon) = explode(' ', $this->geopoint); - $object['geopoint'] = array( - 'type' => 'Point', - 'coordinates' => array($lat, $long) + $object['location'] = array( + 'objectType' => 'place', + 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon), + 'lat' => $lat, + 'lon' => $lon ); + + $loc = Location::fromLatLon($lat, $lon); + + if ($loc) { + $name = $loc->getName(); + + if ($name) { + $object['location']['displayName'] = $name; + } + $url = $loc->getURL(); + + if ($url) { + $object['location']['url'] = $url; + } + } } if (!empty($this->poco)) { From 15d466ebe6576aa34efa7d4b5bb008e0d2cbc116 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 19:52:38 -0400 Subject: [PATCH 11/20] Don't add content as title for notes --- classes/Notice.php | 1 - lib/activity.php | 8 +++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index 541a850f0c..3891f431e4 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1508,7 +1508,6 @@ class Notice extends Managed_DataObject $act->time = strtotime($this->created); $act->link = $this->bestUrl(); $act->content = common_xml_safe_str($this->rendered); - $act->title = common_xml_safe_str($this->content); $profile = $this->getProfile(); diff --git a/lib/activity.php b/lib/activity.php index b8c9426268..2ec2176d39 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -589,7 +589,13 @@ class Activity } else { $xs->element('id', null, $this->id); - $xs->element('title', null, $this->title); + + if ($this->title) { + $xs->element('title', null, $this->title); + } else { + // Require element + $xs->element('title', null, ""); + } $xs->element('content', array('type' => 'html'), $this->content); From 74ec87c27c64585f4e0488e0a2b377865f452abb Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Tue, 4 Jun 2013 22:27:29 -0400 Subject: [PATCH 12/20] Don't set the title of a notice to its plain-text content. --- lib/activityobject.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/activityobject.php b/lib/activityobject.php index 9640f025b2..0eff87146f 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -433,7 +433,6 @@ class ActivityObject $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type; $object->id = $notice->uri; - $object->title = $notice->content; $object->content = $notice->rendered; $object->link = $notice->bestUrl(); @@ -687,7 +686,10 @@ class ActivityObject // content (Add rendered version of the notice?) // displayName - $object['displayName'] = $this->title; + + if ($this->title) { + $object['displayName'] = $this->title; + } // downstreamDuplicates From 7229533b0faff4e2532a67dff5c126e76b3a0f38 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 5 Jun 2013 09:39:13 -0400 Subject: [PATCH 13/20] Use real attachments for JSON output --- classes/Notice.php | 6 +-- lib/activity.php | 45 +++------------------ lib/activityobject.php | 92 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 43 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index 3891f431e4..beb6c4fe6a 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1544,9 +1544,9 @@ class Notice extends Managed_DataObject $attachments = $this->attachments(); foreach ($attachments as $attachment) { - $enclosure = $attachment->getEnclosure(); - if ($enclosure) { - $act->enclosures[] = $enclosure; + // Save local attachments + if (!empty($attachment->filename)) { + $act->attachments[] = ActivityObject::fromFile($attachment); } } diff --git a/lib/activity.php b/lib/activity.php index 2ec2176d39..ecae96b50a 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -100,6 +100,7 @@ class Activity public $title; // title of the activity public $categories = array(); // list of AtomCategory objects public $enclosures = array(); // list of enclosure URL references + public $attachments = array(); // list of attachments public $extra = array(); // extra elements as array(tag, attrs, content) public $source; // ActivitySource object representing 'home feed' @@ -397,47 +398,11 @@ class Activity $activity['object']['objectType'] = 'activity'; } - - // Instead of adding enclosures as an extension to JSON - // Activities, it seems like we should be using the - // attachements property of ActivityObject - - $attachments = array(); - - // XXX: OK, this is kinda cheating. We should probably figure out - // what kind of objects these are based on mime-type and then - // create specific object types. Right now this rely on - // duck-typing. Also, we should include an embed code for - // video attachments. - - foreach ($this->enclosures as $enclosure) { - - if (is_string($enclosure)) { - - $attachments[]['id'] = $enclosure; - - } else { - - $attachments[]['id'] = $enclosure->url; - - $mediaLink = new ActivityStreamsMediaLink( - $enclosure->url, - null, - null, - $enclosure->mimetype - // XXX: Add 'size' as an extension to MediaLink? - ); - - $attachments[]['mediaLink'] = $mediaLink->asArray(); // extension - - if ($enclosure->title) { - $attachments[]['displayName'] = $enclosure->title; - } + foreach ($this->attachments as $attachment) { + if (empty($activity['object']['attachments'])) { + $activity['object']['attachments'] = array(); } - } - - if (!empty($attachments)) { - $activity['object']['attachments'] = $attachments; + $activity['object']['attachments'][] = $attachment->asArray(); } } diff --git a/lib/activityobject.php b/lib/activityobject.php index 0eff87146f..1a81d30687 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -70,6 +70,7 @@ class ActivityObject // ^^^^^^^^^^ tea! const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity'; const SERVICE = 'http://activitystrea.ms/schema/1.0/service'; + const IMAGE = 'http://activitystrea.ms/schema/1.0/image'; // Atom elements we snarf @@ -111,6 +112,8 @@ class ActivityObject public $description; public $extra = array(); + public $stream; + /** * Constructor * @@ -549,6 +552,46 @@ class ActivityObject return $object; } + static function fromFile(File $file) + { + $object = new ActivityObject(); + + if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) { + + $object->type = self::mimeTypeToObjectType($file->mimetype); + $object->id = TagURI::mint(sprintf("file:%d", $file->id)); + $object->link = common_local_url('attachment', array('id' => $file->id)); + + if ($file->title) { + $object->title = $file->title; + } + + if ($file->date) { + $object->date = $file->date; + } + + $thumbnail = $file->getThumbnail(); + + if (!empty($thumbnail)) { + $object->thumbnail = $thumbnail; + } + + switch ($object->type) { + case ActivityObject::IMAGE: + $object->largerImage = $file->url; + break; + case ActivityObject::VIDEO: + case ActivityObject::AUDIO: + $object->stream = $file->url; + break; + } + + Event::handle('EndActivityObjectFromFile', array($file, &$object)); + } + + return $object; + } + function outputTo($xo, $tag='activity:object') { if (!empty($tag)) { @@ -809,6 +852,34 @@ class ActivityObject $object['portablecontacts_net'] = array_filter($this->poco->asArray()); } + if (!empty($this->thumbnail)) { + if (is_string($this->thumbnail)) { + $object['image'] = array('url' => $this->thumbnail); + } else { + $object['image'] = array('url' => $this->thumbnail->url); + if ($this->thumbnail->width) { + $object['image']['width'] = $this->thumbnail->width; + } + if ($this->thumbnail->height) { + $object['image']['height'] = $this->thumbnail->height; + } + } + } + + switch ($object->type) { + case self::IMAGE: + if (!empty($this->largerImage)) { + $object['fullImage'] = array('url' => $this->largerImage); + } + break; + case self::AUDIO: + case self::VIDEO: + if (!empty($this->stream)) { + $object['stream'] = array('url' => $this->stream); + } + break; + } + Event::handle('EndActivityObjectOutputJson', array($this, &$object)); } return array_filter($object); @@ -822,4 +893,25 @@ class ActivityObject return $type; } } + + static function mimeTypeToObjectType($mimeType) { + $ot = null; + $parts = explode('/', $mimeType); + + switch ($parts[0]) { + case 'image': + $ot = self::IMAGE; + break; + case 'audio': + $ot = self::AUDIO; + break; + case 'video': + $ot = self::VIDEO; + break; + default: + $ot = self::FILE; + } + + return $ot; + } } From 5ba2cb07eabb5d2e02d959d454804b4c295fda3e Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 5 Jun 2013 16:11:51 -0400 Subject: [PATCH 14/20] Better handling of null values in ActivityObject::mimeTypeToObjectType --- lib/activityobject.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/lib/activityobject.php b/lib/activityobject.php index 1a81d30687..7e9ddf48af 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -896,6 +896,13 @@ class ActivityObject static function mimeTypeToObjectType($mimeType) { $ot = null; + + // Default + + if (empty($mimeType)) { + return self::FILE; + } + $parts = explode('/', $mimeType); switch ($parts[0]) { From a9b2a860042063e3d1a75c1bd112381b07098763 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 5 Jun 2013 16:12:54 -0400 Subject: [PATCH 15/20] Better URL creation for attachments --- lib/activityobject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/activityobject.php b/lib/activityobject.php index 7e9ddf48af..3be64c13e0 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -560,7 +560,7 @@ class ActivityObject $object->type = self::mimeTypeToObjectType($file->mimetype); $object->id = TagURI::mint(sprintf("file:%d", $file->id)); - $object->link = common_local_url('attachment', array('id' => $file->id)); + $object->link = common_local_url('attachment', array('attachment' => $file->id)); if ($file->title) { $object->title = $file->title; From 0e83c5c824ee930374fecfb13f8af59e06decee3 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 5 Jun 2013 16:51:35 -0400 Subject: [PATCH 16/20] Better type check, better URL --- lib/activityobject.php | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/activityobject.php b/lib/activityobject.php index 3be64c13e0..f83fde73e1 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -576,12 +576,12 @@ class ActivityObject $object->thumbnail = $thumbnail; } - switch ($object->type) { - case ActivityObject::IMAGE: + switch (ActivityObject::canonicalType($object->type)) { + case 'image': $object->largerImage = $file->url; break; - case ActivityObject::VIDEO: - case ActivityObject::AUDIO: + case 'video': + case 'audio': $object->stream = $file->url; break; } @@ -798,8 +798,9 @@ class ActivityObject // TODO: upstreamDuplicates - // url (XXX: need to put the right thing here...) - $object['url'] = $this->id; + if ($this->link) { + $object['url'] = $this->link; + } /* Extensions */ // @fixme these may collide with XML extensions @@ -866,14 +867,14 @@ class ActivityObject } } - switch ($object->type) { - case self::IMAGE: + switch (ActivityObject::canonicalType($object->type)) { + case 'image': if (!empty($this->largerImage)) { $object['fullImage'] = array('url' => $this->largerImage); } break; - case self::AUDIO: - case self::VIDEO: + case 'audio': + case 'video': if (!empty($this->stream)) { $object['stream'] = array('url' => $this->stream); } From 221c724b4c67f8eef86e8ddb15fb02549f82dcb7 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Wed, 5 Jun 2013 16:58:31 -0400 Subject: [PATCH 17/20] Fix the switch on type --- lib/activityobject.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/activityobject.php b/lib/activityobject.php index f83fde73e1..97cf0f5d79 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -867,7 +867,7 @@ class ActivityObject } } - switch (ActivityObject::canonicalType($object->type)) { + switch (ActivityObject::canonicalType($this->type)) { case 'image': if (!empty($this->largerImage)) { $object['fullImage'] = array('url' => $this->largerImage); From 9bb5d8c695b877596adab8267c4ec8d70033acb0 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 7 Jun 2013 00:30:04 -0400 Subject: [PATCH 18/20] Coerce width, height of media link to integer --- lib/activitystreamjsondocument.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/activitystreamjsondocument.php b/lib/activitystreamjsondocument.php index c67fc5a42f..9964f1e60c 100644 --- a/lib/activitystreamjsondocument.php +++ b/lib/activitystreamjsondocument.php @@ -201,9 +201,9 @@ class ActivityStreamsMediaLink extends ActivityStreamsLink { parent::__construct($url, $rel, $mediaType); $this->linkDict = array( - 'width' => $width, - 'height' => $height, - 'duration' => $duration + 'width' => intval($width), + 'height' => intval($height), + 'duration' => intval($duration) ); } From 6164940e8ca8aba0284efe0e07358699df378ee5 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 7 Jun 2013 03:11:23 -0400 Subject: [PATCH 19/20] Some better context for notices as arrays --- classes/Notice.php | 14 ++++++++++++++ lib/activitycontext.php | 12 +++++++++--- lib/activityobject.php | 12 +++++++++++- 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index beb6c4fe6a..abec8b7af4 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1577,6 +1577,7 @@ class Notice extends Managed_DataObject $rprofile = Profile::staticGet('id', $id); if (!empty($rprofile)) { $ctx->attention[] = $rprofile->getUri(); + $ctx->attentionType[$rprofile->getUri()] = ActivityObject::PERSON; } } @@ -1584,6 +1585,19 @@ class Notice extends Managed_DataObject foreach ($groups as $group) { $ctx->attention[] = $group->getUri(); + $ctx->attentionType[$group->getUri()] = ActivityObject::GROUP; + } + + switch ($this->scope) { + case Notice::PUBLIC_SCOPE: + $ctx->attention[] = "http://activityschema.org/collection/public"; + $ctx->attentionType["http://activityschema.org/collection/public"] = ActivityObject::COLLECTION; + break; + case Notice::FOLLOWER_SCOPE: + $surl = common_local_url("subscribers", array('nickname' => $profile->nickname)); + $ctx->attention[] = $surl; + $ctx->attentionType[$surl] = ActivityObject::COLLECTION; + break; } // XXX: deprecated; use ActivityVerb::SHARE instead diff --git a/lib/activitycontext.php b/lib/activitycontext.php index 2eff3fb15f..e383b05734 100644 --- a/lib/activitycontext.php +++ b/lib/activitycontext.php @@ -38,9 +38,11 @@ class ActivityContext public $replyToUrl; public $location; public $attention = array(); + public $attentionType = array(); public $conversation; public $forwardID; // deprecated, use ActivityVerb::SHARE instead public $forwardUrl; // deprecated, use ActivityVerb::SHARE instead + public $scope; const THR = 'http://purl.org/syndication/thread/1.0'; const GEORSS = 'http://www.georss.org/georss'; @@ -167,10 +169,14 @@ class ActivityContext $tos = array(); foreach ($this->attention as $attnUrl) { + if (array_key_exists($attnUrl, $this->attentionType)) { + $type = ActivityObject::canonicalType($this->attentionType[$attnUrl]); + } else { + $type = ActivityObject::canonicalType(ActivityObject::PERSON); + } $to = array( - 'objectType' => 'person', - 'id' => $attnUrl, - 'url' => $attnUrl + 'objectType' => $type, + 'id' => $attnUrl ); $tos[] = $to; } diff --git a/lib/activityobject.php b/lib/activityobject.php index 97cf0f5d79..1352b5bda8 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -71,6 +71,7 @@ class ActivityObject const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity'; const SERVICE = 'http://activitystrea.ms/schema/1.0/service'; const IMAGE = 'http://activitystrea.ms/schema/1.0/image'; + const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection'; // Atom elements we snarf @@ -502,6 +503,10 @@ class ActivityObject $object->poco = PoCo::fromProfile($profile); + if ($profile->getUser()) { + $object->extra[] = array('followers', array('url' => common_local_url('subscribers', array('nickname' => $profile->nickname)))); + } + Event::handle('EndActivityObjectFromProfile', array($profile, &$object)); } @@ -737,7 +742,12 @@ class ActivityObject // downstreamDuplicates // id - $object['id'] = $this->id; + + if ($this->id) { + $object['id'] = $this->id; + } else if ($this->link) { + $object['id'] = $this->link; + } if ($this->type == ActivityObject::PERSON || $this->type == ActivityObject::GROUP) { From 08eca420ca11badcb166961c1f37896ce2c9571c Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Fri, 7 Jun 2013 11:34:54 -0400 Subject: [PATCH 20/20] Add generator to JSON output --- classes/Notice.php | 6 ++++++ lib/activity.php | 9 ++++++--- lib/activityobject.php | 43 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 54 insertions(+), 4 deletions(-) diff --git a/classes/Notice.php b/classes/Notice.php index abec8b7af4..9a4b6db7ff 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1614,6 +1614,12 @@ class Notice extends Managed_DataObject $act->context = $ctx; + $source = $this->getSource(); + + if ($source) { + $act->generator = ActivityObject::fromNoticeSource($source); + } + // Source $atom_feed = $profile->getAtomFeed(); diff --git a/lib/activity.php b/lib/activity.php index ecae96b50a..779be2a9ad 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -106,7 +106,7 @@ class Activity public $source; // ActivitySource object representing 'home feed' public $selfLink; // public $editLink; // - + public $generator; // ActivityObject representing the generating application /** * Turns a regular old Atom into a magical activity * @@ -366,8 +366,11 @@ class Activity // content $activity['content'] = $this->content; - // generator <-- We could use this when we know a notice is created - // locally. Or if we know the upstream Generator. + // generator + + if (!empty($this->generator)) { + $activity['generator'] = $this->generator->asArray(); + } // icon <-- possibly a mini object representing verb? diff --git a/lib/activityobject.php b/lib/activityobject.php index 1352b5bda8..31cdb06af7 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -72,6 +72,7 @@ class ActivityObject const SERVICE = 'http://activitystrea.ms/schema/1.0/service'; const IMAGE = 'http://activitystrea.ms/schema/1.0/image'; const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection'; + const APPLICATION = 'http://activitystrea.ms/schema/1.0/application'; // Atom elements we snarf @@ -597,6 +598,46 @@ class ActivityObject return $object; } + static function fromNoticeSource(Notice_source $source) + { + $object = new ActivityObject(); + + if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) { + $object->type = ActivityObject::APPLICATION; + + if (in_array($source->code, array('web', 'xmpp', 'mail', 'omb', 'system', 'api'))) { + // We use one ID for all well-known StatusNet sources + $object->id = "tag:status.net,2009:notice-source:".$source->code; + } else if ($source->url) { + // They registered with an URL + $object->id = $source->url; + } else { + // Locally-registered, no URL + $object->id = TagURI::mint("notice-source:".$source->code); + } + + if ($source->url) { + $object->link = $source->url; + } + + if ($source->name) { + $object->title = $source->name; + } else { + $object->title = $source->code; + } + + if ($source->created) { + $object->date = $source->created; + } + + $object->extras[] = array('status_net', array('source_code' => $source->code)); + + Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object)); + } + + return $object; + } + function outputTo($xo, $tag='activity:object') { if (!empty($tag)) { @@ -799,7 +840,7 @@ class ActivityObject // summary $object['summary'] = $this->summary; - // summary + // content $object['content'] = $this->content; // published (probably don't need. Might be useful for repeats.)