]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinegroup.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[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         $sitename   = common_config('site', 'name');
108         $avatar     = $this->group->homepage_logo;
109         $title      = sprintf(_("%s timeline"), $this->group->nickname);
110
111         $subtitle   = sprintf(
112             _('Updates from %1$s on %2$s!'),
113             $this->group->nickname,
114             $sitename
115         );
116
117         $logo = ($avatar) ? $avatar : User_group::defaultLogo(AVATAR_PROFILE_SIZE);
118
119         switch($this->format) {
120         case 'xml':
121             $this->showXmlTimeline($this->notices);
122             break;
123         case 'rss':
124                 $this->showRssTimeline(
125                 $this->notices,
126                 $title,
127                 $this->group->homeUrl(),
128                 $subtitle,
129                 null,
130                 $logo
131             );
132             break;
133         case 'atom':
134
135             header('Content-Type: application/atom+xml; charset=utf-8');
136
137             try {
138
139                 $atom = new AtomGroupNoticeFeed($this->group);
140
141                 // @todo set all this Atom junk up inside the feed class
142
143                 #$atom->setId($id);
144                 $atom->setTitle($title);
145                 $atom->setSubtitle($subtitle);
146                 $atom->setLogo($logo);
147                 $atom->setUpdated('now');
148
149                 $atom->addAuthorRaw($this->group->asAtomAuthor());
150                 $atom->setActivitySubject($this->group->asActivitySubject());
151
152                 $atom->addLink($this->group->homeUrl());
153
154                 $id = $this->arg('id');
155                 $aargs = array('format' => 'atom');
156                 if (!empty($id)) {
157                     $aargs['id'] = $id;
158                 }
159
160                 $atom->setId($this->getSelfUri('ApiTimelineGroup', $aargs));
161
162                 $atom->addLink(
163                     $this->getSelfUri('ApiTimelineGroup', $aargs),
164                     array('rel' => 'self', 'type' => 'application/atom+xml')
165                 );
166
167                 $atom->addEntryFromNotices($this->notices);
168
169                 //$this->raw($atom->getString());
170                 print $atom->getString(); // temp hack until PuSH feeds are redone cleanly
171
172             } catch (Atom10FeedException $e) {
173                 $this->serverError(
174                     'Could not generate feed for group - ' . $e->getMessage()
175                 );
176                 return;
177             }
178
179             break;
180         case 'json':
181             $this->showJsonTimeline($this->notices);
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         $notices = array();
202
203         $notice = $this->group->getNotices(
204             ($this->page-1) * $this->count,
205             $this->count,
206             $this->since_id,
207             $this->max_id
208         );
209
210         while ($notice->fetch()) {
211             $notices[] = clone($notice);
212         }
213
214         return $notices;
215     }
216
217     /**
218      * Is this action read only?
219      *
220      * @param array $args other arguments
221      *
222      * @return boolean true
223      */
224
225     function isReadOnly($args)
226     {
227         return true;
228     }
229
230     /**
231      * When was this feed last modified?
232      *
233      * @return string datestamp of the latest notice in the stream
234      */
235
236     function lastModified()
237     {
238         if (!empty($this->notices) && (count($this->notices) > 0)) {
239             return strtotime($this->notices[0]->created);
240         }
241
242         return null;
243     }
244
245     /**
246      * An entity tag for this stream
247      *
248      * Returns an Etag based on the action name, language, group ID and
249      * timestamps of the first and last notice in the timeline
250      *
251      * @return string etag
252      */
253
254     function etag()
255     {
256         if (!empty($this->notices) && (count($this->notices) > 0)) {
257
258             $last = count($this->notices) - 1;
259
260             return '"' . implode(
261                 ':',
262                 array($this->arg('action'),
263                       common_language(),
264                       $this->group->id,
265                       strtotime($this->notices[0]->created),
266                       strtotime($this->notices[$last]->created))
267             )
268             . '"';
269         }
270
271         return null;
272     }
273
274 }