Commit 29d0871e authored by mmn's avatar mmn

Making many of the API actions more consistent with coding style

clientError and serverError exit after they're done so no need for
break or return. Also, $this->format is default.

We also got rid of the incredibly verbose version of $this->isPost()
which was spread all over the place.

Not all of this cleaning up is done yet.
parent 8202e922
......@@ -45,6 +45,8 @@ if (!defined('STATUSNET')) {
*/
class ApiAccountUpdateDeliveryDeviceAction extends ApiAuthAction
{
protected $needPost = true;
/**
* Take arguments for running
*
......@@ -75,15 +77,6 @@ class ApiAccountUpdateDeliveryDeviceAction extends ApiAuthAction
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
// TRANS: Client error message. POST is a HTTP command. It should not be translated.
_('This method requires a POST.'),
400, $this->format
);
return;
}
if (!in_array($this->format, array('xml', 'json'))) {
$this->clientError(
// TRANS: Client error displayed when coming across a non-supported API method.
......@@ -105,8 +98,7 @@ class ApiAccountUpdateDeliveryDeviceAction extends ApiAuthAction
if (empty($this->user)) {
// TRANS: Client error displayed when no existing user is provided for a user's delivery device setting.
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
$original = clone($this->user);
......
......@@ -43,6 +43,8 @@ if (!defined('STATUSNET')) {
*/
class ApiAccountUpdateProfileAction extends ApiAuthAction
{
protected $needPost = true;
/**
* Take arguments for running
*
......@@ -50,7 +52,7 @@ class ApiAccountUpdateProfileAction extends ApiAuthAction
*
* @return boolean success flag
*/
function prepare($args)
protected function prepare($args)
{
parent::prepare($args);
......@@ -69,37 +71,20 @@ class ApiAccountUpdateProfileAction extends ApiAuthAction
*
* See which request params have been set, and update the profile
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
protected function handle()
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
// TRANS: Client error. POST is a HTTP command. It should not be translated.
_('This method requires a POST.'),
400, $this->format
);
return;
}
parent::handle();
if (!in_array($this->format, array('xml', 'json'))) {
$this->clientError(
// TRANS: Client error displayed when coming across a non-supported API method.
_('API method not found.'),
404,
$this->format
);
return;
// TRANS: Client error displayed when coming across a non-supported API method.
$this->clientError(_('API method not found.'), 404);
}
if (empty($this->user)) {
// TRANS: Client error displayed if a user could not be found.
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
$profile = $this->user->getProfile();
......
......@@ -43,43 +43,18 @@ if (!defined('STATUSNET')) {
*/
class ApiAccountUpdateProfileImageAction extends ApiAuthAction
{
/**
* Take arguments for running
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*/
function prepare($args)
{
parent::prepare($args);
$this->user = $this->auth_user;
return true;
}
protected $needPost = true;
/**
* Handle the request
*
* Check whether the credentials are valid and output the result
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
protected function handle()
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
// TRANS: Client error. POST is a HTTP command. It should not be translated.
_('This method requires a POST.'),
400, $this->format
);
return;
}
parent::handle();
// Workaround for PHP returning empty $_POST and $_FILES when POST
// length > post_max_size in php.ini
......@@ -94,20 +69,17 @@ class ApiAccountUpdateProfileImageAction extends ApiAuthAction
'The server was unable to handle that much POST data (%s bytes) due to its current configuration.',
intval($_SERVER['CONTENT_LENGTH']));
$this->clientError(sprintf($msg, $_SERVER['CONTENT_LENGTH']));
return;
}
if (empty($this->user)) {
// TRANS: Client error displayed updating profile image without having a user object.
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
try {
$imagefile = ImageFile::fromUpload('image');
} catch (Exception $e) {
$this->clientError($e->getMessage(), 400, $this->format);
return;
$this->clientError($e->getMessage());
}
$type = $imagefile->preferredType();
......@@ -123,13 +95,6 @@ class ApiAccountUpdateProfileImageAction extends ApiAuthAction
$imagefile->copyTo($filepath);
$profile = $this->user->getProfile();
if (empty($profile)) {
// TRANS: Error message displayed when referring to a user without a profile.
$this->clientError(_('User has no profile.'));
return;
}
$profile->setOriginal($filename);
common_broadcast_profile($profile);
......
......@@ -58,8 +58,7 @@ class ApiAtomServiceAction extends ApiBareAuthAction
if (empty($this->user)) {
// TRANS: Client error displayed when making an Atom API request for an unknown user.
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
return true;
......
......@@ -46,6 +46,8 @@ if (!defined('STATUSNET')) {
*/
class ApiBlockCreateAction extends ApiAuthAction
{
protected $needPost = true;
var $other = null;
/**
......@@ -56,11 +58,10 @@ class ApiBlockCreateAction extends ApiAuthAction
* @return boolean success flag
*
*/
function prepare($args)
protected function prepare($args)
{
parent::prepare($args);
$this->user = $this->auth_user;
$this->other = $this->getTargetProfile($this->arg('id'));
return true;
......@@ -75,36 +76,20 @@ class ApiBlockCreateAction extends ApiAuthAction
*
* @return void
*/
function handle($args)
protected function handle()
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
// TRANS: Client error. POST is a HTTP command. It should not be translated.
_('This method requires a POST.'),
400,
$this->format
);
return;
}
parent::handle();
if (empty($this->user) || empty($this->other)) {
// TRANS: Client error displayed when trying to block a non-existing user or a user from another site.
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
// Don't allow blocking yourself!
if ($this->user->id == $this->other->id) {
$this->clientError(
// TRANS: Client error displayed when users try to block themselves.
_("You cannot block yourself!"),
403,
$this->format
);
return;
// TRANS: Client error displayed when users try to block themselves.
$this->clientError(_("You cannot block yourself!"), 403);
}
if (!$this->user->hasBlocked($this->other)) {
......@@ -122,7 +107,7 @@ class ApiBlockCreateAction extends ApiAuthAction
$this->endDocument($this->format);
} else {
// TRANS: Server error displayed when blocking a user has failed.
$this->serverError(_('Block user failed.'), 500, $this->format);
$this->serverError(_('Block user failed.'), 500);
}
}
}
......@@ -45,6 +45,8 @@ if (!defined('STATUSNET')) {
*/
class ApiBlockDestroyAction extends ApiAuthAction
{
protected $needPost = true;
var $other = null;
/**
......@@ -54,11 +56,10 @@ class ApiBlockDestroyAction extends ApiAuthAction
*
* @return boolean success flag
*/
function prepare($args)
protected function prepare($args)
{
parent::prepare($args);
$this->user = $this->auth_user;
$this->other = $this->getTargetProfile($this->arg('id'));
return true;
......@@ -69,28 +70,15 @@ class ApiBlockDestroyAction extends ApiAuthAction
*
* Save the new message
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
protected function handle()
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
// TRANS: Client error. POST is a HTTP command. It should not be translated.
_('This method requires a POST.'),
400,
$this->format
);
return;
}
parent::handle();
if (empty($this->user) || empty($this->other)) {
// TRANS: Client error when user not found for an API action to remove a block for a user.
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
if ($this->user->hasBlocked($this->other)) {
......
......@@ -70,8 +70,7 @@ class ApiDirectMessageAction extends ApiAuthAction
if (empty($this->user)) {
// TRANS: Client error given when a user was not found (404).
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
$server = common_root_url();
......
......@@ -49,6 +49,8 @@ if (!defined('STATUSNET')) {
*/
class ApiDirectMessageNewAction extends ApiAuthAction
{
protected $needPost = true;
var $other = null;
var $content = null;
......@@ -59,22 +61,17 @@ class ApiDirectMessageNewAction extends ApiAuthAction
*
* @return boolean success flag
*/
function prepare($args)
protected function prepare($args)
{
parent::prepare($args);
$this->user = $this->auth_user;
if (empty($this->user)) {
// TRANS: Client error when user not found for an API direct message action.
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
$this->content = $this->trimmed('text');
$this->user = $this->auth_user;
$user_param = $this->trimmed('user');
$user_id = $this->arg('user_id');
$screen_name = $this->trimmed('screen_name');
......@@ -91,67 +88,38 @@ class ApiDirectMessageNewAction extends ApiAuthAction
*
* Save the new message
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
protected function handle()
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
// TRANS: Client error. POST is a HTTP command. It should not be translated.
_('This method requires a POST.'),
400,
$this->format
);
return;
}
parent::handle();
if (empty($this->content)) {
$this->clientError(
// TRANS: Client error displayed when no message text was submitted (406).
_('No message text!'),
406,
$this->format
);
// TRANS: Client error displayed when no message text was submitted (406).
$this->clientError(_('No message text!'), 406);
} else {
$content_shortened = $this->auth_user->shortenLinks($this->content);
if (Message::contentTooLong($content_shortened)) {
// TRANS: Client error displayed when message content is too long.
// TRANS: %d is the maximum number of characters for a message.
$this->clientError(
// TRANS: Client error displayed when message content is too long.
// TRANS: %d is the maximum number of characters for a message.
sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()),
Message::maxContent()
),
406,
$this->format
);
return;
sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()), Message::maxContent()),
406);
}
}
if (empty($this->other)) {
// TRANS: Client error displayed if a recipient user could not be found (403).
$this->clientError(_('Recipient user not found.'), 403, $this->format);
return;
$this->clientError(_('Recipient user not found.'), 403);
} else if (!$this->user->mutuallySubscribed($this->other)) {
$this->clientError(
// TRANS: Client error displayed trying to direct message another user who's not a friend (403).
_('Cannot send direct messages to users who aren\'t your friend.'),
403,
$this->format
);
return;
// TRANS: Client error displayed trying to direct message another user who's not a friend (403).
$this->clientError(_('Cannot send direct messages to users who aren\'t your friend.'), 403);
} else if ($this->user->id == $this->other->id) {
// Note: sending msgs to yourself is allowed by Twitter
// TRANS: Client error displayed trying to direct message self (403).
$this->clientError(_('Do not send a message to yourself; ' .
'just say it to yourself quietly instead.'), 403, $this->format);
return;
$this->clientError(_('Do not send a message to yourself; just say it to yourself quietly instead.'), 403);
}
$message = Message::saveNew(
......
......@@ -48,6 +48,8 @@ if (!defined('STATUSNET')) {
*/
class ApiFriendshipsCreateAction extends ApiAuthAction
{
protected $needPost = true;
var $other = null;
/**
......@@ -58,11 +60,10 @@ class ApiFriendshipsCreateAction extends ApiAuthAction
* @return boolean success flag
*
*/
function prepare($args)
protected function prepare($args)
{
parent::prepare($args);
$this->user = $this->auth_user;
$this->other = $this->getTargetProfile($this->arg('id'));
return true;
......@@ -73,42 +74,20 @@ class ApiFriendshipsCreateAction extends ApiAuthAction
*
* Check the format and show the user info
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
protected function handle()
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
// TRANS: Client error. POST is a HTTP command. It should not be translated.
_('This method requires a POST.'),
400,
$this->format
);
return;
}
parent::handle();
if (!in_array($this->format, array('xml', 'json'))) {
$this->clientError(
// TRANS: Client error displayed when coming across a non-supported API method.
_('API method not found.'),
404,
$this->format
);
return;
// TRANS: Client error displayed when coming across a non-supported API method.
$this->clientError(_('API method not found.'), 404);
}
if (empty($this->other)) {
$this->clientError(
// TRANS: Client error displayed when trying follow who's profile could not be found.
_('Could not follow user: profile not found.'),
403,
$this->format
);
return;
// TRANS: Client error displayed when trying follow who's profile could not be found.
$this->clientError(_('Could not follow user: profile not found.'), 403);
}
if ($this->user->isSubscribed($this->other)) {
......@@ -118,14 +97,13 @@ class ApiFriendshipsCreateAction extends ApiAuthAction
_('Could not follow user: %s is already on your list.'),
$this->other->nickname
);
$this->clientError($errmsg, 403, $this->format);
return;
$this->clientError($errmsg, 403);
}
try {
Subscription::start($this->user->getProfile(), $this->other);
} catch (Exception $e) {
$this->clientError($e->getMessage(), 403, $this->format);
$this->clientError($e->getMessage(), 403);
}
$this->initDocument($this->format);
......
......@@ -113,13 +113,8 @@ class ApiGNUsocialConfigAction extends ApiAction
$this->endDocument('json');
break;
default:
$this->clientError(
// TRANS: Client error displayed when coming across a non-supported API method.
_('API method not found.'),
404,
$this->format
);
break;
// TRANS: Client error displayed when coming across a non-supported API method.
$this->clientError(_('API method not found.'), 404);
}
}
......
......@@ -61,13 +61,8 @@ class ApiGNUsocialVersionAction extends ApiPrivateAuthAction
$this->endDocument('json');
break;
default:
$this->clientError(
// TRANS: Client error displayed when coming across a non-supported API method.
_('API method not found.'),
404,
$this->format
);
break;
// TRANS: Client error displayed when coming across a non-supported API method.
$this->clientError(_('API method not found.'), 404);
}
}
......
......@@ -49,6 +49,8 @@ if (!defined('STATUSNET')) {
*/
class ApiGroupCreateAction extends ApiAuthAction
{
protected $needPost = true;
var $group = null;
var $nickname = null;
var $fullname = null;
......@@ -65,12 +67,10 @@ class ApiGroupCreateAction extends ApiAuthAction
*
* @return boolean success flag
*/
function prepare($args)
protected function prepare($args)
{
parent::prepare($args);
$this->user = $this->auth_user;
$this->nickname = Nickname::normalize($this->arg('nickname'));
$this->fullname = $this->arg('full_name');
$this->homepage = $this->arg('homepage');
......@@ -86,28 +86,15 @@ class ApiGroupCreateAction extends ApiAuthAction
*
* Save the new group
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
protected function handle()
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
// TRANS: Client error. POST is a HTTP command. It should not be translated.
_('This method requires a POST.'),
400,
$this->format
);
return;
}
parent::handle();
if (empty($this->user)) {
// TRANS: Client error given when a user was not found (404).
$this->clientError(_('No such user.'), 404, $this->format);
return;
$this->clientError(_('No such user.'), 404);
}
if ($this->validateParams() == false) {
......@@ -131,13 +118,8 @@ class ApiGroupCreateAction extends ApiAuthAction
$this->showSingleJsonGroup($group);
break;
default:
$this->clientError(
// TRANS: Client error displayed when coming across a non-supported API method.
_('API method not found.'),
404,
$this->format
);
break;
// TRANS: Client error displayed when coming across a non-supported API method.
$this->clientError(_('API method not found.'), 404);
}
}
......@@ -149,66 +131,36 @@ class ApiGroupCreateAction extends ApiAuthAction
function validateParams()
{
if ($this->groupNicknameExists($this->nickname)) {
$this->clientError(
// TRANS: Client error trying to create a group with a nickname this is already in use.
_('Nickname already in use. Try another one.'),
403,
$this->format
);
return false;
} else if (!User_group::allowedNickname($this->nickname)) {
$this->clientError(
// TRANS: Client error in form for group creation.
_('Not a valid nickname.'),
403,
$this->format
);
return false;
// TRANS: Client error trying to create a group with a nickname this is already in use.
$this->clientError(_('Nickname already in use. Try another one.'), 403);
} elseif (!User_group::allowedNickname($this->nickname)) {
// TRANS: Client error in form for group creation.
$this->clientError(_('Not a valid nickname.'), 403);
} elseif (!is_null($this->homepage)
&& strlen($this->homepage) > 0
&& !common_valid_http_url($this->homepage)) {
$this->clientError(
// TRANS: Client error in form for group creation.
_('Homepage is not a valid URL.'),
403,
$this->format
);
return false;
// TRANS: Client error in form for group creation.
$this->clientError(_('Homepage is not a valid URL.'), 403);
} elseif (
!is_null($this->fullname)
&& mb_strlen($this->fullname) > 255) {
$this->clientError(
// TRANS: Client error in form for group creation.
_('Full name is too long (maximum 255 characters).'),
403,
$this->format
);
return false;
!is_null($this->fullname)
&& mb_strlen($this->fullname) > 255) {
// TRANS: Client error in form for group creation.
$this->clientError(_('Full name is too long (maximum 255 characters).'), 403);
} elseif (User_group::descriptionTooLong($this->description)) {
$this->clientError(
sprintf(
// TRANS: Client error shown when providing too long a description during group creation.
// TRANS: %d is the maximum number of allowed characters.
_m('Description is too long (maximum %d character).',
'Description is too long (maximum %d characters).',
User_group::maxDescription()),
User_group::maxDescription()
),
403,
$this->format
);
return false;
} elseif (
!is_null($this->location)
&& mb_strlen($this->location) > 255) {
$this->clientError(
// TRANS: Client error shown when providing too long a location during group creation.
_('Location is too long (maximum 255 characters).'),
403,
$this->format
);
return false;
// TRANS: Client error shown when providing too long a description during group creation.
// TRANS: %d is the maximum number of allowed characters.
$this->clientError(sprintf(_m('Description is too long (maximum %d character).',
'Description is too long (maximum %d characters).',
User_group::maxDescription()), User_group::maxDescription()), 403);
} elseif (!is_null($this->location)
&& mb_strlen($this->location) > 255) {
// TRANS: Client error shown when providing too long a location during group creation.
$this->clientError(_('Location is too long (maximum 255 characters).'), 403);
}
if (!empty($this->aliasstring)) {
......@@ -221,57 +173,34 @@ class ApiGroupCreateAction extends ApiAuthAction
}
if (count($this->aliases) > common_config('group', 'maxaliases')) {
$this->clientError(