]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/Ostatus_profile.php
wiring in magicsig
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
index 3b79f32c624ddcbb3384c8766c85eae52b1a93bc..0e12f8fc6e14005b50648b22311c832f320e91f6 100644 (file)
@@ -361,7 +361,7 @@ class Ostatus_profile extends Memcached_DataObject
     {
         if ($this->salmonuri) {
 
-            $xml = $activity->asString();
+            $xml = $activity->asString(true);
 
             $salmon = new Salmon(); // ?
 
@@ -508,13 +508,15 @@ class Ostatus_profile extends Memcached_DataObject
             }
         }
 
-        // @fixme save detailed ostatus source info
         // @fixme ensure that groups get handled correctly
 
         $saved = Notice::saveNew($oprofile->localProfile()->id,
                                  $content,
                                  'ostatus',
                                  $params);
+
+        // Record which feed this came through...
+        Ostatus_source::saveNew($saved, $this, 'push');
     }
 
     /**
@@ -522,7 +524,7 @@ class Ostatus_profile extends Memcached_DataObject
      * @return Ostatus_profile
      * @throws FeedSubException
      */
-    public static function ensureProfile($profile_uri)
+    public static function ensureProfile($profile_uri, $hints=array())
     {
         // Get the canonical feed URI and check it
         $discover = new FeedDiscovery();
@@ -545,7 +547,7 @@ class Ostatus_profile extends Memcached_DataObject
 
         if (!empty($subject)) {
             $subjObject = new ActivityObject($subject);
-            return self::ensureActivityObjectProfile($subjObject, $feeduri, $salmonuri);
+            return self::ensureActivityObjectProfile($subjObject, $feeduri, $salmonuri, $hints);
         }
 
         // Otherwise, try the feed author
@@ -554,7 +556,7 @@ class Ostatus_profile extends Memcached_DataObject
 
         if (!empty($author)) {
             $authorObject = new ActivityObject($author);
-            return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri);
+            return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri, $hints);
         }
 
         // Sheesh. Not a very nice feed! Let's try fingerpoken in the
@@ -570,7 +572,7 @@ class Ostatus_profile extends Memcached_DataObject
 
             if (!empty($actor)) {
                 $actorObject = new ActivityObject($actor);
-                return self::ensureActivityObjectProfile($actorObject, $feeduri, $salmonuri);
+                return self::ensureActivityObjectProfile($actorObject, $feeduri, $salmonuri, $hints);
 
             }
 
@@ -578,7 +580,7 @@ class Ostatus_profile extends Memcached_DataObject
 
             if (!empty($author)) {
                 $authorObject = new ActivityObject($author);
-                return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri);
+                return self::ensureActivityObjectProfile($authorObject, $feeduri, $salmonuri, $hints);
             }
         }
 
@@ -688,11 +690,11 @@ class Ostatus_profile extends Memcached_DataObject
         return self::ensureActivityObjectProfile($activity->actor, $feeduri, $salmonuri);
     }
 
-    public static function ensureActivityObjectProfile($object, $feeduri=null, $salmonuri=null)
+    public static function ensureActivityObjectProfile($object, $feeduri=null, $salmonuri=null, $hints=array())
     {
         $profile = self::getActivityObjectProfile($object);
         if (!$profile) {
-            $profile = self::createActivityObjectProfile($object, $feeduri, $salmonuri);
+            $profile = self::createActivityObjectProfile($object, $feeduri, $salmonuri, $hints);
         }
         return $profile;
     }
@@ -745,10 +747,10 @@ class Ostatus_profile extends Memcached_DataObject
         self::createActivityObjectProfile($actor, $feeduri, $salmonuri);
     }
 
-    protected static function createActivityObjectProfile($object, $feeduri=null, $salmonuri=null)
+    protected static function createActivityObjectProfile($object, $feeduri=null, $salmonuri=null, $hints=array())
     {
         $homeuri  = $object->id;
-        $nickname = self::getActivityObjectNickname($object);
+        $nickname = self::getActivityObjectNickname($object, $hints);
         $avatar   = self::getActivityObjectAvatar($object);
 
         if (!$homeuri) {
@@ -756,6 +758,18 @@ class Ostatus_profile extends Memcached_DataObject
             throw new ServerException("No profile URI");
         }
 
+        if (empty($feeduri)) {
+            if (array_key_exists('feedurl', $hints)) {
+                $feeduri = $hints['feedurl'];
+            }
+        }
+
+        if (empty($salmonuri)) {
+            if (array_key_exists('salmon', $hints)) {
+                $salmonuri = $hints['salmon'];
+            }
+        }
+
         if (!$feeduri || !$salmonuri) {
             // Get the canonical feed URI and check it
             $discover = new FeedDiscovery();
@@ -773,7 +787,11 @@ class Ostatus_profile extends Memcached_DataObject
         $profile = new Profile();
         $profile->nickname   = $nickname;
         $profile->fullname   = $object->title;
-        $profile->profileurl = $object->link;
+        if (!empty($object->link)) {
+            $profile->profileurl = $object->link;
+        } else if (array_key_exists('profileurl', $hints)) {
+            $profile->profileurl = $hints['profileurl'];
+        }
         $profile->created    = common_sql_now();
 
         // @fixme bio
@@ -812,12 +830,26 @@ class Ostatus_profile extends Memcached_DataObject
         }
     }
 
