diff --git a/classes/Notice.php b/classes/Notice.php index 541a850f0c..9a4b6db7ff 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(); @@ -1545,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); } } @@ -1578,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; } } @@ -1585,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 @@ -1601,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/classes/User.php b/classes/User.php index 32a04238c2..8d21d2bc19 100644 --- a/classes/User.php +++ b/classes/User.php @@ -1187,14 +1187,16 @@ class User extends Managed_DataObject $service = new ActivityObject(); - $service->type = "service"; - $service->displayName = common_config('site', 'name'); - $service->url = common_root_url(); + $service->type = ActivityObject::SERVICE; + $service->title = common_config('site', 'name'); + $service->link = common_root_url(); + $service->id = $service->link; $act = new Activity(); $act->actor = ActivityObject::fromProfile($profile); $act->verb = ActivityVerb::JOIN; + $act->objects[] = $service; $act->id = TagURI::mint('user:register:%d', @@ -1205,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; } } diff --git a/lib/activity.php b/lib/activity.php index e352baf639..779be2a9ad 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -100,12 +100,13 @@ 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' public $selfLink; // public $editLink; // - + public $generator; // ActivityObject representing the generating application /** * Turns a regular old Atom into a magical activity * @@ -365,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? @@ -374,101 +378,79 @@ class Activity $activity['id'] = $this->id; // object - if ($this->verb == ActivityVerb::POST && count($this->objects) == 1) { - $activity['object'] = $this->objects[0]->asArray(); - - // 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; - - // 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), - '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 - // 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; - } - } - } - - if (!empty($attachments)) { - $activity['object']['attachments'] = $attachments; - } + if (count($this->objects) == 0) { + common_log(LOG_ERR, "Can't save " . $this->id); } 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'; + 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) { + // 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'][] = $oa; + } + + $activity['object'] = $object->asArray(); + + if ($object instanceof Activity) { + $activity['object']['objectType'] = 'activity'; + } + + foreach ($this->attachments as $attachment) { + if (empty($activity['object']['attachments'])) { + $activity['object']['attachments'] = array(); + } + $activity['object']['attachments'][] = $attachment->asArray(); + } + } + + // 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; } } @@ -496,10 +478,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 +507,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; + } } } @@ -569,7 +557,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); 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 4c11be8597..31cdb06af7 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -69,6 +69,10 @@ 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'; + 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 @@ -110,6 +114,8 @@ class ActivityObject public $description; public $extra = array(); + public $stream; + /** * Constructor * @@ -432,7 +438,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(); @@ -499,6 +504,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)); } @@ -549,6 +558,86 @@ 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('attachment' => $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 (ActivityObject::canonicalType($object->type)) { + case 'image': + $object->largerImage = $file->url; + break; + case 'video': + case 'audio': + $object->stream = $file->url; + break; + } + + Event::handle('EndActivityObjectFromFile', array($file, &$object)); + } + + 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)) { @@ -686,12 +775,20 @@ class ActivityObject // content (Add rendered version of the notice?) // displayName - $object['displayName'] = $this->title; + + if ($this->title) { + $object['displayName'] = $this->title; + } // 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) { @@ -721,7 +818,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)) { @@ -733,13 +834,13 @@ 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; - // summary + // content $object['content'] = $this->content; // published (probably don't need. Might be useful for repeats.) @@ -748,33 +849,128 @@ 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 // @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; + } + } } 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)) { - $object['contact'] = array_filter($this->poco->asArray()); + $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 (ActivityObject::canonicalType($this->type)) { + case 'image': + if (!empty($this->largerImage)) { + $object['fullImage'] = array('url' => $this->largerImage); + } + break; + case 'audio': + case 'video': + if (!empty($this->stream)) { + $object['stream'] = array('url' => $this->stream); + } + break; + } + Event::handle('EndActivityObjectOutputJson', array($this, &$object)); } 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; + } + } + + static function mimeTypeToObjectType($mimeType) { + $ot = null; + + // Default + + if (empty($mimeType)) { + return self::FILE; + } + + $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; + } } 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) ); } 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; + } + } }