throw new FeedSubNoHubException();
}
- // Ok this is going to be a terrible hack!
- // Won't be suitable for groups, empty feeds, or getting
- // info that's only available on the profile page.
+ // Try to get a profile from the feed activity:subject
+
+ $feedEl = $discover->feed->documentElement;
+
+ $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
+
+ if (!empty($subject)) {
+ $subjObject = new ActivityObject($subject);
+ return self::ensureActivityObjectProfile($subjObject, $feeduri, $salmonuri);
+ }
+
+ // Otherwise, try the feed author
+
+ $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
+
+ if (!empty($author)) {
+ $authorObject = new ActivityObject($author);
+ return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri);
+ }
+
+ // Sheesh. Not a very nice feed! Let's try fingerpoken in the
+ // entries.
+
$entries = $discover->feed->getElementsByTagNameNS(Activity::ATOM, 'entry');
- if (!$entries || $entries->length == 0) {
- throw new FeedSubException('empty feed');
+
+ if (!empty($entries) && $entries->length > 0) {
+
+ $entry = $entries->item(0);
+
+ $actor = ActivityUtils::child($entry, Activity::ACTOR, Activity::SPEC);
+
+ if (!empty($actor)) {
+ $actorObject = new ActivityObject($actor);
+ return self::ensureActivityObjectProfile($actorObject, $feeduri, $salmonuri);
+
+ }
+
+ $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
+
+ if (!empty($author)) {
+ $authorObject = new ActivityObject($author);
+ return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri);
+ }
}
- $first = new Activity($entries->item(0), $discover->feed);
- return self::ensureActorProfile($first, $feeduri, $salmonuri);
+
+ // XXX: make some educated guesses here
+
+ throw new FeedSubException("Can't find enough profile information to make a feed.");
}
/**
+ *
* Download and update given avatar image
* @param string $url
* @throws Exception in various failure cases
}
}
+ protected static function getActivityObjectAvatar($object)
+ {
+ // XXX: go poke around in the feed
+ return $object->avatar;
+ }
+
/**
* Get an appropriate avatar image source URL, if available.
*
* @param DOMElement $feed
* @return string
*/
+
protected static function getAvatar($actor, $feed)
{
$url = '';
* @param string $salmonuri if we already know the salmon return channel URI
* @return Ostatus_profile
*/
+
public static function ensureActorProfile($activity, $feeduri=null, $salmonuri=null)
{
- $profile = self::getActorProfile($activity);
+ return self::ensureActivityObjectProfile($activity->actor, $feeduri, $salmonuri);
+ }
+
+ public static function ensureActivityObjectProfile($object, $feeduri=null, $salmonuri=null)
+ {
+ $profile = self::getActivityObjectProfile($object);
if (!$profile) {
- $profile = self::createActorProfile($activity, $feeduri, $salmonuri);
+ $profile = self::createActivityObjectProfile($object, $feeduri, $salmonuri);
}
return $profile;
}
*/
protected static function getActorProfile($activity)
{
- $homeuri = self::getActorProfileURI($activity);
- return self::staticGet('uri', $homeuri);
+ return self::getActivityObjectProfile($activity->actor);
+ }
+
+ protected static function getActivityObjectProfile($object)
+ {
+ $uri = self::getActivityObjectProfileURI($object);
+ return Ostatus_profile::staticGet('homeuri', $uri);
+ }
+
+ protected static function getActorProfileURI($activity)
+ {
+ return self::getActivityObjectProfileURI($activity->actor);
}
/**
* @return string
* @throws ServerException
*/
- protected static function getActorProfileURI($activity)
+ protected static function getActivityObjectProfileURI($object)
{
$opts = array('allowed_schemes' => array('http', 'https'));
- $actor = $activity->actor;
- if ($actor->id && Validate::uri($actor->id, $opts)) {
- return $actor->id;
+ if ($object->id && Validate::uri($object->id, $opts)) {
+ return $object->id;
}
- if ($actor->link && Validate::uri($actor->link, $opts)) {
- return $actor->link;
+ if ($object->link && Validate::uri($object->link, $opts)) {
+ return $object->link;
}
throw new ServerException("No author ID URI found");
}
/**
* @fixme validate stuff somewhere
*/
+
protected static function createActorProfile($activity, $feeduri=null, $salmonuri=null)
{
$actor = $activity->actor;
- $homeuri = self::getActorProfileURI($activity);
- $nickname = self::getAuthorNick($activity);
- $avatar = self::getAvatar($actor, $activity->feed);
+
+ self::createActivityObjectProfile($actor, $feeduri, $salmonuri);
+ }
+
+ protected static function createActivityObjectProfile($object, $feeduri=null, $salmonuri=null)
+ {
+ $homeuri = self::getActivityObjectProfileURI($object);
+ $nickname = self::getActivityObjectNickname($object);
+ $avatar = self::getActivityObjectAvatar($object);
if (!$homeuri) {
common_log(LOG_DEBUG, __METHOD__ . " empty actor profile URI: " . var_export($activity, true));
$profile = new Profile();
$profile->nickname = $nickname;
- $profile->fullname = $actor->displayName;
- $profile->homepage = $actor->link; // @fixme
- $profile->profileurl = $homeuri;
+ $profile->fullname = $object->title;
+ $profile->profileurl = $object->link;
+ $profile->created = common_sql_now();
+
// @fixme bio
// @fixme tags/categories
// @fixme location?
// @todo lat/lon/location?
$ok = $profile->insert();
+
if (!$ok) {
throw new ServerException("Can't save local profile");
}
// @fixme either need to do feed discovery here
// or need to split out some of the feed stuff
// so we can leave it empty until later.
+
$oprofile = new Ostatus_profile();
- $oprofile->uri = $homeuri;
- $oprofile->feeduri = $feeduri;
- $oprofile->salmonuri = $salmonuri;
+
+ $oprofile->uri = $homeuri;
+ $oprofile->feeduri = $feeduri;
+ $oprofile->salmonuri = $salmonuri;
$oprofile->profile_id = $profile->id;
- $oprofile->created = common_sql_now();
- $oprofile->modified = common_sql_now();
+ $oprofile->created = common_sql_now();
+ $oprofile->modified = common_sql_now();
$ok = $oprofile->insert();
+
if ($ok) {
$oprofile->updateAvatar($avatar);
return $oprofile;
}
}
- /**
- * @fixme move this into Activity?
- * @param Activity $activity
- * @return string
- */
- protected static function getAuthorNick($activity)
- {
- // @fixme not technically part of the actor?
- foreach (array($activity->entry, $activity->feed) as $source) {
- $author = ActivityUtils::child($source, 'author', Activity::ATOM);
- if ($author) {
- $name = ActivityUtils::child($author, 'name', Activity::ATOM);
- if ($name) {
- return trim($name->textContent);
- }
- }
+ protected static function getActivityObjectNickname($object)
+ {
+ // XXX: check whatever PoCo calls a nickname first
+
+ $nickname = self::nicknameFromURI($object->id);
+
+ if (empty($nickname)) {
+ $nickname = common_nicknamize($object->title);
}
- return false;
+
+ return $nickname;
}
+ protected static function nicknameFromURI($uri)
+ {
+ preg_match('/(\w+):/', $uri, $matches);
+
+ $protocol = $matches[1];
+
+ switch ($protocol) {
+ case 'acct':
+ case 'mailto':
+ if (preg_match("/^$protocol:(.*)?@.*\$/", $uri, $matches)) {
+ return common_canonical_nickname($matches[1]);
+ }
+ return null;
+ case 'http':
+ return common_url_to_nickname($uri);
+ break;
+ default:
+ return null;
+ }
+ }
}