]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - plugins/OStatus/classes/Ostatus_profile.php
Merge branch '1.0.x' into testing
[quix0rs-gnu-social.git] / plugins / OStatus / classes / Ostatus_profile.php
index 3dd00de29039583633c2897377bc72493cb4c588..821ebef3d5da21c7afc7047918580de9ddd4c8f4 100644 (file)
@@ -34,6 +34,7 @@ class Ostatus_profile extends Managed_DataObject
 
     public $profile_id;
     public $group_id;
+    public $peopletag_id;
 
     public $feeduri;
     public $salmonuri;
@@ -60,6 +61,7 @@ class Ostatus_profile extends Managed_DataObject
                 'uri' => array('type' => 'varchar', 'length' => 255, 'not null' => true),
                 'profile_id' => array('type' => 'integer'),
                 'group_id' => array('type' => 'integer'),
+                'peopletag_id' => array('type' => 'integer'),
                 'feeduri' => array('type' => 'varchar', 'length' => 255),
                 'salmonuri' => array('type' => 'varchar', 'length' => 255),
                 'avatar' => array('type' => 'text'),
@@ -70,11 +72,13 @@ class Ostatus_profile extends Managed_DataObject
             'unique keys' => array(
                 'ostatus_profile_profile_id_idx' => array('profile_id'),
                 'ostatus_profile_group_id_idx' => array('group_id'),
+                'ostatus_profile_peopletag_id_idx' => array('peopletag_id'),
                 'ostatus_profile_feeduri_idx' => array('feeduri'),
             ),
             'foreign keys' => array(
                 'ostatus_profile_profile_id_fkey' => array('profile', array('profile_id' => 'id')),
                 'ostatus_profile_group_id_fkey' => array('user_group', array('group_id' => 'id')),
+                'ostatus_profile_peopletag_id_fkey' => array('profile_list', array('peopletag_id' => 'id')),
             ),
         );
     }
@@ -103,6 +107,18 @@ class Ostatus_profile extends Managed_DataObject
         return null;
     }
 
