]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelineuser.php
Do mention lookup for Webfinger accounts in OStatusPlugin
[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         $avatar = $profile->getAvatar(AVATAR_PROFILE_SIZE);
116
117         $sitename   = common_config('site', 'name');
118         $title      = sprintf(_("%s timeline"), $this->user->nickname);
119         $taguribase = TagURI::base();
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         $logo = ($avatar) ? $avatar->displayUrl() : Avatar::defaultImage(AVATAR_PROFILE_SIZE);
130
131         // FriendFeed's SUP protocol
132         // Also added RSS and Atom feeds
133
134         $suplink = common_local_url('sup', null, null, $this->user->id);
135         header('X-SUP-ID: ' . $suplink);
136
137         switch($this->format) {
138         case 'xml':
139             $this->showXmlTimeline($this->notices);
140             break;
141         case 'rss':
142             $this->showRssTimeline(
143                 $this->notices, $title, $link,
144                 $subtitle, $suplink, $logo
145             );
146             break;
147         case 'atom':
148
149             header('Content-Type: application/atom+xml; charset=utf-8');
150
151             // If this was called using an integer ID, i.e.: using the canonical
152             // URL for this user's feed, then pass the User object into the feed,
153             // so the OStatus plugin, and possibly other plugins, can access it.
154             // Feels sorta hacky. -- Z
155
156             $atom = null;
157             $id = $this->arg('id');
158
159             if (strval(intval($id)) === strval($id)) {
160                 $atom = new AtomUserNoticeFeed($this->user);
161             } else {
162                 $atom = new AtomUserNoticeFeed();
163             }
164
165             $atom->setId($id);
166             $atom->setTitle($title);
167             $atom->setSubtitle($subtitle);
168             $atom->setLogo($logo);
169             $atom->setUpdated('now');
170
171             $atom->addLink(
172                 common_local_url(
173                     'showstream',
174                     array('nickname' => $this->user->nickname)
175                 )
176             );
177
178             $id = $this->arg('id');
179             $aargs = array('format' => 'atom');
180             if (!empty($id)) {
181                 $aargs['id'] = $id;
182             }
183
184             $atom->addLink(
185                 $this->getSelfUri('ApiTimelineUser', $aargs),
186                 array('rel' => 'self', 'type' => 'application/atom+xml')
187             );
188
189             $atom->addLink(
190                 $suplink,
191                 array(
192                     'rel' => 'http://api.friendfeed.com/2008/03#sup',
193                     'type' => 'application/json'
194                 )
195             );
196
197             $atom->addEntryFromNotices($this->notices);
198
199             #$this->raw($atom->getString());
200             print $atom->getString(); // temporary for output buffering
201
202             break;
203         case 'json':
204             $this->showJsonTimeline($this->notices);
205             break;
206         default:
207             $this->clientError(_('API method not found.'), $code = 404);
208             break;
209         }
210
211     }
212
213     /**
214      * Get notices
215      *
216      * @return array notices
217      */
218
219     function getNotices()
220     {
221         $notices = array();
222
223         $notice = $this->user->getNotices(
224             ($this->page-1) * $this->count, $this->count,
225             $this->since_id, $this->max_id, $this->since
226         );
227
228         while ($notice->fetch()) {
229             $notices[] = clone($notice);
230         }
231
232         return $notices;
233     }
234
235     /**
236      * Is this action read only?
237      *
238      * @param array $args other arguments
239      *
240      * @return boolean true
241      */
242
243     function isReadOnly($args)
244     {
245         return true;
246     }
247
248     /**
249      * When was this feed last modified?
250      *
251      * @return string datestamp of the latest notice in the stream
252      */
253
254     function lastModified()
255     {
256         if (!empty($this->notices) && (count($this->notices) > 0)) {
257             return strtotime($this->notices[0]->created);
258         }
259
260         return null;
261     }
262
263     /**
264      * An entity tag for this stream
265      *
266      * Returns an Etag based on the action name, language, user ID, and
267      * timestamps of the first and last notice in the timeline
268      *
269      * @return string etag
270      */
271
272     function etag()
273     {
274         if (!empty($this->notices) && (count($this->notices) > 0)) {
275
276             $last = count($this->notices) - 1;
277
278             return '"' . implode(
279                 ':',
280                 array($this->arg('action'),
281                       common_language(),
282                       $this->user->id,
283                       strtotime($this->notices[0]->created),
284                       strtotime($this->notices[$last]->created))
285             )
286             . '"';
287         }
288
289         return null;
290     }
291
292 }