-    protected static function getActivityObjectNickname($object)
+    protected static function getActivityObjectNickname($object, $hints=array())
     {
-        // XXX: check whatever PoCo calls a nickname first
+        if (!empty($object->nickname)) {
+            return common_nicknamize($object->nickname);
+        }
+
+        // Try the definitive ID
 
         $nickname = self::nicknameFromURI($object->id);
 
+        // Try a Webfinger if one was passed (way) down
+
+        if (empty($nickname)) {
+            if (array_key_exists('webfinger', $hints)) {
+                $nickname = self::nicknameFromURI($hints['webfinger']);
+            }
+        }
+
+        // Try the name
+
         if (empty($nickname)) {
             $nickname = common_nicknamize($object->title);
         }
@@ -845,4 +877,120 @@ class Ostatus_profile extends Memcached_DataObject
             return null;
         }
     }
+
+    public static function ensureWebfinger($addr)
+    {
+        // First, look it up
+
+        $oprofile = Ostatus_profile::staticGet('uri', 'acct:'.$addr);
+
+        if (!empty($oprofile)) {
+            return $oprofile;
+        }
+
+        // Now, try some discovery
+
+        $wf = new Webfinger();
+
+        $result = $wf->lookup($addr);
+
+        if (!$result) {
+            return null;
+        }
+
+        foreach ($result->links as $link) {
+            switch ($link['rel']) {
+            case Webfinger::PROFILEPAGE:
+                $profileUrl = $link['href'];
+                break;
+            case 'salmon':
+                $salmonEndpoint = $link['href'];
+                break;
+            case Webfinger::UPDATESFROM:
+                $feedUrl = $link['href'];
+                break;
+            default:
+                common_log(LOG_NOTICE, "Don't know what to do with rel = '{$link['rel']}'");
+                break;
+            }
+        }
+
+        $hints = array('webfinger' => $addr,
+                       'profileurl' => $profileUrl,
+                       'feedurl' => $feedUrl,
+                       'salmon' => $salmonEndpoint);
+
+        // If we got a feed URL, try that
+
+        if (isset($feedUrl)) {
+            try {
+                $oprofile = self::ensureProfile($feedUrl, $hints);
+                return $oprofile;
+            } catch (Exception $e) {
+                common_log(LOG_WARNING, "Failed creating profile from feed URL '$feedUrl': " . $e->getMessage());
+                // keep looking
+            }
+        }
+
+        // If we got a profile page, try that!
+
+        if (isset($profileUrl)) {
+            try {
+                $oprofile = self::ensureProfile($profileUrl, $hints);
+                return $oprofile;
+            } catch (Exception $e) {
+                common_log(LOG_WARNING, "Failed creating profile from profile URL '$profileUrl': " . $e->getMessage());
+                // keep looking
+            }
+        }
+
+        // XXX: try hcard
+        // XXX: try FOAF
+
+        if (isset($salmonEndpoint)) {
+
+            // An account URL, a salmon endpoint, and a dream? Not much to go
+            // on, but let's give it a try
+
+            $uri = 'acct:'.$addr;
+
+            $profile = new Profile();
+
+            $profile->nickname = self::nicknameFromUri($uri);
+            $profile->created  = common_sql_now();
+
+            if (isset($profileUrl)) {
+                $profile->profileurl = $profileUrl;
+            }
+
+            $profile_id = $profile->insert();
+
+            if (!$profile_id) {
+                common_log_db_error($profile, 'INSERT', __FILE__);
+                throw new Exception("Couldn't save profile for '$addr'");
+            }
+
+            $oprofile = new Ostatus_profile();
+
+            $oprofile->uri        = $uri;
+            $oprofile->salmonuri  = $salmonEndpoint;
+            $oprofile->profile_id = $profile_id;
+            $oprofile->created    = common_sql_now();
+
+            if (isset($feedUrl)) {
+                $profile->feeduri = $feedUrl;
+            }
+
+            $result = $oprofile->insert();
+
+            if (!$result) {
+                common_log_db_error($oprofile, 'INSERT', __FILE__);
+                throw new Exception("Couldn't save ostatus_profile for '$addr'");
+            }
+
+            return $oprofile;
+        }
+
+        return null;
+    }
 }