Make Profile::fromUri use UnknownUriException

This commit is contained in:
Mikael Nordfeldth 2014-05-26 15:05:14 +02:00
parent 89e817e5b0
commit 49fa34e234
7 changed files with 50 additions and 37 deletions

View File

@ -423,19 +423,15 @@ class ApiTimelineUserAction extends ApiBareAuthAction
if ($activity->context instanceof ActivityContext) { if ($activity->context instanceof ActivityContext) {
foreach ($activity->context->attention as $uri=>$type) { foreach ($activity->context->attention as $uri=>$type) {
try {
$profile = Profile::fromURI($uri); $profile = Profile::fromUri($uri);
if ($profile->isGroup()) {
if (!empty($profile)) { $options['groups'][] = $profile->id;
$options['replies'][] = $uri;
} else {
$group = User_group::getKV('uri', $uri);
if (!empty($group)) {
$options['groups'][] = $group->id;
} else { } else {
// @fixme: hook for discovery here $options['replies'][] = $uri;
common_log(LOG_WARNING, sprintf('AtomPub post with unknown attention URI %s', $uri));
} }
} catch (UnknownUriException $e) {
common_log(LOG_WARNING, sprintf('AtomPub post with unknown attention URI %s', $uri));
} }
} }

View File

@ -252,10 +252,9 @@ class AtompubsubscriptionfeedAction extends ApiAuthAction
} }
// XXX: OStatus discovery (maybe) // XXX: OStatus discovery (maybe)
try {
$profile = Profile::fromURI($person->id); $profile = Profile::fromUri($person->id);
} catch (UnknownUriException $e) {
if (empty($profile)) {
// TRANS: Client exception thrown when subscribing to a non-existing profile. // TRANS: Client exception thrown when subscribing to a non-existing profile.
// TRANS: %s is the unknown profile ID. // TRANS: %s is the unknown profile ID.
$this->clientError(sprintf(_('Unknown profile %s.'), $person->id)); $this->clientError(sprintf(_('Unknown profile %s.'), $person->id));

View File

@ -1256,10 +1256,9 @@ class Notice extends Managed_DataObject
$sender = Profile::getKV($this->profile_id); $sender = Profile::getKV($this->profile_id);
foreach (array_unique($uris) as $uri) { foreach (array_unique($uris) as $uri) {
try {
$profile = Profile::fromURI($uri); $profile = Profile::fromUri($uri);
} catch (UnknownUriException $e) {
if (!$profile instanceof Profile) {
common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'"); common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'");
continue; continue;
} }

View File

@ -1460,19 +1460,29 @@ class Profile extends Managed_DataObject
return $feed; 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; $profile = null;
if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) { 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); $user = User::getKV('uri', $uri);
if (!empty($user)) { if ($user instanceof User) {
$profile = $user->getProfile(); $profile = $user->getProfile();
} }
Event::handle('EndGetProfileFromURI', array($uri, $profile)); Event::handle('EndGetProfileFromURI', array($uri, $profile));
} }
if (!$profile instanceof Profile) {
throw new UnknownUriException($uri);
}
return $profile; return $profile;
} }

View File

@ -115,10 +115,11 @@ class ActivityImporter extends QueueHandler
$other = $activity->objects[0]; $other = $activity->objects[0];
$otherProfile = Profile::fromUri($other->id); try {
$otherProfile = Profile::fromUri($other->id);
if (empty($otherProfile)) {
// TRANS: Client exception thrown when trying to subscribe to an unknown profile. // 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.')); throw new ClientException(_('Unknown profile.'));
} }

View File

@ -56,7 +56,14 @@ class ActivityMover extends QueueHandler
list ($act, $sink, $userURI, $remoteURI) = $data; list ($act, $sink, $userURI, $remoteURI) = $data;
$user = User::getKV('uri', $userURI); $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 { try {
$this->moveActivity($act, $sink, $user, $remote); $this->moveActivity($act, $sink, $user, $remote);
@ -126,9 +133,11 @@ class ActivityMover extends QueueHandler
"Moving subscription to {$act->objects[0]->id} by ". "Moving subscription to {$act->objects[0]->id} by ".
"{$act->actor->id} to {$remote->nickname}."); "{$act->actor->id} to {$remote->nickname}.");
$sink->postActivity($act); $sink->postActivity($act);
$other = Profile::fromURI($act->objects[0]->id); try {
if (!empty($other)) { $other = Profile::fromUri($act->objects[0]->id);
Subscription::cancel($user->getProfile(), $other); Subscription::cancel($user->getProfile(), $other);
} catch (UnknownUriException $e) {
// Can't cancel subscription if we don't know who to alert
} }
} else { } else {
$otherUser = User::getKV('uri', $act->actor->id); $otherUser = User::getKV('uri', $act->actor->id);

View File

@ -387,16 +387,15 @@ class BookmarkPlugin extends MicroAppPlugin
$options['replies'] = array(); // TODO: context->attention $options['replies'] = array(); // TODO: context->attention
foreach ($activity->context->attention as $attnUrl=>$type) { foreach ($activity->context->attention as $attnUrl=>$type) {
$other = Profile::fromURI($attnUrl); try {
if ($other instanceof Profile) { $other = Profile::fromUri($attnUrl);
$options['replies'][] = $attnUrl; if ($other->isGroup()) {
} else { $options['groups'][] = $other->id;
// Maybe we can get rid of this since every User_group got a Profile? } else {
// TODO: Make sure the above replies get sorted properly for groups (or handled afterwards) $options['replies'][] = $attnUrl;
$group = User_group::getKV('uri', $attnUrl);
if ($group instanceof User_group) {
$options['groups'][] = $attnUrl;
} }
} catch (UnknownUriException $e) {
// We simply don't know this URI, despite lookup attempts.
} }
} }