]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinepublic.php
Add docs for public_timeline
[quix0rs-gnu-social.git] / actions / apitimelinepublic.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the public timeline
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    Craig Andrews <candrews@integralblue.com>
25  * @author    Evan Prodromou <evan@status.net>
26  * @author    Jeffery To <jeffery.to@gmail.com>
27  * @author    mac65 <mac65@mac65.com>
28  * @author    Mike Cochrane <mikec@mikenz.geek.nz>
29  * @author    Robin Millette <robin@millette.info>
30  * @author    Zach Copley <zach@status.net>
31  * @copyright 2009 StatusNet, Inc.
32  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
33  * @link      http://status.net/
34  */
35
36 if (!defined('STATUSNET')) {
37     exit(1);
38 }
39
40 require_once INSTALLDIR . '/lib/apiprivateauth.php';
41
42 /**
43  * Returns the most recent notices (default 20) posted by everybody
44  *
45  * @category API
46  * @package  StatusNet
47  * @author   Craig Andrews <candrews@integralblue.com>
48  * @author   Evan Prodromou <evan@status.net>
49  * @author   Jeffery To <jeffery.to@gmail.com>
50  * @author   mac65 <mac65@mac65.com>
51  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
52  * @author   Robin Millette <robin@millette.info>
53  * @author   Zach Copley <zach@status.net>
54  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
55  * @link     http://status.net/
56  */
57
58 /* External API usage documentation. Please update when you change how this method works. */
59
60 /*! @page publictimeline statuses/public_timeline
61
62     @section Description
63     Returns the 20 most recent notices from users throughout the system who have
64     uploaded their own avatars. Depending on configuration, it may or may not
65     not include notices from automatic posting services.
66
67     @par URL patterns
68     @li /api/statuses/public_timeline.:format
69
70     @par Formats (:format)
71     xml, json, rss, atom
72
73     @par HTTP Method(s)
74     GET
75
76     @par Requires Authentication
77     No
78
79     @param since_id (Optional) Returns only statuses with an ID greater
80     than (that is, more recent than) the specified ID.
81     @param max_id (Optional) Returns only statuses with an ID less than
82     (that is, older than) or equal to the specified ID.
83     @param count (Optional) Specifies the number of statuses to retrieve.
84     @param page (Optional) Specifies the page of results to retrieve.
85
86     @sa @ref apiroot
87
88     @subsection usagenotes Usage notes
89     @li The URL pattern is relative to the @ref apiroot.
90     @li The XML response uses <a href="http://georss.org/Main_Page">GeoRSS</a>
91     to encode the latitude and longitude (see example response below <georss:point>).
92
93     @subsection exampleusage Example usage
94
95     @verbatim
96     curl http://identi.ca/api/statuses/friends_timeline/evan.xml?count=1&page=2
97     @endverbatim
98
99     @subsection exampleresponse Example response
100
101     @verbatim
102     <?xml version="1.0" encoding="UTF-8"?>
103     <statuses type="array">
104      <status>
105       <text>@skwashd oh, commbank reenabled me super quick both times. but disconcerting when you don't expect it though</text>
106       <truncated>false</truncated>
107       <created_at>Sat Apr 17 00:49:12 +0000 2010</created_at>
108       <in_reply_to_status_id>28838393</in_reply_to_status_id>
109       <source>xmpp</source>
110       <id>28838456</id>
111       <in_reply_to_user_id>39303</in_reply_to_user_id>
112       <in_reply_to_screen_name>skwashd</in_reply_to_screen_name>
113       <geo></geo>
114       <favorited>false</favorited>
115       <user>
116        <id>44517</id>
117        <name>joshua may</name>
118        <screen_name>notjosh</screen_name>
119        <location></location>
120        <description></description>
121        <profile_image_url>http://avatar.identi.ca/44517-48-20090321004106.jpeg</profile_image_url>
122        <url></url>
123        <protected>false</protected>
124        <followers_count>17</followers_count>
125        <profile_background_color></profile_background_color>
126        <profile_text_color></profile_text_color>
127        <profile_link_color></profile_link_color>
128        <profile_sidebar_fill_color></profile_sidebar_fill_color>
129        <profile_sidebar_border_color></profile_sidebar_border_color>
130        <friends_count>20</friends_count>
131        <created_at>Sat Mar 21 00:40:25 +0000 2009</created_at>
132        <favourites_count>0</favourites_count>
133        <utc_offset>0</utc_offset>
134        <time_zone>UTC</time_zone>
135        <profile_background_image_url></profile_background_image_url>
136        <profile_background_tile>false</profile_background_tile>
137        <statuses_count>100</statuses_count>
138        <following>false</following>
139        <notifications>false</notifications>
140     </user>
141     </status>
142     [....]
143     </statuses>
144 @endverbatim
145 */
146
147 class ApiTimelinePublicAction extends ApiPrivateAuthAction
148 {
149
150     var $notices = null;
151
152     /**
153      * Take arguments for running
154      *
155      * @param array $args $_REQUEST args
156      *
157      * @return boolean success flag
158      *
159      */
160
161     function prepare($args)
162     {
163         parent::prepare($args);
164
165         $this->notices = $this->getNotices();
166
167         return true;
168     }
169
170     /**
171      * Handle the request
172      *
173      * Just show the notices
174      *
175      * @param array $args $_REQUEST data (unused)
176      *
177      * @return void
178      */
179
180     function handle($args)
181     {
182         parent::handle($args);
183         $this->showTimeline();
184     }
185
186     /**
187      * Show the timeline of notices
188      *
189      * @return void
190      */
191
192     function showTimeline()
193     {
194         $sitename   = common_config('site', 'name');
195         $sitelogo   = (common_config('site', 'logo')) ? common_config('site', 'logo') : Theme::path('logo.png');
196         $title      = sprintf(_("%s public timeline"), $sitename);
197         $taguribase = TagURI::base();
198         $id         = "tag:$taguribase:PublicTimeline";
199         $link       = common_local_url('public');
200         $self       = $this->getSelfUri();
201         $subtitle   = sprintf(_("%s updates from everyone!"), $sitename);
202
203         switch($this->format) {
204         case 'xml':
205             $this->showXmlTimeline($this->notices);
206             break;
207         case 'rss':
208             $this->showRssTimeline(
209                 $this->notices,
210                 $title,
211                 $link,
212                 $subtitle,
213                 null,
214                 $sitelogo,
215                 $self
216             );
217             break;
218         case 'atom':
219
220             header('Content-Type: application/atom+xml; charset=utf-8');
221
222             $atom = new AtomNoticeFeed();
223
224             $atom->setId($id);
225             $atom->setTitle($title);
226             $atom->setSubtitle($subtitle);
227             $atom->setLogo($sitelogo);
228             $atom->setUpdated('now');
229             $atom->addLink(common_local_url('public'));
230             $atom->setSelfLink($self);
231             $atom->addEntryFromNotices($this->notices);
232
233             $this->raw($atom->getString());
234
235             break;
236         case 'json':
237             $this->showJsonTimeline($this->notices);
238             break;
239         default:
240             $this->clientError(_('API method not found.'), $code = 404);
241             break;
242         }
243     }
244
245     /**
246      * Get notices
247      *
248      * @return array notices
249      */
250
251     function getNotices()
252     {
253         $notices = array();
254
255         $notice = Notice::publicStream(
256             ($this->page - 1) * $this->count, $this->count, $this->since_id,
257             $this->max_id
258         );
259
260         while ($notice->fetch()) {
261             $notices[] = clone($notice);
262         }
263
264         return $notices;
265     }
266
267     /**
268      * Is this action read only?
269      *
270      * @param array $args other arguments
271      *
272      * @return boolean true
273      */
274
275     function isReadOnly($args)
276     {
277         return true;
278     }
279
280     /**
281      * When was this feed last modified?
282      *
283      * @return string datestamp of the latest notice in the stream
284      */
285
286     function lastModified()
287     {
288         if (!empty($this->notices) && (count($this->notices) > 0)) {
289             return strtotime($this->notices[0]->created);
290         }
291
292         return null;
293     }
294
295     /**
296      * An entity tag for this stream
297      *
298      * Returns an Etag based on the action name, language, and
299      * timestamps of the first and last notice in the timeline
300      *
301      * @return string etag
302      */
303
304     function etag()
305     {
306         if (!empty($this->notices) && (count($this->notices) > 0)) {
307
308             $last = count($this->notices) - 1;
309
310             return '"' . implode(
311                 ':',
312                 array($this->arg('action'),
313                       common_language(),
314                       strtotime($this->notices[0]->created),
315                       strtotime($this->notices[$last]->created))
316             )
317             . '"';
318         }
319
320         return null;
321     }
322
323 }