3 * StatusNet, the distributed open-source microblogging tool
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Evan Prodromou <evan@status.net>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2010 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
28 * @link http://status.net/
31 if (!defined('STATUSNET')) {
36 * A noun-ish thing in the activity universe
38 * The activity streams spec talks about activity objects, while also having
39 * a tag activity:object, which is in fact an activity object. Aaaaaah!
41 * This is just a thing in the activity universe. Can be the subject, object,
42 * or indirect object (target!) of an activity verb. Rotten name, and I'm
43 * propagating it. *sigh*
47 * @author Evan Prodromou <evan@status.net>
48 * @copyright 2010 StatusNet, Inc.
49 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
50 * @link http://status.net/
54 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
55 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
56 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
57 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
58 const FILE = 'http://activitystrea.ms/schema/1.0/file';
59 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
60 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
61 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
62 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
63 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
64 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
65 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
66 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
67 const _LIST = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
68 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
69 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
71 const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
72 const SERVICE = 'http://activitystrea.ms/schema/1.0/service';
73 const IMAGE = 'http://activitystrea.ms/schema/1.0/image';
74 const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
75 const APPLICATION = 'http://activitystrea.ms/schema/1.0/application';
77 // Atom elements we snarf
79 const TITLE = 'title';
80 const SUMMARY = 'summary';
82 const SOURCE = 'source';
86 const EMAIL = 'email';
88 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
89 const AUTHOR = 'author';
90 const USERIMAGE = 'userImage';
91 const PROFILEURL = 'profileUrl';
92 const NICKNAME = 'nickName';
93 const DISPLAYNAME = 'displayName';
104 public $avatarLinks = array();
109 // @todo move this stuff to it's own PHOTO activity object
110 const MEDIA_DESCRIPTION = 'description';
115 public $extra = array();
122 * This probably needs to be refactored
123 * to generate a local class (ActivityPerson, ActivityFile, ...)
124 * based on the object type.
126 * @param DOMElement $element DOM thing to turn into an Activity thing
128 function __construct($element = null)
130 if (empty($element)) {
134 $this->element = $element;
136 $this->geopoint = $this->_childContent(
138 ActivityContext::POINT,
139 ActivityContext::GEORSS
142 if ($element->tagName == 'author') {
143 $this->_fromAuthor($element);
144 } else if ($element->tagName == 'item') {
145 $this->_fromRssItem($element);
147 $this->_fromAtomEntry($element);
150 // Some per-type attributes...
151 if ($this->type == self::PERSON || $this->type == self::GROUP) {
152 $this->displayName = $this->title;
154 $photos = ActivityUtils::getLinks($element, 'photo');
155 if (count($photos)) {
156 foreach ($photos as $link) {
157 $this->avatarLinks[] = new AvatarLink($link);
160 $avatars = ActivityUtils::getLinks($element, 'avatar');
161 foreach ($avatars as $link) {
162 $this->avatarLinks[] = new AvatarLink($link);
166 $this->poco = new PoCo($element);
169 if ($this->type == self::PHOTO) {
171 $this->thumbnail = ActivityUtils::getLink($element, 'preview');
172 $this->largerImage = ActivityUtils::getLink($element, 'enclosure');
174 $this->description = ActivityUtils::childContent(
176 ActivityObject::MEDIA_DESCRIPTION,
180 if ($this->type == self::_LIST) {
181 $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
182 $this->owner = new ActivityObject($owner);
186 private function _fromAuthor($element)
188 $this->type = $this->_childContent($element,
189 Activity::OBJECTTYPE,
192 if (empty($this->type)) {
193 $this->type = self::PERSON; // XXX: is this fair?
196 // start with <atom:title>
198 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
200 if (!empty($title)) {
201 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
204 // fall back to <atom:name>
206 if (empty($this->title)) {
207 $this->title = $this->_childContent($element, self::NAME);
210 // start with <atom:id>
212 $this->id = $this->_childContent($element, self::ID);
214 // fall back to <atom:uri>
216 if (empty($this->id)) {
217 $this->id = $this->_childContent($element, self::URI);
220 // fall further back to <atom:email>
222 if (empty($this->id)) {
223 $email = $this->_childContent($element, self::EMAIL);
224 if (!empty($email)) {
226 $this->id = 'mailto:'.$email;
230 $this->link = ActivityUtils::getPermalink($element);
232 // fall finally back to <link rel=alternate>
234 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
235 $this->id = $this->link;
239 private function _fromAtomEntry($element)
241 $this->type = $this->_childContent($element, Activity::OBJECTTYPE,
244 if (empty($this->type)) {
245 $this->type = ActivityObject::NOTE;
248 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
249 $this->content = ActivityUtils::getContent($element);
251 // We don't like HTML in our titles, although it's technically allowed
253 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
255 $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
257 $this->source = $this->_getSource($element);
259 $this->link = ActivityUtils::getPermalink($element);
261 $this->id = $this->_childContent($element, self::ID);
263 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
264 $this->id = $this->link;
268 // @todo FIXME: rationalize with Activity::_fromRssItem()
269 private function _fromRssItem($item)
271 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
273 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
275 if (!empty($contentEl)) {
276 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
278 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
279 if (!empty($descriptionEl)) {
280 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
284 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
286 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
288 if (!empty($guidEl)) {
289 $this->id = $guidEl->textContent;
291 if ($guidEl->hasAttribute('isPermaLink')) {
293 $this->link = $this->id;
298 public static function fromRssAuthor($el)
300 $text = $el->textContent;
302 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
305 } else if (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
308 } else if (preg_match('/.*@.*/', $text)) {
316 // Not really enough info
318 $obj = new ActivityObject();
322 $obj->type = ActivityObject::PERSON;
325 if (!empty($email)) {
326 $obj->id = 'mailto:'.$email;
332 public static function fromDcCreator($el)
334 // Not really enough info
336 $text = $el->textContent;
338 $obj = new ActivityObject();
343 $obj->type = ActivityObject::PERSON;
348 public static function fromRssChannel($el)
350 $obj = new ActivityObject();
354 $obj->type = ActivityObject::PERSON; // @fixme guess better
356 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
357 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
358 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
360 if (empty($obj->id)) {
361 $obj->id = $obj->link;
364 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
367 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
370 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
372 if (!empty($imageEl)) {
373 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
374 $al = new AvatarLink();
376 $obj->avatarLinks[] = $al;
382 public static function fromPosterousAuthor($el)
384 $obj = new ActivityObject();
386 $obj->type = ActivityObject::PERSON; // @fixme any others...?
388 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
390 if (!empty($userImage)) {
391 $al = new AvatarLink();
392 $al->url = $userImage;
393 $obj->avatarLinks[] = $al;
396 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
397 $obj->id = $obj->link;
399 $obj->poco = new PoCo();
401 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
402 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
404 $obj->title = $obj->poco->displayName;
409 private function _childContent($element, $tag, $namespace=ActivityUtils::ATOM)
411 return ActivityUtils::childContent($element, $tag, $namespace);
414 // Try to get a unique id for the source feed
416 private function _getSource($element)
418 $sourceEl = ActivityUtils::child($element, 'source');
420 if (empty($sourceEl)) {
423 $href = ActivityUtils::getLink($sourceEl, 'self');
427 return ActivityUtils::childContent($sourceEl, 'id');
432 static function fromNotice(Notice $notice)
434 $object = new ActivityObject();
436 if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
438 $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type;
440 $object->id = $notice->uri;
441 $object->title = 'New ' . ActivityObject::canonicalType($object->type) . ' by ';
443 $object->title .= $notice->getProfile()->getAcctUri();
444 } catch (ProfileNoAcctUriException $e) {
445 $object->title .= $e->profile->nickname;
447 $object->content = $notice->rendered;
448 $object->link = $notice->bestUrl();
450 $object->extra[] = array('status_net', array('notice_id' => $notice->id));
452 Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
458 static function fromProfile(Profile $profile)
460 $object = new ActivityObject();
462 if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
463 $object->type = ActivityObject::PERSON;
464 $object->id = $profile->getUri();
465 $object->title = $profile->getBestName();
466 $object->link = $profile->profileurl;
469 $avatar = Avatar::getUploaded($profile);
470 $object->avatarLinks[] = AvatarLink::fromAvatar($avatar);
471 } catch (NoAvatarException $e) {
472 // Could not find an original avatar to link
481 foreach ($sizes as $size) {
484 $avatar = Avatar::byProfile($profile, $size);
485 $alink = AvatarLink::fromAvatar($avatar);
486 } catch (NoAvatarException $e) {
487 $alink = new AvatarLink();
488 $alink->type = 'image/png';
489 $alink->height = $size;
490 $alink->width = $size;
491 $alink->url = Avatar::defaultImage($size);
494 $object->avatarLinks[] = $alink;
497 if (isset($profile->lat) && isset($profile->lon)) {
498 $object->geopoint = (float)$profile->lat
499 . ' ' . (float)$profile->lon;
502 $object->poco = PoCo::fromProfile($profile);
504 if ($profile->isLocal()) {
505 $object->extra[] = array('followers', array('url' => common_local_url('subscribers', array('nickname' => $profile->nickname))));
508 Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
514 static function fromGroup(User_group $group)
516 $object = new ActivityObject();
518 if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
520 $object->type = ActivityObject::GROUP;
521 $object->id = $group->getUri();
522 $object->title = $group->getBestName();
523 $object->link = $group->getUri();
525 $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
526 AVATAR_PROFILE_SIZE);
528 $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
531 $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
534 $object->poco = PoCo::fromGroup($group);
535 Event::handle('EndActivityObjectFromGroup', array($group, &$object));
541 static function fromPeopletag($ptag)
543 $object = new ActivityObject();
544 if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
545 $object->type = ActivityObject::_LIST;
547 $object->id = $ptag->getUri();
548 $object->title = $ptag->tag;
549 $object->summary = $ptag->description;
550 $object->link = $ptag->homeUrl();
551 $object->owner = Profile::getKV('id', $ptag->tagger);
552 $object->poco = PoCo::fromProfile($object->owner);
553 Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
558 static function fromFile(File $file)
560 $object = new ActivityObject();
562 if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
564 $object->type = self::mimeTypeToObjectType($file->mimetype);
565 $object->id = TagURI::mint(sprintf("file:%d", $file->id));
566 $object->link = common_local_url('attachment', array('attachment' => $file->id));
569 $object->title = $file->title;
573 $object->date = $file->date;
576 $thumbnail = $file->getThumbnail();
578 if (!empty($thumbnail)) {
579 $object->thumbnail = $thumbnail;
582 switch (ActivityObject::canonicalType($object->type)) {
584 $object->largerImage = $file->url;
588 $object->stream = $file->url;
592 Event::handle('EndActivityObjectFromFile', array($file, &$object));
598 static function fromNoticeSource(Notice_source $source)
600 $object = new ActivityObject();
601 $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
602 'activity', 'feed', 'mirror', 'twitter', 'facebook');
604 if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
605 $object->type = ActivityObject::APPLICATION;
607 if (in_array($source->code, $wellKnown)) {
608 // We use one ID for all well-known StatusNet sources
609 $object->id = "tag:status.net,2009:notice-source:".$source->code;
610 } else if ($source->url) {
611 // They registered with an URL
612 $object->id = $source->url;
614 // Locally-registered, no URL
615 $object->id = TagURI::mint("notice-source:".$source->code);
619 $object->link = $source->url;
623 $object->title = $source->name;
625 $object->title = $source->code;
628 if ($source->created) {
629 $object->date = $source->created;
632 $object->extra[] = array('status_net', array('source_code' => $source->code));
634 Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
640 static function fromMessage(Message $message)
642 $object = new ActivityObject();
644 if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
646 $object->type = ActivityObject::NOTE;
647 $object->id = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
648 $object->content = $message->rendered;
649 $object->date = $message->created;
652 $object->link = $message->url;
654 $object->link = common_local_url('showmessage', array('message' => $message->id));
657 $object->extra[] = array('status_net', array('message_id' => $message->id));
659 Event::handle('EndActivityObjectFromMessage', array($message, &$object));
665 function outputTo($xo, $tag='activity:object')
668 $xo->elementStart($tag);
671 if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
672 $xo->element('activity:object-type', null, $this->type);
676 if ($tag == 'author') {
677 $xo->element(self::URI, null, $this->id);
679 $xo->element(self::ID, null, $this->id);
682 if (!empty($this->title)) {
683 $name = common_xml_safe_str($this->title);
684 if ($tag == 'author') {
685 // XXX: Backward compatibility hack -- atom:name should contain
686 // full name here, instead of nickname, i.e.: $name. Change
687 // this in the next version.
688 $xo->element(self::NAME, null, $this->poco->preferredUsername);
690 $xo->element(self::TITLE, null, $name);
694 if (!empty($this->summary)) {
698 common_xml_safe_str($this->summary)
702 if (!empty($this->content)) {
703 // XXX: assuming HTML content here
705 ActivityUtils::CONTENT,
706 array('type' => 'html'),
707 common_xml_safe_str($this->content)
711 if (!empty($this->link)) {
715 'rel' => 'alternate',
716 'type' => 'text/html',
717 'href' => $this->link
723 if(!empty($this->owner)) {
724 $owner = $this->owner->asActivityNoun(self::AUTHOR);
728 if ($this->type == ActivityObject::PERSON
729 || $this->type == ActivityObject::GROUP) {
731 foreach ($this->avatarLinks as $alink) {
735 'type' => $alink->type,
736 'media:width' => $alink->width,
737 'media:height' => $alink->height,
738 'href' => $alink->url,
744 if (!empty($this->geopoint)) {
752 if (!empty($this->poco)) {
753 $this->poco->outputTo($xo);
756 // @fixme there's no way here to make a tree; elements can only contain plaintext
757 // @fixme these may collide with JSON extensions
758 foreach ($this->extra as $el) {
759 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
760 $xo->element($extraTag, $attrs, $content);
763 Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
767 $xo->elementEnd($tag);
773 function asString($tag='activity:object')
775 $xs = new XMLStringer(true);
777 $this->outputTo($xs, $tag);
779 return $xs->getString();
783 * Returns an array based on this Activity Object suitable for
786 * @return array $object the activity object array
793 if (Event::handle('StartActivityObjectOutputJson', array($this, &$object))) {
794 // XXX: attachments are added by Activity
796 // author (Add object for author? Could be useful for repeats.)
798 // content (Add rendered version of the notice?)
800 // downstreamDuplicates
805 $object['id'] = $this->id;
806 } else if ($this->link) {
807 $object['id'] = $this->link;
810 if ($this->type == ActivityObject::PERSON
811 || $this->type == ActivityObject::GROUP) {
814 $object['displayName'] = $this->title;
816 // XXX: Not sure what the best avatar is to use for the
817 // author's "image". For now, I'm using the large size.
820 $avatarMediaLinks = array();
822 foreach ($this->avatarLinks as $a) {
824 // Make a MediaLink for every other Avatar
825 $avatar = new ActivityStreamsMediaLink(
833 // Find the big avatar to use as the "image"
834 if ($a->height == AVATAR_PROFILE_SIZE) {
838 $avatarMediaLinks[] = $avatar->asArray();
841 if (!array_key_exists('status_net', $object)) {
842 $object['status_net'] = array();
845 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
848 if (!empty($imgLink)) {
849 $object['image'] = $imgLink->asArray();
855 // We can probably use the whole schema URL here but probably the
856 // relative simple name is easier to parse
858 $object['objectType'] = ActivityObject::canonicalType($this->type);
861 $object['summary'] = $this->summary;
863 // content, usually rendered HTML
864 $object['content'] = $this->content;
866 // published (probably don't need. Might be useful for repeats.)
868 // updated (probably don't need this)
870 // TODO: upstreamDuplicates
873 $object['url'] = $this->link;
877 // @fixme these may collide with XML extensions
878 // @fixme multiple tags of same name will overwrite each other
879 // @fixme text content from XML extensions will be lost
881 foreach ($this->extra as $e) {
882 list($objectName, $props, $txt) = array_pad($e, 3, null);
883 if (!empty($objectName)) {
884 $parts = explode(":", $objectName);
885 if (count($parts) == 2 && $parts[0] == "statusnet") {
886 if (!array_key_exists('status_net', $object)) {
887 $object['status_net'] = array();
889 $object['status_net'][$parts[1]] = $props;
891 $object[$objectName] = $props;
896 if (!empty($this->geopoint)) {
898 list($lat, $lon) = explode(' ', $this->geopoint);
900 if (!empty($lat) && !empty($lon)) {
901 $object['location'] = array(
902 'objectType' => 'place',
903 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
908 $loc = Location::fromLatLon((float)$lat, (float)$lon);
911 $name = $loc->getName();
914 $object['location']['displayName'] = $name;
916 $url = $loc->getURL();
919 $object['location']['url'] = $url;
925 if (!empty($this->poco)) {
926 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
929 if (!empty($this->thumbnail)) {
930 if (is_string($this->thumbnail)) {
931 $object['image'] = array('url' => $this->thumbnail);
933 $object['image'] = array('url' => $this->thumbnail->url);
934 if ($this->thumbnail->width) {
935 $object['image']['width'] = $this->thumbnail->width;
937 if ($this->thumbnail->height) {
938 $object['image']['height'] = $this->thumbnail->height;
943 switch (ActivityObject::canonicalType($this->type)) {
945 if (!empty($this->largerImage)) {
946 $object['fullImage'] = array('url' => $this->largerImage);
951 if (!empty($this->stream)) {
952 $object['stream'] = array('url' => $this->stream);
957 Event::handle('EndActivityObjectOutputJson', array($this, &$object));
959 return array_filter($object);
962 static function canonicalType($type) {
963 $ns = 'http://activitystrea.ms/schema/1.0/';
964 if (substr($type, 0, mb_strlen($ns)) == $ns) {
965 return substr($type, mb_strlen($ns));
971 static function mimeTypeToObjectType($mimeType) {
976 if (empty($mimeType)) {
980 $parts = explode('/', $mimeType);