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