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