Commit 49fa34e2 authored by mmn's avatar mmn

Make Profile::fromUri use UnknownUriException

parent 89e817e5
......@@ -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));
}
}
......
......@@ -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));
......
......@@ -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;
}
......
......@@ -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;
}
......
......@@ -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.'));
}
......
......@@ -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);
......
......@@ -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.
}
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment