X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=lib%2Fapi.php;h=d79dc327edef80a7b1eecb3886f3b2364e0ffbd5;hb=894b221e8aec57768d6804ee3bf53c85196e09bf;hp=12e3ba5314c564fb499d2be3e755540462fae906;hpb=a3660fbea46be4d9408cd0f824166a75b8c865ab;p=quix0rs-gnu-social.git diff --git a/lib/api.php b/lib/api.php index 12e3ba5314..d79dc327ed 100644 --- a/lib/api.php +++ b/lib/api.php @@ -53,13 +53,19 @@ if (!defined('STATUSNET')) { class ApiAction extends Action { - var $format = null; - var $user = null; - var $page = null; - var $count = null; - var $max_id = null; - var $since_id = null; - var $since = null; + const READ_ONLY = 1; + const READ_WRITE = 2; + + var $format = null; + var $user = null; + var $auth_user = null; + var $page = null; + var $count = null; + var $max_id = null; + var $since_id = null; + var $since = null; + + var $access = self::READ_ONLY; // read (default) or read-write /** * Initialization. @@ -71,6 +77,7 @@ class ApiAction extends Action function prepare($args) { + StatusNet::setApi(true); // reduce exception reports to aid in debugging parent::prepare($args); $this->format = $this->arg('format'); @@ -134,8 +141,8 @@ class ApiAction extends Action $twitter_user['protected'] = false; # not supported by StatusNet yet $twitter_user['followers_count'] = $profile->subscriberCount(); - $user = $profile->getUser(); $design = null; + $user = $profile->getUser(); // Note: some profiles don't have an associated user @@ -145,6 +152,10 @@ class ApiAction extends Action $design = $user->getDesign(); } + if (empty($design)) { + $design = $defaultDesign; + } + $color = Design::toWebColor(empty($design->backgroundcolor) ? $defaultDesign->backgroundcolor : $design->backgroundcolor); $twitter_user['profile_background_color'] = ($color == null) ? '' : '#'.$color->hexValue(); $color = Design::toWebColor(empty($design->textcolor) ? $defaultDesign->textcolor : $design->textcolor); @@ -163,7 +174,7 @@ class ApiAction extends Action $timezone = 'UTC'; - if (!empty($user) && !empty($user->timezone)) { + if (!empty($user) && $user->timezone) { $timezone = $user->timezone; } @@ -188,13 +199,14 @@ class ApiAction extends Action $twitter_user['following'] = false; $twitter_user['notifications'] = false; - if (isset($apidata['user'])) { + if (isset($this->auth_user)) { - $twitter_user['following'] = $apidata['user']->isSubscribed($profile); + $twitter_user['following'] = $this->auth_user->isSubscribed($profile); // Notifications on? $sub = Subscription::pkeyGet(array('subscriber' => - $apidata['user']->id, 'subscribed' => $profile->id)); + $this->auth_user->id, + 'subscribed' => $profile->id)); if ($sub) { $twitter_user['notifications'] = ($sub->jabber || $sub->sms); @@ -216,14 +228,15 @@ class ApiAction extends Action { $base = $this->twitterSimpleStatusArray($notice, $include_user); - if (empty($notice->repeat_of)) { - return $base; - } else { + if (!empty($notice->repeat_of)) { $original = Notice::staticGet('id', $notice->repeat_of); - $original_array = $this->twitterSimpleStatusArray($original, $include_user); - $original_array['retweeted_status'] = $base; - return $original_array; + if (!empty($original)) { + $original_array = $this->twitterSimpleStatusArray($original, $include_user); + $base['retweeted_status'] = $original_array; + } } + + return $base; } function twitterSimpleStatusArray($notice, $include_user=true) @@ -286,7 +299,7 @@ class ApiAction extends Action } } - if ($include_user) { + if ($include_user && $profile) { # Don't get notice (recursive!) $twitter_user = $this->twitterUserArray($profile, false); $twitter_status['user'] = $twitter_user; @@ -345,7 +358,7 @@ class ApiAction extends Action $entry['link'] = common_local_url('shownotice', array('notice' => $notice->id)); $entry['published'] = common_date_iso8601($notice->created); - $taguribase = common_config('integration', 'taguri'); + $taguribase = TagURI::base(); $entry['id'] = "tag:$taguribase:$entry[link]"; $entry['updated'] = $entry['published']; @@ -603,7 +616,7 @@ class ApiAction extends Action $this->endDocument('xml'); } - function showRssTimeline($notice, $title, $link, $subtitle, $suplink=null) + function showRssTimeline($notice, $title, $link, $subtitle, $suplink=null, $logo=null) { $this->initDocument('rss'); @@ -617,6 +630,15 @@ class ApiAction extends Action 'href' => $suplink, 'type' => 'application/json')); } + + if (!is_null($logo)) { + $this->elementStart('image'); + $this->element('link', null, $link); + $this->element('title', null, $title); + $this->element('url', null, $logo); + $this->elementEnd('image'); + } + $this->element('description', null, $subtitle); $this->element('language', null, 'en-us'); $this->element('ttl', null, '40'); @@ -636,7 +658,7 @@ class ApiAction extends Action $this->endTwitterRss(); } - function showAtomTimeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null) + function showAtomTimeline($notice, $title, $id, $link, $subtitle=null, $suplink=null, $selfuri=null, $logo=null) { $this->initDocument('atom'); @@ -645,6 +667,10 @@ class ApiAction extends Action $this->element('id', null, $id); $this->element('link', array('href' => $link, 'rel' => 'alternate', 'type' => 'text/html'), null); + if (!is_null($logo)) { + $this->element('logo',null,$logo); + } + if (!is_null($suplink)) { # For FriendFeed's SUP protocol $this->element('link', array('rel' => 'http://api.friendfeed.com/2008/03#sup', @@ -769,14 +795,14 @@ class ApiAction extends Action $from = $message->getFrom(); - $entry['title'] = sprintf('Message from %s to %s', + $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 = common_config('integration', 'taguri'); + $taguribase = TagURI::base(); $entry['id'] = "tag:$taguribase:$entry[link]"; $entry['updated'] = $entry['published']; @@ -1077,7 +1103,7 @@ class ApiAction extends Action } } - function serverError($msg, $code = 500, $content_type = 'json') + function serverError($msg, $code = 500, $content_type = 'xml') { $action = $this->trimmed('action'); @@ -1128,7 +1154,6 @@ class ApiAction extends Action $this->elementStart('feed', array('xmlns' => 'http://www.w3.org/2005/Atom', 'xml:lang' => 'en-US', 'xmlns:thr' => 'http://purl.org/syndication/thread/1.0')); - Event::handle('StartApiAtom', array($this)); } function endTwitterAtom() @@ -1193,7 +1218,12 @@ class ApiAction extends Action return User_group::staticGet($this->arg('id')); } else if ($this->arg('id')) { $nickname = common_canonical_nickname($this->arg('id')); - return User_group::staticGet('nickname', $nickname); + $local = Local_group::staticGet('nickname', $nickname); + if (empty($local)) { + return null; + } else { + return User_group::staticGet('id', $local->id); + } } else if ($this->arg('group_id')) { // This is to ensure that a non-numeric user_id still // overrides screen_name even if it doesn't get used @@ -1202,14 +1232,24 @@ class ApiAction extends Action } } else if ($this->arg('group_name')) { $nickname = common_canonical_nickname($this->arg('group_name')); - return User_group::staticGet('nickname', $nickname); + $local = Local_group::staticGet('nickname', $nickname); + if (empty($local)) { + return null; + } else { + return User_group::staticGet('id', $local->id); + } } } else if (is_numeric($id)) { return User_group::staticGet($id); } else { $nickname = common_canonical_nickname($id); - return User_group::staticGet('nickname', $nickname); + $local = Local_group::staticGet('nickname', $nickname); + if (empty($local)) { + return null; + } else { + return User_group::staticGet('id', $local->id); + } } } @@ -1224,10 +1264,27 @@ class ApiAction extends Action case 'api': break; default: + + $name = null; + $url = null; + $ns = Notice_source::staticGet($source); + if ($ns) { - $source_name = '' . $ns->name . ''; + $name = $ns->name; + $url = $ns->url; + } else { + $app = Oauth_application::staticGet('name', $source); + if ($app) { + $name = $app->name; + $url = $app->source_url; + } + } + + if (!empty($name) && !empty($url)) { + $source_name = '' . $name . ''; } + break; } return $source_name; @@ -1278,4 +1335,22 @@ class ApiAction extends Action } } + function getSelfUri($action, $aargs) + { + parse_str($_SERVER['QUERY_STRING'], $params); + $pstring = ''; + if (!empty($params)) { + unset($params['p']); + $pstring = http_build_query($params); + } + + $uri = common_local_url($action, $aargs); + + if (!empty($pstring)) { + $uri .= '?' . $pstring; + } + + return $uri; + } + }