123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- <?php
- if (!defined('STATUSNET')) {
- exit(1);
- }
- class ApiListsAction extends ApiBareAuthAction
- {
- var $lists = null;
- var $cursor = 0;
- var $next_cursor = 0;
- var $prev_cursor = 0;
- var $create = false;
-
- protected function prepare(array $args=array())
- {
- parent::prepare($args);
- $this->create = ($_SERVER['REQUEST_METHOD'] == 'POST');
- if (!$this->create) {
- $this->user = $this->getTargetUser($this->arg('user'));
- if (!($user instanceof User)) {
-
- $this->clientError(_('No such user.'), 404);
- }
- $this->target = $user->getProfile();
- $this->getLists();
- }
- return true;
- }
-
- function requiresAuth()
- {
- return parent::requiresAuth() ||
- $this->create || $this->delete;
- }
-
- protected function handle()
- {
- parent::handle();
- if($this->create) {
- return $this->handlePost();
- }
- switch($this->format) {
- case 'xml':
- $this->showXmlLists($this->lists, $this->next_cursor, $this->prev_cursor);
- break;
- case 'json':
- $this->showJsonLists($this->lists, $this->next_cursor, $this->prev_cursor);
- break;
- default:
- $this->clientError(
-
- _('API method not found.'),
- 404,
- $this->format
- );
- break;
- }
- }
-
- function handlePost()
- {
- $name=$this->arg('name');
- if(empty($name)) {
-
-
- print _("A list must have a name.");
- exit(1);
- }
-
-
-
- $private = null;
- if ($this->arg('mode') === 'public') {
- $private = false;
- } else if ($this->arg('mode') === 'private') {
- $private = true;
- }
- $list = Profile_list::ensureTag($this->auth_user->id,
- $this->arg('name'),
- $this->arg('description'),
- $private);
- if (empty($list)) {
- return false;
- }
- switch($this->format) {
- case 'xml':
- $this->showSingleXmlList($list);
- break;
- case 'json':
- $this->showSingleJsonList($list);
- break;
- default:
-
- $this->clientError(_('API method not found.'), 404);
- }
- return true;
- }
-
- function getLists()
- {
- $cursor = (int) $this->arg('cursor', -1);
-
-
- $count = 20;
- $fn = array($this->target, 'getLists');
- list($this->lists,
- $this->next_cursor,
- $this->prev_cursor) = Profile_list::getAtCursor($fn, array($this->scoped), $cursor, $count);
- }
- function isReadOnly($args)
- {
- return false;
- }
- function lastModified()
- {
- if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
- return strtotime($this->lists[0]->created);
- }
- return null;
- }
-
- function etag()
- {
- if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
- $last = count($this->lists) - 1;
- return '"' . implode(
- ':',
- array($this->arg('action'),
- common_language(),
- $this->target->id,
- strtotime($this->lists[0]->created),
- strtotime($this->lists[$last]->created))
- )
- . '"';
- }
- return null;
- }
- }
|