]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/apiaction.php
Add 'profile_image_url_https' to enhance compatiblity with Twitter clients since...
[quix0rs-gnu-social.git] / lib / apiaction.php
index 781d106cf6bf4a68913b54dcd37dd82d891a66bf..f8ffbc84a5b4bc449abe779c160bb2146aee1917 100644 (file)
@@ -119,7 +119,6 @@ class ApiAction extends Action
     const READ_ONLY  = 1;
     const READ_WRITE = 2;
 
-    var $format    = null;
     var $user      = null;
     var $auth_user = null;
     var $page      = null;
@@ -214,7 +213,25 @@ class ApiAction extends Action
         $twitter_user['location'] = ($profile->location) ? $profile->location : null;
         $twitter_user['description'] = ($profile->bio) ? $profile->bio : null;
 
-        $twitter_user['profile_image_url'] = $profile->avatarUrl(AVATAR_STREAM_SIZE);
+        // TODO: avatar url template (example.com/user/avatar?size={x}x{y})
+        $twitter_user['profile_image_url'] = Avatar::urlByProfile($profile, AVATAR_STREAM_SIZE);
+        $twitter_user['profile_image_url_https'] = $twitter_user['profile_image_url'];
+
+        // START introduced by qvitter API, not necessary for StatusNet API
+        $twitter_user['profile_image_url_profile_size'] = Avatar::urlByProfile($profile, AVATAR_PROFILE_SIZE);
+        try {
+            $avatar  = Avatar::getUploaded($profile);
+            $origurl = $avatar->displayUrl();
+        } catch (Exception $e) {
+            $origurl = $twitter_user['profile_image_url_profile_size'];
+        }
+        $twitter_user['profile_image_url_original'] = $origurl;
+
+        $twitter_user['groups_count'] = $profile->getGroupCount();
+        foreach (array('linkcolor', 'backgroundcolor') as $key) {
+            $twitter_user[$key] = Profile_prefs::getConfigData($profile, 'theme', $key);
+        }
+        // END introduced by qvitter API, not necessary for StatusNet API
 
         $twitter_user['url'] = ($profile->homepage) ? $profile->homepage : null;
         $twitter_user['protected'] = (!empty($user) && $user->private_stream) ? true : false;
@@ -263,7 +280,7 @@ class ApiAction extends Action
 
         if ($get_notice) {
             $notice = $profile->getCurrentNotice();
-            if ($notice) {
+            if ($notice instanceof Notice) {
                 // don't get user!
                 $twitter_user['status'] = $this->twitterStatusArray($notice, false);
             }
@@ -299,8 +316,15 @@ class ApiAction extends Action
         $twitter_status['text'] = $notice->content;
         $twitter_status['truncated'] = false; # Not possible on StatusNet
         $twitter_status['created_at'] = $this->dateTwitter($notice->created);
-        $twitter_status['in_reply_to_status_id'] = ($notice->reply_to) ?
-            intval($notice->reply_to) : null;
+        try {
+            // We could just do $notice->reply_to but maybe the future holds a
+            // different story for parenting.
+            $parent = $notice->getParent();
+            $in_reply_to = $parent->id;
+        } catch (Exception $e) {
+            $in_reply_to = null;
+        }
+        $twitter_status['in_reply_to_status_id'] = $in_reply_to;
 
         $source = null;
 
@@ -317,6 +341,7 @@ class ApiAction extends Action
             }
         }
 
+        $twitter_status['uri'] = $notice->getUri();
         $twitter_status['source'] = $source;
         $twitter_status['id'] = intval($notice->id);
 
@@ -343,10 +368,12 @@ class ApiAction extends Action
             $twitter_status['geo'] = null;
         }
 
-        if (isset($this->auth_user)) {
-            $twitter_status['favorited'] = $this->auth_user->hasFave($notice);
+        if (!is_null($this->scoped)) {
+            $twitter_status['favorited'] = $this->scoped->hasFave($notice);
+            $twitter_status['repeated']  = $this->scoped->hasRepeated($notice);
         } else {
             $twitter_status['favorited'] = false;
+            $twitter_status['repeated'] = false;
         }
 
         // Enclosures
@@ -399,6 +426,7 @@ class ApiAction extends Action
             );
         }
 
