3 * StatusNet, the distributed open-source microblogging tool
5 * StatusNet extensions to the Twitter-like API for groups
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.
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.
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/>.
24 * @author Craig Andrews <candrews@integralblue.com>
25 * @author Zach Copley <zach@status.net>
26 * @copyright 2009 StatusNet, Inc.
27 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
28 * @link http://status.net/
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
35 require_once INSTALLDIR.'/lib/twitterapi.php';
38 * Group-specific API methods
40 * This class handles StatusNet group API methods.
44 * @author Craig Andrews <candrews@integralblue.com>
45 * @author Zach Copley <zach@status.net>
46 * @copyright 2009 StatusNet, Inc.
47 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
48 * @link http://status.net/
51 class TwitapigroupsAction extends TwitterapiAction
54 function list_groups($args, $apidata)
56 parent::handle($args);
58 common_debug("in groups api action");
60 $this->auth_user = $apidata['user'];
61 $user = $this->get_user($apidata['api_arg'], $apidata);
64 $this->clientError('Not Found', 404, $apidata['content-type']);
68 $page = (int)$this->arg('page', 1);
69 $count = (int)$this->arg('count', 20);
70 $max_id = (int)$this->arg('max_id', 0);
71 $since_id = (int)$this->arg('since_id', 0);
72 $since = $this->arg('since');
73 $group = $user->getGroups(($page-1)*$count,
74 $count, $since_id, $max_id, $since);
76 $sitename = common_config('site', 'name');
77 $title = sprintf(_("%s's groups"), $user->nickname);
78 $taguribase = common_config('integration', 'taguri');
79 $id = "tag:$taguribase:Groups";
80 $link = common_root_url();
81 $subtitle = sprintf(_("groups %s is a member of on %s"), $user->nickname, $sitename);
83 switch($apidata['content-type']) {
85 $this->show_xml_groups($group);
88 $this->show_rss_groups($group, $title, $link, $subtitle);
91 $selfuri = common_root_url() . 'api/statusnet/groups/list/' . $user->id . '.atom';
92 $this->show_atom_groups($group, $title, $id, $link,
96 $this->show_json_groups($group);
99 $this->clientError(_('API method not found!'), $code = 404);
104 function list_all($args, $apidata)
106 parent::handle($args);
108 common_debug("in groups api action");
110 $page = (int)$this->arg('page', 1);
111 $count = (int)$this->arg('count', 20);
112 $max_id = (int)$this->arg('max_id', 0);
113 $since_id = (int)$this->arg('since_id', 0);
114 $since = $this->arg('since');
117 Use the $page, $count, $max_id, $since_id, and $since parameters
119 $group = new User_group();
120 $group->orderBy('created DESC');
123 $sitename = common_config('site', 'name');
124 $title = sprintf(_("%s groups"), $sitename);
125 $taguribase = common_config('integration', 'taguri');
126 $id = "tag:$taguribase:Groups";
127 $link = common_root_url();
128 $subtitle = sprintf(_("groups on %s"), $sitename);
130 switch($apidata['content-type']) {
132 $this->show_xml_groups($group);
135 $this->show_rss_groups($group, $title, $link, $subtitle);
138 $selfuri = common_root_url() . 'api/statusnet/groups/list_all.atom';
139 $this->show_atom_groups($group, $title, $id, $link,
140 $subtitle, $selfuri);
143 $this->show_json_groups($group);
146 $this->clientError(_('API method not found!'), $code = 404);
151 function show($args, $apidata)
153 parent::handle($args);
155 common_debug("in groups api action");
157 $this->auth_user = $apidata['user'];
158 $group = $this->get_group($apidata['api_arg'], $apidata);
161 $this->clientError('Not Found', 404, $apidata['content-type']);
165 switch($apidata['content-type']) {
167 $this->show_single_xml_group($group);
170 $this->show_single_json_group($group);
173 $this->clientError(_('API method not found!'), $code = 404);
177 function timeline($args, $apidata)
179 parent::handle($args);
181 common_debug("in groups api action");
183 $this->auth_user = $apidata['user'];
184 $group = $this->get_group($apidata['api_arg'], $apidata);
187 $this->clientError('Not Found', 404, $apidata['content-type']);
191 $sitename = common_config('site', 'name');
192 $title = sprintf(_("%s timeline"), $group->nickname);
193 $taguribase = common_config('integration', 'taguri');
194 $id = "tag:$taguribase:GroupTimeline:".$group->id;
195 $link = common_local_url('showgroup',
196 array('nickname' => $group->nickname));
197 $subtitle = sprintf(_('Updates from %1$s on %2$s!'),
198 $group->nickname, $sitename);
200 $page = (int)$this->arg('page', 1);
201 $count = (int)$this->arg('count', 20);
202 $max_id = (int)$this->arg('max_id', 0);
203 $since_id = (int)$this->arg('since_id', 0);
204 $since = $this->arg('since');
206 $notice = $group->getNotices(($page-1)*$count,
207 $count, $since_id, $max_id, $since);
209 switch($apidata['content-type']) {
211 $this->show_xml_timeline($notice);
214 $this->show_rss_timeline($notice, $title, $link, $subtitle);
217 if (isset($apidata['api_arg'])) {
218 $selfuri = common_root_url() .
219 'api/statusnet/groups/timeline/' .
220 $apidata['api_arg'] . '.atom';
222 $selfuri = common_root_url() .
223 'api/statusnet/groups/timeline.atom';
225 $this->show_atom_timeline($notice, $title, $id, $link,
226 $subtitle, null, $selfuri);
229 $this->show_json_timeline($notice);
232 $this->clientError(_('API method not found!'), $code = 404);
236 function membership($args, $apidata)
238 parent::handle($args);
240 common_debug("in groups api action");
242 $this->auth_user = $apidata['user'];
243 $group = $this->get_group($apidata['api_arg'], $apidata);
246 $this->clientError('Not Found', 404, $apidata['content-type']);
250 $sitename = common_config('site', 'name');
251 $title = sprintf(_("Members of %s group"), $group->nickname);
252 $taguribase = common_config('integration', 'taguri');
253 $id = "tag:$taguribase:GroupMembership:".$group->id;
254 $link = common_local_url('showgroup',
255 array('nickname' => $group->nickname));
256 $subtitle = sprintf(_('Members of %1$s on %2$s'),
257 $group->nickname, $sitename);
259 $page = (int)$this->arg('page', 1);
260 $count = (int)$this->arg('count', 20);
261 $max_id = (int)$this->arg('max_id', 0);
262 $since_id = (int)$this->arg('since_id', 0);
263 $since = $this->arg('since');
265 $member = $group->getMembers(($page-1)*$count,
266 $count, $since_id, $max_id, $since);
268 switch($apidata['content-type']) {
270 $this->show_twitter_xml_users($member);
272 //TODO implement the RSS and ATOM content types
274 $this->show_rss_users($member, $title, $link, $subtitle);
277 if (isset($apidata['api_arg'])) {
278 $selfuri = common_root_url() .
279 'api/statusnet/groups/membership/' .
280 $apidata['api_arg'] . '.atom';
282 $selfuri = common_root_url() .
283 'api/statusnet/groups/membership.atom';
285 $this->show_atom_users($member, $title, $id, $link,
286 $subtitle, null, $selfuri);
289 $this->show_json_users($member);
292 $this->clientError(_('API method not found!'), $code = 404);
296 function join($args, $apidata)
298 parent::handle($args);
300 common_debug("in groups api action");
302 $this->auth_user = $apidata['user'];
303 $group = $this->get_group($apidata['api_arg'], $apidata);
306 $this->clientError('Not Found', 404, $apidata['content-type']);
310 if($this->auth_user->isMember($group)){
311 $this->clientError(_('You are already a member of that group'), $code = 403);
315 if (Group_block::isBlocked($group, $this->auth_user->getProfile())) {
316 $this->clientError(_('You have been blocked from that group by the admin.'), 403);
320 $member = new Group_member();
322 $member->group_id = $group->id;
323 $member->profile_id = $this->auth_user->id;
324 $member->created = common_sql_now();
326 $result = $member->insert();
329 common_log_db_error($member, 'INSERT', __FILE__);
330 $this->serverError(sprintf(_('Could not join user %s to group %s'),
331 $this->auth_user->nickname, $group->nickname));
334 switch($apidata['content-type']) {
336 $this->show_single_xml_group($group);
339 $this->show_single_json_group($group);
342 $this->clientError(_('API method not found!'), $code = 404);
346 function leave($args, $apidata)
348 parent::handle($args);
350 common_debug("in groups api action");
352 $this->auth_user = $apidata['user'];
353 $group = $this->get_group($apidata['api_arg'], $apidata);
356 $this->clientError('Not Found', 404, $apidata['content-type']);
360 if(! $this->auth_user->isMember($group)){
361 $this->clientError(_('You are not a member of that group'), $code = 403);
365 $member = new Group_member();
367 $member->group_id = $group->id;
368 $member->profile_id = $this->auth_user->id;
370 if (!$member->find(true)) {
371 $this->serverError(_('Could not find membership record.'));
375 $result = $member->delete();
378 common_log_db_error($member, 'INSERT', __FILE__);
379 $this->serverError(sprintf(_('Could not remove user %s to group %s'),
380 $this->auth_user->nickname, $group->nickname));
383 switch($apidata['content-type']) {
385 $this->show_single_xml_group($group);
388 $this->show_single_json_group($group);
391 $this->clientError(_('API method not found!'), $code = 404);
395 function is_member($args, $apidata)
397 parent::handle($args);
399 common_debug("in groups api action");
401 $this->auth_user = $apidata['user'];
402 $group = User_group::staticGet($args['group_id']);
404 $this->clientError(_('Group not found'), $code = 500);
406 $user = User::staticGet('id', $args['user_id']);
408 $this->clientError(_('User not found'), $code = 500);
411 $is_member=$user->isMember($group);
413 switch($apidata['content-type']) {
415 $this->init_document('xml');
416 $this->element('is_member', null, $is_member);
417 $this->end_document('xml');
420 $this->init_document('json');
421 $this->show_json_objects(array('is_member'=>$is_member));
422 $this->end_document('json');
425 $this->clientError(_('API method not found!'), $code = 404);
429 function create($args, $apidata)
434 function update($args, $apidata)
439 function update_group_logo($args, $apidata)
444 function destroy($args, $apidata)
449 function tag($args, $apidata)