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