]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apitimelinegroup.php
11f73eeedb96f0488a64abf54474b4770f2975c3
[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/twitterapi.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 TwitterapiAction
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->format  = $this->arg('format');
74         $this->notices = $this->getNotices();
75
76         return true;
77     }
78
79     /**
80      * Handle the request
81      *
82      * Just show the notices
83      *
84      * @param array $args $_REQUEST data (unused)
85      *
86      * @return void
87      */
88
89     function handle($args)
90     {
91         parent::handle($args);
92         $this->showTimeline();
93     }
94
95     /**
96      * Show the timeline of notices
97      *
98      * @return void
99      */
100
101     function showTimeline()
102     {
103         $sitename   = common_config('site', 'name');
104         $title      = sprintf(_("%s timeline"), $this->group->nickname);
105         $taguribase = common_config('integration', 'taguri');
106         $id         = "tag:$taguribase:GroupTimeline:" . $this->group->id;
107         $link       = common_local_url(
108             'showgroup',
109             array('nickname' => $this->group->nickname)
110         );
111         $subtitle   = sprintf(
112             _('Updates from %1$s on %2$s!'),
113             $this->group->nickname,
114             $sitename
115         );
116
117         switch($this->format) {
118         case 'xml':
119             $this->show_xml_timeline($this->notices);
120             break;
121         case 'rss':
122             $this->show_rss_timeline($this->notices, $title, $link, $subtitle);
123             break;
124         case 'atom':
125             $selfuri = common_root_url() .
126                 'api/statusnet/groups/timeline/' .
127                     $this->group->nickname . '.atom';
128             $this->show_atom_timeline(
129                 $this->notices,
130                 $title,
131                 $id,
132                 $link,
133                 $subtitle,
134                 null,
135                 $selfuri
136             );
137             break;
138         case 'json':
139             $this->show_json_timeline($this->notices);
140             break;
141         default:
142             $this->clientError(
143                 _('API method not found!'),
144                 404,
145                 $this->format
146             );
147             break;
148         }
149     }
150
151     /**
152      * Get notices
153      *
154      * @return array notices
155      */
156
157     function getNotices()
158     {
159         $notices = array();
160
161         $notice = $this->group->getNotices(
162             ($this->page-1) * $this->count,
163             $this->count,
164             $this->since_id,
165             $this->max_id,
166             $this->since
167         );
168
169         while ($notice->fetch()) {
170             $notices[] = clone($notice);
171         }
172
173         return $notices;
174     }
175
176     /**
177      * Is this action read only?
178      *
179      * @param array $args other arguments
180      *
181      * @return boolean true
182      */
183
184     function isReadOnly($args)
185     {
186         return true;
187     }
188
189     /**
190      * When was this feed last modified?
191      *
192      * @return string datestamp of the latest notice in the stream
193      */
194
195     function lastModified()
196     {
197         if (!empty($this->notices) && (count($this->notices) > 0)) {
198             return strtotime($this->notices[0]->created);
199         }
200
201         return null;
202     }
203
204     /**
205      * An entity tag for this stream
206      *
207      * Returns an Etag based on the action name, language, group ID and
208      * timestamps of the first and last notice in the timeline
209      *
210      * @return string etag
211      */
212
213     function etag()
214     {
215         if (!empty($this->notices) && (count($this->notices) > 0)) {
216
217             $last = count($this->notices) - 1;
218
219             return '"' . implode(
220                 ':',
221                 array($this->arg('action'),
222                       common_language(),
223                       $this->group->id,
224                       strtotime($this->notices[0]->created),
225                       strtotime($this->notices[$last]->created))
226             )
227             . '"';
228         }
229
230         return null;
231     }
232
233 }