Parse an hcard for hints, if available

This commit is contained in:
Evan Prodromou 2010-02-25 19:58:51 -05:00
parent cc0670791e
commit 894b221e8a

View File

@ -644,7 +644,6 @@ class Ostatus_profile extends Memcached_DataObject
'groups' => array(), 'groups' => array(),
'tags' => array()); 'tags' => array());
// Check for optional attributes... // Check for optional attributes...
if (!empty($activity->time)) { if (!empty($activity->time)) {
@ -1155,7 +1154,13 @@ class Ostatus_profile extends Memcached_DataObject
$orig = clone($profile); $orig = clone($profile);
$profile->nickname = self::getActivityObjectNickname($object, $hints); $profile->nickname = self::getActivityObjectNickname($object, $hints);
$profile->fullname = $object->title;
if (!empty($object->title)) {
$profile->fullname = $object->title;
} else if (array_key_exists('fullname', $hints)) {
$profile->fullname = $hints['fullname'];
}
if (!empty($object->link)) { if (!empty($object->link)) {
$profile->profileurl = $object->link; $profile->profileurl = $object->link;
} else if (array_key_exists('profileurl', $hints)) { } else if (array_key_exists('profileurl', $hints)) {
@ -1228,12 +1233,16 @@ class Ostatus_profile extends Memcached_DataObject
{ {
$location = null; $location = null;
if (!empty($object->poco)) { if (!empty($object->poco) &&
if (isset($object->poco->address->formatted)) { isset($object->poco->address->formatted)) {
$location = $object->poco->address->formatted; $location = $object->poco->address->formatted;
if (mb_strlen($location) > 255) { } else if (array_key_exists('location', $hints)) {
$location = mb_substr($note, 0, 255 - 3) . ' … '; $location = $hints['location'];
} }
if (!empty($location)) {
if (mb_strlen($location) > 255) {
$location = mb_substr($note, 0, 255 - 3) . ' … ';
} }
} }
@ -1248,13 +1257,16 @@ class Ostatus_profile extends Memcached_DataObject
if (!empty($object->poco)) { if (!empty($object->poco)) {
$note = $object->poco->note; $note = $object->poco->note;
if (!empty($note)) { } else if (array_key_exists('bio', $hints)) {
if (mb_strlen($note) > Profile::maxBio()) { $note = $hints['bio'];
// XXX: truncate ok? }
$bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' … ';
} else { if (!empty($note)) {
$bio = $note; if (Profile::bioTooLong($note)) {
} // XXX: truncate ok?
$bio = mb_substr($note, 0, Profile::maxBio() - 3) . ' … ';
} else {
$bio = $note;
} }
} }
@ -1270,10 +1282,15 @@ class Ostatus_profile extends Memcached_DataObject
return common_nicknamize($object->poco->preferredUsername); return common_nicknamize($object->poco->preferredUsername);
} }
} }
if (!empty($object->nickname)) { if (!empty($object->nickname)) {
return common_nicknamize($object->nickname); return common_nicknamize($object->nickname);
} }
if (array_key_exists('nickname', $hints)) {
return $hints['nickname'];
}
// Try the definitive ID // Try the definitive ID
$nickname = self::nicknameFromURI($object->id); $nickname = self::nicknameFromURI($object->id);
@ -1347,6 +1364,9 @@ class Ostatus_profile extends Memcached_DataObject
case Webfinger::UPDATESFROM: case Webfinger::UPDATESFROM:
$feedUrl = $link['href']; $feedUrl = $link['href'];
break; break;
case Webfinger::HCARD:
$hcardUrl = $link['href'];
break;
default: default:
common_log(LOG_NOTICE, "Don't know what to do with rel = '{$link['rel']}'"); common_log(LOG_NOTICE, "Don't know what to do with rel = '{$link['rel']}'");
break; break;
@ -1358,6 +1378,12 @@ class Ostatus_profile extends Memcached_DataObject
'feedurl' => $feedUrl, 'feedurl' => $feedUrl,
'salmon' => $salmonEndpoint); 'salmon' => $salmonEndpoint);
if (isset($hcardUrl)) {
$hcardHints = self::slurpHcard($hcardUrl);
// Note: Webfinger > hcard
$hints = array_merge($hcardHints, $hints);
}
// If we got a feed URL, try that // If we got a feed URL, try that
if (isset($feedUrl)) { if (isset($feedUrl)) {
@ -1464,4 +1490,67 @@ class Ostatus_profile extends Memcached_DataObject
return $file; return $file;
} }
protected static function slurpHcard($url)
{
set_include_path(get_include_path() . PATH_SEPARATOR . INSTALLDIR . '/plugins/OStatus/extlib/hkit/');
require_once('hkit.class.php');
$h = new hKit;
// Google Buzz hcards need to be tidied. Probably others too.
$h->tidy_mode = 'proxy'; // 'proxy', 'exec', 'php' or 'none'
// Get by URL
$hcards = $h->getByURL('hcard', $url);
if (empty($hcards)) {
return array();
}
// @fixme more intelligent guess on multi-hcard pages
$hcard = $hcards[0];
$hints = array();
$hints['profileurl'] = $url;
if (array_key_exists('nickname', $hcard)) {
$hints['nickname'] = $hcard['nickname'];
}
if (array_key_exists('fn', $hcard)) {
$hints['fullname'] = $hcard['fn'];
} else if (array_key_exists('n', $hcard)) {
$hints['fullname'] = implode(' ', $hcard['n']);
}
if (array_key_exists('photo', $hcard)) {
$hints['avatar'] = $hcard['photo'];
}
if (array_key_exists('note', $hcard)) {
$hints['bio'] = $hcard['note'];
}
if (array_key_exists('adr', $hcard)) {
if (is_string($hcard['adr'])) {
$hints['location'] = $hcard['adr'];
} else if (is_array($hcard['adr'])) {
$hints['location'] = implode(' ', $hcard['adr']);
}
}
if (array_key_exists('url', $hcard)) {
if (is_string($hcard['url'])) {
$hints['homepage'] = $hcard['url'];
} else if (is_array($hcard['adr'])) {
// HACK get the last one; that's how our hcards look
$hints['homepage'] = $hcard['url'][count($hcard['url'])-1];
}
}
return $hints;
}
} }