Twitter-compatible API - cleaned up sloppy control flow: exit() statements everywhere

darcs-hash:20081001025421-462f3-3bf666327de3a3c5ea506b2c387741dc3d0e82bf.gz
This commit is contained in:
zach 2008-09-30 22:54:21 -04:00
parent dec2f29c6a
commit 6beddfdbb9
11 changed files with 105 additions and 143 deletions

View File

@ -89,13 +89,12 @@ class ApiAction extends Action {
'user' => $this->user);
call_user_func(array($action_obj, $this->api_method), $_REQUEST, $apidata);
# all API methods should exit()
}
} else {
common_user_error("API method not found!", $code=404);
}
common_user_error("API method not found!", $code=404);
}
# Whitelist of API methods that don't need authentication
function requires_auth() {
static $noauth = array( 'statuses/public_timeline',
@ -130,7 +129,6 @@ class ApiAction extends Action {
header('HTTP/1.1 401 Unauthorized');
header('Content-type: text/plain');
print("Could not authenticate you."); # exactly what Twitter says - no \n
exit();
}
}

View File

@ -49,26 +49,19 @@ class TwitapiaccountAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code=404);
}
exit();
}
function end_session($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
function update_location($args, $apidata) {
parent::handle($args);
if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404);
exit;
}
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
exit();
return;
}
$location = trim($this->arg('location'));
@ -77,7 +70,7 @@ class TwitapiaccountAction extends TwitterapiAction {
// XXX: But Twitter just truncates and runs with it. -- Zach
$this->client_error(_('That\'s too long. Max notice size is 255 chars.'), 406, $apidate['content-type']);
exit();
return;
}
$user = $apidata['user'];
@ -85,7 +78,7 @@ class TwitapiaccountAction extends TwitterapiAction {
if (!$profile) {
common_server_error(_('User has no profile.'));
exit();
return;
}
$orig_profile = clone($profile);
@ -99,7 +92,7 @@ class TwitapiaccountAction extends TwitterapiAction {
if (!$result) {
common_log_db_error($profile, 'UPDATE', __FILE__);
common_server_error(_('Couldn\'t save profile.'));
exit();
return;
}
common_broadcast_profile($profile);
@ -108,20 +101,16 @@ class TwitapiaccountAction extends TwitterapiAction {
$this->init_document($type);
$this->show_profile($profile, $type);
$this->end_document($type);
exit();
}
function update_delivery_device($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
function rate_limit_status($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
}

View File

@ -26,13 +26,11 @@ class TwitapiblocksAction extends TwitterapiAction {
function create($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
function destroy($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
}

View File

@ -104,7 +104,6 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404);
}
exit();
}
// had to change this from "new" to "create" to avoid PHP reserved word
@ -113,7 +112,7 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
exit();
return;
}
$user = $apidata['user'];
@ -130,30 +129,30 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
} else if (mb_strlen($status) > 140) {
$this->client_error(_('That\'s too long. Max message size is 140 chars.'),
$code = 406, $apidata['content-type']);
exit();
return;
}
$other = $this->get_user($this->trimmed('user'));
if (!$other) {
$this->client_error(_('Recipient user not found.'), $code = 403, $apidata['content-type']);
exit();
return;
} else if (!$user->mutuallySubscribed($other)) {
$this->client_error(_('Can\'t send direct messages to users who aren\'t your friend.'),
$code = 403, $apidata['content-type']);
exit();
return;
} else if ($user->id == $other->id) {
// Sending msgs to yourself is allowed by Twitter
$this->client_error(_('Don\'t send a message to yourself; just say it to yourself quietly instead.'),
$code = 403, $apidata['content-type']);
exit();
return;
}
$message = Message::saveNew($user->id, $other->id, $content, $source);
if (is_string($message)) {
$this->server_error($message);
exit();
return;
}
$this->notify($user, $other, $message);
@ -164,13 +163,11 @@ class Twitapidirect_messagesAction extends TwitterapiAction {
$this->show_single_json_dmsg($message);
}
exit();
}
function destroy($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
function show_xml_dmsgs($message) {

View File

@ -66,7 +66,7 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!$profile) {
common_server_error(_('User has no profile.'));
exit();
return;
}
$page = $this->arg('page');
@ -83,7 +83,7 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!$notice) {
common_server_error(_('Could not retrieve favorite notices.'));
exit();
return;
}
$sitename = common_config('site', 'name');
@ -111,7 +111,6 @@ class TwitapifavoritesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404);
}
exit();
}
function create($args, $apidata) {
@ -119,14 +118,14 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404);
exit;
return;
}
// Check for RESTfulness
if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
// XXX: Twitter just prints the err msg, no XML / JSON.
$this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
exit();
return;
}
$user = $apidata['user'];
@ -135,13 +134,13 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!$notice) {
$this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
exit();
return;
}
// XXX: Twitter lets you fave things repeatedly via api.
if ($user->hasFave($notice)) {
$this->client_error(_('This notice is already a favorite!'), 403, $apidata['content-type']);
exit();
return;
}
common_debug("notice: " . $apidata['api_arg']);
@ -150,7 +149,7 @@ class TwitapifavoritesAction extends TwitterapiAction {
if (!$fave) {
common_server_error(_('Could not create favorite.'));
exit();
return;
}
$this->notify($fave, $notice, $user);
@ -162,13 +161,11 @@ class TwitapifavoritesAction extends TwitterapiAction {
$this->show_single_json_status($notice);
}
exit();
}
function destroy($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
// XXX: these two funcs swiped from faves. Maybe put in util.php, or some common base class?

View File

@ -42,7 +42,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
exit();
return;
}
$id = $apidata['api_arg'];
@ -51,7 +51,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!$other) {
$this->client_error(_('Could not follow user: User not found.'), 403, $apidata['content-type']);
exit();
return;
}
$user = $apidata['user'];
@ -59,7 +59,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if ($user->isSubscribed($other)) {
$errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
$this->client_error($errmsg, 403, $apidata['content-type']);
exit();
return;
}
$sub = new Subscription();
@ -75,7 +75,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!$result) {
$errmsg = sprintf(_('Could not follow user: %s is already on your list.'), $other->nickname);
$this->client_error($errmsg, 400, $apidata['content-type']);
exit();
return;
}
$sub->query('COMMIT');
@ -86,7 +86,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
$this->init_document($type);
$this->show_profile($other, $type);
$this->end_document($type);
exit();
}
//destroy
@ -106,7 +106,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
$this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
exit();
return;
}
$id = $apidata['api_arg'];
@ -126,14 +126,14 @@ class TwitapifriendshipsAction extends TwitterapiAction {
$sub->query('COMMIT');
} else {
$this->client_error(_('You are not friends with the specified user.'), 403, $apidata['content-type']);
exit();
return;
}
$type = $apidata['content-type'];
$this->init_document($type);
$this->show_profile($other, $type);
$this->end_document($type);
exit();
}
// Tests if a friendship exists between two users.
@ -154,7 +154,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404);
exit;
return;
}
$user_a_id = $this->trimmed('user_a');
@ -165,7 +165,7 @@ class TwitapifriendshipsAction extends TwitterapiAction {
if (!$user_a || !$user_b) {
$this->client_error(_('Two user ids or screen_names must be supplied.'), 400, $apidata['content-type']);
exit();
return;
}
if ($user_a->isSubscribed($user_b)) {
@ -189,7 +189,6 @@ class TwitapifriendshipsAction extends TwitterapiAction {
break;
}
exit();
}
}