+        $twitter_group['admin_count'] = $group->getAdminCount();
         $twitter_group['member_count'] = $group->getMemberCount();
         $twitter_group['original_logo'] = $group->original_logo;
         $twitter_group['homepage_logo'] = $group->homepage_logo;
@@ -1328,86 +1356,6 @@ class ApiAction extends Action
         return;
     }
 
-    function clientError($msg, $code = 400, $format = null)
-    {
-        $action = $this->trimmed('action');
-        if ($format === null) {
-            $format = $this->format;
-        }
-
-        common_debug("User error '$code' on '$action': $msg", __FILE__);
-
-        if (!array_key_exists($code, ClientErrorAction::$status)) {
-            $code = 400;
-        }
-
-        $status_string = ClientErrorAction::$status[$code];
-
-        // Do not emit error header for JSONP
-        if (!isset($this->callback)) {
-            header('HTTP/1.1 ' . $code . ' ' . $status_string);
-        }
-
-        switch($format) {
-        case 'xml':
-            $this->initDocument('xml');
-            $this->elementStart('hash');
-            $this->element('error', null, $msg);
-            $this->element('request', null, $_SERVER['REQUEST_URI']);
-            $this->elementEnd('hash');
-            $this->endDocument('xml');
-            break;
-        case 'json':
-            $this->initDocument('json');
-            $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
-            print(json_encode($error_array));
-            $this->endDocument('json');
-            break;
-        case 'text':
-            header('Content-Type: text/plain; charset=utf-8');
-            print $msg;
-            break;
-        default:
-            // If user didn't request a useful format, throw a regular client error
-            throw new ClientException($msg, $code);
-        }
-    }
-
-    function serverError($msg, $code = 500, $content_type = null)
-    {
-        $action = $this->trimmed('action');
-        if ($content_type === null) {
-            $content_type = $this->format;
-        }
-
-        common_debug("Server error '$code' on '$action': $msg", __FILE__);
-
-        if (!array_key_exists($code, ServerErrorAction::$status)) {
-            $code = 400;
-        }
-
-        $status_string = ServerErrorAction::$status[$code];
-
-        // Do not emit error header for JSONP
-        if (!isset($this->callback)) {
-            header('HTTP/1.1 '.$code.' '.$status_string);
-        }
-
-        if ($content_type == 'xml') {
-            $this->initDocument('xml');
-            $this->elementStart('hash');
-            $this->element('error', null, $msg);
-            $this->element('request', null, $_SERVER['REQUEST_URI']);
-            $this->elementEnd('hash');
-            $this->endDocument('xml');
-        } else {
-            $this->initDocument('json');
-            $error_array = array('error' => $msg, 'request' => $_SERVER['REQUEST_URI']);
-            print(json_encode($error_array));
-            $this->endDocument('json');
-        }
-    }
-
     function initTwitterRss()
     {
         $this->startXML();
@@ -1520,7 +1468,10 @@ class ApiAction extends Action
             } else if ($this->arg('screen_name')) {
                 $nickname = common_canonical_nickname($this->arg('screen_name'));
                 $user = User::getKV('nickname', $nickname);
-                return $user ? $user->getProfile() : null;
+                return $user instanceof User ? $user->getProfile() : null;
+            } else {
+                // Fall back to trying the currently authenticated user
+                return $this->scoped;
             }
         } else if (self::is_decimal($id)) {
             return Profile::getKV($id);
@@ -1550,6 +1501,8 @@ class ApiAction extends Action
 
         } else if (self::is_decimal($id)) {
             return User_group::getKV('id', $id);
+        } else if ($this->arg('uri')) { // FIXME: move this into empty($id) check?
+            return User_group::getKV('uri', urldecode($this->arg('uri')));
         } else {
             return User_group::getForNickname($id);
         }