New actions for blocks via API

This commit is contained in:
Zach Copley 2009-10-09 13:35:54 -07:00
parent ada84698f2
commit 09f4d1ef23
5 changed files with 249 additions and 134 deletions

121
actions/apiblockcreate.php Normal file
View File

@ -0,0 +1,121 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Block a user via the API
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category API
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
require_once INSTALLDIR . '/lib/apiauth.php';
/**
* Blocks the user specified in the ID parameter as the authenticating user.
* Destroys a friendship to the blocked user if it exists. Returns the
* blocked user in the requested format when successful.
*
* @category API
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class ApiBlockCreateAction extends ApiAuthAction
{
var $format = null;
var $user = null;
var $other = null;
/**
* Take arguments for running
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*
*/
function prepare($args)
{
parent::prepare($args);
if ($this->requiresAuth()) {
if ($this->checkBasicAuthUser() == false) {
return;
}
}
$this->user = $this->auth_user;
$this->other = $this->getTargetUser($this->arg('id'));
$this->format = $this->arg('format');
return true;
}
/**
* Handle the request
*
* Save the new message
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
_('This method requires a POST.'),
400,
$this->format
);
return;
}
if (empty($this->user) || empty($this->other)) {
$this->clientError(_('No such user!'), 404, $this->format);
return;
}
if ($this->user->hasBlocked($this->other)
|| $this->user->block($this->other)
) {
$this->init_document($this->format);
$this->show_profile($this->other, $this->format);
$this->end_document($this->format);
} else {
$this->serverError(_('Block user failed.'), 500, $this->format);
}
}
}

120
actions/apiblockdestroy.php Normal file
View File

@ -0,0 +1,120 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Un-block a user via the API
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category API
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @copyright 2009 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
require_once INSTALLDIR . '/lib/apiauth.php';
/**
* Un-blocks the user specified in the ID parameter for the authenticating user.
* Returns the un-blocked user in the requested format when successful.
*
* @category API
* @package StatusNet
* @author Zach Copley <zach@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class ApiBlockDestroyAction extends ApiAuthAction
{
var $format = null;
var $user = null;
var $other = null;
/**
* Take arguments for running
*
* @param array $args $_REQUEST args
*
* @return boolean success flag
*
*/
function prepare($args)
{
parent::prepare($args);
if ($this->requiresAuth()) {
if ($this->checkBasicAuthUser() == false) {
return;
}
}
$this->user = $this->auth_user;
$this->other = $this->getTargetUser($this->arg('id'));
$this->format = $this->arg('format');
return true;
}
/**
* Handle the request
*
* Save the new message
*
* @param array $args $_REQUEST data (unused)
*
* @return void
*/
function handle($args)
{
parent::handle($args);
if ($_SERVER['REQUEST_METHOD'] != 'POST') {
$this->clientError(
_('This method requires a POST.'),
400,
$this->format
);
return;
}
if (empty($this->user) || empty($this->other)) {
$this->clientError(_('No such user!'), 404, $this->format);
return;
}
if (!$this->user->hasBlocked($this->other)
|| $this->user->unblock($this->other)
) {
$this->init_document($this->format);
$this->show_profile($this->other, $this->format);
$this->end_document($this->format);
} else {
$this->serverError(_('Unblock user failed.'));
}
}
}

View File

@ -1,74 +0,0 @@
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008, 2009, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
require_once(INSTALLDIR.'/lib/twitterapi.php');
class TwitapiblocksAction extends TwitterapiAction
{
function create($args, $apidata)
{
parent::handle($args);
$blockee = $this->get_user($apidata['api_arg'], $apidata);
if (empty($blockee)) {
$this->clientError('Not Found', 404, $apidata['content-type']);
return;
}
$user = $apidata['user']; // Always the auth user
if ($user->hasBlocked($blockee) || $user->block($blockee)) {
$type = $apidata['content-type'];
$this->init_document($type);
$this->show_profile($blockee, $type);
$this->end_document($type);
} else {
$this->serverError(_('Block user failed.'));
}
}
function destroy($args, $apidata)
{
parent::handle($args);
$blockee = $this->get_user($apidata['api_arg'], $apidata);
if (empty($blockee)) {
$this->clientError('Not Found', 404, $apidata['content-type']);
return;
}
$user = $apidata['user'];
if (!$user->hasBlocked($blockee) || $user->unblock($blockee)) {
$type = $apidata['content-type'];
$this->init_document($type);
$this->show_profile($blockee, $type);
$this->end_document($type);
} else {
$this->serverError(_('Unblock user failed.'));
}
}
}

View File

@ -1,57 +0,0 @@
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2008, 2009, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
require_once(INSTALLDIR.'/lib/twitterapi.php');
class TwitapihelpAction extends TwitterapiAction
{
/* Returns the string "ok" in the requested format with a 200 OK HTTP status code.
* URL:http://identi.ca/api/help/test.format
* Formats: xml, json
*/
function test($args, $apidata)
{
parent::handle($args);
if ($apidata['content-type'] == 'xml') {
$this->init_document('xml');
$this->element('ok', null, 'true');
$this->end_document('xml');
} elseif ($apidata['content-type'] == 'json') {
$this->init_document('json');
print '"ok"';
$this->end_document('json');
} else {
$this->clientError(_('API method not found!'), $code=404);
}
}
function downtime_schedule($args, $apidata)
{
parent::handle($args);
$this->serverError(_('API method under construction.'), $code=501);
}
}

View File

@ -474,10 +474,15 @@ class Router
// blocks
$m->connect('api/blocks/:method/:argument',
array('action' => 'api',
'apiaction' => 'blocks'));
$m->connect('api/blocks/create/:id.:format',
array('action' => 'ApiBlockCreate',
'id' => '[a-zA-Z0-9]+',
'format' => '(xml|json)'));
$m->connect('api/blocks/destroy/:id.:format',
array('action' => 'ApiBlockDestroy',
'id' => '[a-zA-Z0-9]+',
'format' => '(xml|json)'));
// help
$m->connect('api/help/test.:format',