]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinehome.php
Merge branch '1.0.x' into schema-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
60 class ApiTimelineHomeAction extends ApiBareAuthAction
61 {
62     var $notices  = null;
63
64     /**
65      * Take arguments for running
66      *
67      * @param array $args $_REQUEST args
68      *
69      * @return boolean success flag
70      *
71      */
72
73     function prepare($args)
74     {
75         parent::prepare($args);
76
77         $this->user = $this->getTargetUser($this->arg('id'));
78
79         if (empty($this->user)) {
80             $this->clientError(_('No such user.'), 404, $this->format);
81             return;
82         }
83
84         $this->notices = $this->getNotices();
85
86         return true;
87     }
88
89     /**
90      * Handle the request
91      *
92      * Just show the notices
93      *
94      * @param array $args $_REQUEST data (unused)
95      *
96      * @return void
97      */
98
99     function handle($args)
100     {
101         parent::handle($args);
102         $this->showTimeline();
103     }
104
105     /**
106      * Show the timeline of notices
107      *
108      * @return void
109      */
110
111     function showTimeline()
112     {
113         $profile    = $this->user->getProfile();
114         $avatar     = $profile->getAvatar(AVATAR_PROFILE_SIZE);
115         $sitename   = common_config('site', 'name');
116         $title      = sprintf(_("%s and friends"), $this->user->nickname);
117         $taguribase = TagURI::base();
118         $id         = "tag:$taguribase:HomeTimeline:" . $this->user->id;
119
120         $subtitle   = sprintf(
121             // TRANS: Message is used as a subtitle. %1$s is a user nickname, %2$s is a site name.
122             _('Updates from %1$s and friends on %2$s!'),
123             $this->user->nickname, $sitename
124         );
125
126         $link = common_local_url(
127             'all',
128             array('nickname' => $this->user->nickname)
129         );
130
131         $self = $this->getSelfUri();
132
133         $logo = (!empty($avatar))
134             ? $avatar->displayUrl()
135             : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
136
137         switch($this->format) {
138         case 'xml':
139             $this->showXmlTimeline($this->notices);
140             break;
141         case 'rss':
142             $this->showRssTimeline(
143                 $this->notices,
144                 $title,
145                 $link,
146                 $subtitle,
147                 null,
148                 $logo,
149                 $self
150             );
151             break;
152         case 'atom':
153
154             header('Content-Type: application/atom+xml; charset=utf-8');
155
156             $atom = new AtomNoticeFeed($this->auth_user);
157
158             $atom->setId($id);
159             $atom->setTitle($title);
160             $atom->setSubtitle($subtitle);
161             $atom->setLogo($logo);
162             $atom->setUpdated('now');
163
164             $atom->addLink($link);
165             $atom->setSelfLink($self);
166
167             $atom->addEntryFromNotices($this->notices);
168             $this->raw($atom->getString());
169
170             break;
171         case 'json':
172             $this->showJsonTimeline($this->notices);
173             break;
174         default:
175             $this->clientError(_('API method not found.'), $code = 404);
176             break;
177         }
178     }
179
180     /**
181      * Get notices
182      *
183      * @return array notices
184      */
185
186     function getNotices()
187     {
188         $notices = array();
189
190         if (!empty($this->auth_user) && $this->auth_user->id == $this->user->id) {
191             $notice = $this->user->noticeInbox(
192                 ($this->page-1) * $this->count,
193                 $this->count, $this->since_id,
194                 $this->max_id
195             );
196         } else {
197             $notice = $this->user->noticesWithFriends(
198                 ($this->page-1) * $this->count,
199                 $this->count, $this->since_id,
200                 $this->max_id
201             );
202         }
203
204         while ($notice->fetch()) {
205             $notices[] = clone($notice);
206         }
207
208         return $notices;
209     }
210
211     /**
212      * Is this action read only?
213      *
214      * @param array $args other arguments
215      *
216      * @return boolean true
217      */
218
219     function isReadOnly($args)
220     {
221         return true;
222     }
223
224     /**
225      * When was this feed last modified?
226      *
227      * @return string datestamp of the latest notice in the stream
228      */
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
248     function etag()
249     {
250         if (!empty($this->notices) && (count($this->notices) > 0)) {
251
252             $last = count($this->notices) - 1;
253
254             return '"' . implode(
255                 ':',
256                 array($this->arg('action'),
257                       common_user_cache_hash($this->auth_user),
258                       common_language(),
259                       $this->user->id,
260                       strtotime($this->notices[0]->created),
261                       strtotime($this->notices[$last]->created))
262             )
263             . '"';
264         }
265
266         return null;
267     }
268
269 }