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