]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinegroup.php
Remove more redundant $formats
[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    Zach Copley <zach@status.net>
25  * @copyright 2009 StatusNet, Inc.
26  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
27  * @link      http://status.net/
28  */
29
30 if (!defined('STATUSNET')) {
31     exit(1);
32 }
33
34 require_once INSTALLDIR . '/lib/api.php';
35
36 /**
37  * Returns the most recent notices (default 20) posted to the group specified by ID
38  *
39  * @category API
40  * @package  StatusNet
41  * @author   Zach Copley <zach@status.net>
42  * @license  http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
43  * @link     http://status.net/
44  */
45
46 class ApiTimelineGroupAction extends ApiAction
47 {
48
49     var $group   = null;
50     var $notices = null;
51
52     /**
53      * Take arguments for running
54      *
55      * @param array $args $_REQUEST args
56      *
57      * @return boolean success flag
58      *
59      */
60
61     function prepare($args)
62     {
63         parent::prepare($args);
64
65         $this->page     = (int)$this->arg('page', 1);
66         $this->count    = (int)$this->arg('count', 20);
67         $this->max_id   = (int)$this->arg('max_id', 0);
68         $this->since_id = (int)$this->arg('since_id', 0);
69         $this->since    = $this->arg('since');
70
71         $this->group = $this->getTargetGroup($this->arg('id'));
72
73         $this->notices = $this->getNotices();
74
75         return true;
76     }
77
78     /**
79      * Handle the request
80      *
81      * Just show the notices
82      *
83      * @param array $args $_REQUEST data (unused)
84      *
85      * @return void
86      */
87
88     function handle($args)
89     {
90         parent::handle($args);
91         $this->showTimeline();
92     }
93
94     /**
95      * Show the timeline of notices
96      *
97      * @return void
98      */
99
100     function showTimeline()
101     {
102         $sitename   = common_config('site', 'name');
103         $title      = sprintf(_("%s timeline"), $this->group->nickname);
104         $taguribase = common_config('integration', 'taguri');
105         $id         = "tag:$taguribase:GroupTimeline:" . $this->group->id;
106         $link       = common_local_url(
107             'showgroup',
108             array('nickname' => $this->group->nickname)
109         );
110         $subtitle   = sprintf(
111             _('Updates from %1$s on %2$s!'),
112             $this->group->nickname,
113             $sitename
114         );
115
116         switch($this->format) {
117         case 'xml':
118             $this->show_xml_timeline($this->notices);
119             break;
120         case 'rss':
121             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
122             break;
123         case 'atom':
124             $selfuri = common_root_url() .
125                 'api/statusnet/groups/timeline/' .
126                     $this->group->nickname . '.atom';
127             $this->show_atom_timeline(
128                 $this->notices,
129                 $title,
130                 $id,
131                 $link,
132                 $subtitle,
133                 null,
134                 $selfuri
135             );
136             break;
137         case 'json':
138             $this->show_json_timeline($this->notices);
139             break;
140         default:
141             $this->clientError(
142                 _('API method not found!'),
143                 404,
144                 $this->format
145             );
146             break;
147         }
148     }
149
150     /**
151      * Get notices
152      *
153      * @return array notices
154      */
155
156     function getNotices()
157     {
158         $notices = array();
159
160         $notice = $this->group->getNotices(
161             ($this->page-1) * $this->count,
162             $this->count,
163             $this->since_id,
164             $this->max_id,
165             $this->since
166         );
167
168         while ($notice->fetch()) {
169             $notices[] = clone($notice);
170         }
171
172         return $notices;
173     }
174
175     /**
176      * Is this action read only?
177      *
178      * @param array $args other arguments
179      *
180      * @return boolean true
181      */
182
183     function isReadOnly($args)
184     {
185         return true;
186     }
187
188     /**
189      * When was this feed last modified?
190      *
191      * @return string datestamp of the latest notice in the stream
192      */
193
194     function lastModified()
195     {
196         if (!empty($this->notices) && (count($this->notices) > 0)) {
197             return strtotime($this->notices[0]->created);
198         }
199
200         return null;
201     }
202
203     /**
204      * An entity tag for this stream
205      *
206      * Returns an Etag based on the action name, language, group ID and
207      * timestamps of the first and last notice in the timeline
208      *
209      * @return string etag
210      */
211
212     function etag()
213     {
214         if (!empty($this->notices) && (count($this->notices) > 0)) {
215
216             $last = count($this->notices) - 1;
217
218             return '"' . implode(
219                 ':',
220                 array($this->arg('action'),
221                       common_language(),
222                       $this->group->id,
223                       strtotime($this->notices[0]->created),
224                       strtotime($this->notices[$last]->created))
225             )
226             . '"';
227         }
228
229         return null;
230     }
231
232 }