]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelineuser.php
Merge remote branch 'origin/master' into twitstream
[quix0rs-gnu-social.git] / actions / apitimelineuser.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a user's 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 authenticating
45  * user. Another user's timeline can be requested via the id parameter. This
46  * is the API equivalent of the user profile web page.
47  *
48  * @category API
49  * @package  StatusNet
50  * @author   Craig Andrews <candrews@integralblue.com>
51  * @author   Evan Prodromou <evan@status.net>
52  * @author   Jeffery To <jeffery.to@gmail.com>
53  * @author   mac65 <mac65@mac65.com>
54  * @author   Mike Cochrane <mikec@mikenz.geek.nz>
55  * @author   Robin Millette <robin@millette.info>
56  * @author   Zach Copley <zach@status.net>
57  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
58  * @link     http://status.net/
59  */
60 class ApiTimelineUserAction 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     function prepare($args)
72     {
73         parent::prepare($args);
74
75         $this->user = $this->getTargetUser($this->arg('id'));
76
77         if (empty($this->user)) {
78             // TRANS: Client error displayed requesting most recent notices for a non-existing user.
79             $this->clientError(_('No such user.'), 404, $this->format);
80             return;
81         }
82
83         $this->notices = $this->getNotices();
84
85         return true;
86     }
87
88     /**
89      * Handle the request
90      *
91      * Just show the notices
92      *
93      * @param array $args $_REQUEST data (unused)
94      *
95      * @return void
96      */
97     function handle($args)
98     {
99         parent::handle($args);
100         $this->showTimeline();
101     }
102
103     /**
104      * Show the timeline of notices
105      *
106      * @return void
107      */
108     function showTimeline()
109     {
110         $profile = $this->user->getProfile();
111
112         // We'll use the shared params from the Atom stub
113         // for other feed types.
114         $atom = new AtomUserNoticeFeed($this->user, $this->auth_user);
115
116         $link = common_local_url(
117             'showstream',
118             array('nickname' => $this->user->nickname)
119         );
120
121         $self = $this->getSelfUri();
122
123         // FriendFeed's SUP protocol
124         // Also added RSS and Atom feeds
125
126         $suplink = common_local_url('sup', null, null, $this->user->id);
127         header('X-SUP-ID: ' . $suplink);
128
129         switch($this->format) {
130         case 'xml':
131             $this->showXmlTimeline($this->notices);
132             break;
133         case 'rss':
134             $this->showRssTimeline(
135                 $this->notices,
136                 $atom->title,
137                 $link,
138                 $atom->subtitle,
139                 $suplink,
140                 $atom->logo,
141                 $self
142             );
143             break;
144         case 'atom':
145             header('Content-Type: application/atom+xml; charset=utf-8');
146
147             $atom->setId($self);
148             $atom->setSelfLink($self);
149             $atom->addEntryFromNotices($this->notices);
150             $this->raw($atom->getString());
151
152             break;
153         case 'json':
154             $this->showJsonTimeline($this->notices);
155             break;
156         default:
157             // TRANS: Client error displayed when trying to handle an unknown API method.
158             $this->clientError(_('API method not found.'), $code = 404);
159             break;
160         }
161     }
162
163     /**
164      * Get notices
165      *
166      * @return array notices
167      */
168     function getNotices()
169     {
170         $notices = array();
171
172         $notice = $this->user->getNotices(
173             ($this->page-1) * $this->count, $this->count,
174             $this->since_id, $this->max_id
175         );
176
177         while ($notice->fetch()) {
178             $notices[] = clone($notice);
179         }
180
181         return $notices;
182     }
183
184     /**
185      * Is this action read only?
186      *
187      * @param array $args other arguments
188      *
189      * @return boolean true
190      */
191     function isReadOnly($args)
192     {
193         return true;
194     }
195
196     /**
197      * When was this feed last modified?
198      *
199      * @return string datestamp of the latest notice in the stream
200      */
201     function lastModified()
202     {
203         if (!empty($this->notices) && (count($this->notices) > 0)) {
204             return strtotime($this->notices[0]->created);
205         }
206
207         return null;
208     }
209
210     /**
211      * An entity tag for this stream
212      *
213      * Returns an Etag based on the action name, language, user ID, and
214      * timestamps of the first and last notice in the timeline
215      *
216      * @return string etag
217      */
218     function etag()
219     {
220         if (!empty($this->notices) && (count($this->notices) > 0)) {
221             $last = count($this->notices) - 1;
222
223             return '"' . implode(
224                 ':',
225                 array($this->arg('action'),
226                       common_user_cache_hash($this->auth_user),
227                       common_language(),
228                       $this->user->id,
229                       strtotime($this->notices[0]->created),
230                       strtotime($this->notices[$last]->created))
231             )
232             . '"';
233         }
234
235         return null;
236     }
237 }