]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinehome.php
Merge branch 'instrument' into 0.9.x
[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 require_once INSTALLDIR . '/lib/apibareauth.php';
42
43 /**
44  * Returns the most recent notices (default 20) posted by the target user.
45  * This is the equivalent of 'You and friends' page accessed via Web.
46  *
47  * @category API
48  * @package  StatusNet
49  * @author   Craig Andrews <candrews@integralblue.com>
50  * @author   Evan Prodromou <evan@status.net>
51  * @author   Jeffery To <jeffery.to@gmail.com>
52  * @author   mac65 <mac65@mac65.com>
53  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
54  * @author   Robin Millette <robin@millette.info>
55  * @author   Zach Copley <zach@status.net>
56  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
57  * @link     http://status.net/
58  */
59 class ApiTimelineHomeAction extends ApiBareAuthAction
60 {
61     var $notices  = null;
62
63     /**
64      * Take arguments for running
65      *
66      * @param array $args $_REQUEST args
67      *
68      * @return boolean success flag
69      */
70     function prepare($args)
71     {
72         parent::prepare($args);
73
74         $this->user = $this->getTargetUser($this->arg('id'));
75
76         if (empty($this->user)) {
77             // TRANS: Client error displayed when requesting most recent dents by user and friends for a non-existing user.
78             $this->clientError(_('No such user.'), 404, $this->format);
79             return;
80         }
81
82         $this->notices = $this->getNotices();
83
84         return true;
85     }
86
87     /**
88      * Handle the request
89      *
90      * Just show the notices
91      *
92      * @param array $args $_REQUEST data (unused)
93      *
94      * @return void
95      */
96     function handle($args)
97     {
98         parent::handle($args);
99         $this->showTimeline();
100     }
101
102     /**
103      * Show the timeline of notices
104      *
105      * @return void
106      */
107     function showTimeline()
108     {
109         $profile    = $this->user->getProfile();
110         $avatar     = $profile->getAvatar(AVATAR_PROFILE_SIZE);
111         $sitename   = common_config('site', 'name');
112         // TRANS: Timeline title for user and friends. %s is a user nickname.
113         $title      = sprintf(_("%s and friends"), $this->user->nickname);
114         $taguribase = TagURI::base();
115         $id         = "tag:$taguribase:HomeTimeline:" . $this->user->id;
116
117         $subtitle   = sprintf(
118             // TRANS: Message is used as a subtitle. %1$s is a user nickname, %2$s is a site name.
119             _('Updates from %1$s and friends on %2$s!'),
120             $this->user->nickname, $sitename
121         );
122
123         $link = common_local_url(
124             'all',
125             array('nickname' => $this->user->nickname)
126         );
127
128         $self = $this->getSelfUri();
129
130         $logo = (!empty($avatar))
131             ? $avatar->displayUrl()
132             : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
133
134         switch($this->format) {
135         case 'xml':
136             $this->showXmlTimeline($this->notices);
137             break;
138         case 'rss':
139             $this->showRssTimeline(
140                 $this->notices,
141                 $title,
142                 $link,
143                 $subtitle,
144                 null,
145                 $logo,
146                 $self
147             );
148             break;
149         case 'atom':
150
151             header('Content-Type: application/atom+xml; charset=utf-8');
152
153             $atom = new AtomNoticeFeed($this->auth_user);
154
155             $atom->setId($id);
156             $atom->setTitle($title);
157             $atom->setSubtitle($subtitle);
158             $atom->setLogo($logo);
159             $atom->setUpdated('now');
160
161             $atom->addLink($link);
162             $atom->setSelfLink($self);
163
164             $atom->addEntryFromNotices($this->notices);
165             $this->raw($atom->getString());
166
167             break;
168         case 'json':
169             $this->showJsonTimeline($this->notices);
170             break;
171         default:
172             // TRANS: Client error displayed when trying to handle an unknown API method.
173             $this->clientError(_('API method not found.'), $code = 404);
174             break;
175         }
176     }
177
178     /**
179      * Get notices
180      *
181      * @return array notices
182      */
183     function getNotices()
184     {
185         $notices = array();
186
187         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
188             $notice = $this->user->noticeInbox(
189                 ($this->page-1) * $this->count,
190                 $this->count, $this->since_id,
191                 $this->max_id
192             );
193         } else {
194             $notice = $this->user->noticesWithFriends(
195                 ($this->page-1) * $this->count,
196                 $this->count, $this->since_id,
197                 $this->max_id
198             );
199         }
200
201         while ($notice->fetch()) {
202             $notices[] = clone($notice);
203         }
204
205         return $notices;
206     }
207
208     /**
209      * Is this action read only?
210      *
211      * @param array $args other arguments
212      *
213      * @return boolean true
214      */
215     function isReadOnly($args)
216     {
217         return true;
218     }
219
220     /**
221      * When was this feed last modified?
222      *
223      * @return string datestamp of the latest notice in the stream
224      */
225     function lastModified()
226     {
227         if (!empty($this->notices) && (count($this->notices) > 0)) {
228             return strtotime($this->notices[0]->created);
229         }
230
231         return null;
232     }
233
234     /**
235      * An entity tag for this stream
236      *
237      * Returns an Etag based on the action name, language, user ID, and
238      * timestamps of the first and last notice in the timeline
239      *
240      * @return string etag
241      */
242     function etag()
243     {
244         if (!empty($this->notices) && (count($this->notices) > 0)) {
245
246             $last = count($this->notices) - 1;
247
248             return '"' . implode(
249                 ':',
250                 array($this->arg('action'),
251                       common_user_cache_hash($this->auth_user),
252                       common_language(),
253                       $this->user->id,
254                       strtotime($this->notices[0]->created),
255                       strtotime($this->notices[$last]->created))
256             )
257             . '"';
258         }
259
260         return null;
261     }
262 }