]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinehome.php
Removing unnecessary require_once lines (autoload!)
[quix0rs-gnu-social.git] / actions / apitimelinehome.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show the home 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  * @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 if (!defined('STATUSNET')) {
38     exit(1);
39 }
40
41 /**
42  * Returns the most recent notices (default 20) posted by the target user.
43  * This is the equivalent of 'You and friends' page accessed via Web.
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 class ApiTimelineHomeAction extends ApiBareAuthAction
58 {
59     var $notices  = null;
60
61     /**
62      * Take arguments for running
63      *
64      * @param array $args $_REQUEST args
65      *
66      * @return boolean success flag
67      */
68     function prepare($args)
69     {
70         parent::prepare($args);
71
72         $this->user = $this->getTargetUser($this->arg('id'));
73
74         if (empty($this->user)) {
75             // TRANS: Client error displayed when requesting most recent dents by user and friends for a non-existing user.
76             $this->clientError(_('No such user.'), 404, $this->format);
77             return;
78         }
79
80         $this->notices = $this->getNotices();
81
82         return true;
83     }
84
85     /**
86      * Handle the request
87      *
88      * Just show the notices
89      *
90      * @param array $args $_REQUEST data (unused)
91      *
92      * @return void
93      */
94     function handle($args)
95     {
96         parent::handle($args);
97         $this->showTimeline();
98     }
99
100     /**
101      * Show the timeline of notices
102      *
103      * @return void
104      */
105     function showTimeline()
106     {
107         $profile    = $this->user->getProfile();
108         $avatar     = $profile->getAvatar(AVATAR_PROFILE_SIZE);
109         $sitename   = common_config('site', 'name');
110         // TRANS: Timeline title for user and friends. %s is a user nickname.
111         $title      = sprintf(_("%s and friends"), $this->user->nickname);
112         $taguribase = TagURI::base();
113         $id         = "tag:$taguribase:HomeTimeline:" . $this->user->id;
114
115         $subtitle   = sprintf(
116             // TRANS: Message is used as a subtitle. %1$s is a user nickname, %2$s is a site name.
117             _('Updates from %1$s and friends on %2$s!'),
118             $this->user->nickname, $sitename
119         );
120
121         $link = common_local_url(
122             'all',
123             array('nickname' => $this->user->nickname)
124         );
125
126         $self = $this->getSelfUri();
127
128         $logo = (!empty($avatar))
129             ? $avatar->displayUrl()
130             : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
131
132         switch($this->format) {
133         case 'xml':
134             $this->showXmlTimeline($this->notices);
135             break;
136         case 'rss':
137             $this->showRssTimeline(
138                 $this->notices,
139                 $title,
140                 $link,
141                 $subtitle,
142                 null,
143                 $logo,
144                 $self
145             );
146             break;
147         case 'atom':
148
149             header('Content-Type: application/atom+xml; charset=utf-8');
150
151             $atom = new AtomNoticeFeed($this->auth_user);
152
153             $atom->setId($id);
154             $atom->setTitle($title);
155             $atom->setSubtitle($subtitle);
156             $atom->setLogo($logo);
157             $atom->setUpdated('now');
158
159             $atom->addLink($link);
160             $atom->setSelfLink($self);
161
162             $atom->addEntryFromNotices($this->notices);
163             $this->raw($atom->getString());
164
165             break;
166         case 'json':
167             $this->showJsonTimeline($this->notices);
168             break;
169         case 'as':
170             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
171             $doc = new ActivityStreamJSONDocument($this->auth_user);
172             $doc->setTitle($title);
173             $doc->addLink($link, 'alternate', 'text/html');
174             $doc->addItemsFromNotices($this->notices);
175             $this->raw($doc->asString());
176             break;
177         default:
178             // TRANS: Client error displayed when coming across a non-supported API method.
179             $this->clientError(_('API method not found.'), $code = 404);
180             break;
181         }
182     }
183
184     /**
185      * Get notices
186      *
187      * @return array notices
188      */
189     function getNotices()
190     {
191         $notices = array();
192
193         $profile = null;
194
195         if (isset($this->auth_user)) {
196             $profile = $this->auth_user->getProfile();
197         }
198
199         $stream = new InboxNoticeStream($this->user, $profile);
200         
201         $notice = $stream->getNotices(($this->page-1) * $this->count,
202                                       $this->count,
203                                       $this->since_id,
204                                       $this->max_id);
205
206         while ($notice->fetch()) {
207             $notices[] = clone($notice);
208         }
209
210         return $notices;
211     }
212
213     /**
214      * Is this action read only?
215      *
216      * @param array $args other arguments
217      *
218      * @return boolean true
219      */
220     function isReadOnly($args)
221     {
222         return true;
223     }
224
225     /**
226      * When was this feed last modified?
227      *
228      * @return string datestamp of the latest notice in the stream
229      */
230     function lastModified()
231     {
232         if (!empty($this->notices) && (count($this->notices) > 0)) {
233             return strtotime($this->notices[0]->created);
234         }
235
236         return null;
237     }
238
239     /**
240      * An entity tag for this stream
241      *
242      * Returns an Etag based on the action name, language, user ID, and
243      * timestamps of the first and last notice in the timeline
244      *
245      * @return string etag
246      */
247     function etag()
248     {
249         if (!empty($this->notices) && (count($this->notices) > 0)) {
250
251             $last = count($this->notices) - 1;
252
253             return '"' . implode(
254                 ':',
255                 array($this->arg('action'),
256                       common_user_cache_hash($this->auth_user),
257                       common_language(),
258                       $this->user->id,
259                       strtotime($this->notices[0]->created),
260                       strtotime($this->notices[$last]->created))
261             )
262             . '"';
263         }
264
265         return null;
266     }
267 }