3 * StatusNet, the distributed open-source microblogging tool
5 * Show, update or delete a list.
9 * LICENCE: This program is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU Affero General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Affero General Public License for more details.
19 * You should have received a copy of the GNU Affero General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * @author Shashi Gowda <connect2shashi@gmail.com>
25 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
26 * @link http://status.net/
29 if (!defined('STATUSNET')) {
33 class ApiListAction extends ApiBareAuthAction
36 * The list in question in the current request
41 * Is this an update request?
46 * Is this a delete request?
51 * Set the flags for handling the request. Show list if this is a GET
52 * request, update it if it is POST, delete list if method is DELETE
53 * or if method is POST and an argument _method is set to DELETE. Act
54 * like we don't know if the current user has no access to the list.
57 * - user: the user id or nickname
58 * - id: the id of the tag or the tag itself
60 * @return boolean success flag
62 protected function prepare(array $args=array())
64 parent::prepare($args);
66 $this->delete = ($_SERVER['REQUEST_METHOD'] == 'DELETE' ||
67 ($this->trimmed('_method') == 'DELETE' &&
68 $_SERVER['REQUEST_METHOD'] == 'POST'));
70 // update list if method is POST or PUT and $this->delete is not true
71 $this->update = (!$this->delete &&
72 in_array($_SERVER['REQUEST_METHOD'], array('POST', 'PUT')));
74 $this->user = $this->getTargetUser($this->arg('user'));
75 $this->list = $this->getTargetList($this->arg('user'), $this->arg('id'));
77 if (empty($this->list)) {
78 // TRANS: Client error displayed when referring to a non-existing list.
79 $this->clientError(_('List not found.'), 404);
88 * @return boolean success flag
90 protected function handle()
95 $this->handleDelete();
104 switch($this->format) {
106 $this->showSingleXmlList($this->list);
109 $this->showSingleJsonList($this->list);
112 // TRANS: Client error displayed when coming across a non-supported API method.
113 $this->clientError(_('API method not found.'), 404);
118 * require authentication if it is a write action or user is ambiguous
121 function requiresAuth()
123 return parent::requiresAuth() ||
124 $this->create || $this->delete;
130 * @return boolean success
134 if($this->auth_user->id != $this->list->tagger) {
135 // TRANS: Client error displayed when trying to update another user's list.
136 $this->clientError(_('You cannot update lists that do not belong to you.'), 401);
139 $new_list = clone($this->list);
140 $new_list->tag = common_canonical_tag($this->arg('name'));
141 $new_list->description = common_canonical_tag($this->arg('description'));
142 $new_list->private = ($this->arg('mode') === 'private') ? true : false;
144 $result = $new_list->update($this->list);
147 // TRANS: Client error displayed when an unknown error occurs updating a list.
148 $this->clientError(_('An error occured.'), 503);
151 switch($this->format) {
153 $this->showSingleXmlList($new_list);
156 $this->showSingleJsonList($new_list);
159 // TRANS: Client error displayed when coming across a non-supported API method.
160 $this->clientError(_('API method not found.'), 404);
167 * @return boolean success
169 function handleDelete()
171 if($this->auth_user->id != $this->list->tagger) {
172 // TRANS: Client error displayed when trying to delete another user's list.
173 $this->clientError(_('You cannot delete lists that do not belong to you.'), 401);
176 $record = clone($this->list);
177 $this->list->delete();
179 switch($this->format) {
181 $this->showSingleXmlList($record);
184 $this->showSingleJsonList($record);
187 // TRANS: Client error displayed when coming across a non-supported API method.
188 $this->clientError(_('API method not found.'), 404);
193 * Indicate that this resource is not read-only.
195 * @return boolean is_read-only=false
197 function isReadOnly(array $args=array())
203 * When was the list (people tag) last updated?
205 * @return String time_last_modified
207 function lastModified()
209 if(!empty($this->list)) {
210 return strtotime($this->list->modified);
216 * An entity tag for this list
218 * Returns an Etag based on the action name, language, user ID and
219 * timestamps of the first and last list the user has joined
221 * @return string etag
225 if (!empty($this->list)) {
227 return '"' . implode(
229 array($this->arg('action'),
232 strtotime($this->list->created),
233 strtotime($this->list->modified))