]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/twitapigroups.php
Merge branch '0.8.x' into 0.9.x
[quix0rs-gnu-social.git] / actions / twitapigroups.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * StatusNet extensions to the Twitter-like API for groups
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  Twitter
23  * @package   StatusNet
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/
29  */
30
31 if (!defined('STATUSNET') && !defined('LACONICA')) {
32     exit(1);
33 }
34
35 require_once INSTALLDIR.'/lib/twitterapi.php';
36
37 /**
38  * Group-specific API methods
39  *
40  * This class handles StatusNet group API methods.
41  *
42  * @category  Twitter
43  * @package   StatusNet
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/
49  */
50
51  class TwitapigroupsAction extends TwitterapiAction
52  {
53
54      function list_groups($args, $apidata)
55      {
56          parent::handle($args);
57          
58          common_debug("in groups api action");
59          
60          $this->auth_user = $apidata['user'];
61          $user = $this->get_user($apidata['api_arg'], $apidata);
62
63          if (empty($user)) {
64              $this->clientError('Not Found', 404, $apidata['content-type']);
65              return;
66          }
67
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);
75
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);
82
83          switch($apidata['content-type']) {
84          case 'xml':
85              $this->show_xml_groups($group);
86              break;
87          case 'rss':
88              $this->show_rss_groups($group, $title, $link, $subtitle);
89              break;
90          case 'atom':
91              $selfuri = common_root_url() . 'api/statusnet/groups/list/' . $user->id . '.atom';
92              $this->show_atom_groups($group, $title, $id, $link,
93                  $subtitle, $selfuri);
94              break;
95          case 'json':
96              $this->show_json_groups($group);
97              break;
98          default:
99              $this->clientError(_('API method not found!'), $code = 404);
100              break;
101          }
102      }
103
104      function list_all($args, $apidata)
105      {
106          parent::handle($args);
107          
108          common_debug("in groups api action");
109          
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');
115
116          /*      TODO:
117          Use the $page, $count, $max_id, $since_id, and $since parameters
118          */
119          $group = new User_group();
120          $group->orderBy('created DESC');
121          $group->find();
122
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);
129
130          switch($apidata['content-type']) {
131          case 'xml':
132              $this->show_xml_groups($group);
133              break;
134          case 'rss':
135              $this->show_rss_groups($group, $title, $link, $subtitle);
136              break;
137          case 'atom':
138              $selfuri = common_root_url() . 'api/statusnet/groups/list_all.atom';
139              $this->show_atom_groups($group, $title, $id, $link,
140                  $subtitle, $selfuri);
141              break;
142          case 'json':
143              $this->show_json_groups($group);
144              break;
145          default:
146              $this->clientError(_('API method not found!'), $code = 404);
147              break;
148          }
149      }
150
151      function show($args, $apidata)
152      {
153          parent::handle($args);
154
155          common_debug("in groups api action");
156
157          $this->auth_user = $apidata['user'];
158          $group = $this->get_group($apidata['api_arg'], $apidata);
159
160          if (empty($group)) {
161              $this->clientError('Not Found', 404, $apidata['content-type']);
162              return;
163          }
164
165          switch($apidata['content-type']) {
166           case 'xml':
167              $this->show_single_xml_group($group);
168              break;
169           case 'json':
170              $this->show_single_json_group($group);
171              break;
172           default:
173              $this->clientError(_('API method not found!'), $code = 404);
174          }
175      }
176
177      function timeline($args, $apidata)
178      {
179          parent::handle($args);
180
181          common_debug("in groups api action");
182
183          $this->auth_user = $apidata['user'];
184          $group = $this->get_group($apidata['api_arg'], $apidata);
185
186          if (empty($group)) {
187              $this->clientError('Not Found', 404, $apidata['content-type']);
188              return;
189          }
190
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);
199
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');
205
206          $notice = $group->getNotices(($page-1)*$count,
207              $count, $since_id, $max_id, $since);
208
209          switch($apidata['content-type']) {
210           case 'xml':
211              $this->show_xml_timeline($notice);
212              break;
213           case 'rss':
214              $this->show_rss_timeline($notice, $title, $link, $subtitle);
215              break;
216           case 'atom':
217              if (isset($apidata['api_arg'])) {
218                  $selfuri = common_root_url() .
219                      'api/statusnet/groups/timeline/' .
220                          $apidata['api_arg'] . '.atom';
221              } else {
222                  $selfuri = common_root_url() .
223                   'api/statusnet/groups/timeline.atom';
224              }
225              $this->show_atom_timeline($notice, $title, $id, $link,
226                  $subtitle, null, $selfuri);
227              break;
228           case 'json':
229              $this->show_json_timeline($notice);
230              break;
231           default:
232              $this->clientError(_('API method not found!'), $code = 404);
233          }
234      }
235
236      function membership($args, $apidata)
237      {
238          parent::handle($args);
239
240          common_debug("in groups api action");
241
242          $this->auth_user = $apidata['user'];
243          $group = $this->get_group($apidata['api_arg'], $apidata);
244          
245          if (empty($group)) {
246              $this->clientError('Not Found', 404, $apidata['content-type']);
247              return;
248          }
249
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);
258
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');
264
265          $member = $group->getMembers(($page-1)*$count,
266              $count, $since_id, $max_id, $since);
267
268          switch($apidata['content-type']) {
269           case 'xml':
270              $this->show_twitter_xml_users($member);
271              break;
272           //TODO implement the RSS and ATOM content types
273           /*case 'rss':
274              $this->show_rss_users($member, $title, $link, $subtitle);
275              break;*/
276           /*case 'atom':
277              if (isset($apidata['api_arg'])) {
278                  $selfuri = common_root_url() .
279                      'api/statusnet/groups/membership/' .
280                          $apidata['api_arg'] . '.atom';
281              } else {
282                  $selfuri = common_root_url() .
283                   'api/statusnet/groups/membership.atom';
284              }
285              $this->show_atom_users($member, $title, $id, $link,
286                  $subtitle, null, $selfuri);
287              break;*/
288           case 'json':
289              $this->show_json_users($member);
290              break;
291           default:
292              $this->clientError(_('API method not found!'), $code = 404);
293          }
294      }
295
296      function join($args, $apidata)
297      {
298          parent::handle($args);
299
300          common_debug("in groups api action");
301
302          $this->auth_user = $apidata['user'];
303          $group = $this->get_group($apidata['api_arg'], $apidata);
304
305          if (empty($group)) {
306              $this->clientError('Not Found', 404, $apidata['content-type']);
307              return false;
308          }
309
310          if($this->auth_user->isMember($group)){
311             $this->clientError(_('You are already a member of that group'), $code = 403);
312             return false;
313          }
314
315          if (Group_block::isBlocked($group, $this->auth_user->getProfile())) {
316             $this->clientError(_('You have been blocked from that group by the admin.'), 403);
317             return false;
318          }
319
320          $member = new Group_member();
321
322          $member->group_id   = $group->id;
323          $member->profile_id = $this->auth_user->id;
324          $member->created    = common_sql_now();
325
326          $result = $member->insert();
327
328          if (!$result) {
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));
332          }
333
334          switch($apidata['content-type']) {
335           case 'xml':
336              $this->show_single_xml_group($group);
337              break;
338           case 'json':
339              $this->show_single_json_group($group);
340              break;
341           default:
342              $this->clientError(_('API method not found!'), $code = 404);
343          }
344      }
345
346      function leave($args, $apidata)
347      {
348          parent::handle($args);
349
350          common_debug("in groups api action");
351
352          $this->auth_user = $apidata['user'];
353          $group = $this->get_group($apidata['api_arg'], $apidata);
354
355          if (empty($group)) {
356              $this->clientError('Not Found', 404, $apidata['content-type']);
357              return false;
358          }
359
360          if(! $this->auth_user->isMember($group)){
361             $this->clientError(_('You are not a member of that group'), $code = 403);
362             return false;
363          }
364
365          $member = new Group_member();
366
367          $member->group_id   = $group->id;
368          $member->profile_id = $this->auth_user->id;
369
370          if (!$member->find(true)) {
371             $this->serverError(_('Could not find membership record.'));
372             return;
373          }
374
375          $result = $member->delete();
376
377          if (!$result) {
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));
381          }
382
383          switch($apidata['content-type']) {
384           case 'xml':
385              $this->show_single_xml_group($group);
386              break;
387           case 'json':
388              $this->show_single_json_group($group);
389              break;
390           default:
391              $this->clientError(_('API method not found!'), $code = 404);
392          }
393      }
394
395      function is_member($args, $apidata)
396      {
397          parent::handle($args);
398
399          common_debug("in groups api action");
400
401          $this->auth_user = $apidata['user'];
402          $group = User_group::staticGet($args['group_id']);
403          if(! $group){
404             $this->clientError(_('Group not found'), $code = 500);
405          }
406          $user = User::staticGet('id', $args['user_id']);
407          if(! $user){
408             $this->clientError(_('User not found'), $code = 500);
409          }
410          
411          $is_member=$user->isMember($group);
412
413          switch($apidata['content-type']) {
414           case 'xml':
415              $this->init_document('xml');
416              $this->element('is_member', null, $is_member);
417              $this->end_document('xml');
418              break;
419           case 'json':
420              $this->init_document('json');
421              $this->show_json_objects(array('is_member'=>$is_member));
422              $this->end_document('json');
423              break;
424           default:
425              $this->clientError(_('API method not found!'), $code = 404);
426          }
427      }
428
429      function create($args, $apidata)
430      {
431         die("todo");
432      }
433
434      function update($args, $apidata)
435      {
436         die("todo");
437      }
438
439      function update_group_logo($args, $apidata)
440      {
441         die("todo");
442      }
443
444      function destroy($args, $apidata)
445      {
446         die("todo");
447      }
448
449      function tag($args, $apidata)
450      {
451         die("todo");
452      }
453 }