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