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