]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apisubscriptions.php
9dbddbd9bdf3df6691be74da8f148269089652b7
[quix0rs-gnu-social.git] / actions / apisubscriptions.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Base class for showing subscription information in the API
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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apibareauth.php';
35
36 /**
37  * This class outputs a list of profiles as Twitter-style user and status objects.
38  * It is used by the API methods /api/statuses/(friends|followers). To support the
39  * social graph methods it also can output a simple list of IDs.
40  *
41  * @category API
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class ApiSubscriptionsAction extends ApiBareAuthAction
49 {
50
51     var $page     = null;
52     var $count    = null;
53     var $user     = null;
54     var $profiles = null;
55     var $tag      = null;
56     var $lite     = null;
57     var $ids_only = null;
58
59     /**
60      * Take arguments for running
61      *
62      * @param array $args $_REQUEST args
63      *
64      * @return boolean success flag
65      *
66      */
67
68     function prepare($args)
69     {
70         parent::prepare($args);
71
72         $this->page     = (int)$this->arg('page', 1);
73         $this->tag      = $this->arg('tag');
74
75         // Note: Twitter no longer supports 'lite'
76         $this->lite     = $this->arg('lite');
77
78         $this->ids_only = $this->arg('ids_only');
79
80         // If called as a social graph method, show 5000 per page, otherwise 100
81
82         $this->count    = isset($this->ids_only) ?
83             5000 : (int)$this->arg('count', 100);
84
85         $this->user = $this->getTargetUser($this->arg('id'));
86
87         if (empty($this->user)) {
88             $this->clientError(_('No such user!'), 404, $this->format);
89             return false;
90         }
91
92         $this->profiles = $this->getProfiles();
93
94         return true;
95     }
96
97     /**
98      * Handle the request
99      *
100      * Show the profiles
101      *
102      * @param array $args $_REQUEST data (unused)
103      *
104      * @return void
105      */
106
107     function handle($args)
108     {
109         parent::handle($args);
110
111         if (!in_array($this->format, array('xml', 'json'))) {
112             $this->clientError(_('API method not found!'), $code = 404);
113             return;
114         }
115
116         $this->init_document($this->format);
117
118         if (isset($this->ids_only)) {
119             $this->showIds();
120         } else {
121             $this->showProfiles(isset($this->lite) ? false : true);
122         }
123
124         $this->end_document($this->format);
125     }
126
127     /**
128      * Get profiles - should get overrrided
129      *
130      * @return array Profiles
131      */
132
133     function getProfiles()
134     {
135     }
136
137     /**
138      * Is this action read only?
139      *
140      * @param array $args other arguments
141      *
142      * @return boolean true
143      */
144
145     function isReadOnly($args)
146     {
147         return true;
148     }
149
150     /**
151      * When was this feed last modified?
152      *
153      * @return string datestamp of the latest profile in the stream
154      */
155
156     function lastModified()
157     {
158         if (!empty($this->profiles) && (count($this->profiles) > 0)) {
159             return strtotime($this->profiles[0]->created);
160         }
161
162         return null;
163     }
164
165     /**
166      * An entity tag for this action
167      *
168      * Returns an Etag based on the action name, language, user ID, and
169      * timestamps of the first and last profiles in the subscriptions list
170      * There's also an indicator to show whether this action is being called
171      * as /api/statuses/(friends|followers) or /api/(friends|followers)/ids
172      *
173      * @return string etag
174      */
175
176     function etag()
177     {
178         if (!empty($this->profiles) && (count($this->profiles) > 0)) {
179
180             $last = count($this->profiles) - 1;
181
182             return '"' . implode(
183                 ':',
184                 array($this->arg('action'),
185                       common_language(),
186                       $this->user->id,
187                       isset($this->ids_only) ? 'IDs' : 'Profiles',
188                       strtotime($this->profiles[0]->created),
189                       strtotime($this->profiles[$last]->created))
190             )
191             . '"';
192         }
193
194         return null;
195     }
196
197     /**
198      * Show the profiles as Twitter-style useres and statuses
199      *
200      * @param boolean $include_statuses Whether to include the latest status
201      *                                  with each user. Default true.
202      *
203      * @return void
204      */
205
206     function showProfiles($include_statuses = true)
207     {
208         switch ($this->format) {
209         case 'xml':
210             $this->elementStart('users', array('type' => 'array'));
211             foreach ($this->profiles as $profile) {
212                 $this->show_profile(
213                     $profile,
214                     $this->format,
215                     null,
216                     $include_statuses
217                 );
218             }
219             $this->elementEnd('users');
220             break;
221         case 'json':
222             $arrays = array();
223             foreach ($this->profiles as $profile) {
224                 $arrays[] = $this->twitter_user_array(
225                     $profile,
226                     $include_statuses
227                 );
228             }
229             print json_encode($arrays);
230             break;
231         default:
232             $this->clientError(_('Unsupported format.'));
233             break;
234         }
235     }
236
237     /**
238      * Show the IDs of the profiles only. 5000 per page. To support
239      * the 'social graph' methods: /api/(friends|followers)/ids
240      *
241      * @return void
242      */
243
244     function showIds()
245     {
246         switch ($this->format) {
247         case 'xml':
248             $this->elementStart('ids');
249             foreach ($this->profiles as $profile) {
250                 $this->element('id', null, $profile->id);
251             }
252             $this->elementEnd('ids');
253             break;
254         case 'json':
255             $ids = array();
256             foreach ($this->profiles as $profile) {
257                 $ids[] = (int)$profile->id;
258             }
259             print json_encode($ids);
260             break;
261         default:
262             $this->clientError(_('Unsupported format.'));
263             break;
264         }
265     }
266
267 }