function asArray() { $object = array(); if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) { // XXX: attachments are added by Activity // author (Add object for author? Could be useful for repeats.) // content (Add rendered version of the notice?) // downstreamDuplicates // 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) { // displayName $object['displayName'] = $this->title; // XXX: Not sure what the best avatar is to use for the // author's "image". For now, I'm using the large size. $imgLink = null; $avatarMediaLinks = array(); foreach ($this->avatarLinks as $a) { // Make a MediaLink for every other Avatar $avatar = new ActivityStreamsMediaLink($a->url, $a->width, $a->height, $a->type, 'avatar'); // Find the big avatar to use as the "image" if ($a->height == AVATAR_PROFILE_SIZE) { $imgLink = $avatar; } $avatarMediaLinks[] = $avatar->asArray(); } if (!array_key_exists('status_net', $object)) { $object['status_net'] = array(); } $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension // image if (!empty($imgLink)) { $object['image'] = $imgLink->asArray(); } } // objectType // // We can probably use the whole schema URL here but probably the // relative simple name is easier to parse $object['objectType'] = self::canonicalType($this->type); // summary $object['summary'] = $this->summary; // content, usually rendered HTML $object['content'] = $this->content; // published (probably don't need. Might be useful for repeats.) // updated (probably don't need this) // TODO: upstreamDuplicates 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) = array_pad($e, 3, null); 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, $lon) = explode(' ', $this->geopoint); if (!empty($lat) && !empty($lon)) { $object['location'] = array('objectType' => 'place', 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon), 'lat' => $lat, 'lon' => $lon); $loc = Location::fromLatLon((double) $lat, (double) $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['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->getUrl()); if ($this->thumbnail->width) { $object['image']['width'] = $this->thumbnail->width; } if ($this->thumbnail->height) { $object['image']['height'] = $this->thumbnail->height; } } } switch (self::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); }
/** * Returns an array based on this activity suitable * for encoding as a JSON object * * @return array $activity */ function asArray() { $activity = array(); // actor $activity['actor'] = $this->actor->asArray(); // body $activity['body'] = $this->content; // generator <-- We could use this when we know a notice is created // locally. Or if we know the upstream Generator. // icon <-- I've decided to use the posting user's stream avatar here // for now (also included in the avatarLinks extension) // 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)); } $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 // attachedObjects property of ActivityObject $attachedObjects = 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)) { $attachedObjects[]['id'] = $enclosure; } else { $attachedObjects[]['id'] = $enclosure->url; $mediaLink = new ActivityStreamsMediaLink($enclosure->url, null, null, $enclosure->mimetype); $attachedObjects[]['mediaLink'] = $mediaLink->asArray(); // extension if ($enclosure->title) { $attachedObjects[]['displayName'] = $enclosure->title; } } } if (!empty($attachedObjects)) { $activity['object']['attachedObjects'] = $attachedObjects; } } else { $activity['object'] = array(); foreach ($this->objects as $object) { $activity['object'][] = $object->asArray(); } } $activity['postedTime'] = self::iso8601Date($this->time); // Change to exactly be RFC3339? // provider $provider = array('objectType' => 'service', 'displayName' => common_config('site', 'name'), 'url' => common_root_url()); $activity['provider'] = $provider; // target if (!empty($this->target)) { $activity['target'] = $this->target->asArray(); } // title $activity['title'] = $this->title; // updatedTime <-- Should we use this to indicate the time we received // 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); /* Purely extensions hereafter */ $tags = array(); // Use an Activity Object for term? Which object? Note? foreach ($this->categories as $cat) { $tags[] = $cat->term; } $activity['tags'] = $tags; // XXX: a bit of a hack... Since JSON isn't namespaced we probably // shouldn't be using 'statusnet:notice_info', but this will work // for the moment. foreach ($this->extra as $e) { list($objectName, $props, $txt) = $e; if (!empty($objectName)) { $activity[$objectName] = $props; } } return array_filter($activity); }
/** * Returns an array based on this activity suitable * for encoding as a JSON object * * @return array $activity */ function asArray() { $activity = array(); // actor $activity['actor'] = $this->actor->asArray(); // 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. // icon <-- possibly a mini object representing verb? // id $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); $attachments[]['mediaLink'] = $mediaLink->asArray(); // extension 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 $activity['published'] = self::iso8601Date($this->time); // provider $provider = array('objectType' => 'service', 'displayName' => common_config('site', 'name'), 'url' => common_root_url()); $activity['provider'] = $provider; // target if (!empty($this->target)) { $activity['target'] = $this->target->asArray(); } // title $activity['title'] = $this->title; // updated <-- Optional. Should we use this to indicate the time we r // 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); // url $activity['url'] = $this->id; /* Purely extensions hereafter */ if ($activity['verb'] == 'post') { $tags = array(); foreach ($this->categories as $cat) { if (mb_strlen($cat->term) > 0) { // Couldn't figure out which object type to use, so... $tags[] = array('objectType' => 'http://activityschema.org/object/hashtag', 'displayName' => $cat->term); } } if (count($tags) > 0) { $activity['object']['tags'] = $tags; } } // XXX: a bit of a hack... Since JSON isn't namespaced we probably // shouldn't be using 'statusnet:notice_info', but this will work // for the moment. foreach ($this->extra as $e) { list($objectName, $props, $txt) = $e; if (!empty($objectName)) { $activity[$objectName] = $props; } } return array_filter($activity); }
function asArray() { $object = array(); if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) { // XXX: attachments are added by Activity // author (Add object for author? Could be useful for repeats.) // content (Add rendered version of the notice?) // displayName $object['displayName'] = $this->title; // downstreamDuplicates // id $object['id'] = $this->id; if ($this->type == ActivityObject::PERSON || $this->type == ActivityObject::GROUP) { // XXX: Not sure what the best avatar is to use for the // author's "image". For now, I'm using the large size. $imgLink = null; $avatarMediaLinks = array(); foreach ($this->avatarLinks as $a) { // Make a MediaLink for every other Avatar $avatar = new ActivityStreamsMediaLink($a->url, $a->width, $a->height, $a->type, 'avatar'); // Find the big avatar to use as the "image" if ($a->height == AVATAR_PROFILE_SIZE) { $imgLink = $avatar; } $avatarMediaLinks[] = $avatar->asArray(); } $object['avatarLinks'] = $avatarMediaLinks; // extension // image if (!empty($imgLink)) { $object['image'] = $imgLink->asArray(); } } // objectType // // 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); // summary $object['summary'] = $this->summary; // summary $object['content'] = $this->content; // published (probably don't need. Might be useful for repeats.) // updated (probably don't need this) // TODO: upstreamDuplicates // url (XXX: need to put the right thing here...) $object['url'] = $this->id; /* 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($this->geopoint)) { list($lat, $long) = explode(' ', $this->geopoint); $object['geopoint'] = array('type' => 'Point', 'coordinates' => array($lat, $long)); } if (!empty($this->poco)) { $object['contact'] = array_filter($this->poco->asArray()); } Event::handle('EndActivityObjectOutputJson', array($this, &$object)); } return array_filter($object); }
function asArray() { $object = array(); // XXX: attachedObjects are added by Activity // displayName $object['displayName'] = $this->title; // TODO: downstreamDuplicates // embedCode (used for video) // id // // XXX: Should we use URL here? or a crazy tag URI? $object['id'] = $this->id; if ($this->type == ActivityObject::PERSON || $this->type == ActivityObject::GROUP) { // XXX: Not sure what the best avatar is to use for the // author's "image". For now, I'm using the large size. $avatarLarge = null; $avatarMediaLinks = array(); foreach ($this->avatarLinks as $a) { // Make a MediaLink for every other Avatar $avatar = new ActivityStreamsMediaLink($a->url, $a->width, $a->height, $a->type, 'avatar'); // Find the big avatar to use as the "image" if ($a->height == AVATAR_PROFILE_SIZE) { $imgLink = $avatar; } $avatarMediaLinks[] = $avatar->asArray(); } $object['avatarLinks'] = $avatarMediaLinks; // extension // image $object['image'] = $imgLink->asArray(); } // objectType // // We can probably use the whole schema URL here but probably the // relative simple name is easier to parse $object['type'] = substr($this->type, strrpos($this->type, '/') + 1); // summary $object['summary'] = $this->summary; // TODO: upstreamDuplicates // url (XXX: need to put the right thing here...) $object['url'] = $this->id; /* Extensions */ foreach ($this->extra as $e) { list($objectName, $props, $txt) = $e; $object[$objectName] = $props; } // GeoJSON if (!empty($this->geopoint)) { list($lat, $long) = explode(' ', $this->geopoint); $object['geopoint'] = array('type' => 'Point', 'coordinates' => array($lat, $long)); } if (!empty($this->poco)) { $object['contact'] = $this->poco->asArray(); } return array_filter($object); }