3 * StatusNet, the distributed open-source microblogging tool
5 * List existing lists or create a new 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')) {
34 * Action handler for Twitter list_memeber methods
38 * @author Shashi Gowda <connect2shashi@gmail.com>
39 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
40 * @link http://status.net/
41 * @see ApiBareAuthAction
43 class ApiListsAction extends ApiBareAuthAction
52 * Set the flags for handling the request. List lists created by user if this
53 * is a GET request, create a new list if it is a POST request.
56 * - user: the user id or nickname
57 * Parameters for POST request
58 * - name: name of the new list (the people tag itself)
59 * - mode: (optional) mode for the new list private/public
60 * - description: (optional) description for the list
62 * @return boolean success flag
64 protected function prepare(array $args=array())
66 parent::prepare($args);
68 $this->create = ($_SERVER['REQUEST_METHOD'] == 'POST');
72 $this->user = $this->getTargetUser($this->arg('user'));
74 if (!($user instanceof User)) {
75 // TRANS: Client error displayed trying to perform an action related to a non-existing user.
76 $this->clientError(_('No such user.'), 404);
78 $this->target = $user->getProfile();
86 * require authentication if it is a write action or user is ambiguous
89 function requiresAuth()
91 return parent::requiresAuth() ||
92 $this->create || $this->delete;
97 * Show the lists the user has created if the request method is GET
98 * Create a new list by diferring to handlePost() if it is POST.
100 protected function handle()
105 return $this->handlePost();
108 switch($this->format) {
110 $this->showXmlLists($this->lists, $this->next_cursor, $this->prev_cursor);
113 $this->showJsonLists($this->lists, $this->next_cursor, $this->prev_cursor);
117 // TRANS: Client error displayed when coming across a non-supported API method.
118 _('API method not found.'),
129 * @return boolean success
131 function handlePost()
133 $name=$this->arg('name');
136 // TRANS: Client error displayed when trying to create a list without a name.
137 print _("A list must have a name.");
141 // twitter creates a new list by appending a number to the end
142 // if the list by the given name already exists
143 // it makes more sense to return the existing list instead
146 if ($this->arg('mode') === 'public') {
148 } else if ($this->arg('mode') === 'private') {
152 $list = Profile_list::ensureTag($this->auth_user->id,
154 $this->arg('description'),
160 switch($this->format) {
162 $this->showSingleXmlList($list);
165 $this->showSingleJsonList($list);
168 // TRANS: Client error displayed when coming across a non-supported API method.
169 $this->clientError(_('API method not found.'), 404);
179 $cursor = (int) $this->arg('cursor', -1);
181 // twitter fixes count at 20
182 // there is no argument named count
184 $fn = array($this->target, 'getLists');
188 $this->prev_cursor) = Profile_list::getAtCursor($fn, array($this->scoped), $cursor, $count);
191 function isReadOnly($args)
196 function lastModified()
198 if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
199 return strtotime($this->lists[0]->created);
206 * An entity tag for this list of lists
208 * Returns an Etag based on the action name, language, user ID and
209 * timestamps of the first and last list the user has joined
211 * @return string etag
215 if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
217 $last = count($this->lists) - 1;
219 return '"' . implode(
221 array($this->arg('action'),
224 strtotime($this->lists[0]->created),
225 strtotime($this->lists[$last]->created))