From: Mikael Nordfeldth Date: Sat, 28 Jun 2014 19:16:45 +0000 (+0200) Subject: getIdentifiers, resolveUri, findLocalObject Activity algorithms X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=96babc59f581e5ac1bcb4bb71cc06042e4ec1719;p=quix0rs-gnu-social.git getIdentifiers, resolveUri, findLocalObject Activity algorithms Also modified related classes to support this feature. --- diff --git a/classes/Notice.php b/classes/Notice.php index 4c831c7cc7..3f138e1cfe 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -213,6 +213,28 @@ class Notice extends Managed_DataObject return $this->uri; } + /* + * Get a Notice object by URI. Will call external plugins for help + * using the event StartGetNoticeFromURI. + * + * @param string $uri A unique identifier for a resource (notice in this case) + */ + static function fromUri($uri) + { + $notice = null; + + if (Event::handle('StartGetNoticeFromUri', array($uri, &$notice))) { + $notice = Notice::getKV('uri', $uri); + Event::handle('EndGetNoticeFromUri', array($uri, $notice)); + } + + if (!$notice instanceof Notice) { + throw new UnknownUriException($uri); + } + + return $notice; + } + /* * @param $root boolean If true, link to just the conversation root. * diff --git a/lib/activityobject.php b/lib/activityobject.php index 505a16e755..2b6105e0d4 100644 --- a/lib/activityobject.php +++ b/lib/activityobject.php @@ -440,7 +440,7 @@ class ActivityObject $object->type = (empty($notice->object_type)) ? ActivityObject::NOTE : $notice->object_type; $object->id = $notice->uri; - $object->title = 'New ' . ActivityObject::canonicalType($object->type) . ' by '; + $object->title = 'New ' . self::canonicalType($object->type) . ' by '; try { $object->title .= $notice->getProfile()->getAcctUri(); } catch (ProfileNoAcctUriException $e) { @@ -584,7 +584,7 @@ class ActivityObject $object->thumbnail = null; } - switch (ActivityObject::canonicalType($object->type)) { + switch (self::canonicalType($object->type)) { case 'image': $object->largerImage = $file->url; break; @@ -860,7 +860,7 @@ 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; @@ -945,7 +945,7 @@ class ActivityObject } } - switch (ActivityObject::canonicalType($this->type)) { + switch (self::canonicalType($this->type)) { case 'image': if (!empty($this->largerImage)) { $object['fullImage'] = array('url' => $this->largerImage); @@ -964,13 +964,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) { diff --git a/lib/activityutils.php b/lib/activityutils.php index 8a7039d909..07d8a86141 100644 --- a/lib/activityutils.php +++ b/lib/activityutils.php @@ -349,12 +349,57 @@ class ActivityUtils static function compareTypes($type, $objects) // this does verbs too! { - $type = ActivityObject::canonicalType($type); + $type = self::resolveUri($type); foreach ((array)$objects as $object) { - if ($type === ActivityObject::canonicalType($object)) { + if ($type === self::resolveUri($object)) { return true; } } return false; } + + static function resolveUri($uri, $make_relative=false) + { + if (empty($uri)) { + throw new ServerException('No URI to resolve in ActivityUtils::resolveUri'); + } + + if (!$make_relative && parse_url($uri, PHP_URL_SCHEME) == '') { // relative -> absolute + $uri = Activity::SCHEMA . $uri; + } elseif ($make_relative) { // absolute -> relative + $uri = basename($uri); //preg_replace('/^http:\/\/activitystrea\.ms\/schema\/1\.0\//', '', $uri); + } // absolute schemas pass through unharmed + + return $uri; + } + + static function findLocalObject(array $uris, $type=ActivityObject::NOTE) { + $object = null; + // TODO: Extend this in plugins etc. + if (Event::handle('StartFindLocalActivityObject', array($uris, $type, &$object))) { + switch (self::resolveUri($type)) { + case ActivityObject::PERSON: + // GROUP will also be here in due time... + $object = new Profile(); + break; + default: + $object = new Notice(); + } + } + foreach (array_unique($uris) as $uri) { + try { + // the exception thrown will cancel before reaching $object + $object = call_user_func(array($object, 'fromUri'), $uri); + break; + } catch (Exception $e) { + common_debug('Could not find local activity object from uri: '.$uri); + } + } + if (!empty($object)) { + Event::handle('EndFindLocalActivityObject', array($object->getUri(), $type, $object)); + } else { + throw new Exception('Could not find any activityobject stored locally with given URI'); + } + return $object; + } }