]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apilists.php
Merge remote-tracking branch 'mainline/1.0.x' into people_tags_rebase
[quix0rs-gnu-social.git] / actions / apilists.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * List existing lists or create a new list.
6  *
7  * PHP version 5
8  *
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.
13  *
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.
18  *
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/>.
21  *
22  * @category  API
23  * @package   StatusNet
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/
27  */
28
29 if (!defined('STATUSNET')) {
30     exit(1);
31 }
32
33 require_once INSTALLDIR . '/lib/apibareauth.php';
34
35 /**
36  * Action handler for Twitter list_memeber methods
37  *
38  * @category API
39  * @package  StatusNet
40  * @author   Shashi Gowda <connect2shashi@gmail.com>
41  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
42  * @link     http://status.net/
43  * @see      ApiBareAuthAction
44  */
45
46 class ApiListsAction extends ApiBareAuthAction
47 {
48     var $lists   = null;
49     var $cursor = 0;
50     var $next_cursor = 0;
51     var $prev_cursor = 0;
52     var $create = false;
53
54     /**
55      * Set the flags for handling the request. List lists created by user if this
56      * is a GET request, create a new list if it is a POST request.
57      *
58      * Takes parameters:
59      *     - user: the user id or nickname
60      * Parameters for POST request
61      *     - name: name of the new list (the people tag itself)
62      *     - mode: (optional) mode for the new list private/public
63      *     - description: (optional) description for the list
64      *
65      * @return boolean success flag
66      */
67
68     function prepare($args)
69     {
70         parent::prepare($args);
71
72         $this->create = ($_SERVER['REQUEST_METHOD'] == 'POST');
73
74         if (!$this->create) {
75
76             $this->user = $this->getTargetUser($this->arg('user'));
77
78             if (empty($this->user)) {
79                 $this->clientError(_('No such user.'), 404, $this->format);
80                 return false;
81             }
82             $this->getLists();
83         }
84
85         return true;
86     }
87
88     /**
89      * require authentication if it is a write action or user is ambiguous
90      *
91      */
92
93     function requiresAuth()
94     {
95         return parent::requiresAuth() ||
96             $this->create || $this->delete;
97     }
98
99     /**
100      * Handle request:
101      *     Show the lists the user has created if the request method is GET
102      *     Create a new list by diferring to handlePost() if it is POST.
103      */
104
105     function handle($args)
106     {
107         parent::handle($args);
108
109         if($this->create) {
110             return $this->handlePost();
111         }
112
113         switch($this->format) {
114         case 'xml':
115             $this->showXmlLists($this->lists, $this->next_cursor, $this->prev_cursor);
116             break;
117         case 'json':
118             $this->showJsonLists($this->lists, $this->next_cursor, $this->prev_cursor);
119             break;
120         default:
121             $this->clientError(
122                 _('API method not found.'),
123                 404,
124                 $this->format
125             );
126             break;
127         }
128     }
129
130     /**
131      * Create a new list
132      *
133      * @return boolean success
134      */
135
136     function handlePost()
137     {
138         $name=$this->arg('name');
139         if(empty($name)) {
140             // mimick twitter
141             print _("A list's name can't be blank.");
142             exit(1);
143         }
144
145         // twitter creates a new list by appending a number to the end
146         // if the list by the given name already exists
147         // it makes more sense to return the existing list instead
148
149         $private = null;
150         if ($this->arg('mode') === 'public') {
151             $private = false;
152         } else if ($this->arg('mode') === 'private') {
153             $private = true;
154         }
155
156         $list = Profile_list::ensureTag($this->auth_user->id,
157                                         $this->arg('name'),
158                                         $this->arg('description'),
159                                         $private);
160         if (empty($list)) {
161             return false;
162         }
163
164         switch($this->format) {
165         case 'xml':
166             $this->showSingleXmlList($list);
167             break;
168         case 'json':
169             $this->showSingleJsonList($list);
170             break;
171         default:
172             $this->clientError(
173                 _('API method not found.'),
174                 404,
175                 $this->format
176             );
177             break;
178         }
179         return true;
180     }
181
182     /**
183      * Get lists
184      */
185
186     function getLists()
187     {
188         $cursor = (int) $this->arg('cursor', -1);
189
190         // twitter fixes count at 20
191         // there is no argument named count
192         $count = 20;
193         $profile = $this->user->getProfile();
194         $fn = array($profile, 'getOwnedTags');
195
196         list($this->lists,
197              $this->next_cursor,
198              $this->prev_cursor) = Profile_list::getAtCursor($fn, array($this->auth_user), $cursor, $count);
199     }
200
201     function isReadOnly($args)
202     {
203         return false;
204     }
205
206     function lastModified()
207     {
208         if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
209             return strtotime($this->lists[0]->created);
210         }
211
212         return null;
213     }
214
215     /**
216      * An entity tag for this list of lists
217      *
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
220      *
221      * @return string etag
222      */
223
224     function etag()
225     {
226         if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
227
228             $last = count($this->lists) - 1;
229
230             return '"' . implode(
231                 ':',
232                 array($this->arg('action'),
233                       common_language(),
234                       $this->user->id,
235                       strtotime($this->lists[0]->created),
236                       strtotime($this->lists[$last]->created))
237             )
238             . '"';
239         }
240
241         return null;
242     }
243
244 }