]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigrouplist.php
New individual actions for dealing with groups via API
[quix0rs-gnu-social.git] / actions / apigrouplist.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * Check to see whether a user a member of a group
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/apibareauth.php';
35
36 /**
37  * Returns whether a user is a member of a specified group.
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 ApiGroupListAction extends ApiBareAuthAction
47 {
48     var $format   = null;
49     var $user     = null;
50     var $page     = null;
51     var $count    = null;
52     var $max_id   = null;
53     var $since_id = null;
54     var $since    = null;
55     var $groups   = null;
56
57     /**
58      * Take arguments for running
59      *
60      * @param array $args $_REQUEST args
61      *
62      * @return boolean success flag
63      *
64      */
65
66     function prepare($args)
67     {
68         parent::prepare($args);
69
70         if ($this->requiresAuth()) {
71             if ($this->checkBasicAuthUser() == false) {
72                 return;
73             }
74         }
75
76         $this->page     = (int)$this->arg('page', 1);
77         $this->count    = (int)$this->arg('count', 20);
78         $this->max_id   = (int)$this->arg('max_id', 0);
79         $this->since_id = (int)$this->arg('since_id', 0);
80         $this->since    = $this->arg('since');
81         
82         $this->user   = $this->getTargetUser($id);
83         $this->format = $this->arg('format');
84         $this->groups = $this->getGroups();
85
86         return true;
87     }
88
89     /**
90      * Handle the request
91      *
92      * Show the user's groups
93      *
94      * @param array $args $_REQUEST data (unused)
95      *
96      * @return void
97      */
98
99     function handle($args)
100     {
101         parent::handle($args);
102
103         if (empty($this->user)) {
104             $this->clientError(_('No such user!'), 404, $this->format);
105             return;
106         }
107
108         $sitename   = common_config('site', 'name');
109         $title      = sprintf(_("%s's groups"), $this->user->nickname);
110         $taguribase = common_config('integration', 'taguri');
111         $id         = "tag:$taguribase:Groups";
112         $link       = common_local_url(
113             'usergroups',
114             array('nickname' => $this->user->nickname)
115         );
116         $subtitle   = sprintf(
117             _("Groups %s is a member of on %s."),
118             $this->user->nickname,
119             $sitename
120         );
121
122         switch($this->format) {
123         case 'xml':
124             $this->show_xml_groups($this->groups);
125             break;
126         case 'rss':
127             $this->show_rss_groups($this->groups, $title, $link, $subtitle);
128             break;
129         case 'atom':
130             $selfuri = common_root_url() . 'api/statusnet/groups/list/' .
131                 $this->user->id . '.atom';
132             $this->show_atom_groups(
133                 $this->groups,
134                 $title,
135                 $id,
136                 $link,
137                 $subtitle,
138                 $selfuri
139             );
140             break;
141         case 'json':
142             $this->show_json_groups($this->groups);
143             break;
144         default:
145             $this->clientError(
146                 _('API method not found!'),
147                 404,
148                 $this->format
149             );
150             break;
151         }
152
153     }
154
155     /**
156      * Get groups
157      *
158      * @return array groups
159      */
160
161     function getGroups()
162     {
163         $groups = array();
164
165         $group = $this->user->getGroups(
166             ($this->page - 1) * $this->count,
167             $this->count,
168             $this->since_id,
169             $this->max_id,
170             $this->since
171         );
172
173         while ($group->fetch()) {
174             $groups[] = clone($group);
175         }
176
177         return $groups;
178     }
179
180     /**
181      * Is this action read only?
182      *
183      * @param array $args other arguments
184      *
185      * @return boolean true
186      */
187
188     function isReadOnly($args)
189     {
190         return true;
191     }
192
193     /**
194      * When was this feed last modified?
195      *
196      * @return string datestamp of the latest group the user has joined
197      */
198
199     function lastModified()
200     {
201         if (!empty($this->groups) && (count($this->groups) > 0)) {
202             return strtotime($this->groups[0]->created);
203         }
204
205         return null;
206     }
207
208     /**
209      * An entity tag for this list of groups
210      *
211      * Returns an Etag based on the action name, language, user ID and
212      * timestamps of the first and last group the user has joined
213      *
214      * @return string etag
215      */
216
217     function etag()
218     {
219         if (!empty($this->groups) && (count($this->groups) > 0)) {
220
221             $last = count($this->groups) - 1;
222
223             return '"' . implode(
224                 ':',
225                 array($this->arg('action'),
226                       common_language(),
227                       $this->user->id,
228                       strtotime($this->groups[0]->created),
229                       strtotime($this->groups[$last]->created))
230             )
231             . '"';
232         }
233
234         return null;
235     }
236
237 }