Handle attempt to subscribe a local user via their remote webfinger reference more gracefully.

The ensure* family of functions will now return an OStatusShadowException in this case, which gives us a pleasant error message instead of a giant exception backtrace when you do 'sub somebody@this.local.server'.

Can be extended later to allow actually using the local profile, since we could figure it out.
This commit is contained in:
Brion Vibber 2010-04-06 23:32:04 +00:00
parent cda03ff328
commit d352e5f5c4

View File

@ -726,7 +726,8 @@ class Ostatus_profile extends Memcached_DataObject
* *
* @param string $profile_url * @param string $profile_url
* @return Ostatus_profile * @return Ostatus_profile
* @throws Exception * @throws Exception on various error conditions
* @throws OStatusShadowException if this reference would obscure a local user/group
*/ */
public static function ensureProfileURL($profile_url, $hints=array()) public static function ensureProfileURL($profile_url, $hints=array())
@ -813,7 +814,7 @@ class Ostatus_profile extends Memcached_DataObject
* remote profiles. * remote profiles.
* *
* @return mixed Ostatus_profile or null * @return mixed Ostatus_profile or null
* @throws Exception for local profiles * @throws OStatusShadowException for local profiles
*/ */
static function getFromProfileURL($profile_url) static function getFromProfileURL($profile_url)
{ {
@ -836,7 +837,7 @@ class Ostatus_profile extends Memcached_DataObject
$user = User::staticGet('id', $profile->id); $user = User::staticGet('id', $profile->id);
if (!empty($user)) { if (!empty($user)) {
throw new Exception("'$profile_url' is the profile for local user '{$user->nickname}'."); throw new OStatusShadowException($profile, "'$profile_url' is the profile for local user '{$user->nickname}'.");
} }
// Continue discovery; it's a remote profile // Continue discovery; it's a remote profile
@ -1538,6 +1539,7 @@ class Ostatus_profile extends Memcached_DataObject
* @param string $addr webfinger address * @param string $addr webfinger address
* @return Ostatus_profile * @return Ostatus_profile
* @throws Exception on error conditions * @throws Exception on error conditions
* @throws OStatusShadowException if this reference would obscure a local user/group
*/ */
public static function ensureWebfinger($addr) public static function ensureWebfinger($addr)
{ {
@ -1616,9 +1618,18 @@ class Ostatus_profile extends Memcached_DataObject
$oprofile = self::ensureProfileURL($hints['profileurl'], $hints); $oprofile = self::ensureProfileURL($hints['profileurl'], $hints);
self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri); self::cacheSet(sprintf('ostatus_profile:webfinger:%s', $addr), $oprofile->uri);
return $oprofile; return $oprofile;
} catch (OStatusShadowException $e) {
// We've ended up with a remote reference to a local user or group.
// @fixme ideally we should be able to say who it was so we can
// go back and refer to it the regular way
throw $e;
} catch (Exception $e) { } catch (Exception $e) {
common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage()); common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
// keep looking // keep looking
//
// @fixme this means an error discovering from profile page
// may give us a corrupt entry using the webfinger URI, which
// will obscure the correct page-keyed profile later on.
} }
} }
@ -1720,3 +1731,24 @@ class Ostatus_profile extends Memcached_DataObject
return $file; return $file;
} }
} }
/**
* Exception indicating we've got a remote reference to a local user,
* not a remote user!
*
* If we can ue a local profile after all, it's available as $e->profile.
*/
class OStatusShadowException extends Exception
{
public $profile;
/**
* @param Profile $profile
* @param string $message
*/
function __construct($profile, $message) {
$this->profile = $profile;
parent::__construct($message);
}
}