From 99454be38cf1dc7f962441d23ccc0a59e7b05f3d Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sat, 20 Mar 2010 16:06:22 -0500 Subject: Move activity classes to their own files Moved the various classes used by the Activity class to their own files. There were >10 classes in the same file, with around 1500 lines in the file. Just too big. This change makes autoloading work for these classes, so also removed the hard require in lib/common.php. --- lib/activityobject.php | 494 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 494 insertions(+) create mode 100644 lib/activityobject.php (limited to 'lib/activityobject.php') diff --git a/lib/activityobject.php b/lib/activityobject.php new file mode 100644 index 000000000..b1e9071ed --- /dev/null +++ b/lib/activityobject.php @@ -0,0 +1,494 @@ +. + * + * @category Feed + * @package StatusNet + * @author Evan Prodromou + * @author Zach Copley + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ + +if (!defined('STATUSNET')) { + exit(1); +} + +/** + * A noun-ish thing in the activity universe + * + * The activity streams spec talks about activity objects, while also having + * a tag activity:object, which is in fact an activity object. Aaaaaah! + * + * This is just a thing in the activity universe. Can be the subject, object, + * or indirect object (target!) of an activity verb. Rotten name, and I'm + * propagating it. *sigh* + * + * @category OStatus + * @package StatusNet + * @author Evan Prodromou + * @copyright 2010 StatusNet, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3 + * @link http://status.net/ + */ + +class ActivityObject +{ + const ARTICLE = 'http://activitystrea.ms/schema/1.0/article'; + const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry'; + const NOTE = 'http://activitystrea.ms/schema/1.0/note'; + const STATUS = 'http://activitystrea.ms/schema/1.0/status'; + const FILE = 'http://activitystrea.ms/schema/1.0/file'; + const PHOTO = 'http://activitystrea.ms/schema/1.0/photo'; + const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album'; + const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist'; + const VIDEO = 'http://activitystrea.ms/schema/1.0/video'; + const AUDIO = 'http://activitystrea.ms/schema/1.0/audio'; + const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark'; + const PERSON = 'http://activitystrea.ms/schema/1.0/person'; + const GROUP = 'http://activitystrea.ms/schema/1.0/group'; + const PLACE = 'http://activitystrea.ms/schema/1.0/place'; + const COMMENT = 'http://activitystrea.ms/schema/1.0/comment'; + // ^^^^^^^^^^ tea! + + // Atom elements we snarf + + const TITLE = 'title'; + const SUMMARY = 'summary'; + const ID = 'id'; + const SOURCE = 'source'; + + const NAME = 'name'; + const URI = 'uri'; + const EMAIL = 'email'; + + public $element; + public $type; + public $id; + public $title; + public $summary; + public $content; + public $link; + public $source; + public $avatarLinks = array(); + public $geopoint; + public $poco; + public $displayName; + + /** + * Constructor + * + * This probably needs to be refactored + * to generate a local class (ActivityPerson, ActivityFile, ...) + * based on the object type. + * + * @param DOMElement $element DOM thing to turn into an Activity thing + */ + + function __construct($element = null) + { + if (empty($element)) { + return; + } + + $this->element = $element; + + $this->geopoint = $this->_childContent( + $element, + ActivityContext::POINT, + ActivityContext::GEORSS + ); + + if ($element->tagName == 'author') { + $this->_fromAuthor($element); + } else if ($element->tagName == 'item') { + $this->_fromRssItem($element); + } else { + $this->_fromAtomEntry($element); + } + + // Some per-type attributes... + if ($this->type == self::PERSON || $this->type == self::GROUP) { + $this->displayName = $this->title; + + $photos = ActivityUtils::getLinks($element, 'photo'); + if (count($photos)) { + foreach ($photos as $link) { + $this->avatarLinks[] = new AvatarLink($link); + } + } else { + $avatars = ActivityUtils::getLinks($element, 'avatar'); + foreach ($avatars as $link) { + $this->avatarLinks[] = new AvatarLink($link); + } + } + + $this->poco = new PoCo($element); + } + } + + private function _fromAuthor($element) + { + $this->type = self::PERSON; // XXX: is this fair? + $this->title = $this->_childContent($element, self::NAME); + $this->id = $this->_childContent($element, self::URI); + + if (empty($this->id)) { + $email = $this->_childContent($element, self::EMAIL); + if (!empty($email)) { + // XXX: acct: ? + $this->id = 'mailto:'.$email; + } + } + } + + private function _fromAtomEntry($element) + { + $this->type = $this->_childContent($element, Activity::OBJECTTYPE, + Activity::SPEC); + + if (empty($this->type)) { + $this->type = ActivityObject::NOTE; + } + + $this->id = $this->_childContent($element, self::ID); + $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY); + $this->content = ActivityUtils::getContent($element); + + // We don't like HTML in our titles, although it's technically allowed + + $title = ActivityUtils::childHtmlContent($element, self::TITLE); + + $this->title = html_entity_decode(strip_tags($title)); + + $this->source = $this->_getSource($element); + + $this->link = ActivityUtils::getPermalink($element); + } + + // @fixme rationalize with Activity::_fromRssItem() + + private function _fromRssItem($item) + { + $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS); + + $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS); + + if (!empty($contentEl)) { + $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES); + } else { + $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS); + if (!empty($descriptionEl)) { + $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES); + } + } + + $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS); + + $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS); + + if (!empty($guidEl)) { + $this->id = $guidEl->textContent; + + if ($guidEl->hasAttribute('isPermaLink')) { + // overwrites + $this->link = $this->id; + } + } + } + + public static function fromRssAuthor($el) + { + $text = $el->textContent; + + if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) { + $email = $match[1]; + $name = $match[2]; + } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) { + $name = $match[1]; + $email = $match[2]; + } else if (preg_match('/.*@.*/', $text)) { + $email = $text; + $name = null; + } else { + $name = $text; + $email = null; + } + + // Not really enough info + + $obj = new ActivityObject(); + + $obj->element = $el; + + $obj->type = ActivityObject::PERSON; + $obj->title = $name; + + if (!empty($email)) { + $obj->id = 'mailto:'.$email; + } + + return $obj; + } + + public static function fromDcCreator($el) + { + // Not really enough info + + $text = $el->textContent; + + $obj = new ActivityObject(); + + $obj->element = $el; + + $obj->title = $text; + $obj->type = ActivityObject::PERSON; + + return $obj; + } + + public static function fromRssChannel($el) + { + $obj = new ActivityObject(); + + $obj->element = $el; + + $obj->type = ActivityObject::PERSON; // @fixme guess better + + $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS); + $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS); + $obj->id = ActivityUtils::getLink($el, Activity::SELF); + + if (empty($obj->id)) { + $obj->id = $obj->link; + } + + $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS); + + if (!empty($desc)) { + $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES); + } + + $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS); + + if (!empty($imageEl)) { + $obj->avatarLinks[] = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS); + } + + return $obj; + } + + private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM) + { + return ActivityUtils::childContent($element, $tag, $namespace); + } + + // Try to get a unique id for the source feed + + private function _getSource($element) + { + $sourceEl = ActivityUtils::child($element, 'source'); + + if (empty($sourceEl)) { + return null; + } else { + $href = ActivityUtils::getLink($sourceEl, 'self'); + if (!empty($href)) { + return $href; + } else { + return ActivityUtils::childContent($sourceEl, 'id'); + } + } + } + + static function fromNotice(Notice $notice) + { + $object = new ActivityObject(); + + $object->type = ActivityObject::NOTE; + + $object->id = $notice->uri; + $object->title = $notice->content; + $object->content = $notice->rendered; + $object->link = $notice->bestUrl(); + + return $object; + } + + static function fromProfile(Profile $profile) + { + $object = new ActivityObject(); + + $object->type = ActivityObject::PERSON; + $object->id = $profile->getUri(); + $object->title = $profile->getBestName(); + $object->link = $profile->profileurl; + + $orig = $profile->getOriginalAvatar(); + + if (!empty($orig)) { + $object->avatarLinks[] = AvatarLink::fromAvatar($orig); + } + + $sizes = array( + AVATAR_PROFILE_SIZE, + AVATAR_STREAM_SIZE, + AVATAR_MINI_SIZE + ); + + foreach ($sizes as $size) { + + $alink = null; + $avatar = $profile->getAvatar($size); + + if (!empty($avatar)) { + $alink = AvatarLink::fromAvatar($avatar); + } else { + $alink = new AvatarLink(); + $alink->type = 'image/png'; + $alink->height = $size; + $alink->width = $size; + $alink->url = Avatar::defaultImage($size); + } + + $object->avatarLinks[] = $alink; + } + + if (isset($profile->lat) && isset($profile->lon)) { + $object->geopoint = (float)$profile->lat + . ' ' . (float)$profile->lon; + } + + $object->poco = PoCo::fromProfile($profile); + + return $object; + } + + static function fromGroup($group) + { + $object = new ActivityObject(); + + $object->type = ActivityObject::GROUP; + $object->id = $group->getUri(); + $object->title = $group->getBestName(); + $object->link = $group->getUri(); + + $object->avatarLinks[] = AvatarLink::fromFilename( + $group->homepage_logo, + AVATAR_PROFILE_SIZE + ); + + $object->avatarLinks[] = AvatarLink::fromFilename( + $group->stream_logo, + AVATAR_STREAM_SIZE + ); + + $object->avatarLinks[] = AvatarLink::fromFilename( + $group->mini_logo, + AVATAR_MINI_SIZE + ); + + $object->poco = PoCo::fromGroup($group); + + return $object; + } + + function asString($tag='activity:object') + { + $xs = new XMLStringer(true); + + $xs->elementStart($tag); + + $xs->element('activity:object-type', null, $this->type); + + $xs->element(self::ID, null, $this->id); + + if (!empty($this->title)) { + $xs->element( + self::TITLE, + null, + common_xml_safe_str($this->title) + ); + } + + if (!empty($this->summary)) { + $xs->element( + self::SUMMARY, + null, + common_xml_safe_str($this->summary) + ); + } + + if (!empty($this->content)) { + // XXX: assuming HTML content here + $xs->element( + ActivityUtils::CONTENT, + array('type' => 'html'), + common_xml_safe_str($this->content) + ); + } + + if (!empty($this->link)) { + $xs->element( + 'link', + array( + 'rel' => 'alternate', + 'type' => 'text/html', + 'href' => $this->link + ), + null + ); + } + + if ($this->type == ActivityObject::PERSON + || $this->type == ActivityObject::GROUP) { + + foreach ($this->avatarLinks as $avatar) { + $xs->element( + 'link', array( + 'rel' => 'avatar', + 'type' => $avatar->type, + 'media:width' => $avatar->width, + 'media:height' => $avatar->height, + 'href' => $avatar->url + ), + null + ); + } + } + + if (!empty($this->geopoint)) { + $xs->element( + 'georss:point', + null, + $this->geopoint + ); + } + + if (!empty($this->poco)) { + $xs->raw($this->poco->asString()); + } + + $xs->elementEnd($tag); + + return $xs->getString(); + } +} -- cgit v1.2.3-54-g00ecf From 323ff31fbd59181f4cd9a3fc4da40a1f9ff8bc99 Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sat, 20 Mar 2010 16:53:30 -0500 Subject: special-case Posterous author element for activity actor --- lib/activity.php | 18 +++++++++--------- lib/activityobject.php | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 9 deletions(-) (limited to 'lib/activityobject.php') diff --git a/lib/activity.php b/lib/activity.php index b1744e68f..691ace1f6 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -238,17 +238,17 @@ class Activity $this->time = strtotime($pubDateEl->textContent); } - $authorEl = $this->_child($item, self::AUTHOR, self::RSS); - - if (!empty($authorEl)) { + if ($authorEl = $this->_child($item, self::AUTHOR, self::RSS)) { $this->actor = ActivityObject::fromRssAuthor($authorEl); + } else if ($dcCreatorEl = $this->_child($item, self::CREATOR, self::DC)) { + $this->actor = ActivityObject::fromDcCreator($dcCreatorEl); + } else if ($posterousEl = $this->_child($item, ActivityObject::AUTHOR, ActivityObject::POSTEROUS)) { + // Special case for Posterous.com + $this->actor = ActivityObject::fromPosterousAuthor($posterousEl); + } else if (!empty($channel)) { + $this->actor = ActivityObject::fromRssChannel($channel); } else { - $dcCreatorEl = $this->_child($item, self::CREATOR, self::DC); - if (!empty($dcCreatorEl)) { - $this->actor = ActivityObject::fromDcCreator($dcCreatorEl); - } else if (!empty($channel)) { - $this->actor = ActivityObject::fromRssChannel($channel); - } + // No actor! } $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, self::RSS); diff --git a/lib/activityobject.php b/lib/activityobject.php index b1e9071ed..18e3e21dd 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -80,6 +80,13 @@ class ActivityObject const URI = 'uri'; const EMAIL = 'email'; + const POSTEROUS = 'http://posterous.com/help/rss/1.0'; + const AUTHOR = 'author'; + const USERIMAGE = 'userImage'; + const PROFILEURL = 'profileUrl'; + const NICKNAME = 'nickName'; + const DISPLAYNAME = 'displayName'; + public $element; public $type; public $id; @@ -296,6 +303,31 @@ class ActivityObject return $obj; } + public static function fromPosterousAuthor($el) + { + $obj = new ActivityObject(); + + $obj->type = ActivityObject::PERSON; // @fixme any others...? + + $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS); + + if (!empty($userImage)) { + $obj->avatarLinks[] = $userImage; + } + + $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS); + $obj->id = $obj->link; + + $obj->poco = new PoCo(); + + $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS); + $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS); + + $obj->title = $obj->poco->displayName; + + return $obj; + } + private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM) { return ActivityUtils::childContent($element, $tag, $namespace); -- cgit v1.2.3-54-g00ecf From 97bd7e22da893ac2d93e66d7f4747358713e739b Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sat, 20 Mar 2010 17:18:24 -0500 Subject: correct creation of avatar links for RSS and Posterous elements --- lib/activityobject.php | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'lib/activityobject.php') diff --git a/lib/activityobject.php b/lib/activityobject.php index 18e3e21dd..e5cea727b 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -297,7 +297,10 @@ class ActivityObject $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS); if (!empty($imageEl)) { - $obj->avatarLinks[] = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS); + $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS); + $al = new AvatarLink(); + $al->url = $url; + $obj->avatarLinks[] = $al; } return $obj; @@ -312,7 +315,9 @@ class ActivityObject $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS); if (!empty($userImage)) { - $obj->avatarLinks[] = $userImage; + $al = new AvatarLink(); + $al->url = $userImage; + $obj->avatarLinks[] = $al; } $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS); -- cgit v1.2.3-54-g00ecf From fcb614d0eb1f98bf8704654ed06e1f9d9733d359 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Sun, 21 Mar 2010 16:25:12 -0700 Subject: Pull info as well as when we have an old-style ActivityStreams feed. This fixes subscription setup for Cliqset feeds, which currently have a bogus activity:actor/atom:id but a good atom:author/atom:uri --- lib/activityobject.php | 21 +++++++++++++++++++-- lib/activityutils.php | 22 ++++++++++++++++++++++ plugins/OStatus/classes/Ostatus_profile.php | 6 +----- 3 files changed, 42 insertions(+), 7 deletions(-) (limited to 'lib/activityobject.php') diff --git a/lib/activityobject.php b/lib/activityobject.php index e5cea727b..0a358ccab 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -156,7 +156,11 @@ class ActivityObject { $this->type = self::PERSON; // XXX: is this fair? $this->title = $this->_childContent($element, self::NAME); - $this->id = $this->_childContent($element, self::URI); + + $id = $this->_childContent($element, self::URI); + if (ActivityUtils::validateUri($id)) { + $this->id = $id; + } if (empty($this->id)) { $email = $this->_childContent($element, self::EMAIL); @@ -169,6 +173,15 @@ class ActivityObject private function _fromAtomEntry($element) { + if ($element->localName == 'actor') { + // Old-fashioned ... + // First pull anything from , then we'll add on top. + $author = ActivityUtils::child($element->parentNode, 'author'); + if ($author) { + $this->_fromAuthor($author); + } + } + $this->type = $this->_childContent($element, Activity::OBJECTTYPE, Activity::SPEC); @@ -176,7 +189,11 @@ class ActivityObject $this->type = ActivityObject::NOTE; } - $this->id = $this->_childContent($element, self::ID); + $id = $this->_childContent($element, self::ID); + if (ActivityUtils::validateUri($id)) { + $this->id = $id; + } + $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY); $this->content = ActivityUtils::getContent($element); diff --git a/lib/activityutils.php b/lib/activityutils.php index c85a3db55..a7e99fb11 100644 --- a/lib/activityutils.php +++ b/lib/activityutils.php @@ -240,4 +240,26 @@ class ActivityUtils throw new ClientException(_("Can't handle embedded Base64 content yet.")); } } + + /** + * Is this a valid URI for remote profile/notice identification? + * Does not have to be a resolvable URL. + * @param string $uri + * @return boolean + */ + static function validateUri($uri) + { + if (Validate::uri($uri)) { + return true; + } + + // Possibly an upstream bug; tag: URIs aren't validated properly + // unless you explicitly ask for them. All other schemes are accepted + // for basic URI validation without asking. + if (Validate::uri($uri, array('allowed_scheme' => array('tag')))) { + return true; + } + + return false; + } } diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php index 5595a9d29..e33509c47 100644 --- a/plugins/OStatus/classes/Ostatus_profile.php +++ b/plugins/OStatus/classes/Ostatus_profile.php @@ -1170,11 +1170,7 @@ class Ostatus_profile extends Memcached_DataObject protected static function getActivityObjectProfileURI($object) { if ($object->id) { - // Possibly an upstream bug; tag: URIs are rejected unless you - // explicitly ask for them. All other schemes are accepted for - // basic URI validation without asking. - if (Validate::uri($object->id) || - Validate::uri($object->id, array('allowed_scheme' => array('tag')))) { + if (ActivityUtils::validateUri($object->id)) { return $object->id; } } -- cgit v1.2.3-54-g00ecf From b8e97ac7098783f0380c7f8f61c20a100e814dc0 Mon Sep 17 00:00:00 2001 From: Zach Copley Date: Mon, 22 Mar 2010 18:53:09 -0700 Subject: Some initial media parsing - Activity now returns a list of activity objects - Processing of photo objects --- lib/activity.php | 24 +-- lib/activityobject.php | 20 +++ plugins/OStatus/actions/groupsalmon.php | 3 +- plugins/OStatus/actions/usersalmon.php | 5 +- plugins/OStatus/classes/Ostatus_profile.php | 2 +- scripts/importtwitteratom.php | 2 +- tests/ActivityParseTests.php | 233 ++++++++++++++++++++++++++-- tests/UserFeedParseTest.php | 8 +- 8 files changed, 266 insertions(+), 31 deletions(-) (limited to 'lib/activityobject.php') diff --git a/lib/activity.php b/lib/activity.php index bd1d5d56c..f9192c6b8 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -53,6 +53,7 @@ class Activity { const SPEC = 'http://activitystrea.ms/spec/1.0/'; const SCHEMA = 'http://activitystrea.ms/schema/1.0/'; + const MEDIA = 'http://purl.org/syndication/atommedia'; const VERB = 'verb'; const OBJECT = 'object'; @@ -85,7 +86,7 @@ class Activity public $actor; // an ActivityObject public $verb; // a string (the URL) - public $object; // an ActivityObject + public $objects = array(); // an array of ActivityObjects public $target; // an ActivityObject public $context; // an ActivityObject public $time; // Time of the activity @@ -161,12 +162,15 @@ class Activity // XXX: do other implied stuff here } - $objectEl = $this->_child($entry, self::OBJECT); + $objectEls = $entry->getElementsByTagNameNS(self::SPEC, self::OBJECT); - if (!empty($objectEl)) { - $this->object = new ActivityObject($objectEl); + if ($objectEls->length > 0) { + for ($i = 0; $i < $objectEls->length; $i++) { + $objectEl = $objectEls->item($i); + $this->objects[] = new ActivityObject($objectEl); + } } else { - $this->object = new ActivityObject($entry); + $this->objects[] = new ActivityObject($entry); } $actorEl = $this->_child($entry, self::ACTOR); @@ -280,8 +284,8 @@ class Activity } } - $this->object = new ActivityObject($item); - $this->context = new ActivityContext($item); + $this->objects[] = new ActivityObject($item); + $this->context = new ActivityContext($item); } /** @@ -339,8 +343,10 @@ class Activity $xs->element('activity:verb', null, $this->verb); - if ($this->object) { - $xs->raw($this->object->asString()); + if (!empty($this->objects)) { + foreach($this->objects as $object) { + $xs->raw($object->asString()); + } } if ($this->target) { diff --git a/lib/activityobject.php b/lib/activityobject.php index 0a358ccab..34d1b9170 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -100,6 +100,13 @@ class ActivityObject public $poco; public $displayName; + // @todo move this stuff to it's own PHOTO activity object + const MEDIA_DESCRIPTION = 'description'; + + public $thumbnail; + public $largerImage; + public $description; + /** * Constructor * @@ -150,6 +157,19 @@ class ActivityObject $this->poco = new PoCo($element); } + + if ($this->type == self::PHOTO) { + + $this->thumbnail = ActivityUtils::getLink($element, 'preview'); + $this->largerImage = ActivityUtils::getLink($element, 'enclosure'); + + $this->description = ActivityUtils::childContent( + $element, + ActivityObject::MEDIA_DESCRIPTION, + Activity::MEDIA + ); + + } } private function _fromAuthor($element) diff --git a/plugins/OStatus/actions/groupsalmon.php b/plugins/OStatus/actions/groupsalmon.php index 29377b5fa..d60725a71 100644 --- a/plugins/OStatus/actions/groupsalmon.php +++ b/plugins/OStatus/actions/groupsalmon.php @@ -60,7 +60,8 @@ class GroupsalmonAction extends SalmonAction function handlePost() { - switch ($this->act->object->type) { + // @fixme process all objects? + switch ($this->act->objects[0]->type) { case ActivityObject::ARTICLE: case ActivityObject::BLOGENTRY: case ActivityObject::NOTE: diff --git a/plugins/OStatus/actions/usersalmon.php b/plugins/OStatus/actions/usersalmon.php index 15e8c1869..ecdcfa193 100644 --- a/plugins/OStatus/actions/usersalmon.php +++ b/plugins/OStatus/actions/usersalmon.php @@ -55,9 +55,10 @@ class UsersalmonAction extends SalmonAction */ function handlePost() { - common_log(LOG_INFO, "Received post of '{$this->act->object->id}' from '{$this->act->actor->id}'"); + common_log(LOG_INFO, "Received post of '{$this->act->objects[0]->id}' from '{$this->act->actor->id}'"); - switch ($this->act->object->type) { + // @fixme: process all activity objects? + switch ($this->act->objects[0]->type) { case ActivityObject::ARTICLE: case ActivityObject::BLOGENTRY: case ActivityObject::NOTE: diff --git a/plugins/OStatus/classes/Ostatus_profile.php b/plugins/OStatus/classes/Ostatus_profile.php index 0eb5b8b82..df937643b 100644 --- a/plugins/OStatus/classes/Ostatus_profile.php +++ b/plugins/OStatus/classes/Ostatus_profile.php @@ -494,7 +494,7 @@ class Ostatus_profile extends Memcached_DataObject // It's not always an ActivityObject::NOTE, but... let's just say it is. - $note = $activity->object; + $note = $activity->objects[0]; // The id URI will be used as a unique identifier for for the notice, // protecting against duplicate saves. It isn't required to be a URL; diff --git a/scripts/importtwitteratom.php b/scripts/importtwitteratom.php index 7316f2108..c12e3b91a 100644 --- a/scripts/importtwitteratom.php +++ b/scripts/importtwitteratom.php @@ -102,7 +102,7 @@ function importActivityStream($user, $doc) for ($i = $entries->length - 1; $i >= 0; $i--) { $entry = $entries->item($i); $activity = new Activity($entry, $feed); - $object = $activity->object; + $object = $activity->objects[0]; if (!have_option('q', 'quiet')) { print $activity->content . "\n"; } diff --git a/tests/ActivityParseTests.php b/tests/ActivityParseTests.php index 02d2ed734..fec8829eb 100644 --- a/tests/ActivityParseTests.php +++ b/tests/ActivityParseTests.php @@ -25,11 +25,11 @@ class ActivityParseTests extends PHPUnit_Framework_TestCase $this->assertEquals($act->time, 1243860840); $this->assertEquals($act->verb, ActivityVerb::POST); - $this->assertFalse(empty($act->object)); - $this->assertEquals($act->object->title, 'Punctuation Changeset'); - $this->assertEquals($act->object->type, 'http://versioncentral.example.org/activity/changeset'); - $this->assertEquals($act->object->summary, 'Fixing punctuation because it makes it more readable.'); - $this->assertEquals($act->object->id, 'tag:versioncentral.example.org,2009:/change/1643245'); + $this->assertFalse(empty($act->objects[0])); + $this->assertEquals($act->objects[0]->title, 'Punctuation Changeset'); + $this->assertEquals($act->objects[0]->type, 'http://versioncentral.example.org/activity/changeset'); + $this->assertEquals($act->objects[0]->summary, 'Fixing punctuation because it makes it more readable.'); + $this->assertEquals($act->objects[0]->id, 'tag:versioncentral.example.org,2009:/change/1643245'); } public function testExample3() @@ -56,12 +56,12 @@ class ActivityParseTests extends PHPUnit_Framework_TestCase $this->assertEquals($act->actor->title, 'John Doe'); $this->assertEquals($act->actor->id, 'mailto:johndoe@example.com'); - $this->assertFalse(empty($act->object)); - $this->assertEquals($act->object->type, ActivityObject::NOTE); - $this->assertEquals($act->object->id, 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a'); - $this->assertEquals($act->object->title, 'Atom-Powered Robots Run Amok'); - $this->assertEquals($act->object->summary, 'Some text.'); - $this->assertEquals($act->object->link, 'http://example.org/2003/12/13/atom03.html'); + $this->assertFalse(empty($act->objects[0])); + $this->assertEquals($act->objects[0]->type, ActivityObject::NOTE); + $this->assertEquals($act->objects[0]->id, 'urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a'); + $this->assertEquals($act->objects[0]->title, 'Atom-Powered Robots Run Amok'); + $this->assertEquals($act->objects[0]->summary, 'Some text.'); + $this->assertEquals($act->objects[0]->link, 'http://example.org/2003/12/13/atom03.html'); $this->assertFalse(empty($act->context)); @@ -90,8 +90,8 @@ class ActivityParseTests extends PHPUnit_Framework_TestCase $this->assertEquals('http://example.net/conversation/11', $act->context->conversation); $this->assertEquals(array('http://example.net/user/1'), $act->context->attention); - $this->assertFalse(empty($act->object)); - $this->assertEquals($act->object->content, + $this->assertFalse(empty($act->objects[0])); + $this->assertEquals($act->objects[0]->content, '@evan now is the time for all good men to come to the aid of their country. #'); $this->assertFalse(empty($act->actor)); @@ -215,6 +215,96 @@ class ActivityParseTests extends PHPUnit_Framework_TestCase $this->assertNull($actor->poco->address); $this->assertEquals(0, count($actor->poco->urls)); } + + // Media test - cliqset + public function testExample8() + { + global $_example8; + $dom = DOMDocument::loadXML($_example8); + + $feed = $dom->documentElement; + + $entries = $feed->getElementsByTagName('entry'); + + $entry = $entries->item(0); + + $act = new Activity($entry, $feed); + + $this->assertFalse(empty($act)); + $this->assertEquals($act->time, 1269221753); + $this->assertEquals($act->verb, ActivityVerb::POST); + $this->assertEquals($act->summary, 'zcopley posted 5 photos on Flickr'); + + $this->assertFalse(empty($act->objects)); + $this->assertEquals(sizeof($act->objects), 5); + + $this->assertEquals($act->objects[0]->type, ActivityObject::PHOTO); + $this->assertEquals($act->objects[0]->title, 'IMG_1368'); + $this->assertNull($act->objects[0]->description); + $this->assertEquals( + $act->objects[0]->thumbnail, + 'http://media.cliqset.com/6f6fbee9d7dfbffc73b6ef626275eb5f_thumb.jpg' + ); + $this->assertEquals( + $act->objects[0]->link, + 'http://www.flickr.com/photos/zcopley/4452933806/' + ); + + $this->assertEquals($act->objects[1]->type, ActivityObject::PHOTO); + $this->assertEquals($act->objects[1]->title, 'IMG_1365'); + $this->assertNull($act->objects[1]->description); + $this->assertEquals( + $act->objects[1]->thumbnail, + 'http://media.cliqset.com/b8f3932cd0bba1b27f7c8b3ef986915e_thumb.jpg' + ); + $this->assertEquals( + $act->objects[1]->link, + 'http://www.flickr.com/photos/zcopley/4442630390/' + ); + + $this->assertEquals($act->objects[2]->type, ActivityObject::PHOTO); + $this->assertEquals($act->objects[2]->title, 'Classic'); + $this->assertEquals( + $act->objects[2]->description, + '-Powered by pikchur.com/n0u' + ); + $this->assertEquals( + $act->objects[2]->thumbnail, + 'http://media.cliqset.com/fc54c15f850b7a9a8efa644087a48c91_thumb.jpg' + ); + $this->assertEquals( + $act->objects[2]->link, + 'http://www.flickr.com/photos/zcopley/4430754103/' + ); + + $this->assertEquals($act->objects[3]->type, ActivityObject::PHOTO); + $this->assertEquals($act->objects[3]->title, 'IMG_1363'); + $this->assertNull($act->objects[3]->description); + + $this->assertEquals( + $act->objects[3]->thumbnail, + 'http://media.cliqset.com/4b1d307c9217e2114391a8b229d612cb_thumb.jpg' + ); + $this->assertEquals( + $act->objects[3]->link, + 'http://www.flickr.com/photos/zcopley/4416969717/' + ); + + $this->assertEquals($act->objects[4]->type, ActivityObject::PHOTO); + $this->assertEquals($act->objects[4]->title, 'IMG_1361'); + $this->assertNull($act->objects[4]->description); + + $this->assertEquals( + $act->objects[4]->thumbnail, + 'http://media.cliqset.com/23d9b4b96b286e0347d36052f22f6e60_thumb.jpg' + ); + $this->assertEquals( + $act->objects[4]->link, + 'http://www.flickr.com/photos/zcopley/4417734232/' + ); + + } + } $_example1 = << EXAMPLE7; + +$_example8 = << + + + Activity Stream for: zcopley + http://cliqset.com/feed/atom?uid=zcopley + + 0 + http://activitystrea.ms/schema/1.0/post + 2010-03-22T01:35:53.000Z + + flickr + http://flickr.com + http://cliqset-services.s3.amazonaws.com/flickr.png + + + http://activitystrea.ms/schema/1.0/photo + IMG_1368 + + + + + http://activitystrea.ms/schema/1.0/photo + IMG_1365 + + + + + http://activitystrea.ms/schema/1.0/photo + Classic + + + -Powered by pikchur.com/n0u + + + http://activitystrea.ms/schema/1.0/photo + IMG_1363 + + + + + http://activitystrea.ms/schema/1.0/photo + IMG_1361 + + + + zcopley posted some photos on Flickr + zcopley posted 5 photos on Flickr + + 2010-03-22T20:46:42.778Z + tag:cliqset.com,2010-03-22:/user/zcopley/SVgAZubGhtAnSAee + + + zcopley + http://cliqset.com/user/zcopley + + + http://activitystrea.ms/schema/1.0/person + zcopley + + Zach + Copley + + + + + + + +EXAMPLE8; + +$_example9 = << + + + + Google Buzz + 2010-03-22T01:55:53.596Z + tag:google.com,2009:buzz-feed/public/posted/117848251937215158042 + Google - Google Buzz + + Buzz by Zach Copley from Flickr + IMG_1366 + 2010-03-18T04:29:23.000Z + 2010-03-18T05:14:03.325Z + tag:google.com,2009:buzz/z12zwdhxowq2d13q204cjr04kzu0cns5gh0 + + + Zach Copley + http://www.google.com/profiles/zcopley + + <div>IMG_1366</div> + + + IMG_1366 + + + + + IMG_1365 + + + http://activitystrea.ms/schema/1.0/post + + http://activitystrea.ms/schema/1.0/photo + tag:google.com,2009:buzz/z12zwdhxowq2d13q204cjr04kzu0cns5gh0 + Buzz by Zach Copley from Flickr + <div>IMG_1366</div> + + + + + 0 + + +EXAMPLE9; diff --git a/tests/UserFeedParseTest.php b/tests/UserFeedParseTest.php index b3f9a6417..208e71be6 100644 --- a/tests/UserFeedParseTest.php +++ b/tests/UserFeedParseTest.php @@ -66,11 +66,11 @@ class UserFeedParseTests extends PHPUnit_Framework_TestCase // test the post //var_export($act1); - $this->assertEquals($act1->object->type, 'http://activitystrea.ms/schema/1.0/note'); - $this->assertEquals($act1->object->title, 'And now for something completely insane...'); + $this->assertEquals($act1->objects[0]->type, 'http://activitystrea.ms/schema/1.0/note'); + $this->assertEquals($act1->objects[0]->title, 'And now for something completely insane...'); - $this->assertEquals($act1->object->content, 'And now for something completely insane...'); - $this->assertEquals($act1->object->id, 'http://localhost/statusnet/notice/3'); + $this->assertEquals($act1->objects[0]->content, 'And now for something completely insane...'); + $this->assertEquals($act1->objects[0]->id, 'http://localhost/statusnet/notice/3'); } -- cgit v1.2.3-54-g00ecf From 29566c5d4b17eb42d3f69bb6b8e74259ee4218fb Mon Sep 17 00:00:00 2001 From: Evan Prodromou Date: Sat, 27 Mar 2010 22:44:10 -0400 Subject: move check for bad IDs from activityobject to activity and make simpler --- lib/activity.php | 11 +++++++++++ lib/activityobject.php | 25 +++++++------------------ 2 files changed, 18 insertions(+), 18 deletions(-) (limited to 'lib/activityobject.php') diff --git a/lib/activity.php b/lib/activity.php index f9192c6b8..5d6230c6d 100644 --- a/lib/activity.php +++ b/lib/activity.php @@ -179,6 +179,17 @@ class Activity $this->actor = new ActivityObject($actorEl); + // Cliqset has bad actor IDs (just nickname of user). We + // work around it by getting the author data and using its + // id instead + + if (!preg_match('/^\w+:/', $this->actor->id)) { + $authorEl = ActivityUtils::child($entry, 'author'); + if (!empty($authorEl)) { + $authorObj = new ActivityObject($authorEl); + $this->actor->id = $authorObj->id; + } + } } else if (!empty($feed) && $subjectEl = $this->_child($feed, self::SUBJECT)) { diff --git a/lib/activityobject.php b/lib/activityobject.php index 34d1b9170..677a48197 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -177,10 +177,7 @@ class ActivityObject $this->type = self::PERSON; // XXX: is this fair? $this->title = $this->_childContent($element, self::NAME); - $id = $this->_childContent($element, self::URI); - if (ActivityUtils::validateUri($id)) { - $this->id = $id; - } + $this->id = $this->_childContent($element, self::URI); if (empty($this->id)) { $email = $this->_childContent($element, self::EMAIL); @@ -193,15 +190,6 @@ class ActivityObject private function _fromAtomEntry($element) { - if ($element->localName == 'actor') { - // Old-fashioned ... - // First pull anything from , then we'll add on top. - $author = ActivityUtils::child($element->parentNode, 'author'); - if ($author) { - $this->_fromAuthor($author); - } - } - $this->type = $this->_childContent($element, Activity::OBJECTTYPE, Activity::SPEC); @@ -209,11 +197,6 @@ class ActivityObject $this->type = ActivityObject::NOTE; } - $id = $this->_childContent($element, self::ID); - if (ActivityUtils::validateUri($id)) { - $this->id = $id; - } - $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY); $this->content = ActivityUtils::getContent($element); @@ -226,6 +209,12 @@ class ActivityObject $this->source = $this->_getSource($element); $this->link = ActivityUtils::getPermalink($element); + + $this->id = $this->_childContent($element, self::ID); + + if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID + $this->id = $this->link; + } } // @fixme rationalize with Activity::_fromRssItem() -- cgit v1.2.3-54-g00ecf