]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinegroup.php
Merge branch '0.9.x' 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  * @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         $taguribase = TagURI::base();
111         $id         = "tag:$taguribase:GroupTimeline:" . $this->group->id;
112
113         $subtitle   = sprintf(
114             _('Updates from %1$s on %2$s!'),
115             $this->group->nickname,
116             $sitename
117         );
118
119         $logo = ($avatar) ? $avatar : User_group::defaultLogo(AVATAR_PROFILE_SIZE);
120
121         switch($this->format) {
122         case 'xml':
123             $this->showXmlTimeline($this->notices);
124             break;
125         case 'rss':
126                 $this->showRssTimeline(
127                 $this->notices,
128                 $title,
129                 $this->group->homeUrl(),
130                 $subtitle,
131                 null,
132                 $logo
133             );
134             break;
135         case 'atom':
136
137             header('Content-Type: application/atom+xml; charset=utf-8');
138
139             try {
140
141                 // If this was called using an integer ID, i.e.: using the canonical
142                 // URL for this group's feed, then pass the Group object into the feed, 
143                 // so the OStatus plugin, and possibly other plugins, can access it. 
144                 // Feels sorta hacky. -- Z
145
146                 $atom = null;
147                 $id = $this->arg('id');
148
149                 if (strval(intval($id)) === strval($id)) {
150                     $atom = new AtomGroupNoticeFeed($this->group);
151                 } else {
152                     $atom = new AtomGroupNoticeFeed();
153                 }
154
155                 $atom->setId($id);
156                 $atom->setTitle($title);
157                 $atom->setSubtitle($subtitle);
158                 $atom->setLogo($logo);
159                 $atom->setUpdated('now');
160
161                 $atom->addAuthorRaw($this->group->asAtomAuthor());
162                 $atom->setActivitySubject($this->group->asActivitySubject());
163
164                 $atom->addLink($this->group->homeUrl());
165
166                 $id = $this->arg('id');
167                 $aargs = array('format' => 'atom');
168                 if (!empty($id)) {
169                     $aargs['id'] = $id;
170                 }
171
172                 $atom->addLink(
173                     $this->getSelfUri('ApiTimelineGroup', $aargs),
174                     array('rel' => 'self', 'type' => 'application/atom+xml')
175                 );
176
177                 $atom->addEntryFromNotices($this->notices);
178
179                 //$this->raw($atom->getString());
180                 print $atom->getString(); // temp hack until PuSH feeds are redone cleanly
181
182             } catch (Atom10FeedException $e) {
183                 $this->serverError(
184                     'Could not generate feed for group - ' . $e->getMessage()
185                 );
186                 return;
187             }
188
189             break;
190         case 'json':
191             $this->showJsonTimeline($this->notices);
192             break;
193         default:
194             $this->clientError(
195                 _('API method not found.'),
196                 404,
197                 $this->format
198             );
199             break;
200         }
201     }
202
203     /**
204      * Get notices
205      *
206      * @return array notices
207      */
208
209     function getNotices()
210     {
211         $notices = array();
212
213         $notice = $this->group->getNotices(
214             ($this->page-1) * $this->count,
215             $this->count,
216             $this->since_id,
217             $this->max_id,
218             $this->since
219         );
220
221         while ($notice->fetch()) {
222             $notices[] = clone($notice);
223         }
224
225         return $notices;
226     }
227
228     /**
229      * Is this action read only?
230      *
231      * @param array $args other arguments
232      *
233      * @return boolean true
234      */
235
236     function isReadOnly($args)
237     {
238         return true;
239     }
240
241     /**
242      * When was this feed last modified?
243      *
244      * @return string datestamp of the latest notice in the stream
245      */
246
247     function lastModified()
248     {
249         if (!empty($this->notices) && (count($this->notices) > 0)) {
250             return strtotime($this->notices[0]->created);
251         }
252
253         return null;
254     }
255
256     /**
257      * An entity tag for this stream
258      *
259      * Returns an Etag based on the action name, language, group ID and
260      * timestamps of the first and last notice in the timeline
261      *
262      * @return string etag
263      */
264
265     function etag()
266     {
267         if (!empty($this->notices) && (count($this->notices) > 0)) {
268
269             $last = count($this->notices) - 1;
270
271             return '"' . implode(
272                 ':',
273                 array($this->arg('action'),
274                       common_language(),
275                       $this->group->id,
276                       strtotime($this->notices[0]->created),
277                       strtotime($this->notices[$last]->created))
278             )
279             . '"';
280         }
281
282         return null;
283     }
284
285 }