]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apiusertimeline.php
New actions for /statuses/friends and /statuses/followers + social graph methods
[quix0rs-gnu-social.git] / actions / apiusertimeline.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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR.'/lib/apibareauth.php';
35
36 /**
37  * Returns the most recent notices (default 20) posted by the authenticating
38  * user. Another user's timeline can be requested via the id parameter. This
39  * is the API equivalent of the user profile web page.
40  *
41  * @category API
42  * @package  StatusNet
43  * @author   Zach Copley <zach@status.net>
44  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
45  * @link     http://status.net/
46  */
47
48 class ApiUserTimelineAction extends ApiBareAuthAction
49 {
50
51     var $user    = null;
52     var $notices = null;
53
54     /**
55      * Take arguments for running
56      *
57      * @param array $args $_REQUEST args
58      *
59      * @return boolean success flag
60      *
61      */
62
63     function prepare($args)
64     {
65         parent::prepare($args);
66
67         $this->page     = (int)$this->arg('page', 1);
68         $this->count    = (int)$this->arg('count', 20);
69         $this->max_id   = (int)$this->arg('max_id', 0);
70         $this->since_id = (int)$this->arg('since_id', 0);
71         $this->since    = $this->arg('since');
72
73         if ($this->requiresAuth()) {
74             if ($this->checkBasicAuthUser() == false) {
75                 return;
76             }
77         }
78
79         $this->user = $this->getTargetUser($this->arg('id'));
80
81         if (empty($this->user)) {
82             $this->clientError(_('No such user!'), 404, $this->arg('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         $sitename   = common_config('site', 'name');
118         $title      = sprintf(_("%s timeline"), $this->user->nickname);
119         $taguribase = common_config('integration', 'taguri');
120         $id         = "tag:$taguribase:UserTimeline:" . $this->user->id;
121         $link       = common_local_url(
122             'showstream',
123             array('nickname' => $this->user->nickname)
124         );
125         $subtitle   = sprintf(
126             _('Updates from %1$s on %2$s!'),
127             $this->user->nickname, $sitename
128         );
129
130         // FriendFeed's SUP protocol
131         // Also added RSS and Atom feeds
132
133         $suplink = common_local_url('sup', null, null, $this->user->id);
134         header('X-SUP-ID: ' . $suplink);
135
136         switch($this->arg('format')) {
137         case 'xml':
138             $this->show_xml_timeline($this->notices);
139             break;
140         case 'rss':
141             $this->show_rss_timeline(
142                 $this->notices, $title, $link,
143                 $subtitle, $suplink
144             );
145             break;
146         case 'atom':
147             if (isset($apidata['api_arg'])) {
148                 $selfuri = common_root_url() .
149                     'api/statuses/user_timeline/' .
150                     $apidata['api_arg'] . '.atom';
151             } else {
152                 $selfuri = common_root_url() .
153                     'api/statuses/user_timeline.atom';
154             }
155             $this->show_atom_timeline(
156                 $this->notices, $title, $id, $link,
157                 $subtitle, $suplink, $selfuri
158             );
159             break;
160         case 'json':
161             $this->show_json_timeline($this->notices);
162             break;
163         default:
164             $this->clientError(_('API method not found!'), $code = 404);
165             break;
166         }
167
168     }
169
170     /**
171      * Get notices
172      *
173      * @return array notices
174      */
175
176     function getNotices()
177     {
178         $notices = array();
179
180         $notice = $this->user->getNotices(
181             ($this->page-1) * $this->count, $this->count,
182             $this->since_id, $this->max_id, $this->since
183         );
184
185         while ($notice->fetch()) {
186             $notices[] = clone($notice);
187         }
188
189         return $notices;
190     }
191
192     /**
193      * Is this action read only?
194      *
195      * @param array $args other arguments
196      *
197      * @return boolean true
198      */
199
200     function isReadOnly($args)
201     {
202         return true;
203     }
204
205     /**
206      * When was this feed last modified?
207      *
208      * @return string datestamp of the latest notice in the stream
209      */
210
211     function lastModified()
212     {
213         if (!empty($this->notices) && (count($this->notices) > 0)) {
214             return strtotime($this->notices[0]->created);
215         }
216
217         return null;
218     }
219
220     /**
221      * An entity tag for this stream
222      *
223      * Returns an Etag based on the action name, language, user ID, and
224      * timestamps of the first and last notice in the timeline
225      *
226      * @return string etag
227      */
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_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 }