X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Factivityobject.php;h=2fe52eefcc5a84f883757f9aba8818446d1a657b;hb=b24d711f552854d88f2df2120672bdaf173335fc;hp=3be64c13e047b8bad29edb318c5e7bc5d19dff66;hpb=a9b2a860042063e3d1a75c1bd112381b07098763;p=quix0rs-gnu-social.git diff --git a/lib/activityobject.php b/lib/activityobject.php index 3be64c13e0..2fe52eefcc 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -32,6 +32,8 @@ if (!defined('STATUSNET')) { exit(1); } +require_once(INSTALLDIR.'/lib/activitystreamjsondocument.php'); + /** * A noun-ish thing in the activity universe * @@ -71,6 +73,8 @@ class ActivityObject 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 @@ -191,15 +195,22 @@ class ActivityObject $this->type = self::PERSON; // XXX: is this fair? } - // start with - $title = ActivityUtils::childHtmlContent($element, self::TITLE); + // Start with + + $this->title = ActivityUtils::childContent($element, PoCo::DISPLAYNAME, PoCo::NS); - if (!empty($title)) { - $this->title = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8'); + // try falling back to + + if (empty($this->title)) { + $title = ActivityUtils::childHtmlContent($element, self::TITLE); + + if (!empty($title)) { + $this->title = common_strip_html($title); + } } - // fall back to + // fall back to as a last resort if (empty($this->title)) { $this->title = $this->_childContent($element, self::NAME); @@ -247,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); @@ -266,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); @@ -427,88 +439,7 @@ class ActivityObject } } - static function fromNotice(Notice $notice) - { - $object = new ActivityObject(); - - if (Event::handle('StartActivityObjectFromNotice', array($notice, &$object))) { - - $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type; - - $object->id = $notice->uri; - $object->content = $notice->rendered; - $object->link = $notice->bestUrl(); - - Event::handle('EndActivityObjectFromNotice', array($notice, &$object)); - } - - return $object; - } - - static function fromProfile(Profile $profile) - { - $object = new ActivityObject(); - - if (Event::handle('StartActivityObjectFromProfile', array($profile, &$object))) { - $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); - - 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)); - } - - return $object; - } - - static function fromGroup($group) + static function fromGroup(User_group $group) { $object = new ActivityObject(); @@ -545,7 +476,7 @@ class ActivityObject $object->title = $ptag->tag; $object->summary = $ptag->description; $object->link = $ptag->homeUrl(); - $object->owner = Profile::staticGet('id', $ptag->tagger); + $object->owner = Profile::getKV('id', $ptag->tagger); $object->poco = PoCo::fromProfile($object->owner); Event::handle('EndActivityObjectFromPeopletag', array($ptag, &$object)); } @@ -570,19 +501,22 @@ class ActivityObject $object->date = $file->date; } - $thumbnail = $file->getThumbnail(); - - if (!empty($thumbnail)) { + try { + $thumbnail = $file->getThumbnail(); $object->thumbnail = $thumbnail; + } catch (UseFileAsThumbnailException $e) { + $object->thumbnail = null; + } catch (UnsupportedMediaException $e) { + $object->thumbnail = null; } - switch ($object->type) { - case ActivityObject::IMAGE: - $object->largerImage = $file->url; + switch (self::canonicalType($object->type)) { + case 'image': + $object->largerImage = $file->getUrl(); break; - case ActivityObject::VIDEO: - case ActivityObject::AUDIO: - $object->stream = $file->url; + case 'video': + case 'audio': + $object->stream = $file->getUrl(); break; } @@ -592,6 +526,73 @@ class ActivityObject return $object; } + 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)); + + Event::handle('EndActivityObjectFromNoticeSource', array($source, &$object)); + } + + return $object; + } + + static function fromMessage(Message $message) + { + $object = new ActivityObject(); + + if (Event::handle('StartActivityObjectFromMessage', array($message, &$object))) { + + $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; + + if ($message->url) { + $object->link = $message->url; + } else { + $object->link = common_local_url('showmessage', array('message' => $message->id)); + } + + $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)) { @@ -658,17 +659,16 @@ class ActivityObject if ($this->type == ActivityObject::PERSON || $this->type == ActivityObject::GROUP) { - foreach ($this->avatarLinks as $avatar) { - $xo->element( - 'link', array( - 'rel' => 'avatar', - 'type' => $avatar->type, - 'media:width' => $avatar->width, - 'media:height' => $avatar->height, - 'href' => $avatar->url - ), - null - ); + 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); } } @@ -687,7 +687,7 @@ class ActivityObject // @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) = $el; + list($extraTag, $attrs, $content) = array_pad($el, 3, null); $xo->element($extraTag, $attrs, $content); } @@ -728,20 +728,22 @@ class ActivityObject // content (Add rendered version of the notice?) - // displayName - - if ($this->title) { - $object['displayName'] = $this->title; - } - // downstreamDuplicates // id - $object['id'] = $this->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. @@ -784,12 +786,12 @@ class ActivityObject // We can probably use the whole schema URL here but probably the // relative simple name is easier to parse - $object['objectType'] = ActivityObject::canonicalType($this->type); + $object['objectType'] = self::canonicalType($this->type); // summary $object['summary'] = $this->summary; - // summary + // content, usually rendered HTML $object['content'] = $this->content; // published (probably don't need. Might be useful for repeats.) @@ -798,8 +800,9 @@ class ActivityObject // TODO: upstreamDuplicates - // url (XXX: need to put the right thing here...) - $object['url'] = $this->id; + if ($this->link) { + $object['url'] = $this->link; + } /* Extensions */ // @fixme these may collide with XML extensions @@ -807,7 +810,7 @@ class ActivityObject // @fixme text content from XML extensions will be lost foreach ($this->extra as $e) { - list($objectName, $props, $txt) = $e; + list($objectName, $props, $txt) = array_pad($e, 3, null); if (!empty($objectName)) { $parts = explode(":", $objectName); if (count($parts) == 2 && $parts[0] == "statusnet") { @@ -825,25 +828,27 @@ class ActivityObject list($lat, $lon) = explode(' ', $this->geopoint); - $object['location'] = array( - 'objectType' => 'place', - 'position' => sprintf("%+02.5F%+03.5F/", $lat, $lon), - 'lat' => $lat, - 'lon' => $lon - ); + 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($lat, $lon); + $loc = Location::fromLatLon((float)$lat, (float)$lon); - if ($loc) { - $name = $loc->getName(); + if ($loc) { + $name = $loc->getName(); - if ($name) { - $object['location']['displayName'] = $name; - } - $url = $loc->getURL(); + if ($name) { + $object['location']['displayName'] = $name; + } + $url = $loc->getURL(); - if ($url) { - $object['location']['url'] = $url; + if ($url) { + $object['location']['url'] = $url; + } } } } @@ -856,7 +861,7 @@ class ActivityObject if (is_string($this->thumbnail)) { $object['image'] = array('url' => $this->thumbnail); } else { - $object['image'] = array('url' => $this->thumbnail->url); + $object['image'] = array('url' => $this->thumbnail->getUrl()); if ($this->thumbnail->width) { $object['image']['width'] = $this->thumbnail->width; } @@ -866,14 +871,14 @@ class ActivityObject } } - switch ($object->type) { - case self::IMAGE: + switch (self::canonicalType($this->type)) { + case 'image': if (!empty($this->largerImage)) { $object['fullImage'] = array('url' => $this->largerImage); } break; - case self::AUDIO: - case self::VIDEO: + case 'audio': + case 'video': if (!empty($this->stream)) { $object['stream'] = array('url' => $this->stream); } @@ -885,13 +890,18 @@ class ActivityObject return array_filter($object); } - static function canonicalType($type) { - $ns = 'http://activitystrea.ms/schema/1.0/'; - if (substr($type, 0, mb_strlen($ns)) == $ns) { - return substr($type, mb_strlen($ns)); - } else { - return $type; + 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) {