]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apilists.php
Merge remote-tracking branch 'upstream/master' into social-master
[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 /**
34  * Action handler for Twitter list_memeber methods
35  *
36  * @category API
37  * @package  StatusNet
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
42  */
43 class ApiListsAction extends ApiBareAuthAction
44 {
45     var $lists   = null;
46     var $cursor = 0;
47     var $next_cursor = 0;
48     var $prev_cursor = 0;
49     var $create = false;
50
51     /**
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.
54      *
55      * Takes parameters:
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
61      *
62      * @return boolean success flag
63      */
64     protected function prepare(array $args=array())
65     {
66         parent::prepare($args);
67
68         $this->create = ($_SERVER['REQUEST_METHOD'] == 'POST');
69
70         if (!$this->create) {
71
72             $this->user = $this->getTargetUser($this->arg('user'));
73
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);
77             }
78             $this->target = $user->getProfile();
79             $this->getLists();
80         }
81
82         return true;
83     }
84
85     /**
86      * require authentication if it is a write action or user is ambiguous
87      *
88      */
89     function requiresAuth()
90     {
91         return parent::requiresAuth() ||
92             $this->create || $this->delete;
93     }
94
95     /**
96      * Handle request:
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.
99      */
100     protected function handle()
101     {
102         parent::handle();
103
104         if($this->create) {
105             return $this->handlePost();
106         }
107
108         switch($this->format) {
109         case 'xml':
110             $this->showXmlLists($this->lists, $this->next_cursor, $this->prev_cursor);
111             break;
112         case 'json':
113             $this->showJsonLists($this->lists, $this->next_cursor, $this->prev_cursor);
114             break;
115         default:
116             $this->clientError(
117                 // TRANS: Client error displayed when coming across a non-supported API method.
118                 _('API method not found.'),
119                 404,
120                 $this->format
121             );
122             break;
123         }
124     }
125
126     /**
127      * Create a new list
128      *
129      * @return boolean success
130      */
131     function handlePost()
132     {
133         $name=$this->arg('name');
134         if(empty($name)) {
135             // mimick twitter
136             // TRANS: Client error displayed when trying to create a list without a name.
137             print _("A list must have a name.");
138             exit(1);
139         }
140
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
144
145         $private = null;
146         if ($this->arg('mode') === 'public') {
147             $private = false;
148         } else if ($this->arg('mode') === 'private') {
149             $private = true;
150         }
151
152         $list = Profile_list::ensureTag($this->auth_user->id,
153                                         $this->arg('name'),
154                                         $this->arg('description'),
155                                         $private);
156         if (empty($list)) {
157             return false;
158         }
159
160         switch($this->format) {
161         case 'xml':
162             $this->showSingleXmlList($list);
163             break;
164         case 'json':
165             $this->showSingleJsonList($list);
166             break;
167         default:
168             // TRANS: Client error displayed when coming across a non-supported API method.
169             $this->clientError(_('API method not found.'), 404);
170         }
171         return true;
172     }
173
174     /**
175      * Get lists
176      */
177     function getLists()
178     {
179         $cursor = (int) $this->arg('cursor', -1);
180
181         // twitter fixes count at 20
182         // there is no argument named count
183         $count = 20;
184         $fn = array($this->target, 'getLists');
185
186         list($this->lists,
187              $this->next_cursor,
188              $this->prev_cursor) = Profile_list::getAtCursor($fn, array($this->auth_user), $cursor, $count);
189     }
190
191     function isReadOnly(array $args=array())
192     {
193         return false;
194     }
195
196     function lastModified()
197     {
198         if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
199             return strtotime($this->lists[0]->created);
200         }
201
202         return null;
203     }
204
205     /**
206      * An entity tag for this list of lists
207      *
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
210      *
211      * @return string etag
212      */
213     function etag()
214     {
215         if (!$this->create && !empty($this->lists) && (count($this->lists) > 0)) {
216
217             $last = count($this->lists) - 1;
218
219             return '"' . implode(
220                 ':',
221                 array($this->arg('action'),
222                       common_language(),
223                       $this->target->id,
224                       strtotime($this->lists[0]->created),
225                       strtotime($this->lists[$last]->created))
226             )
227             . '"';
228         }
229
230         return null;
231     }
232 }