]> git.mxchange.org Git - quix0rs-gnu-social.git/commitdiff
Make Profile::fromUri use UnknownUriException
authorMikael Nordfeldth <mmn@hethane.se>
Mon, 26 May 2014 13:05:14 +0000 (15:05 +0200)
committerMikael Nordfeldth <mmn@hethane.se>
Mon, 26 May 2014 13:05:14 +0000 (15:05 +0200)
actions/apitimelineuser.php
actions/atompubsubscriptionfeed.php
classes/Notice.php
classes/Profile.php
lib/activityimporter.php
lib/activitymover.php
plugins/Bookmark/BookmarkPlugin.php

index f551dc72f7fad48bd7b7ccf584cf4fb8779c920a..b509def613d29bbb26a9ace44d76375904dc7d07 100644 (file)
@@ -423,19 +423,15 @@ class ApiTimelineUserAction extends ApiBareAuthAction
         if ($activity->context instanceof ActivityContext) {
 
             foreach ($activity->context->attention as $uri=>$type) {
-
-                $profile = Profile::fromURI($uri);
-
-                if (!empty($profile)) {
-                    $options['replies'][] = $uri;
-                } else {
-                    $group = User_group::getKV('uri', $uri);
-                    if (!empty($group)) {
-                        $options['groups'][] = $group->id;
+                try {
+                    $profile = Profile::fromUri($uri);
+                    if ($profile->isGroup()) {
+                        $options['groups'][] = $profile->id;
                     } else {
-                        // @fixme: hook for discovery here
-                        common_log(LOG_WARNING, sprintf('AtomPub post with unknown attention URI %s', $uri));
+                        $options['replies'][] = $uri;
                     }
+                } catch (UnknownUriException $e) {
+                    common_log(LOG_WARNING, sprintf('AtomPub post with unknown attention URI %s', $uri));
                 }
             }
 
index 5c7184e5c1da365bdf9bb1b24aa1403f65a0f000..c0f695dc42cc7603db9c89ea21cb3e9721723254 100644 (file)
@@ -252,10 +252,9 @@ class AtompubsubscriptionfeedAction extends ApiAuthAction
             }
 
             // XXX: OStatus discovery (maybe)
-
-            $profile = Profile::fromURI($person->id);
-
-            if (empty($profile)) {
+            try {
+                $profile = Profile::fromUri($person->id);
+            } catch (UnknownUriException $e) {
                 // TRANS: Client exception thrown when subscribing to a non-existing profile.
                 // TRANS: %s is the unknown profile ID.
                 $this->clientError(sprintf(_('Unknown profile %s.'), $person->id));
index 8114ca9e670ebdb19facbc54c45695d374a26115..25422aa332d5b8ef6b421075f8e505aeaeff3899 100644 (file)
@@ -1256,10 +1256,9 @@ class Notice extends Managed_DataObject
         $sender = Profile::getKV($this->profile_id);
 
         foreach (array_unique($uris) as $uri) {
-
-            $profile = Profile::fromURI($uri);
-
-            if (!$profile instanceof Profile) {
+            try {
+                $profile = Profile::fromUri($uri);
+            } catch (UnknownUriException $e) {
                 common_log(LOG_WARNING, "Unable to determine profile for URI '$uri'");
                 continue;
             }
index 3f0b87e79fc550ebb68c6c31b3ba0608e5278930..961187bb93cb436135eac7159798f74914e5ecde 100644 (file)
@@ -1460,19 +1460,29 @@ class Profile extends Managed_DataObject
         return $feed;
     }
 
-    static function fromURI($uri)
+    /*
+     * Get a Profile object by URI. Will call external plugins for help
+     * using the event StartGetProfileFromURI.
+     *
+     * @param string $uri A unique identifier for a resource (profile/group/whatever)
+     */
+    static function fromUri($uri)
     {
         $profile = null;
 
         if (Event::handle('StartGetProfileFromURI', array($uri, &$profile))) {
-            // Get a local user
+            // Get a local user when plugin lookup (like OStatus) fails
             $user = User::getKV('uri', $uri);
-            if (!empty($user)) {
+            if ($user instanceof User) {
                 $profile = $user->getProfile();
             }
             Event::handle('EndGetProfileFromURI', array($uri, $profile));
         }
 
+        if (!$profile instanceof Profile) {
+            throw new UnknownUriException($uri);
+        }
+
         return $profile;
     }
 
index 27f89225c2ba93ac49d30f1bace8d6ad61572a5c..8846831b9062205532a0cdd65a7a1ae00ff1d73a 100644 (file)
@@ -115,10 +115,11 @@ class ActivityImporter extends QueueHandler
 
             $other = $activity->objects[0];
 
-            $otherProfile = Profile::fromUri($other->id);
-
-            if (empty($otherProfile)) {
+            try {
+                $otherProfile = Profile::fromUri($other->id);
                 // TRANS: Client exception thrown when trying to subscribe to an unknown profile.
+            } catch (UnknownUriException $e) {
+                // Let's convert it to a client exception instead of server.
                 throw new ClientException(_('Unknown profile.'));
             }
 
index 0493750d0877e5b8f8108011027e13b16eda9dc9..66ae7e5074e2045ea4c706d39501e75c3ce386a0 100644 (file)
@@ -56,7 +56,14 @@ class ActivityMover extends QueueHandler
         list ($act, $sink, $userURI, $remoteURI) = $data;
 
         $user   = User::getKV('uri', $userURI);
-        $remote = Profile::fromURI($remoteURI);
+        try {
+            $remote = Profile::fromUri($remoteURI);
+        } catch (UnknownUriException $e) {
+            // Don't retry. It's hard to tell whether it's because of
+            // lookup failures or because the URI is permanently gone.
+            // If we knew it was temporary, we'd return false here.
+            return true;
+        }
 
         try {
             $this->moveActivity($act, $sink, $user, $remote);
@@ -126,9 +133,11 @@ class ActivityMover extends QueueHandler
                            "Moving subscription to {$act->objects[0]->id} by ".
                            "{$act->actor->id} to {$remote->nickname}.");
                 $sink->postActivity($act);
-                $other = Profile::fromURI($act->objects[0]->id);
-                if (!empty($other)) {
+                try {
+                    $other = Profile::fromUri($act->objects[0]->id);
                     Subscription::cancel($user->getProfile(), $other);
+                } catch (UnknownUriException $e) {
+                    // Can't cancel subscription if we don't know who to alert
                 }
             } else {
                 $otherUser = User::getKV('uri', $act->actor->id);
index e6a2d45290bb99bf6ceb9ae8b6fec2c5b504a2e6..381dc4dfc936ba715b6e99328b4274c43b199442 100644 (file)
@@ -387,16 +387,15 @@ class BookmarkPlugin extends MicroAppPlugin
         $options['replies'] = array();  // TODO: context->attention
 
         foreach ($activity->context->attention as $attnUrl=>$type) {
-            $other = Profile::fromURI($attnUrl);
-            if ($other instanceof Profile) {
-                $options['replies'][] = $attnUrl;
-            } else {
-                // Maybe we can get rid of this since every User_group got a Profile?
-                // TODO: Make sure the above replies get sorted properly for groups (or handled afterwards)
-                $group = User_group::getKV('uri', $attnUrl);
-                if ($group instanceof User_group) {
-                    $options['groups'][] = $attnUrl;
+            try {
+                $other = Profile::fromUri($attnUrl);
+                if ($other->isGroup()) {
+                    $options['groups'][] = $other->id;
+                } else {
+                    $options['replies'][] = $attnUrl;
                 }
+            } catch (UnknownUriException $e) {
+                // We simply don't know this URI, despite lookup attempts.
             }
         }