]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinegroup.php
Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x
[quix0rs-gnu-social.git] / actions / apitimelinegroup.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Show a group'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
40 /**
41  * Returns the most recent notices (default 20) posted to the group 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
53 class ApiTimelineGroupAction extends ApiPrivateAuthAction
54 {
55
56     var $group   = null;
57     var $notices = null;
58
59     /**
60      * Take arguments for running
61      *
62      * @param array $args $_REQUEST args
63      *
64      * @return boolean success flag
65      *
66      */
67
68     function prepare($args)
69     {
70         parent::prepare($args);
71
72         $this->group   = $this->getTargetGroup($this->arg('id'));
73
74         return true;
75     }
76
77     /**
78      * Handle the request
79      *
80      * Just show the notices
81      *
82      * @param array $args $_REQUEST data (unused)
83      *
84      * @return void
85      */
86
87     function handle($args)
88     {
89         parent::handle($args);
90
91         if (empty($this->group)) {
92             $this->clientError(_('Group not found.'), 404, $this->format);
93             return false;
94         }
95
96         $this->notices = $this->getNotices();
97         $this->showTimeline();
98     }
99
100     /**
101      * Show the timeline of notices
102      *
103      * @return void
104      */
105
106     function showTimeline()
107     {
108         // We'll pull common formatting out of this for other formats
109         $atom = new AtomGroupNoticeFeed($this->group, $this->auth_user);
110
111         $self = $this->getSelfUri();
112
113         switch($this->format) {
114         case 'xml':
115             $this->showXmlTimeline($this->notices);
116             break;
117         case 'rss':
118             $this->showRssTimeline(
119                 $this->notices,
120                 $atom->title,
121                 $this->group->homeUrl(),
122                 $atom->subtitle,
123                 null,
124                 $atom->logo,
125                 $self
126             );
127             break;
128         case 'atom':
129
130             header('Content-Type: application/atom+xml; charset=utf-8');
131
132             try {
133                 $atom->addAuthorRaw($this->group->asAtomAuthor());
134                 $atom->setActivitySubject($this->group->asActivitySubject());
135                 $atom->setId($self);
136                 $atom->setSelfLink($self);
137                 $atom->addEntryFromNotices($this->notices);
138                 $this->raw($atom->getString());
139             } catch (Atom10FeedException $e) {
140                 $this->serverError(
141                     'Could not generate feed for group - ' . $e->getMessage()
142                 );
143                 return;
144             }
145
146             break;
147         case 'json':
148             $this->showJsonTimeline($this->notices);
149             break;
150         default:
151             $this->clientError(
152                 _('API method not found.'),
153                 404,
154                 $this->format
155             );
156             break;
157         }
158     }
159
160     /**
161      * Get notices
162      *
163      * @return array notices
164      */
165
166     function getNotices()
167     {
168         $notices = array();
169
170         $notice = $this->group->getNotices(
171             ($this->page-1) * $this->count,
172             $this->count,
173             $this->since_id,
174             $this->max_id
175         );
176
177         while ($notice->fetch()) {
178             $notices[] = clone($notice);
179         }
180
181         return $notices;
182     }
183
184     /**
185      * Is this action read only?
186      *
187      * @param array $args other arguments
188      *
189      * @return boolean true
190      */
191
192     function isReadOnly($args)
193     {
194         return true;
195     }
196
197     /**
198      * When was this feed last modified?
199      *
200      * @return string datestamp of the latest notice in the stream
201      */
202
203     function lastModified()
204     {
205         if (!empty($this->notices) && (count($this->notices) > 0)) {
206             return strtotime($this->notices[0]->created);
207         }
208
209         return null;
210     }
211
212     /**
213      * An entity tag for this stream
214      *
215      * Returns an Etag based on the action name, language, group ID and
216      * timestamps of the first and last notice in the timeline
217      *
218      * @return string etag
219      */
220
221     function etag()
222     {
223         if (!empty($this->notices) && (count($this->notices) > 0)) {
224
225             $last = count($this->notices) - 1;
226
227             return '"' . implode(
228                 ':',
229                 array($this->arg('action'),
230                       common_language(),
231                       $this->group->id,
232                       strtotime($this->notices[0]->created),
233                       strtotime($this->notices[$last]->created))
234             )
235             . '"';
236         }
237
238         return null;
239     }
240
241 }