+    /**
+     * Fetch the StatusNet-side peopletag for this feed
+     * @return Profile
+     */
+    public function localPeopletag()
+    {
+        if ($this->peopletag_id) {
+            return Profile_list::staticGet('id', $this->peopletag_id);
+        }
+        return null;
+    }
+
     /**
      * Returns an ActivityObject describing this remote user or group profile.
      * Can then be used to generate Atom chunks.
@@ -113,6 +129,8 @@ class Ostatus_profile extends Managed_DataObject
     {
         if ($this->isGroup()) {
             return ActivityObject::fromGroup($this->localGroup());
+        } else if ($this->isPeopletag()) {
+            return ActivityObject::fromPeopletag($this->localPeopletag());
         } else {
             return ActivityObject::fromProfile($this->localProfile());
         }
@@ -134,6 +152,9 @@ class Ostatus_profile extends Managed_DataObject
         if ($this->isGroup()) {
             $noun = ActivityObject::fromGroup($this->localGroup());
             return $noun->asString('activity:' . $element);
+        } else if ($this->isPeopletag()) {
+            $noun = ActivityObject::fromPeopletag($this->localPeopletag());
+            return $noun->asString('activity:' . $element);
         } else {
             $noun = ActivityObject::fromProfile($this->localProfile());
             return $noun->asString('activity:' . $element);
@@ -145,16 +166,34 @@ class Ostatus_profile extends Managed_DataObject
      */
     function isGroup()
     {
-        if ($this->profile_id && !$this->group_id) {
+        if ($this->profile_id || $this->peopletag_id && !$this->group_id) {
+            return false;
+        } else if ($this->group_id && !$this->profile_id && !$this->peopletag_id) {
+            return true;
+        } else if ($this->group_id && ($this->profile_id || $this->peopletag_id)) {
+            // TRANS: Server exception. %s is a URI
+            throw new ServerException(_m("Invalid ostatus_profile state: two or more IDs set for %s", $this->uri));
+        } else {
+            // TRANS: Server exception. %s is a URI
+            throw new ServerException(_m("Invalid ostatus_profile state: all IDs empty for %s", $this->uri));
+        }
+    }
+
+    /**
+     * @return boolean true if this is a remote peopletag
+     */
+    function isPeopletag()
+    {
+        if ($this->profile_id || $this->group_id && !$this->peopletag_id) {
             return false;
-        } else if ($this->group_id && !$this->profile_id) {
+        } else if ($this->peopletag_id && !$this->profile_id && !$this->group_id) {
             return true;
-        } else if ($this->group_id && $this->profile_id) {
-            // TRANS: Server exception. %s is a URI.
-            throw new ServerException(sprintf(_m('Invalid ostatus_profile state: both group and profile IDs set for %s.'),$this->uri));
+        } else if ($this->peopletag_id && ($this->profile_id || $this->group_id)) {
+            // TRANS: Server exception. %s is a URI
+            throw new ServerException(_m("Invalid ostatus_profile state: two or more IDs set for %s", $this->uri));
         } else {
-            // TRANS: Server exception. %s is a URI.
-            throw new ServerException(sprintf(_m('Invalid ostatus_profile state: both group and profile IDs empty for %s.'),$this->uri));
+            // TRANS: Server exception. %s is a URI
+            throw new ServerException(_m("Invalid ostatus_profile state: all IDs empty for %s", $this->uri));
         }
     }
 
@@ -214,8 +253,15 @@ class Ostatus_profile extends Managed_DataObject
         if ($this->isGroup()) {
             $members = $this->localGroup()->getMembers(0, 1);
             $count = $members->N;
+        } else if ($this->isPeopletag()) {
+            $subscribers = $this->localPeopletag()->getSubscribers(0, 1);
+            $count = $subscribers->N;
         } else {
-            $count = $this->localProfile()->subscriberCount();
+            $profile = $this->localProfile();
+            $count = $profile->subscriberCount();
+            if ($profile->hasLocalTags()) {
+                $count = 1;
+            }
         }
         common_log(LOG_INFO, __METHOD__ . " SUB COUNT BEFORE: $count");
 
@@ -235,7 +281,7 @@ class Ostatus_profile extends Managed_DataObject
      * @param string  $verb   Activity::SUBSCRIBE or Activity::JOIN
      * @param Object  $object object of the action; must define asActivityNoun($tag)
      */
-    public function notify($actor, $verb, $object=null)
+    public function notify($actor, $verb, $object=null, $target=null)
     {
         if (!($actor instanceof Profile)) {
             $type = gettype($actor);
@@ -277,6 +323,9 @@ class Ostatus_profile extends Managed_DataObject
             $entry->raw($actor->asAtomAuthor());
             $entry->raw($actor->asActivityActor());
             $entry->raw($object->asActivityNoun('object'));
+            if ($target != null) {
+                $entry->raw($target->asActivityNoun('target'));
+            }
             $entry->elementEnd('entry');
 
             $xml = $entry->getString();
@@ -293,6 +342,7 @@ class Ostatus_profile extends Managed_DataObject
      * an acceptable response from the remote site.
      *
      * @param mixed $entry XML string, Notice, or Activity
+     * @param Profile $actor
      * @return boolean success
      */
     public function notifyActivity($entry, $actor)
@@ -345,6 +395,8 @@ class Ostatus_profile extends Managed_DataObject
     {
         if ($this->isGroup()) {
             return $this->localGroup()->getBestName();
+        } else if ($this->isPeopletag()) {
+            return $this->localPeopletag()->getBestName();
         } else {
             return $this->localProfile()->getBestName();
         }
@@ -419,7 +471,8 @@ class Ostatus_profile extends Managed_DataObject
     {
         $activity = new Activity($entry, $feed);
 
-        if (Event::handle('StartHandleFeedEntry', array($activity))) {
+        if (Event::handle('StartHandleFeedEntryWithProfile', array($activity, $this)) &&
+            Event::handle('StartHandleFeedEntry', array($activity))) {
 
             // @todo process all activity objects
             switch ($activity->objects[0]->type) {
@@ -441,6 +494,7 @@ class Ostatus_profile extends Managed_DataObject
             }
 
             Event::handle('EndHandleFeedEntry', array($activity));
+            Event::handle('EndHandleFeedEntryWithProfile', array($activity, $this));
         }
     }
 
@@ -453,36 +507,10 @@ class Ostatus_profile extends Managed_DataObject
      */
     public function processPost($activity, $method)
     {
-        if ($this->isGroup()) {
-            // A group feed will contain posts from multiple authors.
-            // @fixme validate these profiles in some way!
-            $oprofile = self::ensureActorProfile($activity);
-            if ($oprofile->isGroup()) {
-                // Groups can't post notices in StatusNet.
-                common_log(LOG_WARNING, "OStatus: skipping post with group listed as author: $oprofile->uri in feed from $this->uri");
-                return false;
-            }
-        } else {
-            $actor = $activity->actor;
+        $oprofile = $this->checkAuthorship($activity);
 
-            if (empty($actor)) {
-                // OK here! assume the default
-            } else if ($actor->id == $this->uri || $actor->link == $this->uri) {
-                $this->updateFromActivityObject($actor);
-            } else if ($actor->id) {
-                // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner.
-                // This isn't what we expect from mainline OStatus person feeds!
-                // Group feeds go down another path, with different validation...
-                // Most likely this is a plain ol' blog feed of some kind which
-                // doesn't match our expectations. We'll take the entry, but ignore
-                // the <author> info.
-                common_log(LOG_WARNING, "Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}");
-            } else {
-                // Plain <author> without ActivityStreams actor info.
-                // We'll just ignore this info for now and save the update under the feed's identity.
-            }
-
-            $oprofile = $this;
+        if (empty($oprofile)) {
+            return false;
         }
 
         // It's not always an ActivityObject::NOTE, but... let's just say it is.
@@ -573,6 +601,7 @@ class Ostatus_profile extends Managed_DataObject
                         'rendered' => $rendered,
                         'replies' => array(),
                         'groups' => array(),
+                        'peopletags' => array(),
                         'tags' => array(),
                         'urls' => array());
 
@@ -609,6 +638,10 @@ class Ostatus_profile extends Managed_DataObject
             }
         }
 
+        if ($this->isPeopletag()) {
+            $options['peopletags'][] = $this->localPeopletag();
+        }
+
         // Atom categories <-> hashtags
         foreach ($activity->categories as $cat) {
             if ($cat->term) {
@@ -897,54 +930,19 @@ class Ostatus_profile extends Managed_DataObject
      * @return Ostatus_profile
      * @throws Exception
      */
+
     public static function ensureAtomFeed($feedEl, $hints)
     {
-        // Try to get a profile from the feed activity:subject
-
-        $subject = ActivityUtils::child($feedEl, Activity::SUBJECT, Activity::SPEC);
+        $author = ActivityUtils::getFeedAuthor($feedEl);
 
-        if (!empty($subject)) {
-            $subjObject = new ActivityObject($subject);
-            return self::ensureActivityObjectProfile($subjObject, $hints);
+        if (empty($author)) {
+            // XXX: make some educated guesses here
+            // TRANS: Feed sub exception.
+            throw new FeedSubException(_m('Can\'t find enough profile '.
+                                          'information to make a feed.'));
         }
 
-        // Otherwise, try the feed author
-
-        $author = ActivityUtils::child($feedEl, Activity::AUTHOR, Activity::ATOM);
-
-        if (!empty($author)) {
-            $authorObject = new ActivityObject($author);
-            return self::ensureActivityObjectProfile($authorObject, $hints);
-        }
-
-        // Sheesh. Not a very nice feed! Let's try fingerpoken in the
-        // entries.
-
-        $entries = $feedEl->getElementsByTagNameNS(Activity::ATOM, 'entry');
-
-        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, $hints);
-
-            }
-
-            $author = ActivityUtils::child($entry, Activity::AUTHOR, Activity::ATOM);
-
-            if (!empty($author)) {
-                $authorObject = new ActivityObject($author);
-                return self::ensureActivityObjectProfile($authorObject, $hints);
-            }
-        }
-
-        // XXX: make some educated guesses here
-        // TRANS: Feed sub exception.
-        throw new FeedSubException(_m('Can\'t find enough profile information to make a feed.'));
+        return self::ensureActivityObjectProfile($author, $hints);
     }
 
     /**
@@ -1132,7 +1130,8 @@ class Ostatus_profile extends Managed_DataObject
                 return $url;
             }
         }
-        return common_path('plugins/OStatus/images/96px-Feed-icon.svg.png');
+
+        return Plugin::staticPath('OStatus', 'images/96px-Feed-icon.svg.png');
     }
 
     /**
@@ -1259,6 +1258,14 @@ class Ostatus_profile extends Managed_DataObject
             throw new Exception(_m('Local group can\'t be referenced as remote.'));
         }
 
+        $ptag = Profile_list::staticGet('uri', $homeuri);
+        if ($ptag) {
+            $local_user = User::staticGet('id', $ptag->tagger);
+            if (!empty($local_user)) {
+                throw new Exception("Local peopletag can't be referenced as remote.");
+            }
+        }
+
         if (array_key_exists('feedurl', $hints)) {
             $feeduri = $hints['feedurl'];
         } else {
@@ -1310,7 +1317,7 @@ class Ostatus_profile extends Managed_DataObject
             // TRANS: Server exception.
                 throw new ServerException(_m('Can\'t save local profile.'));
             }
-        } else {
+        } else if ($object->type == ActivityObject::GROUP) {
             $group = new User_group();
             $group->uri = $homeuri;
             $group->created = common_sql_now();
@@ -1321,6 +1328,16 @@ class Ostatus_profile extends Managed_DataObject
                 // TRANS: Server exception.
                 throw new ServerException(_m('Can\'t save local profile.'));
             }
+        } else if ($object->type == ActivityObject::_LIST) {
+            $ptag = new Profile_list();
+            $ptag->uri = $homeuri;
+            $ptag->created = common_sql_now();
+            self::updatePeopletag($ptag, $object, $hints);
+
+            $oprofile->peopletag_id = $ptag->insert();
+            if (!$oprofile->peopletag_id) {
+                throw new ServerException("Can't save local peopletag");
+            }
         }
 
         $ok = $oprofile->insert();
@@ -1355,12 +1372,16 @@ class Ostatus_profile extends Managed_DataObject
         if ($this->isGroup()) {
             $group = $this->localGroup();
             self::updateGroup($group, $object, $hints);
+        } else if ($this->isPeopletag()) {
+            $ptag = $this->localPeopletag();
+            self::updatePeopletag($ptag, $object, $hints);
         } else {
             $profile = $this->localProfile();
             self::updateProfile($profile, $object, $hints);
         }
+
         $avatar = self::getActivityObjectAvatar($object, $hints);
-        if ($avatar) {
+        if ($avatar && !isset($ptag)) {
             try {
                 $this->updateAvatar($avatar);
             } catch (Exception $ex) {
@@ -1373,7 +1394,17 @@ class Ostatus_profile extends Managed_DataObject
     {
         $orig = clone($profile);
 
-        $profile->nickname = self::getActivityObjectNickname($object, $hints);
+        // Existing nickname is better than nothing.
+
+        if (!array_key_exists('nickname', $hints)) {
+            $hints['nickname'] = $profile->nickname;
+        }
+
+        $nickname = self::getActivityObjectNickname($object, $hints);
+
+        if (!empty($nickname)) {
+            $profile->nickname = $nickname;
+        }
 
         if (!empty($object->title)) {
             $profile->fullname = $object->title;
@@ -1389,9 +1420,23 @@ class Ostatus_profile extends Managed_DataObject
             $profile->profileurl = $object->id;
         }
 
-        $profile->bio      = self::getActivityObjectBio($object, $hints);
-        $profile->location = self::getActivityObjectLocation($object, $hints);
-        $profile->homepage = self::getActivityObjectHomepage($object, $hints);
+        $bio = self::getActivityObjectBio($object, $hints);
+
+        if (!empty($bio)) {
+            $profile->bio = $bio;
+        }
+
+        $location = self::getActivityObjectLocation($object, $hints);
+
+        if (!empty($location)) {
+            $profile->location = $location;
+        }
+
+        $homepage = self::getActivityObjectHomepage($object, $hints);
+
+        if (!empty($homepage)) {
+            $profile->homepage = $homepage;
+        }
 
         if (!empty($object->geopoint)) {
             $location = ActivityContext::locationFromPoint($object->geopoint);
@@ -1434,6 +1479,27 @@ class Ostatus_profile extends Managed_DataObject
         }
     }
 
+    protected static function updatePeopletag($tag, $object, $hints=array()) {
+        $orig = clone($tag);
+
+        $tag->tag = $object->title;
+
+        if (!empty($object->link)) {
+            $tag->mainpage = $object->link;
+        } else if (array_key_exists('profileurl', $hints)) {
+            $tag->mainpage = $hints['profileurl'];
+        }
+
+        $tag->description = $object->summary;
+        $tagger = self::ensureActivityObjectProfile($object->owner);
+        $tag->tagger = $tagger->profile_id;
+
+        if ($tag->id) {
+            common_log(LOG_DEBUG, "Updating OStatus peopletag $tag->id from remote info $object->id: " . var_export($object, true) . var_export($hints, true));
+            $tag->update($orig);
+        }
+    }
+
     protected static function getActivityObjectHomepage($object, $hints=array())
     {
         $homepage = null;
@@ -1514,8 +1580,11 @@ class Ostatus_profile extends Managed_DataObject
         }
 
         // Try the profile url (like foo.example.com or example.com/user/foo)
-
-        $profileUrl = ($object->link) ? $object->link : $hints['profileurl'];
+        if (!empty($object->link)) {
+            $profileUrl = $object->link;
+        } else if (!empty($hints['profileurl'])) {
+            $profileUrl = $hints['profileurl'];
+        }
 
         if (!empty($profileUrl)) {
             $nickname = self::nicknameFromURI($profileUrl);
@@ -1546,9 +1615,11 @@ class Ostatus_profile extends Managed_DataObject
 
     protected static function nicknameFromURI($uri)
     {
-        preg_match('/(\w+):/', $uri, $matches);
-
-        $protocol = $matches[1];
+        if (preg_match('/(\w+):/', $uri, $matches)) {
+            $protocol = $matches[1];
+        } else {
+            return null;
+        }
 
         switch ($protocol) {
         case 'acct':
@@ -1794,12 +1865,54 @@ class Ostatus_profile extends Managed_DataObject
                 case 'mailto':
                     $rest = $match[2];
                     $oprofile = Ostatus_profile::ensureWebfinger($rest);
+                    break;
                 default:
-                    common_log("Unrecognized URI protocol for profile: $protocol ($uri)");
+                    throw new ServerException("Unrecognized URI protocol for profile: $protocol ($uri)");
                     break;
                 }
+            } else {
+                throw new ServerException("No URI protocol for profile: ($uri)");
             }
         }
+
+        return $oprofile;
+    }
+
+    function checkAuthorship($activity)
+    {
+        if ($this->isGroup() || $this->isPeopletag()) {
+            // A group or propletag feed will contain posts from multiple authors.
+            $oprofile = self::ensureActorProfile($activity);
+            if ($oprofile->isGroup() || $oprofile->isPeopletag()) {
+                // Groups can't post notices in StatusNet.
+                common_log(LOG_WARNING,
+                    "OStatus: skipping post with group listed ".
+                    "as author: $oprofile->uri in feed from $this->uri");
+                return false;
+            }
+        } else {
+            $actor = $activity->actor;
+
+            if (empty($actor)) {
+                // OK here! assume the default
+            } else if ($actor->id == $this->uri || $actor->link == $this->uri) {
+                $this->updateFromActivityObject($actor);
+            } else if ($actor->id) {
+                // We have an ActivityStreams actor with an explicit ID that doesn't match the feed owner.
+                // This isn't what we expect from mainline OStatus person feeds!
+                // Group feeds go down another path, with different validation...
+                // Most likely this is a plain ol' blog feed of some kind which
+                // doesn't match our expectations. We'll take the entry, but ignore
+                // the <author> info.
+                common_log(LOG_WARNING, "Got an actor '{$actor->title}' ({$actor->id}) on single-user feed for {$this->uri}");
+            } else {
+                // Plain <author> without ActivityStreams actor info.
+                // We'll just ignore this info for now and save the update under the feed's identity.
+            }
+
+            $oprofile = $this;
+        }
+
         return $oprofile;
     }
 }