From 49fa34e23413f2901674ac0f2dc65b4bc8db6ddd Mon Sep 17 00:00:00 2001 From: Mikael Nordfeldth Date: Mon, 26 May 2014 15:05:14 +0200 Subject: [PATCH] Make Profile::fromUri use UnknownUriException --- actions/apitimelineuser.php | 18 +++++++----------- actions/atompubsubscriptionfeed.php | 7 +++---- classes/Notice.php | 7 +++---- classes/Profile.php | 16 +++++++++++++--- lib/activityimporter.php | 7 ++++--- lib/activitymover.php | 15 ++++++++++++--- plugins/Bookmark/BookmarkPlugin.php | 17 ++++++++--------- 7 files changed, 50 insertions(+), 37 deletions(-) diff --git a/actions/apitimelineuser.php b/actions/apitimelineuser.php index f551dc72f7..b509def613 100644 --- a/actions/apitimelineuser.php +++ b/actions/apitimelineuser.php @@ -423,19 +423,15 @@ class ApiTimelineUserAction extends ApiBareAuthAction if ($activity->context instanceof ActivityContext) { foreach ($activity->context->attention as $uri=>$type) { - - $profile = Profile::fromURI($uri); - - if (!empty($profile)) { - $options['replies'][] = $uri; - } else { - $group = User_group::getKV('uri', $uri); - if (!empty($group)) { - $options['groups'][] = $group->id; + try { + $profile = Profile::fromUri($uri); + if ($profile->isGroup()) { + $options['groups'][] = $profile->id; } else { - // @fixme: hook for discovery here - common_log(LOG_WARNING, sprintf('AtomPub post with unknown attention URI %s', $uri)); + $options['replies'][] = $uri; } + } catch (UnknownUriException $e) { + common_log(LOG_WARNING, sprintf('AtomPub post with unknown attention URI %s', $uri)); } } diff --git a/actions/atompubsubscriptionfeed.php b/actions/atompubsubscriptionfeed.php index 5c7184e5c1..c0f695dc42 100644 --- a/actions/atompubsubscriptionfeed.php +++ b/actions/atompubsubscriptionfeed.php @@ -252,10 +252,9 @@ class AtompubsubscriptionfeedAction extends ApiAuthAction } // XXX: OStatus discovery (maybe) - - $profile = Profile::fromURI($person->id); - - if (empty($profile)) { + try { + $profile = Profile::fromUri($person->id); + } catch (UnknownUriException $e) { // TRANS: Client exception thrown when subscribing to a non-existing profile. // TRANS: %s is the unknown profile ID. $this->clientError(sprintf(_('Unknown profile %s.'), $person->id)); diff --git a/classes/Notice.php b/classes/Notice.php index 8114ca9e67..25422aa332 100644 --- a/classes/Notice.php +++ b/classes/Notice.php @@ -1256,10 +1256,9 @@ class Notice extends Managed_DataObject $sender = Profile::getKV($this->profile_id); foreach (array_unique($uris) as $uri) { - - $profile = Profile::fromURI($uri); - - if (!$profile instanceof Profile) { + try { + $profile = Profile::fromUri($uri); + } catch (UnknownUriException $e) { common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'"); continue; } diff --git a/classes/Profile.php b/classes/Profile.php index 3f0b87e79f..961187bb93 100644 --- a/classes/Profile.php +++ b/classes/Profile.php @@ -1460,19 +1460,29 @@ class Profile extends Managed_DataObject return $feed; } - static function fromURI($uri) + /* + * Get a Profile object by URI. Will call external plugins for help + * using the event StartGetProfileFromURI. + * + * @param string $uri A unique identifier for a resource (profile/group/whatever) + */ + static function fromUri($uri) { $profile = null; if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) { - // Get a local user + // Get a local user when plugin lookup (like OStatus) fails $user = User::getKV('uri', $uri); - if (!empty($user)) { + if ($user instanceof User) { $profile = $user->getProfile(); } Event::handle('EndGetProfileFromURI', array($uri, $profile)); } + if (!$profile instanceof Profile) { + throw new UnknownUriException($uri); + } + return $profile; } diff --git a/lib/activityimporter.php b/lib/activityimporter.php index 27f89225c2..8846831b90 100644 --- a/lib/activityimporter.php +++ b/lib/activityimporter.php @@ -115,10 +115,11 @@ class ActivityImporter extends QueueHandler $other = $activity->objects[0]; - $otherProfile = Profile::fromUri($other->id); - - if (empty($otherProfile)) { + try { + $otherProfile = Profile::fromUri($other->id); // TRANS: Client exception thrown when trying to subscribe to an unknown profile. + } catch (UnknownUriException $e) { + // Let's convert it to a client exception instead of server. throw new ClientException(_('Unknown profile.')); } diff --git a/lib/activitymover.php b/lib/activitymover.php index 0493750d08..66ae7e5074 100644 --- a/lib/activitymover.php +++ b/lib/activitymover.php @@ -56,7 +56,14 @@ class ActivityMover extends QueueHandler list ($act, $sink, $userURI, $remoteURI) = $data; $user = User::getKV('uri', $userURI); - $remote = Profile::fromURI($remoteURI); + try { + $remote = Profile::fromUri($remoteURI); + } catch (UnknownUriException $e) { + // Don't retry. It's hard to tell whether it's because of + // lookup failures or because the URI is permanently gone. + // If we knew it was temporary, we'd return false here. + return true; + } try { $this->moveActivity($act, $sink, $user, $remote); @@ -126,9 +133,11 @@ class ActivityMover extends QueueHandler "Moving subscription to {$act->objects[0]->id} by ". "{$act->actor->id} to {$remote->nickname}."); $sink->postActivity($act); - $other = Profile::fromURI($act->objects[0]->id); - if (!empty($other)) { + try { + $other = Profile::fromUri($act->objects[0]->id); Subscription::cancel($user->getProfile(), $other); + } catch (UnknownUriException $e) { + // Can't cancel subscription if we don't know who to alert } } else { $otherUser = User::getKV('uri', $act->actor->id); diff --git a/plugins/Bookmark/BookmarkPlugin.php b/plugins/Bookmark/BookmarkPlugin.php index e6a2d45290..381dc4dfc9 100644 --- a/plugins/Bookmark/BookmarkPlugin.php +++ b/plugins/Bookmark/BookmarkPlugin.php @@ -387,16 +387,15 @@ class BookmarkPlugin extends MicroAppPlugin $options['replies'] = array(); // TODO: context->attention foreach ($activity->context->attention as $attnUrl=>$type) { - $other = Profile::fromURI($attnUrl); - if ($other instanceof Profile) { - $options['replies'][] = $attnUrl; - } else { - // Maybe we can get rid of this since every User_group got a Profile? - // TODO: Make sure the above replies get sorted properly for groups (or handled afterwards) - $group = User_group::getKV('uri', $attnUrl); - if ($group instanceof User_group) { - $options['groups'][] = $attnUrl; + try { + $other = Profile::fromUri($attnUrl); + if ($other->isGroup()) { + $options['groups'][] = $other->id; + } else { + $options['replies'][] = $attnUrl; } + } catch (UnknownUriException $e) { + // We simply don't know this URI, despite lookup attempts. } }