]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinelist.php
Qvitter API changes (thanks hannes2peer)
[quix0rs-gnu-social.git] / actions / apitimelinelist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a list's notices
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    Zach Copley <zach@status.net>
28  * @copyright 2009 StatusNet, Inc.
29  * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
30  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
31  * @link      http://status.net/
32  */
33
34 if (!defined('STATUSNET')) {
35     exit(1);
36 }
37
38 require_once INSTALLDIR . '/lib/atomlistnoticefeed.php';
39
40 /**
41  * Returns the most recent notices (default 20) posted to the list specified by ID
42  *
43  * @category API
44  * @package  StatusNet
45  * @author   Craig Andrews <candrews@integralblue.com>
46  * @author   Evan Prodromou <evan@status.net>
47  * @author   Jeffery To <jeffery.to@gmail.com>
48  * @author   Zach Copley <zach@status.net>
49  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
50  * @link     http://status.net/
51  */
52 class ApiTimelineListAction extends ApiPrivateAuthAction
53 {
54
55     var $list   = null;
56     var $notices = array();
57     var $next_cursor = 0;
58     var $prev_cursor = 0;
59     var $cursor = -1;
60
61     /**
62      * Take arguments for running
63      *
64      * @param array $args $_REQUEST args
65      *
66      * @return boolean success flag
67      *
68      */
69     function prepare($args)
70     {
71         parent::prepare($args);
72
73         $this->cursor = (int) $this->arg('cursor', -1);
74         $this->list = $this->getTargetList($this->arg('user'), $this->arg('id'));
75
76         return true;
77     }
78
79     /**
80      * Handle the request
81      *
82      * Just show the notices
83      *
84      * @param array $args $_REQUEST data (unused)
85      *
86      * @return void
87      */
88     function handle($args)
89     {
90         parent::handle($args);
91
92         if (empty($this->list)) {
93             // TRANS: Client error displayed trying to perform an action related to a non-existing list.
94             $this->clientError(_('List not found.'), 404, $this->format);
95             return false;
96         }
97
98         $this->getNotices();
99         $this->showTimeline();
100     }
101
102     /**
103      * Show the timeline of notices
104      *
105      * @return void
106      */
107     function showTimeline()
108     {
109         // We'll pull common formatting out of this for other formats
110         $atom = new AtomListNoticeFeed($this->list, $this->auth_user);
111
112         $self = $this->getSelfUri();
113
114         switch($this->format) {
115         case 'xml':
116             $this->initDocument('xml');
117             $this->elementStart('statuses_list',
118                     array('xmlns:statusnet' => 'http://status.net/schema/api/1/'));
119             $this->elementStart('statuses', array('type' => 'array'));
120
121             foreach ($this->notices as $n) {
122                 $twitter_status = $this->twitterStatusArray($n);
123                 $this->showTwitterXmlStatus($twitter_status);
124             }
125
126             $this->elementEnd('statuses');
127             $this->element('next_cursor', null, $this->next_cursor);
128             $this->element('previous_cursor', null, $this->prev_cursor);
129             $this->elementEnd('statuses_list');
130             $this->endDocument('xml');
131             break;
132         case 'rss':
133             $this->showRssTimeline(
134                 $this->notices,
135                 $atom->title,
136                 $this->list->getUri(),
137                 $atom->subtitle,
138                 null,
139                 $atom->logo,
140                 $self
141             );
142             break;
143         case 'atom':
144             header('Content-Type: application/atom+xml; charset=utf-8');
145
146             try {
147                 $atom->setId($self);
148                 $atom->setSelfLink($self);
149                 $atom->addEntryFromNotices($this->notices);
150                 $this->raw($atom->getString());
151             } catch (Atom10FeedException $e) {
152                 // TRANS: Server error displayed whe trying to get a timeline fails.
153                 // TRANS: %s is the error message.
154                 $this->serverError( sprintf(_('Could not generate feed for list - %s'),$e->getMessage()));
155                 return;
156             }
157
158             break;
159         case 'json':
160             $this->initDocument('json');
161
162             $statuses = array();
163             foreach ($this->notices as $n) {
164                 $twitter_status = $this->twitterStatusArray($n);
165                 array_push($statuses, $twitter_status);
166             }
167
168             $statuses_list = array('statuses' => $statuses,
169                                    'next_cursor' => $this->next_cusror,
170                                    'next_cursor_str' => strval($this->next_cusror),
171                                    'previous_cursor' => $this->prev_cusror,
172                                    'previous_cursor_str' => strval($this->prev_cusror)
173                                    );
174             $this->showJsonObjects($statuses_list);
175
176             $this->initDocument('json');
177             break;
178         default:
179             $this->clientError(
180                 // TRANS: Client error displayed when coming across a non-supported API method.
181                 _('API method not found.'),
182                 404,
183                 $this->format
184             );
185             break;
186         }
187     }
188
189     /**
190      * Get notices
191      *
192      * @return array notices
193      */
194     function getNotices()
195     {
196         $fn = array($this->list, 'getNotices');
197         list($this->notices, $this->next_cursor, $this->prev_cursor) =
198                 Profile_list::getAtCursor($fn, array(), $this->cursor, 20);
199         if (!$this->notices) {
200             $this->notices = array();
201         }
202     }
203
204     /**
205      * Is this action read only?
206      *
207      * @param array $args other arguments
208      *
209      * @return boolean true
210      */
211     function isReadOnly($args)
212     {
213         return true;
214     }
215
216     /**
217      * When was this feed last modified?
218      *
219      * @return string datestamp of the latest notice in the stream
220      */
221     function lastModified()
222     {
223         if (!empty($this->notices) && (count($this->notices) > 0)) {
224             return strtotime($this->notices[0]->created);
225         }
226
227         return null;
228     }
229
230     /**
231      * An entity tag for this stream
232      *
233      * Returns an Etag based on the action name, language, list ID and
234      * timestamps of the first and last notice in the timeline
235      *
236      * @return string etag
237      */
238     function etag()
239     {
240         if (!empty($this->notices) && (count($this->notices) > 0)) {
241
242             $last = count($this->notices) - 1;
243
244             return '"' . implode(
245                 ':',
246                 array($this->arg('action'),
247                       common_language(),
248                       $this->list->id,
249                       strtotime($this->notices[0]->created),
250                       strtotime($this->notices[$last]->created))
251             )
252             . '"';
253         }
254
255         return null;
256     }
257 }