]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinehome.php
7ef3da79f0de65c11471623fd693d392c5dcfd31
[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         $sitename   = common_config('site', 'name');
109         // TRANS: Timeline title for user and friends. %s is a user nickname.
110         $title      = sprintf(_("%s and friends"), $this->user->nickname);
111         $taguribase = TagURI::base();
112         $id         = "tag:$taguribase:HomeTimeline:" . $this->user->id;
113
114         $subtitle   = sprintf(
115             // TRANS: Message is used as a subtitle. %1$s is a user nickname, %2$s is a site name.
116             _('Updates from %1$s and friends on %2$s!'),
117             $this->user->nickname, $sitename
118         );
119
120         $logo = $profile->avatarUrl(AVATAR_PROFILE_SIZE);
121         $link = common_local_url('all',
122                     array('nickname' => $this->user->nickname));
123         $self = $this->getSelfUri();
124
125         switch($this->format) {
126         case 'xml':
127             $this->showXmlTimeline($this->notices);
128             break;
129         case 'rss':
130             $this->showRssTimeline(
131                 $this->notices,
132                 $title,
133                 $link,
134                 $subtitle,
135                 null,
136                 $logo,
137                 $self
138             );
139             break;
140         case 'atom':
141
142             header('Content-Type: application/atom+xml; charset=utf-8');
143
144             $atom = new AtomNoticeFeed($this->auth_user);
145
146             $atom->setId($id);
147             $atom->setTitle($title);
148             $atom->setSubtitle($subtitle);
149             $atom->setLogo($logo);
150             $atom->setUpdated('now');
151
152             $atom->addLink($link);
153             $atom->setSelfLink($self);
154
155             $atom->addEntryFromNotices($this->notices);
156             $this->raw($atom->getString());
157
158             break;
159         case 'json':
160             $this->showJsonTimeline($this->notices);
161             break;
162         case 'as':
163             header('Content-Type: ' . ActivityStreamJSONDocument::CONTENT_TYPE);
164             $doc = new ActivityStreamJSONDocument($this->auth_user);
165             $doc->setTitle($title);
166             $doc->addLink($link, 'alternate', 'text/html');
167             $doc->addItemsFromNotices($this->notices);
168             $this->raw($doc->asString());
169             break;
170         default:
171             // TRANS: Client error displayed when coming across a non-supported API method.
172             $this->clientError(_('API method not found.'), $code = 404);
173             break;
174         }
175     }
176
177     /**
178      * Get notices
179      *
180      * @return array notices
181      */
182     function getNotices()
183     {
184         $notices = array();
185
186         $profile = null;
187
188         if (isset($this->auth_user)) {
189             $profile = $this->auth_user->getProfile();
190         }
191
192         $stream = new InboxNoticeStream($this->user, $profile);
193         
194         $notice = $stream->getNotices(($this->page-1) * $this->count,
195                                       $this->count,
196                                       $this->since_id,
197                                       $this->max_id);
198
199         while ($notice->fetch()) {
200             $notices[] = clone($notice);
201         }
202
203         return $notices;
204     }
205
206     /**
207      * Is this action read only?
208      *
209      * @param array $args other arguments
210      *
211      * @return boolean true
212      */
213     function isReadOnly($args)
214     {
215         return true;
216     }
217
218     /**
219      * When was this feed last modified?
220      *
221      * @return string datestamp of the latest notice in the stream
222      */
223     function lastModified()
224     {
225         if (!empty($this->notices) && (count($this->notices) > 0)) {
226             return strtotime($this->notices[0]->created);
227         }
228
229         return null;
230     }
231
232     /**
233      * An entity tag for this stream
234      *
235      * Returns an Etag based on the action name, language, user ID, and
236      * timestamps of the first and last notice in the timeline
237      *
238      * @return string etag
239      */
240     function etag()
241     {
242         if (!empty($this->notices) && (count($this->notices) > 0)) {
243
244             $last = count($this->notices) - 1;
245
246             return '"' . implode(
247                 ':',
248                 array($this->arg('action'),
249                       common_user_cache_hash($this->auth_user),
250                       common_language(),
251                       $this->user->id,
252                       strtotime($this->notices[0]->created),
253                       strtotime($this->notices[$last]->created))
254             )
255             . '"';
256         }
257
258         return null;
259     }
260 }