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('GNUSOCIAL')) {
35 require_once(INSTALLDIR . '/lib/activitystreamjsondocument.php');
38 * A noun-ish thing in the activity universe
40 * The activity streams spec talks about activity objects, while also having
41 * a tag activity:object, which is in fact an activity object. Aaaaaah!
43 * This is just a thing in the activity universe. Can be the subject, object,
44 * or indirect object (target!) of an activity verb. Rotten name, and I'm
45 * propagating it. *sigh*
49 * @author Evan Prodromou <evan@status.net>
50 * @copyright 2010 StatusNet, Inc.
51 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
52 * @link http://status.net/
56 const ARTICLE = 'http://activitystrea.ms/schema/1.0/article';
57 const BLOGENTRY = 'http://activitystrea.ms/schema/1.0/blog-entry';
58 const NOTE = 'http://activitystrea.ms/schema/1.0/note';
59 const STATUS = 'http://activitystrea.ms/schema/1.0/status';
60 const FILE = 'http://activitystrea.ms/schema/1.0/file';
61 const PHOTO = 'http://activitystrea.ms/schema/1.0/photo';
62 const ALBUM = 'http://activitystrea.ms/schema/1.0/photo-album';
63 const PLAYLIST = 'http://activitystrea.ms/schema/1.0/playlist';
64 const VIDEO = 'http://activitystrea.ms/schema/1.0/video';
65 const AUDIO = 'http://activitystrea.ms/schema/1.0/audio';
66 const BOOKMARK = 'http://activitystrea.ms/schema/1.0/bookmark';
67 const PERSON = 'http://activitystrea.ms/schema/1.0/person';
68 const GROUP = 'http://activitystrea.ms/schema/1.0/group';
69 const _LIST = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
70 const PLACE = 'http://activitystrea.ms/schema/1.0/place';
71 const COMMENT = 'http://activitystrea.ms/schema/1.0/comment';
73 const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
74 const SERVICE = 'http://activitystrea.ms/schema/1.0/service';
75 const IMAGE = 'http://activitystrea.ms/schema/1.0/image';
76 const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
77 const APPLICATION = 'http://activitystrea.ms/schema/1.0/application';
79 // Atom elements we snarf
81 const TITLE = 'title';
82 const SUMMARY = 'summary';
84 const SOURCE = 'source';
88 const EMAIL = 'email';
90 const POSTEROUS = 'http://posterous.com/help/rss/1.0';
91 const AUTHOR = 'author';
92 const USERIMAGE = 'userImage';
93 const PROFILEURL = 'profileUrl';
94 const NICKNAME = 'nickName';
95 const DISPLAYNAME = 'displayName';
96 const MEDIA_DESCRIPTION = 'description';
104 public $link; // think APP (Atom Publishing Protocol)
107 public $avatarLinks = [];
111 // @todo move this stuff to it's own PHOTO activity object
123 * This probably needs to be refactored
124 * to generate a local class (ActivityPerson, ActivityFile, ...)
125 * based on the object type.
127 * @param DOMElement $element DOM thing to turn into an Activity thing
129 public function __construct($element = null)
131 if (empty($element)) {
135 $this->element = $element;
137 $this->geopoint = $this->_childContent(
139 ActivityContext::POINT,
140 ActivityContext::GEORSS
143 if ($element->tagName == 'author') {
144 $this->_fromAuthor($element);
145 } elseif ($element->tagName == 'item') {
146 $this->_fromRssItem($element);
148 $this->_fromAtomEntry($element);
151 // Some per-type attributes...
152 if ($this->type == self::PERSON || $this->type == self::GROUP) {
153 $this->displayName = $this->title;
155 $photos = ActivityUtils::getLinks($element, 'photo');
156 if (count($photos)) {
157 foreach ($photos as $link) {
158 $this->avatarLinks[] = new AvatarLink($link);
161 $avatars = ActivityUtils::getLinks($element, 'avatar');
162 foreach ($avatars as $link) {
163 $this->avatarLinks[] = new AvatarLink($link);
167 $this->poco = new PoCo($element);
170 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 _childContent($element, $tag, $namespace = ActivityUtils::ATOM)
188 return ActivityUtils::childContent($element, $tag, $namespace);
191 private function _fromAuthor($element)
193 $this->type = $this->_childContent(
195 Activity::OBJECTTYPE,
199 if (empty($this->type)) {
200 $this->type = self::PERSON; // XXX: is this fair?
204 // Start with <poco::displayName>
206 $this->title = ActivityUtils::childContent($element, PoCo::DISPLAYNAME, PoCo::NS);
208 // try falling back to <atom:title>
210 if (empty($this->title)) {
211 $title = ActivityUtils::childHtmlContent($element, self::TITLE);
213 if (!empty($title)) {
214 $this->title = common_strip_html($title);
218 // fall back to <atom:name> as a last resort
220 if (empty($this->title)) {
221 $this->title = $this->_childContent($element, self::NAME);
224 // start with <atom:id>
226 $this->id = $this->_childContent($element, self::ID);
228 // fall back to <atom:uri>
230 if (empty($this->id)) {
231 $this->id = $this->_childContent($element, self::URI);
234 // fall further back to <atom:email>
236 if (empty($this->id)) {
237 $email = $this->_childContent($element, self::EMAIL);
238 if (!empty($email)) {
240 $this->id = 'mailto:' . $email;
244 $this->link = ActivityUtils::getPermalink($element);
246 // fall finally back to <link rel=alternate>
248 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
249 $this->id = $this->link;
253 // @todo FIXME: rationalize with Activity::_fromRssItem()
255 private function _fromRssItem($item)
257 if (empty($this->type)) {
258 $this->type = ActivityObject::NOTE;
261 $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
263 $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
265 if (!empty($contentEl)) {
266 $this->content = htmlspecialchars_decode($contentEl->textContent, ENT_QUOTES);
268 $descriptionEl = ActivityUtils::child($item, Activity::DESCRIPTION, Activity::RSS);
269 if (!empty($descriptionEl)) {
270 $this->content = htmlspecialchars_decode($descriptionEl->textContent, ENT_QUOTES);
274 $this->link = ActivityUtils::childContent($item, ActivityUtils::LINK, Activity::RSS);
276 $guidEl = ActivityUtils::child($item, Activity::GUID, Activity::RSS);
278 if (!empty($guidEl)) {
279 $this->id = $guidEl->textContent;
281 if ($guidEl->hasAttribute('isPermaLink') && $guidEl->getAttribute('isPermaLink') != 'false') {
283 $this->link = $this->id;
288 private function _fromAtomEntry($element)
290 $this->type = $this->_childContent(
292 Activity::OBJECTTYPE,
296 if (empty($this->type)) {
297 $this->type = ActivityObject::NOTE;
300 $this->summary = ActivityUtils::childHtmlContent($element, self::SUMMARY);
301 $this->content = ActivityUtils::getContent($element);
303 // We don't like HTML in our titles, although it's technically allowed
304 $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
306 $this->source = $this->_getSource($element);
308 $this->link = ActivityUtils::getPermalink($element);
309 $this->selfLink = ActivityUtils::getSelfLink($element);
311 $this->id = $this->_childContent($element, self::ID);
313 if (empty($this->id) && !empty($this->link)) { // fallback if there's no ID
314 $this->id = $this->link;
317 $els = $element->childNodes;
319 for ($i = 0; $i < $els->length; $i++) {
320 $link = $els->item($i);
321 if ($link->localName == ActivityUtils::LINK && $link->namespaceURI == ActivityUtils::ATOM) {
323 foreach ($link->attributes as $attrName => $attrNode) {
324 $attrs[$attrName] = $attrNode->nodeValue;
326 $this->extra[] = [$link->localName,
333 private function _getSource($element)
335 $sourceEl = ActivityUtils::child($element, 'source');
337 if (empty($sourceEl)) {
340 $href = ActivityUtils::getLink($sourceEl, 'self');
344 return ActivityUtils::childContent($sourceEl, 'id');
349 public static function fromRssAuthor($el)
351 $text = $el->textContent;
353 if (preg_match('/^(.*?) \((.*)\)$/', $text, $match)) {
356 } elseif (preg_match('/^(.*?) <(.*)>$/', $text, $match)) {
359 } elseif (preg_match('/.*@.*/', $text)) {
367 // Not really enough info
369 $obj = new ActivityObject();
373 $obj->type = ActivityObject::PERSON;
376 if (!empty($email)) {
377 $obj->id = 'mailto:' . $email;
383 public static function fromDcCreator($el)
385 // Not really enough info
387 $text = $el->textContent;
389 $obj = new ActivityObject();
394 $obj->type = ActivityObject::PERSON;
399 public static function fromRssChannel($el)
401 $obj = new ActivityObject();
405 $obj->type = ActivityObject::PERSON; // @fixme guess better
407 $obj->title = ActivityUtils::childContent($el, ActivityObject::TITLE, Activity::RSS);
408 $obj->link = ActivityUtils::childContent($el, ActivityUtils::LINK, Activity::RSS);
409 $obj->id = ActivityUtils::getLink($el, Activity::SELF);
411 if (empty($obj->id)) {
412 $obj->id = $obj->link;
415 $desc = ActivityUtils::childContent($el, Activity::DESCRIPTION, Activity::RSS);
418 $obj->content = htmlspecialchars_decode($desc, ENT_QUOTES);
421 $imageEl = ActivityUtils::child($el, Activity::IMAGE, Activity::RSS);
423 if (!empty($imageEl)) {
424 $url = ActivityUtils::childContent($imageEl, Activity::URL, Activity::RSS);
425 $al = new AvatarLink();
427 $obj->avatarLinks[] = $al;
433 // Try to get a unique id for the source feed
435 public static function fromPosterousAuthor($el)
437 $obj = new ActivityObject();
439 $obj->type = ActivityObject::PERSON; // @fixme any others...?
441 $userImage = ActivityUtils::childContent($el, self::USERIMAGE, self::POSTEROUS);
443 if (!empty($userImage)) {
444 $al = new AvatarLink();
445 $al->url = $userImage;
446 $obj->avatarLinks[] = $al;
449 $obj->link = ActivityUtils::childContent($el, self::PROFILEURL, self::POSTEROUS);
450 $obj->id = $obj->link;
452 $obj->poco = new PoCo();
454 $obj->poco->preferredUsername = ActivityUtils::childContent($el, self::NICKNAME, self::POSTEROUS);
455 $obj->poco->displayName = ActivityUtils::childContent($el, self::DISPLAYNAME, self::POSTEROUS);
457 $obj->title = $obj->poco->displayName;
462 public static function fromGroup(User_group $group)
464 $object = new ActivityObject();
466 if (Event::handle('StartActivityObjectFromGroup', [$group, &$object])) {
467 $object->type = ActivityObject::GROUP;
468 $object->id = $group->getUri();
469 $object->title = $group->getBestName();
470 $object->link = $group->getUri();
472 $object->avatarLinks[] = AvatarLink::fromFilename(
473 $group->homepage_logo,
477 $object->avatarLinks[] = AvatarLink::fromFilename(
482 $object->avatarLinks[] = AvatarLink::fromFilename(
487 $object->poco = PoCo::fromGroup($group);
488 Event::handle('EndActivityObjectFromGroup', [$group, &$object]);
494 public static function fromPeopletag($ptag)
496 $object = new ActivityObject();
497 if (Event::handle('StartActivityObjectFromPeopletag', [$ptag, &$object])) {
498 $object->type = ActivityObject::_LIST;
500 $object->id = $ptag->getUri();
501 $object->title = $ptag->tag;
502 $object->summary = $ptag->description;
503 $object->link = $ptag->homeUrl();
504 $object->owner = Profile::getKV('id', $ptag->tagger);
505 $object->poco = PoCo::fromProfile($object->owner);
506 Event::handle('EndActivityObjectFromPeopletag', [$ptag, &$object]);
511 public static function fromFile(File $file)
513 $object = new ActivityObject();
515 if (Event::handle('StartActivityObjectFromFile', [$file, &$object])) {
516 $object->type = self::mimeTypeToObjectType($file->mimetype);
517 $object->id = TagURI::mint(sprintf("file:%d", $file->id));
518 $object->link = $file->getAttachmentUrl();
521 $object->title = $file->title;
525 $object->date = $file->date;
529 $thumbnail = $file->getThumbnail();
530 $object->thumbnail = $thumbnail;
531 } catch (UseFileAsThumbnailException $e) {
532 $object->thumbnail = null;
533 } catch (UnsupportedMediaException $e) {
534 $object->thumbnail = null;
537 switch (self::canonicalType($object->type)) {
539 $object->largerImage = $file->getUrl();
543 $object->stream = $file->getUrl();
547 Event::handle('EndActivityObjectFromFile', [$file, &$object]);
553 public static function mimeTypeToObjectType($mimeType)
559 if (empty($mimeType)) {
563 $parts = explode('/', $mimeType);
582 public static function canonicalType($type)
584 return ActivityUtils::resolveUri($type, true);
587 public static function fromNoticeSource(Notice_source $source)
589 $object = new ActivityObject();
590 $wellKnown = ['web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
591 'activity', 'feed', 'mirror', 'twitter', 'facebook'];
593 if (Event::handle('StartActivityObjectFromNoticeSource', [$source, &$object])) {
594 $object->type = ActivityObject::APPLICATION;
596 if (in_array($source->code, $wellKnown)) {
597 // We use one ID for all well-known StatusNet sources
598 $object->id = "tag:status.net,2009:notice-source:" . $source->code;
599 } elseif ($source->url) {
600 // They registered with an URL
601 $object->id = $source->url;
603 // Locally-registered, no URL
604 $object->id = TagURI::mint("notice-source:" . $source->code);
608 $object->link = $source->url;
612 $object->title = $source->name;
614 $object->title = $source->code;
617 if ($source->created) {
618 $object->date = $source->created;
621 $object->extra[] = ['status_net', ['source_code' => $source->code]];
623 Event::handle('EndActivityObjectFromNoticeSource', [$source, &$object]);
629 public static function fromMessage(Message $message)
631 $object = new ActivityObject();
633 if (Event::handle('StartActivityObjectFromMessage', [$message, &$object])) {
634 $object->type = ActivityObject::NOTE;
635 $object->id = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
636 $object->content = $message->rendered;
637 $object->date = $message->created;
640 $object->link = $message->url;
642 $object->link = common_local_url('showmessage', ['message' => $message->id]);
645 $object->extra[] = ['status_net', ['message_id' => $message->id]];
647 Event::handle('EndActivityObjectFromMessage', [$message, &$object]);
654 * Returns an array based on this Activity Object suitable for
657 * @return array $object the activity object array
660 public function asString($tag = 'activity:object')
662 $xs = new XMLStringer(true);
664 $this->outputTo($xs, $tag);
666 return $xs->getString();
669 public function outputTo($xo, $tag = 'activity:object')
672 $xo->elementStart($tag);
675 if (Event::handle('StartActivityObjectOutputAtom', [$this, $xo])) {
676 $xo->element('activity:object-type', null, $this->type);
680 if ($tag == 'author') {
681 $xo->element(self::URI, null, $this->id);
683 $xo->element(self::ID, null, $this->id);
686 if (!empty($this->title)) {
687 $name = common_xml_safe_str($this->title);
688 if ($tag == 'author') {
689 // XXX: Backward compatibility hack -- atom:name should contain
690 // full name here, instead of nickname, i.e.: $name. Change
691 // this in the next version.
692 $xo->element(self::NAME, null, $this->poco->preferredUsername);
694 $xo->element(self::TITLE, null, $name);
698 if (!empty($this->summary)) {
702 common_xml_safe_str($this->summary)
706 if (!empty($this->content)) {
707 // XXX: assuming HTML content here
709 ActivityUtils::CONTENT,
711 common_xml_safe_str($this->content)
715 if (!empty($this->link)) {
719 'rel' => 'alternate',
720 'type' => 'text/html',
721 'href' => $this->link
726 if (!empty($this->selfLink)) {
731 'type' => 'application/atom+xml',
732 'href' => $this->selfLink
737 if (!empty($this->owner)) {
738 $owner = $this->owner->asActivityNoun(self::AUTHOR);
742 if ($this->type == ActivityObject::PERSON
743 || $this->type == ActivityObject::GROUP) {
744 foreach ($this->avatarLinks as $alink) {
749 'type' => $alink->type,
750 'media:width' => $alink->width,
751 'media:height' => $alink->height,
752 'href' => $alink->url,
758 if (!empty($this->geopoint)) {
766 if (!empty($this->poco)) {
767 $this->poco->outputTo($xo);
770 // @fixme there's no way here to make a tree; elements can only contain plaintext
771 // @fixme these may collide with JSON extensions
772 foreach ($this->extra as $el) {
773 list($extraTag, $attrs, $content) = array_pad($el, 3, null);
774 $xo->element($extraTag, $attrs, $content);
777 Event::handle('EndActivityObjectOutputAtom', [$this, $xo]);
781 $xo->elementEnd($tag);
787 public function asArray()
791 if (Event::handle('StartActivityObjectOutputJson', [$this, &$object])) {
792 // XXX: attachments are added by Activity
794 // author (Add object for author? Could be useful for repeats.)
796 // content (Add rendered version of the notice?)
798 // downstreamDuplicates
803 $object['id'] = $this->id;
804 } elseif ($this->link) {
805 $object['id'] = $this->link;
808 if ($this->type == ActivityObject::PERSON
809 || $this->type == ActivityObject::GROUP) {
812 $object['displayName'] = $this->title;
814 // XXX: Not sure what the best avatar is to use for the
815 // author's "image". For now, I'm using the large size.
818 $avatarMediaLinks = [];
820 foreach ($this->avatarLinks as $a) {
822 // Make a MediaLink for every other Avatar
823 $avatar = new ActivityStreamsMediaLink(
831 // Find the big avatar to use as the "image"
832 if ($a->height == AVATAR_PROFILE_SIZE) {
836 $avatarMediaLinks[] = $avatar->asArray();
839 if (!array_key_exists('status_net', $object)) {
840 $object['status_net'] = [];
843 $object['status_net']['avatarLinks'] = $avatarMediaLinks; // extension
846 if (!empty($imgLink)) {
847 $object['image'] = $imgLink->asArray();
853 // We can probably use the whole schema URL here but probably the
854 // relative simple name is easier to parse
856 $object['objectType'] = self::canonicalType($this->type);
859 $object['summary'] = $this->summary;
861 // content, usually rendered HTML
862 $object['content'] = $this->content;
864 // published (probably don't need. Might be useful for repeats.)
866 // updated (probably don't need this)
868 // TODO: upstreamDuplicates
871 $object['url'] = $this->link;
875 // @fixme these may collide with XML extensions
876 // @fixme multiple tags of same name will overwrite each other
877 // @fixme text content from XML extensions will be lost
879 foreach ($this->extra as $e) {
880 list($objectName, $props, $txt) = array_pad($e, 3, null);
881 if (!empty($objectName)) {
882 $parts = explode(":", $objectName);
883 if (count($parts) == 2 && $parts[0] == "statusnet") {
884 if (!array_key_exists('status_net', $object)) {
885 $object['status_net'] = [];
887 $object['status_net'][$parts[1]] = $props;
889 $object[$objectName] = $props;
894 if (!empty($this->geopoint)) {
895 list($lat, $lon) = explode(' ', $this->geopoint);
897 if (!empty($lat) && !empty($lon)) {
898 $object['location'] = [
899 'objectType' => 'place',
900 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon),
905 $loc = Location::fromLatLon((float)$lat, (float)$lon);
908 $name = $loc->getName();
911 $object['location']['displayName'] = $name;
913 $url = $loc->getURL();
916 $object['location']['url'] = $url;
922 if (!empty($this->poco)) {
923 $object['portablecontacts_net'] = array_filter($this->poco->asArray());
926 if (!empty($this->thumbnail)) {
927 if (is_string($this->thumbnail)) {
928 $object['image'] = ['url' => $this->thumbnail];
930 $object['image'] = ['url' => $this->thumbnail->getUrl()];
931 if ($this->thumbnail->width) {
932 $object['image']['width'] = $this->thumbnail->width;
934 if ($this->thumbnail->height) {
935 $object['image']['height'] = $this->thumbnail->height;
940 switch (self::canonicalType($this->type)) {
942 if (!empty($this->largerImage)) {
943 $object['fullImage'] = ['url' => $this->largerImage];
948 if (!empty($this->stream)) {
949 $object['stream'] = ['url' => $this->stream];
954 Event::handle('EndActivityObjectOutputJson', [$this, &$object]);
956 return array_filter($object);
959 public function getIdentifiers()
962 foreach (['id', 'link', 'url'] as $id) {
963 if (isset($this->$id)) {
967 return array_unique($ids);