]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelineuser.php
Merge branch 'master' of gitorious.org:statusnet/mainline
[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  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
33  * @link      http://status.net/
34  */
35
36 if (!defined('STATUSNET')) {
37     exit(1);
38 }
39
40 require_once INSTALLDIR . '/lib/apibareauth.php';
41
42 /**
43  * Returns the most recent notices (default 20) posted by the authenticating
44  * user. Another user's timeline can be requested via the id parameter. This
45  * is the API equivalent of the user profile web page.
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 ApiTimelineUserAction extends ApiBareAuthAction
61 {
62
63     var $notices = null;
64
65     /**
66      * Take arguments for running
67      *
68      * @param array $args $_REQUEST args
69      *
70      * @return boolean success flag
71      *
72      */
73
74     function prepare($args)
75     {
76         parent::prepare($args);
77
78         $this->user = $this->getTargetUser($this->arg('id'));
79
80         if (empty($this->user)) {
81             $this->clientError(_('No such user.'), 404, $this->format);
82             return;
83         }
84
85         $this->notices = $this->getNotices();
86
87         return true;
88     }
89
90     /**
91      * Handle the request
92      *
93      * Just show the notices
94      *
95      * @param array $args $_REQUEST data (unused)
96      *
97      * @return void
98      */
99
100     function handle($args)
101     {
102         parent::handle($args);
103         $this->showTimeline();
104     }
105
106     /**
107      * Show the timeline of notices
108      *
109      * @return void
110      */
111
112     function showTimeline()
113     {
114         $profile = $this->user->getProfile();
115
116         // We'll use the shared params from the Atom stub
117         // for other feed types.
118         $atom = new AtomUserNoticeFeed($this->user, $this->auth_user);
119
120         $link = common_local_url(
121             'showstream',
122             array('nickname' => $this->user->nickname)
123         );
124
125         $self = $this->getSelfUri();
126
127         // FriendFeed's SUP protocol
128         // Also added RSS and Atom feeds
129
130         $suplink = common_local_url('sup', null, null, $this->user->id);
131         header('X-SUP-ID: ' . $suplink);
132
133         switch($this->format) {
134         case 'xml':
135             $this->showXmlTimeline($this->notices);
136             break;
137         case 'rss':
138             $this->showRssTimeline(
139                 $this->notices,
140                 $atom->title,
141                 $link,
142                 $atom->subtitle,
143                 $suplink,
144                 $atom->logo,
145                 $self
146             );
147             break;
148         case 'atom':
149
150             header('Content-Type: application/atom+xml; charset=utf-8');
151
152             $atom->setId($self);
153             $atom->setSelfLink($self);
154             $atom->addEntryFromNotices($this->notices);
155             $this->raw($atom->getString());
156
157             break;
158         case 'json':
159             $this->showJsonTimeline($this->notices);
160             break;
161         default:
162             $this->clientError(_('API method not found.'), $code = 404);
163             break;
164         }
165
166     }
167
168     /**
169      * Get notices
170      *
171      * @return array notices
172      */
173
174     function getNotices()
175     {
176         $notices = array();
177
178         $notice = $this->user->getNotices(
179             ($this->page-1) * $this->count, $this->count,
180             $this->since_id, $this->max_id
181         );
182
183         while ($notice->fetch()) {
184             $notices[] = clone($notice);
185         }
186
187         return $notices;
188     }
189
190     /**
191      * Is this action read only?
192      *
193      * @param array $args other arguments
194      *
195      * @return boolean true
196      */
197
198     function isReadOnly($args)
199     {
200         return true;
201     }
202
203     /**
204      * When was this feed last modified?
205      *
206      * @return string datestamp of the latest notice in the stream
207      */
208
209     function lastModified()
210     {
211         if (!empty($this->notices) && (count($this->notices) > 0)) {
212             return strtotime($this->notices[0]->created);
213         }
214
215         return null;
216     }
217
218     /**
219      * An entity tag for this stream
220      *
221      * Returns an Etag based on the action name, language, user ID, and
222      * timestamps of the first and last notice in the timeline
223      *
224      * @return string etag
225      */
226
227     function etag()
228     {
229         if (!empty($this->notices) && (count($this->notices) > 0)) {
230
231             $last = count($this->notices) - 1;
232
233             return '"' . implode(
234                 ':',
235                 array($this->arg('action'),
236                       common_language(),
237                       $this->user->id,
238                       strtotime($this->notices[0]->created),
239                       strtotime($this->notices[$last]->created))
240             )
241             . '"';
242         }
243
244         return null;
245     }
246
247 }