]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/activityobject.php
??? Not callable?
[quix0rs-gnu-social.git] / lib / activityobject.php
index f774dd3c8bc0abdf35d1d474e4cfbb45628358b7..aa7636c713bc37ebdd7214505ef97439127dadf3 100644 (file)
@@ -32,6 +32,8 @@ if (!defined('STATUSNET')) {
     exit(1);
 }
 
+require_once(INSTALLDIR.'/lib/activitystreamjsondocument.php');
+
 /**
  * A noun-ish thing in the activity universe
  *
@@ -64,9 +66,15 @@ class ActivityObject
     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 _LIST     = 'http://activitystrea.ms/schema/1.0/list'; // LIST is reserved
     const PLACE     = 'http://activitystrea.ms/schema/1.0/place';
     const COMMENT   = 'http://activitystrea.ms/schema/1.0/comment';
     // ^^^^^^^^^^ tea!
+    const ACTIVITY = 'http://activitystrea.ms/schema/1.0/activity';
+    const SERVICE   = 'http://activitystrea.ms/schema/1.0/service';
+    const IMAGE     = 'http://activitystrea.ms/schema/1.0/image';
+    const COLLECTION = 'http://activitystrea.ms/schema/1.0/collection';
+    const APPLICATION = 'http://activitystrea.ms/schema/1.0/application';
 
     // Atom elements we snarf
 
@@ -92,6 +100,7 @@ class ActivityObject
     public $title;
     public $summary;
     public $content;
+    public $owner;
     public $link;
     public $source;
     public $avatarLinks = array();
@@ -107,6 +116,8 @@ class ActivityObject
     public $description;
     public $extra = array();
 
+    public $stream;
+
     /**
      * Constructor
      *
@@ -168,6 +179,10 @@ class ActivityObject
                 Activity::MEDIA
             );
         }
+        if ($this->type == self::_LIST) {
+            $owner = ActivityUtils::child($this->element, Activity::AUTHOR, Activity::SPEC);
+            $this->owner = new ActivityObject($owner);
+        }
     }
 
     private function _fromAuthor($element)
@@ -179,16 +194,23 @@ class ActivityObject
         if (empty($this->type)) {
             $this->type = self::PERSON; // XXX: is this fair?
         }
-        
-        // start with <atom:title>
 
-        $title = ActivityUtils::childHtmlContent($element, self::TITLE);
 
-        if (!empty($title)) {
-            $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');
+        // Start with <poco::displayName>
+
+        $this->title = ActivityUtils::childContent($element, PoCo::DISPLAYNAME, PoCo::NS);
+
+        // try falling back to <atom:title>
+
+        if (empty($this->title)) {
+            $title = ActivityUtils::childHtmlContent($element, self::TITLE);
+
+            if (!empty($title)) {
+                $this->title = common_strip_html($title);
+            }
         }
 
-        // fall back to <atom:name>
+        // fall back to <atom:name> as a last resort
 
         if (empty($this->title)) {
             $this->title = $this->_childContent($element, self::NAME);
@@ -236,10 +258,7 @@ class ActivityObject
         $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), ENT_QUOTES, 'UTF-8');
+        $this->title = common_strip_html(ActivityUtils::childHtmlContent($element, self::TITLE));
 
         $this->source  = $this->_getSource($element);
 
@@ -255,6 +274,10 @@ class ActivityObject
     // @todo FIXME: rationalize with Activity::_fromRssItem()
     private function _fromRssItem($item)
     {
+        if (empty($this->type)) {
+            $this->type = ActivityObject::NOTE;
+        }
+
         $this->title = ActivityUtils::childContent($item, ActivityObject::TITLE, Activity::RSS);
 
         $contentEl = ActivityUtils::child($item, ActivityUtils::CONTENT, Activity::CONTENTNS);
@@ -416,218 +439,496 @@ class ActivityObject
         }
     }
 
-    static function fromNotice(Notice $notice)
+    static function fromGroup(User_group $group)
     {
         $object = new ActivityObject();
-               
-               if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) {
 
-                       $object->type    = ActivityObject::NOTE;
+        if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
 
-                       $object->id      = $notice->uri;
-                       $object->title   = $notice->content;
-                       $object->content = $notice->rendered;
-                       $object->link    = $notice->bestUrl();
+            $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);
+            Event::handle('EndActivityObjectFromGroup', array($group, &$object));
+        }
 
-                       Event::handle('EndActivityObjectFromNotice', array($notice, &$object));
-               }
+        return $object;
+    }
 
+    static function fromPeopletag($ptag)
+    {
+        $object = new ActivityObject();
+        if (Event::handle('StartActivityObjectFromPeopletag', array($ptag, &$object))) {
+            $object->type    = ActivityObject::_LIST;
+
+            $object->id      = $ptag->getUri();
+            $object->title   = $ptag->tag;
+            $object->summary = $ptag->description;
+            $object->link    = $ptag->homeUrl();
+            $object->owner   = Profile::getKV('id', $ptag->tagger);
+            $object->poco    = PoCo::fromProfile($object->owner);
+            Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object));
+        }
         return $object;
     }
 
-    static function fromProfile(Profile $profile)
+    static function fromFile(File $file)
     {
         $object = new ActivityObject();
 
-               if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) {
+        if (Event::handle('StartActivityObjectFromFile', array($file, &$object))) {
 
-                       $object->type   = ActivityObject::PERSON;
-                       $object->id     = $profile->getUri();
-                       $object->title  = $profile->getBestName();
-                       $object->link   = $profile->profileurl;
+            $object->type = self::mimeTypeToObjectType($file->mimetype);
+            $object->id   = TagURI::mint(sprintf("file:%d", $file->id));
+            $object->link = common_local_url('attachment', array('attachment' => $file->id));
 
-                       $orig = $profile->getOriginalAvatar();
+            if ($file->title) {
+                $object->title = $file->title;
+            }
 
-                       if (!empty($orig)) {
-                               $object->avatarLinks[] = AvatarLink::fromAvatar($orig);
-                       }
+            if ($file->date) {
+                $object->date = $file->date;
+            }
 
-                       $sizes = array(
-                AVATAR_PROFILE_SIZE,
-                AVATAR_STREAM_SIZE,
-                AVATAR_MINI_SIZE
-            );
+            try {
+                $thumbnail = $file->getThumbnail();
+                $object->thumbnail = $thumbnail;
+            } catch (UseFileAsThumbnailException $e) {
+                $object->thumbnail = null;
+            } catch (UnsupportedMediaException $e) {
+                $object->thumbnail = null;
+            }
+
+            switch (self::canonicalType($object->type)) {
+            case 'image':
+                $object->largerImage = $file->url;
+                break;
+            case 'video':
+            case 'audio':
+                $object->stream = $file->url;
+                break;
+            }
 
-                       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);
-
-                                       if ($size == AVATAR_PROFILE_SIZE) {
-                                               // Hack for Twitter import: we don't have a 96x96 image,
-                                               // but we do have a 73x73 image. For now, fake it with that.
-                                               $avatar = $profile->getAvatar(73);
-                                               if ($avatar) {
-                                                       $alink = AvatarLink::fromAvatar($avatar);
-                                                       $alink->height= $size;
-                                                       $alink->width = $size;
-                                               }
-                                       }
-                               }
-
-                               $object->avatarLinks[] = $alink;
-                       }
-
-                       if (isset($profile->lat) && isset($profile->lon)) {
-                               $object->geopoint = (float)$profile->lat
-                                       . ' ' . (float)$profile->lon;
-                       }
-
-                       $object->poco = PoCo::fromProfile($profile);
-
-                       Event::handle('EndActivityObjectFromProfile', array($profile, &$object));
-               }
+            Event::handle('EndActivityObjectFromFile', array($file, &$object));
+        }
 
         return $object;
     }
 
-    static function fromGroup($group)
+    static function fromNoticeSource(Notice_source $source)
     {
         $object = new ActivityObject();
+        $wellKnown = array('web', 'xmpp', 'mail', 'omb', 'system', 'api', 'ostatus',
+                           'activity', 'feed', 'mirror', 'twitter', 'facebook');
+
+        if (Event::handle('StartActivityObjectFromNoticeSource', array($source, &$object))) {
+            $object->type = ActivityObject::APPLICATION;
+
+            if (in_array($source->code, $wellKnown)) {
+                // We use one ID for all well-known StatusNet sources
+                $object->id = "tag:status.net,2009:notice-source:".$source->code;
+            } else if ($source->url) {
+                // They registered with an URL
+                $object->id = $source->url;
+            } else {
+                // Locally-registered, no URL
+                $object->id = TagURI::mint("notice-source:".$source->code);
+            }
+
+            if ($source->url) {
+                $object->link = $source->url;
+            }
+
+            if ($source->name) {
+                $object->title = $source->name;
+            } else {
+                $object->title = $source->code;
+            }
+
+            if ($source->created) {
+                $object->date = $source->created;
+            }
+            
+            $object->extra[] = array('status_net', array('source_code' => $source->code));
 
-               if (Event::handle('StartActivityObjectFromGroup', array($group, &$object))) {
+            Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object));
+        }
 
-                       $object->type   = ActivityObject::GROUP;
-                       $object->id     = $group->getUri();
-                       $object->title  = $group->getBestName();
-                       $object->link   = $group->getUri();
+        return $object;
+    }
 
-                       $object->avatarLinks[] = AvatarLink::fromFilename($group->homepage_logo,
-                                                                                                                         AVATAR_PROFILE_SIZE);
+    static function fromMessage(Message $message)
+    {
+        $object = new ActivityObject();
 
-                       $object->avatarLinks[] = AvatarLink::fromFilename($group->stream_logo,
-                                                                                                                         AVATAR_STREAM_SIZE);
+        if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) {
 
-                       $object->avatarLinks[] = AvatarLink::fromFilename($group->mini_logo,
-                                                                                                                         AVATAR_MINI_SIZE);
+            $object->type    = ActivityObject::NOTE;
+            $object->id      = ($message->uri) ? $message->uri : (($message->url) ? $message->url : TagURI::mint(sprintf("message:%d", $message->id)));
+            $object->content = $message->rendered;
+            $object->date    = $message->created;
 
-                       $object->poco = PoCo::fromGroup($group);
+            if ($message->url) {
+                $object->link = $message->url;
+            } else {
+                $object->link = common_local_url('showmessage', array('message' => $message->id));
+            }
 
-                       Event::handle('EndActivityObjectFromGroup', array($group, &$object));
-               }
+            $object->extra[] = array('status_net', array('message_id' => $message->id));
+            
+            Event::handle('EndActivityObjectFromMessage', array($message, &$object));
+        }
 
         return $object;
     }
-       
-       function outputTo($xo, $tag='activity:object')
-       {
-               if (!empty($tag)) {
-                       $xo->elementStart($tag);
-               }
 
-        $xo->element('activity:object-type', null, $this->type);
+    function outputTo($xo, $tag='activity:object')
+    {
+        if (!empty($tag)) {
+            $xo->elementStart($tag);
+        }
 
-        // <author> uses URI
+        if (Event::handle('StartActivityObjectOutputAtom', array($this, $xo))) {
+            $xo->element('activity:object-type', null, $this->type);
 
-        if ($tag == 'author') {
-            $xo->element(self::URI, null, $this->id);
-        } else {
-            $xo->element(self::ID, null, $this->id);
-        }
+            // <author> uses URI
 
-        if (!empty($this->title)) {
-            $name = common_xml_safe_str($this->title);
             if ($tag == 'author') {
-                $xo->element(self::NAME, null, $name);
+                $xo->element(self::URI, null, $this->id);
             } else {
-                $xo->element(self::TITLE, null, $name);
+                $xo->element(self::ID, null, $this->id);
             }
-        }
-
-        if (!empty($this->summary)) {
-            $xo->element(
-                self::SUMMARY,
-                null,
-                common_xml_safe_str($this->summary)
-            );
-        }
 
-        if (!empty($this->content)) {
-            // XXX: assuming HTML content here
-            $xo->element(
-                ActivityUtils::CONTENT,
-                array('type' => 'html'),
-                common_xml_safe_str($this->content)
-            );
-        }
+            if (!empty($this->title)) {
+                $name = common_xml_safe_str($this->title);
+                if ($tag == 'author') {
+                    // XXX: Backward compatibility hack -- atom:name should contain
+                    // full name here, instead of nickname, i.e.: $name. Change
+                    // this in the next version.
+                    $xo->element(self::NAME, null, $this->poco->preferredUsername);
+                } else {
+                    $xo->element(self::TITLE, null, $name);
+                }
+            }
 
-        if (!empty($this->link)) {
-            $xo->element(
-                'link',
-                array(
-                    'rel' => 'alternate',
-                    'type' => 'text/html',
-                    'href' => $this->link
-                ),
-                null
-            );
-        }
+            if (!empty($this->summary)) {
+                $xo->element(
+                    self::SUMMARY,
+                    null,
+                    common_xml_safe_str($this->summary)
+                );
+            }
 
-        if ($this->type == ActivityObject::PERSON
-            || $this->type == ActivityObject::GROUP) {
+            if (!empty($this->content)) {
+                // XXX: assuming HTML content here
+                $xo->element(
+                    ActivityUtils::CONTENT,
+                    array('type' => 'html'),
+                    common_xml_safe_str($this->content)
+                );
+            }
 
-            foreach ($this->avatarLinks as $avatar) {
+            if (!empty($this->link)) {
                 $xo->element(
-                    'link', array(
-                        'rel'  => 'avatar',
-                        'type'         => $avatar->type,
-                        'media:width'  => $avatar->width,
-                        'media:height' => $avatar->height,
-                        'href' => $avatar->url
+                    'link',
+                    array(
+                        'rel' => 'alternate',
+                        'type' => 'text/html',
+                        'href' => $this->link
                     ),
                     null
                 );
             }
-        }
 
-        if (!empty($this->geopoint)) {
-            $xo->element(
-                'georss:point',
-                null,
-                $this->geopoint
-            );
-        }
+            if(!empty($this->owner)) {
+                $owner = $this->owner->asActivityNoun(self::AUTHOR);
+                $xo->raw($owner);
+            }
 
-        if (!empty($this->poco)) {
-            $this->poco->outputTo($xo);
-        }
+            if ($this->type == ActivityObject::PERSON
+                || $this->type == ActivityObject::GROUP) {
+
+                foreach ($this->avatarLinks as $alink) {
+                    $xo->element('link',
+                            array(
+                                'rel'          => 'avatar',
+                                'type'         => $alink->type,
+                                'media:width'  => $alink->width,
+                                'media:height' => $alink->height,
+                                'href'         => $alink->url,
+                                ),
+                            null);
+                }
+            }
+
+            if (!empty($this->geopoint)) {
+                $xo->element(
+                    'georss:point',
+                    null,
+                    $this->geopoint
+                );
+            }
 
-        foreach ($this->extra as $el) {
-            list($extraTag, $attrs, $content) = $el;
-            $xo->element($extraTag, $attrs, $content);
+            if (!empty($this->poco)) {
+                $this->poco->outputTo($xo);
+            }
+
+            // @fixme there's no way here to make a tree; elements can only contain plaintext
+            // @fixme these may collide with JSON extensions
+            foreach ($this->extra as $el) {
+                list($extraTag, $attrs, $content) = array_pad($el, 3, null);
+                $xo->element($extraTag, $attrs, $content);
+            }
+
+            Event::handle('EndActivityObjectOutputAtom', array($this, $xo));
         }
 
-               if (!empty($tag)) {
-                       $xo->elementEnd($tag);
-               }
+        if (!empty($tag)) {
+            $xo->elementEnd($tag);
+        }
 
         return;
-       }
+    }
 
     function asString($tag='activity:object')
     {
         $xs = new XMLStringer(true);
 
-               $this->outputTo($xs, $tag);
+        $this->outputTo($xs, $tag);
 
         return $xs->getString();
     }
+
+    /*
+     * Returns an array based on this Activity Object suitable for
+     * encoding as JSON.
+     *
+     * @return array $object the activity object array
+     */
+
+    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((float)$lat, (float)$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->url);
+                    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);
+    }
+
+    public function getIdentifiers() {
+        $ids = array();
+        foreach(array('id', 'link', 'url') as $id) {
+            if (isset($this->$id)) {
+                $ids[] = $this->$id;
+            }
+        }
+        return array_unique($ids);
+    }
+
+    static function canonicalType($type) {
+        return ActivityUtils::resolveUri($type, true);
+    }
+
+    static function mimeTypeToObjectType($mimeType) {
+        $ot = null;
+
+        // Default
+
+        if (empty($mimeType)) {
+            return self::FILE;
+        }
+
+        $parts = explode('/', $mimeType);
+
+        switch ($parts[0]) {
+        case 'image':
+            $ot = self::IMAGE;
+            break;
+        case 'audio':
+            $ot = self::AUDIO;
+            break;
+        case 'video':
+            $ot = self::VIDEO;
+            break;
+        default:
+            $ot = self::FILE;
+        }
+
+        return $ot;
+    }
 }