View File

@ -45,13 +45,12 @@ class TwitapihelpAction extends TwitterapiAction {
} else {
common_user_error(_('API method not found!'), $code=404);
}
exit();
}
function downtime_schedule($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
}

View File

@ -27,13 +27,11 @@ class TwitapinotificationsAction extends TwitterapiAction {
function follow($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
function leave($args, $apidata) {
parent::handle($args);
common_server_error(_('API method under construction.'), $code=501);
exit();
}
}

View File

@ -98,7 +98,6 @@ class TwitapistatusesAction extends TwitterapiAction {
common_server_error(_('Couldn\'t find any statuses.'), $code = 503);
}
exit();
}
/*
@ -177,7 +176,6 @@ class TwitapistatusesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404);
}
exit();
}
/*
@ -235,7 +233,7 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!$profile) {
common_server_error(_('User has no profile.'));
exit();
return;
}
$count = $this->arg('count');
@ -290,7 +288,6 @@ class TwitapistatusesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404);
}
exit();
}
function update($args, $apidata) {
@ -299,12 +296,12 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404);
exit;
return;
}
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->client_error(_('This method requires a POST.'), 400, $apidata['content-type']);
exit();
return;
}
$user = $apidata['user'];
@ -322,7 +319,7 @@ class TwitapistatusesAction extends TwitterapiAction {
// No error is given, but the status is not posted to the
// user's timeline. Seems bad. Shouldn't we throw an
// errror? -- Zach
exit();
return;
} else if (mb_strlen($status) > 140) {
@ -331,7 +328,7 @@ class TwitapistatusesAction extends TwitterapiAction {
// that assume Twitter will truncate for them. Should we just
// truncate too? -- Zach
$this->client_error(_('That\'s too long. Max notice size is 140 chars.'), $code = 406, $apidata['content-type']);
exit();
return;
}
$reply_to = NULL;
@ -345,7 +342,7 @@ class TwitapistatusesAction extends TwitterapiAction {
$reply_to = $in_reply_to_status_id;
} else {
$this->client_error(_('Not found'), $code = 404, $apidata['content-type']);
exit();
return;
}
}
@ -353,7 +350,7 @@ class TwitapistatusesAction extends TwitterapiAction {
if (is_string($notice)) {
$this->server_error($notice);
exit();
return;
}
common_broadcast_notice($notice);
@ -367,7 +364,6 @@ class TwitapistatusesAction extends TwitterapiAction {
$apidata['api_arg'] = $notice->id;
$this->show($args, $apidata);
exit();
}
/*
@ -447,7 +443,6 @@ class TwitapistatusesAction extends TwitterapiAction {
common_user_error(_('API method not found!'), $code = 404);
}
exit();
}
function show($args, $apidata) {
@ -455,7 +450,7 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404);
exit;
return;
}
$notice_id = $apidata['api_arg'];
@ -472,7 +467,6 @@ class TwitapistatusesAction extends TwitterapiAction {
$this->client_error(_('No status with that ID found.'), 404, $apidata['content-type']);
}
exit();
}
@ -497,14 +491,14 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404);
exit;
return;
}
// Check for RESTfulness
if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
// XXX: Twitter just prints the err msg, no XML / JSON.
$this->client_error(_('This method requires a POST or DELETE.'), 400, $apidata['content-type']);
exit();
return;
}
$user = $apidata['user'];
@ -513,7 +507,7 @@ class TwitapistatusesAction extends TwitterapiAction {
if (!$notice) {
$this->client_error(_('No status found with that ID.'), 404, $apidata['content-type']);
exit();
return;
}
if ($user->id == $notice->profile_id) {
@ -532,7 +526,6 @@ class TwitapistatusesAction extends TwitterapiAction {
$this->client_error(_('You may not delete another user\'s status.'), 403, $apidata['content-type']);
}
exit();
}
# User Methods
@ -626,7 +619,6 @@ class TwitapistatusesAction extends TwitterapiAction {
$this->init_document($type);
$this->show_profiles($others, $type);
$this->end_document($type);
exit();
}
function get_subs_user($apidata) {
@ -673,7 +665,6 @@ class TwitapistatusesAction extends TwitterapiAction {
break;
default:
$this->client_error(_('unsupported file type'));
exit();
}
}

View File

@ -53,7 +53,7 @@ class TwitapiusersAction extends TwitterapiAction {
if (!in_array($apidata['content-type'], array('xml', 'json'))) {
common_user_error(_('API method not found!'), $code = 404);
exit;
return;
}
$user = null;
@ -78,14 +78,14 @@ class TwitapiusersAction extends TwitterapiAction {
if (!$user) {
// XXX: Twitter returns a random(?) user instead of throwing and err! -- Zach
$this->client_error(_('User not found.'), 404, $apidata['content-type']);
exit();
return;
}
$profile = $user->getProfile();
if (!$profile) {
common_server_error(_('User has no profile.'));
exit();
return;
}
$twitter_user = $this->twitter_user_array($profile, true);
@ -125,6 +125,5 @@ class TwitapiusersAction extends TwitterapiAction {
$this->end_document('json');
}
exit();
}
}

View File

@ -194,7 +194,6 @@ class TwitterapiAction extends Action {
$twitter_status = $this->twitter_status_array($notice);
$this->show_twitter_xml_status($twitter_status);
$this->end_document('xml');
exit();
}
function show_single_json_status($notice) {
@ -202,7 +201,6 @@ class TwitterapiAction extends Action {
$status = $this->twitter_status_array($notice);
$this->show_json_objects($status);
$this->end_document('json');
exit();
}
function show_single_xml_dmsg($message) {
@ -469,7 +467,6 @@ class TwitterapiAction extends Action {
$this->end_document('json');
}
exit();
}
function init_twitter_rss() {