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