]> git.mxchange.org Git - quix0rs-gnu-social.git/blobdiff - lib/apiaction.php
Misses this file to merge. I like the comments.
[quix0rs-gnu-social.git] / lib / apiaction.php
index dd4047125a0443f6218ebb57bc497e4e6e333266..0eea08bed63f147a4c05b1edaec6fbfc68793cd2 100644 (file)
@@ -54,7 +54,7 @@
 
     @subsection usermethods_sec User Methods
 
-    @subsection directmessagemethods_sec Direct Message Methods
+    @subsection directmessagemethods_sec Direct Message Methods (now a plugin)
 
     @subsection friendshipmethods_sec Friendship Methods
 
@@ -123,10 +123,13 @@ class ApiAction extends Action
     var $auth_user = null;
     var $page      = null;
     var $count     = null;
+    var $offset    = null;
+    var $limit     = null;
     var $max_id    = null;
     var $since_id  = null;
     var $source    = null;
     var $callback  = null;
+    var $format    = null;
 
     var $access    = self::READ_ONLY;  // read (default) or read-write
 
@@ -141,7 +144,7 @@ class ApiAction extends Action
      */
     protected function prepare(array $args=array())
     {
-        StatusNet::setApi(true); // reduce exception reports to aid in debugging
+        GNUsocial::setApi(true); // reduce exception reports to aid in debugging
         parent::prepare($args);
 
         $this->format   = $this->arg('format');
@@ -151,8 +154,12 @@ class ApiAction extends Action
         $this->max_id   = (int)$this->arg('max_id', 0);
         $this->since_id = (int)$this->arg('since_id', 0);
 
+        // These two are not used everywhere, mainly just AtompubAction extensions
+        $this->offset   = ($this->page-1) * $this->count;
+        $this->limit    = $this->count + 1;
+
         if ($this->arg('since')) {
-            header('X-StatusNet-Warning: since parameter is disabled; use since_id');
+            header('X-GNUsocial-Warning: since parameter is disabled; use since_id');
         }
 
         $this->source = $this->trimmed('source');
@@ -215,6 +222,8 @@ class ApiAction extends Action
 
         // 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 {
@@ -241,8 +250,6 @@ class ApiAction extends Action
 
         $twitter_user['created_at'] = $this->dateTwitter($profile->created);
 
-        $twitter_user['favourites_count'] = $profile->faveCount(); // British spelling!
-
         $timezone = 'UTC';
 
         if (!empty($user) && $user->timezone) {
@@ -257,22 +264,20 @@ class ApiAction extends Action
         $twitter_user['statuses_count'] = $profile->noticeCount();
 
         // Is the requesting user following this user?
+        // These values might actually also mean "unknown". Ambiguity issues?
         $twitter_user['following'] = false;
         $twitter_user['statusnet_blocking'] = false;
         $twitter_user['notifications'] = false;
 
-        if (isset($this->auth_user)) {
-
-            $twitter_user['following'] = $this->auth_user->isSubscribed($profile);
-            $twitter_user['statusnet_blocking']  = $this->auth_user->hasBlocked($profile);
-
-            // Notifications on?
-            $sub = Subscription::pkeyGet(array('subscriber' =>
-                                               $this->auth_user->id,
-                                               'subscribed' => $profile->id));
-
-            if ($sub) {
+        if ($this->scoped instanceof Profile) {
+            try {
+                $sub = Subscription::getSubscription($this->scoped, $profile);
+                // Notifications on?
+                $twitter_user['following'] = true;
+                $twitter_user['statusnet_blocking']  = $this->scoped->hasBlocked($profile);
                 $twitter_user['notifications'] = ($sub->jabber || $sub->sms);
+            } catch (NoResultException $e) {
+                // well, the values are already false...
             }
         }
 
@@ -288,6 +293,9 @@ class ApiAction extends Action
 
         $twitter_user['statusnet_profile_url'] = $profile->profileurl;
 
+        // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
+        Event::handle('TwitterUserArray', array($profile, &$twitter_user, $this->scoped, array()));
+
         return $twitter_user;
     }
 
@@ -295,11 +303,12 @@ class ApiAction extends Action
     {
         $base = $this->twitterSimpleStatusArray($notice, $include_user);
 
+        // FIXME: MOVE TO SHARE PLUGIN
         if (!empty($notice->repeat_of)) {
             $original = Notice::getKV('id', $notice->repeat_of);
-            if (!empty($original)) {
-                $original_array = $this->twitterSimpleStatusArray($original, $include_user);
-                $base['retweeted_status'] = $original_array;
+            if ($original instanceof Notice) {
+                $orig_array = $this->twitterSimpleStatusArray($original, $include_user);
+                $base['retweeted_status'] = $orig_array;
             }
         }
 
@@ -327,7 +336,7 @@ class ApiAction extends Action
         $source = null;
 
         $ns = $notice->getSource();
-        if ($ns) {
+        if ($ns instanceof Notice_source) {
             if (!empty($ns->name) && !empty($ns->url)) {
                 $source = '<a href="'
                    . htmlspecialchars($ns->url)
@@ -366,14 +375,6 @@ class ApiAction extends Action
             $twitter_status['geo'] = null;
         }
 
-        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
         $attachments = $notice->attachments();
 
@@ -382,13 +383,15 @@ class ApiAction extends Action
             $twitter_status['attachments'] = array();
 
             foreach ($attachments as $attachment) {
-                $enclosure_o=$attachment->getEnclosure();
-                if ($enclosure_o) {
+                try {
+                    $enclosure_o = $attachment->getEnclosure();
                     $enclosure = array();
                     $enclosure['url'] = $enclosure_o->url;
                     $enclosure['mimetype'] = $enclosure_o->mimetype;
                     $enclosure['size'] = $enclosure_o->size;
                     $twitter_status['attachments'][] = $enclosure;
+                } catch (ServerException $e) {
+                    // There was not enough metadata available
                 }
             }
         }
@@ -404,6 +407,10 @@ class ApiAction extends Action
         $twitter_status['statusnet_html'] = $notice->rendered;
         $twitter_status['statusnet_conversation_id'] = intval($notice->conversation);
 
+        // The event call to handle NoticeSimpleStatusArray lets plugins add data to the output array
+        Event::handle('NoticeSimpleStatusArray', array($notice, &$twitter_status, $this->scoped,
+                                                       array('include_user'=>$include_user)));
+
         return $twitter_status;
     }
 
@@ -416,11 +423,11 @@ class ApiAction extends Action
         $twitter_group['nickname'] = $group->nickname;
         $twitter_group['fullname'] = $group->fullname;
 
-        if (isset($this->auth_user)) {
-            $twitter_group['member'] = $this->auth_user->isMember($group);
+        if ($this->scoped instanceof Profile) {
+            $twitter_group['member'] = $this->scoped->isMember($group);
             $twitter_group['blocked'] = Group_block::isBlocked(
                 $group,
-                $this->auth_user->getProfile()
+                $this->scoped
             );
         }
 
@@ -471,8 +478,8 @@ class ApiAction extends Action
         $twitter_list['member_count'] = $list->taggedCount();
         $twitter_list['uri'] = $list->getUri();
 
-        if (isset($this->auth_user)) {
-            $twitter_list['following'] = $list->hasSubscriber($this->auth_user);
+        if ($this->scoped instanceof Profile) {
+            $twitter_list['following'] = $list->hasSubscriber($this->scoped);
         } else {
             $twitter_list['following'] = false;
         }
@@ -508,13 +515,15 @@ class ApiAction extends Action
             $enclosures = array();
 
             foreach ($attachments as $attachment) {
-                $enclosure_o=$attachment->getEnclosure();
-                if ($enclosure_o) {
+                try {
+                    $enclosure_o = $attachment->getEnclosure();
                     $enclosure = array();
                     $enclosure['url'] = $enclosure_o->url;
                     $enclosure['mimetype'] = $enclosure_o->mimetype;
                     $enclosure['size'] = $enclosure_o->size;
                     $enclosures[] = $enclosure;
+                } catch (ServerException $e) {
+                    // There was not enough metadata available
                 }
             }
 
@@ -559,33 +568,29 @@ class ApiAction extends Action
         $relationship = array();
 
         $relationship['source'] =
-            $this->relationshipDetailsArray($source, $target);
+            $this->relationshipDetailsArray($source->getProfile(), $target->getProfile());
         $relationship['target'] =
-            $this->relationshipDetailsArray($target, $source);
+            $this->relationshipDetailsArray($target->getProfile(), $source->getProfile());
 
         return array('relationship' => $relationship);
     }
 
-    function relationshipDetailsArray($source, $target)
+    function relationshipDetailsArray(Profile $source, Profile $target)
     {
         $details = array();
 
-        $details['screen_name'] = $source->nickname;
+        $details['screen_name'] = $source->getNickname();
         $details['followed_by'] = $target->isSubscribed($source);
-        $details['following'] = $source->isSubscribed($target);
-
-        $notifications = false;
-
-        if ($source->isSubscribed($target)) {
-            $sub = Subscription::pkeyGet(array('subscriber' =>
-                $source->id, 'subscribed' => $target->id));
 
-            if (!empty($sub)) {
-                $notifications = ($sub->jabber || $sub->sms);
-            }
+        try {
+            $sub = Subscription::getSubscription($source, $target);
+            $details['following'] = true;
+            $details['notifications_enabled'] = ($sub->jabber || $sub->sms);
+        } catch (NoResultException $e) {
+            $details['following'] = false;
+            $details['notifications_enabled'] = false;
         }
 
-        $details['notifications_enabled'] = $notifications;
         $details['blocking'] = $source->hasBlocked($target);
         $details['id'] = intval($source->id);
 
@@ -636,6 +641,7 @@ class ApiAction extends Action
                 $this->showGeoXML($value);
                 break;
             case 'retweeted_status':
+                // FIXME: MOVE TO SHARE PLUGIN
                 $this->showTwitterXmlStatus($value, 'retweeted_status');
                 break;
             default:
@@ -769,7 +775,7 @@ class ApiAction extends Action
     function showSingleAtomStatus($notice)
     {
         header('Content-Type: application/atom+xml; charset=utf-8');
-        print $notice->asAtomEntry(true, true, true, $this->auth_user);
+        print $notice->asAtomEntry(true, true, true, $this->scoped);
     }
 
     function show_single_json_status($notice)
@@ -954,104 +960,6 @@ class ApiAction extends Action
         $this->elementEnd('entry');
     }
 
-    function showXmlDirectMessage($dm, $namespaces=false)
-    {
-        $attrs = array();
-        if ($namespaces) {
-            $attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
-        }
-        $this->elementStart('direct_message', $attrs);
-        foreach($dm as $element => $value) {
-            switch ($element) {
-            case 'sender':
-            case 'recipient':
-                $this->showTwitterXmlUser($value, $element);
-                break;
-            case 'text':
-                $this->element($element, null, common_xml_safe_str($value));
-                break;
-            default:
-                $this->element($element, null, $value);
-                break;
-            }
-        }
-        $this->elementEnd('direct_message');
-    }
-
-    function directMessageArray($message)
-    {
-        $dmsg = array();
-
-        $from_profile = $message->getFrom();
-        $to_profile = $message->getTo();
-
-        $dmsg['id'] = intval($message->id);
-        $dmsg['sender_id'] = intval($from_profile->id);
-        $dmsg['text'] = trim($message->content);
-        $dmsg['recipient_id'] = intval($to_profile->id);
-        $dmsg['created_at'] = $this->dateTwitter($message->created);
-        $dmsg['sender_screen_name'] = $from_profile->nickname;
-        $dmsg['recipient_screen_name'] = $to_profile->nickname;
-        $dmsg['sender'] = $this->twitterUserArray($from_profile, false);
-        $dmsg['recipient'] = $this->twitterUserArray($to_profile, false);
-
-        return $dmsg;
-    }
-
-    function rssDirectMessageArray($message)
-    {
-        $entry = array();
-
-        $from = $message->getFrom();
-
-        $entry['title'] = sprintf('Message from %1$s to %2$s',
-            $from->nickname, $message->getTo()->nickname);
-
-        $entry['content'] = common_xml_safe_str($message->rendered);
-        $entry['link'] = common_local_url('showmessage', array('message' => $message->id));
-        $entry['published'] = common_date_iso8601($message->created);
-
-        $taguribase = TagURI::base();
-
-        $entry['id'] = "tag:$taguribase:$entry[link]";
-        $entry['updated'] = $entry['published'];
-
-        $entry['author-name'] = $from->getBestName();
-        $entry['author-uri'] = $from->homepage;
-
-        $entry['avatar'] = $from->avatarUrl(AVATAR_STREAM_SIZE);
-        try {
-            $avatar = $from->getAvatar(AVATAR_STREAM_SIZE);
-            $entry['avatar-type'] = $avatar->mediatype;
-        } catch (Exception $e) {
-            $entry['avatar-type'] = 'image/png';
-        }
-
-        // RSS item specific
-
-        $entry['description'] = $entry['content'];
-        $entry['pubDate'] = common_date_rfc2822($message->created);
-        $entry['guid'] = $entry['link'];
-
-        return $entry;
-    }
-
-    function showSingleXmlDirectMessage($message)
-    {
-        $this->initDocument('xml');
-        $dmsg = $this->directMessageArray($message);
-        $this->showXmlDirectMessage($dmsg, true);
-        $this->endDocument('xml');
-    }
-
-    function showSingleJsonDirectMessage($message)
-    {
-        $this->initDocument('json');
-        $dmsg = $this->directMessageArray($message);
-        $this->showJsonObjects($dmsg);
-        $this->endDocument('json');
-    }
-
     function showAtomGroups($group, $title, $id, $link, $subtitle=null, $selfuri=null)
     {
         $this->initDocument('atom');
@@ -1322,7 +1230,6 @@ class ApiAction extends Action
         default:
             // TRANS: Client error on an API request with an unsupported data format.
             $this->clientError(_('Not a supported data format.'));
-            break;
         }
 
         return;
@@ -1349,7 +1256,6 @@ class ApiAction extends Action
         default:
             // TRANS: Client error on an API request with an unsupported data format.
             $this->clientError(_('Not a supported data format.'));
-            break;
         }
         return;
     }
@@ -1404,7 +1310,6 @@ class ApiAction extends Action
         default:
             // TRANS: Client error on an API request with an unsupported data format.
             $this->clientError(_('Not a supported data format.'));
-            return;
         }
         return;
     }
@@ -1434,7 +1339,7 @@ class ApiAction extends Action
                 return User::getKV('nickname', $nickname);
             } else {
                 // Fall back to trying the currently authenticated user
-                return $this->auth_user;
+                return $this->scoped->getUser();
             }
 
         } else if (self::is_decimal($id)) {
@@ -1530,7 +1435,7 @@ class ApiAction extends Action
             }
 
             if (!empty($list) && $list->private) {
-                if ($this->auth_user->id == $list->tagger) {
+                if ($this->scoped->id == $list->tagger) {
                     return $list;
                 }
             } else {
@@ -1598,6 +1503,11 @@ class ApiAction extends Action
             $aargs['id'] = $id;
         }
 
+        $user = $this->arg('user');
+        if (!empty($user)) {
+            $aargs['user'] = $user;
+        }
+
         $tag = $this->arg('tag');
         if (!empty($tag)) {
             $aargs['tag'] = $tag;