]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apilists.php
Merge branch 'lists_fixes' into 1.0.x
[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 class ApiListsAction extends ApiBareAuthAction
46 {
47     var $lists   = null;
48     var $cursor = 0;
49     var $next_cursor = 0;
50     var $prev_cursor = 0;
51     var $create = false;
52
53     /**
54      * Set the flags for handling the request. List lists created by user if this
55      * is a GET request, create a new list if it is a POST request.
56      *
57      * Takes parameters:
58      *     - user: the user id or nickname
59      * Parameters for POST request
60      *     - name: name of the new list (the people tag itself)
61      *     - mode: (optional) mode for the new list private/public
62      *     - description: (optional) description for the list
63      *
64      * @return boolean success flag
65      */
66     function prepare($args)
67     {
68         parent::prepare($args);
69
70         $this->create = ($_SERVER['REQUEST_METHOD'] == 'POST');
71
72         if (!$this->create) {
73
74             $this->user = $this->getTargetUser($this->arg('user'));
75
76             if (empty($this->user)) {
77                 // TRANS: Client error displayed trying to perform an action related to a non-existing user.
78                 $this->clientError(_('No such user.'), 404, $this->format);
79                 return false;
80             }
81             $this->getLists();
82         }
83
84         return true;
85     }
86
87     /**
88      * require authentication if it is a write action or user is ambiguous
89      *
90      */
91     function requiresAuth()
92     {
93         return parent::requiresAuth() ||
94             $this->create || $this->delete;
95     }
96
97     /**
98      * Handle request:
99      *     Show the lists the user has created if the request method is GET
100      *     Create a new list by diferring to handlePost() if it is POST.
101      */
102     function handle($args)
103     {
104         parent::handle($args);
105
106         if($this->create) {
107             return $this->handlePost();
108         }
109
110         switch($this->format) {
111         case 'xml':
112             $this->showXmlLists($this->lists, $this->next_cursor, $this->prev_cursor);
113             break;
114         case 'json':
115             $this->showJsonLists($this->lists, $this->next_cursor, $this->prev_cursor);
116             break;
117         default:
118             $this->clientError(
119                 // TRANS: Client error displayed when coming across a non-supported API method.
120                 _('API method not found.'),
121                 404,
122                 $this->format
123             );
124             break;
125         }
126     }
127
128     /**
129      * Create a new list
130      *
131      * @return boolean success
132      */
133     function handlePost()
134     {
135         $name=$this->arg('name');
136         if(empty($name)) {
137             // mimick twitter
138             // TRANS: Client error displayed when trying to create a list without a name.
139             print _("A list must have a name.");
140             exit(1);
141         }
142
143         // twitter creates a new list by appending a number to the end
144         // if the list by the given name already exists
145         // it makes more sense to return the existing list instead
146
147         $private = null;
148         if ($this->arg('mode') === 'public') {
149             $private = false;
150         } else if ($this->arg('mode') === 'private') {
151             $private = true;
152         }
153
154         $list = Profile_list::ensureTag($this->auth_user->id,
155                                         $this->arg('name'),
156                                         $this->arg('description'),
157                                         $private);
158         if (empty($list)) {
159             return false;
160         }
161
162         switch($this->format) {
163         case 'xml':
164             $this->showSingleXmlList($list);
165             break;
166         case 'json':
167             $this->showSingleJsonList($list);
168             break;
169         default:
170             $this->clientError(
171                 // TRANS: Client error displayed when coming across a non-supported API method.
172                 _('API method not found.'),
173                 404,
174                 $this->format
175             );
176             break;
177         }
178         return true;
179     }
180
181     /**
182      * Get lists
183      */
184     function getLists()
185     {
186         $cursor = (int) $this->arg('cursor', -1);
187
188         // twitter fixes count at 20
189         // there is no argument named count
190         $count = 20;
191         $profile = $this->user->getProfile();
192         $fn = array($profile, 'getLists');
193
194         list($this->lists,
195              $this->next_cursor,
196              $this->prev_cursor) = Profile_list::getAtCursor($fn, array($this->auth_user), $cursor, $count);
197     }
198
199     function isReadOnly($args)
200     {
201         return false;
202     }
203
204     function lastModified()
205     {
206         if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
207             return strtotime($this->lists[0]->created);
208         }
209
210         return null;
211     }
212
213     /**
214      * An entity tag for this list of lists
215      *
216      * Returns an Etag based on the action name, language, user ID and
217      * timestamps of the first and last list the user has joined
218      *
219      * @return string etag
220      */
221     function etag()
222     {
223         if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
224
225             $last = count($this->lists) - 1;
226
227             return '"' . implode(
228                 ':',
229                 array($this->arg('action'),
230                       common_language(),
231                       $this->user->id,
232                       strtotime($this->lists[0]->created),
233                       strtotime($this->lists[$last]->created))
234             )
235             . '"';
236         }
237
238         return null;
239     }
240 }