]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinefriends.php
Properly unlink all old avatars when deleting/uploading a new
[quix0rs-gnu-social.git] / actions / apitimelinefriends.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the friends 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-2010 StatusNet, Inc.
32  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
33  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
34  * @link      http://status.net/
35  */
36
37 /* External API usage documentation. Please update when you change how this method works. */
38
39 /*! @page friendstimeline statuses/friends_timeline
40
41     @section Description
42     Returns the 20 most recent statuses posted by the authenticating
43     user and that user's friends. This is the equivalent of "You and
44     friends" page in the web interface.
45
46     @par URL patterns
47     @li /api/statuses/friends_timeline.:format
48     @li /api/statuses/friends_timeline/:id.:format
49
50     @par Formats (:format)
51     xml, json, rss, atom
52
53     @par ID (:id)
54     username, user id
55
56     @par HTTP Method(s)
57     GET
58
59     @par Requires Authentication
60     Sometimes (see: @ref authentication)
61
62     @param user_id (Optional) Specifies a user by ID
63     @param screen_name (Optional) Specifies a user by screename (nickname)
64     @param since_id (Optional) Returns only statuses with an ID greater
65     than (that is, more recent than) the specified ID.
66     @param max_id (Optional) Returns only statuses with an ID less than
67     (that is, older than) or equal to the specified ID.
68     @param count (Optional) Specifies the number of statuses to retrieve.
69     @param page (Optional) Specifies the page of results to retrieve.
70
71     @sa @ref authentication
72     @sa @ref apiroot
73
74     @subsection usagenotes Usage notes
75     @li The URL pattern is relative to the @ref apiroot.
76     @li The XML response uses <a href="http://georss.org/Main_Page">GeoRSS</a>
77     to encode the latitude and longitude (see example response below <georss:point>).
78
79     @subsection exampleusage Example usage
80
81     @verbatim
82     curl http://identi.ca/api/statuses/friends_timeline/evan.xml?count=1&page=2
83     @endverbatim
84
85     @subsection exampleresponse Example response
86
87     @verbatim
88     <?xml version="1.0"?>
89     <statuses type="array">
90       <status>
91         <text>back from the !yul !drupal meet with Evolving Web folk, @anarcat, @webchick and others, and an interesting refresher on SQL indexing</text>
92         <truncated>false</truncated>
93         <created_at>Wed Mar 31 01:33:02 +0000 2010</created_at>
94         <in_reply_to_status_id/>
95         <source>&lt;a href="http://code.google.com/p/microblog-purple/"&gt;mbpidgin&lt;/a&gt;</source>
96         <id>26674201</id>
97         <in_reply_to_user_id/>
98         <in_reply_to_screen_name/>
99         <geo/>
100         <favorited>false</favorited>
101         <user>
102           <id>246</id>
103           <name>Mark</name>
104           <screen_name>lambic</screen_name>
105           <location>Montreal, Canada</location>
106           <description>Geek</description>
107           <profile_image_url>http://avatar.identi.ca/246-48-20080702141545.png</profile_image_url>
108           <url>http://lambic.co.uk</url>
109           <protected>false</protected>
110           <followers_count>73</followers_count>
111           <profile_background_color>#F0F2F5</profile_background_color>
112           <profile_text_color/>
113           <profile_link_color>#002E6E</profile_link_color>
114           <profile_sidebar_fill_color>#CEE1E9</profile_sidebar_fill_color>
115           <profile_sidebar_border_color/>
116           <friends_count>58</friends_count>
117           <created_at>Wed Jul 02 14:12:15 +0000 2008</created_at>
118           <favourites_count>2</favourites_count>
119           <utc_offset>-14400</utc_offset>
120           <time_zone>US/Eastern</time_zone>
121           <profile_background_image_url/>
122           <profile_background_tile>false</profile_background_tile>
123           <statuses_count>933</statuses_count>
124           <following>false</following>
125           <notifications>false</notifications>
126         </user>
127       </status>
128     </statuses>
129     @endverbatim
130 */
131
132 if (!defined('STATUSNET')) {
133     exit(1);
134 }
135
136 /**
137  * Returns the most recent notices (default 20) posted by the target user.
138  * This is the equivalent of 'You and friends' page accessed via Web.
139  *
140  * @category API
141  * @package  StatusNet
142  * @author   Craig Andrews <candrews@integralblue.com>
143  * @author   Evan Prodromou <evan@status.net>
144  * @author   Jeffery To <jeffery.to@gmail.com>
145  * @author   mac65 <mac65@mac65.com>
146  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
147  * @author   Robin Millette <robin@millette.info>
148  * @author   Zach Copley <zach@status.net>
149  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
150  * @link     http://status.net/
151  */
152 class ApiTimelineFriendsAction extends ApiBareAuthAction
153 {
154     var $notices  = null;
155
156     /**
157      * Take arguments for running
158      *
159      * @param array $args $_REQUEST args
160      *
161      * @return boolean success flag
162      *
163      */
164     function prepare($args)
165     {
166         parent::prepare($args);
167         $this->user = $this->getTargetUser($this->arg('id'));
168
169         if (empty($this->user)) {
170             // TRANS: Client error displayed when requesting dents of a user and friends for a user that does not exist.
171             $this->clientError(_('No such user.'), 404, $this->format);
172             return;
173         }
174
175         $this->notices = $this->getNotices();
176
177         return true;
178     }
179
180     /**
181      * Handle the request
182      *
183      * Just show the notices
184      *
185      * @param array $args $_REQUEST data (unused)
186      *
187      * @return void
188      */
189     function handle($args)
190     {
191         parent::handle($args);
192         $this->showTimeline();
193     }
194
195     /**
196      * Show the timeline of notices
197      *
198      * @return void
199      */
200     function showTimeline()
201     {
202         $profile    = $this->user->getProfile();
203         $sitename   = common_config('site', 'name');
204         // TRANS: Title of API timeline for a user and friends.
205         // TRANS: %s is a username.
206         $title      = sprintf(_("%s and friends"), $this->user->nickname);
207         $taguribase = TagURI::base();
208         $id         = "tag:$taguribase:FriendsTimeline:" . $this->user->id;
209
210         $subtitle = sprintf(
211             // TRANS: Message is used as a subtitle. %1$s is a user nickname, %2$s is a site name.
212             _('Updates from %1$s and friends on %2$s!'),
213             $this->user->nickname,
214             $sitename
215         );
216
217         $logo = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
218         $link = common_local_url('all',
219                     array('nickname' => $this->user->nickname));
220         $self = $this->getSelfUri();
221
222         switch($this->format) {
223         case 'xml':
224             $this->showXmlTimeline($this->notices);
225             break;
226         case 'rss':
227
228             $this->showRssTimeline(
229                 $this->notices,
230                 $title,
231                 $link,
232                 $subtitle,
233                 null,
234                 $logo,
235                 $self
236             );
237             break;
238         case 'atom':
239             header('Content-Type: application/atom+xml; charset=utf-8');
240
241             $atom = new AtomNoticeFeed($this->auth_user);
242
243             $atom->setId($id);
244             $atom->setTitle($title);
245             $atom->setSubtitle($subtitle);
246             $atom->setLogo($logo);
247             $atom->setUpdated('now');
248             $atom->addLink($link);
249             $atom->setSelfLink($self);
250
251             $atom->addEntryFromNotices($this->notices);
252
253             $this->raw($atom->getString());
254
255             break;
256         case 'json':
257             $this->showJsonTimeline($this->notices);
258             break;
259         case 'as':
260             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
261             $doc = new ActivityStreamJSONDocument($this->auth_user, $title);
262             $doc->addLink($link, 'alternate', 'text/html');
263             $doc->addItemsFromNotices($this->notices);
264             $this->raw($doc->asString());
265             break;
266         default:
267             // TRANS: Client error displayed when coming across a non-supported API method.
268             $this->clientError(_('API method not found.'), 404);
269             break;
270         }
271     }
272
273     /**
274      * Get notices
275      *
276      * @return array notices
277      */
278     function getNotices()
279     {
280         $notices = array();
281
282         $profile = null;
283
284         if (isset($this->auth_user)) {
285             $profile = $this->auth_user->getProfile();
286         }
287
288         $stream = new InboxNoticeStream($this->user, $profile);
289         
290         $notice = $stream->getNotices(($this->page-1) * $this->count,
291                                       $this->count,
292                                       $this->since_id,
293                                       $this->max_id);
294
295         while ($notice->fetch()) {
296             $notices[] = clone($notice);
297         }
298
299         return $notices;
300     }
301
302     /**
303      * Is this action read only?
304      *
305      * @param array $args other arguments
306      *
307      * @return boolean true
308      */
309     function isReadOnly($args)
310     {
311         return true;
312     }
313
314     /**
315      * When was this feed last modified?
316      *
317      * @return string datestamp of the latest notice in the stream
318      */
319     function lastModified()
320     {
321         if (!empty($this->notices) && (count($this->notices) > 0)) {
322             return strtotime($this->notices[0]->created);
323         }
324
325         return null;
326     }
327
328     /**
329      * An entity tag for this stream
330      *
331      * Returns an Etag based on the action name, language, user ID, and
332      * timestamps of the first and last notice in the timeline
333      *
334      * @return string etag
335      */
336     function etag()
337     {
338         if (!empty($this->notices) && (count($this->notices) > 0)) {
339             $last = count($this->notices) - 1;
340
341             return '"' . implode(
342                                  ':',
343                                  array($this->arg('action'),
344                                        common_user_cache_hash($this->auth_user),
345                                        common_language(),
346                                        $this->user->id,
347                                        strtotime($this->notices[0]->created),
348                                        strtotime($this->notices[$last]->created))
349                                  )
350               . '"';
351         }
352
353         return null;
354     }
355 }