]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/apigroupmembership.php
Merge branch 'master' into testing
[quix0rs-gnu-social.git] / actions / apigroupmembership.php
1 <?php
2 /**
3  * StatusNet, the distributed open-source microblogging tool
4  *
5  * List a group's members
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 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  * List 20 newest members of the group specified by name or 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 ApiGroupMembershipAction extends ApiPrivateAuthAction
53 {
54     var $group    = null;
55     var $profiles = null;
56
57     /**
58      * Take arguments for running
59      *
60      * @param array $args $_REQUEST args
61      *
62      * @return boolean success flag
63      */
64     function prepare($args)
65     {
66         parent::prepare($args);
67
68         $this->group    = $this->getTargetGroup($this->arg('id'));
69         if (empty($this->group)) {
70             // TRANS: Client error displayed trying to show group membership on a non-existing group.
71             $this->clientError(_('Group not found.'), 404, $this->format);
72             return false;
73         }
74
75         $this->profiles = $this->getProfiles();
76
77         return true;
78     }
79
80     /**
81      * Handle the request
82      *
83      * Show the members of the group
84      *
85      * @param array $args $_REQUEST data (unused)
86      *
87      * @return void
88      */
89     function handle($args)
90     {
91         parent::handle($args);
92
93         // XXX: RSS and Atom
94
95         switch($this->format) {
96         case 'xml':
97             $this->showTwitterXmlUsers($this->profiles);
98             break;
99         case 'json':
100             $this->showJsonUsers($this->profiles);
101             break;
102         default:
103             $this->clientError(
104                 // TRANS: Client error displayed when coming across a non-supported API method.
105                 _('API method not found.'),
106                 404,
107                 $this->format
108             );
109             break;
110         }
111     }
112
113     /**
114      * Fetch the members of a group
115      *
116      * @return array $profiles list of profiles
117      */
118     function getProfiles()
119     {
120         $profiles = array();
121
122         $profile = $this->group->getMembers(
123             ($this->page - 1) * $this->count,
124             $this->count,
125             $this->since_id,
126             $this->max_id
127         );
128
129         while ($profile->fetch()) {
130             $profiles[] = clone($profile);
131         }
132
133         return $profiles;
134     }
135
136     /**
137      * Is this action read only?
138      *
139      * @param array $args other arguments
140      *
141      * @return boolean true
142      */
143     function isReadOnly($args)
144     {
145         return true;
146     }
147
148     /**
149      * When was this list of profiles last modified?
150      *
151      * @return string datestamp of the lastest profile in the group
152      */
153     function lastModified()
154     {
155         if (!empty($this->profiles) && (count($this->profiles) > 0)) {
156             return strtotime($this->profiles[0]->created);
157         }
158
159         return null;
160     }
161
162     /**
163      * An entity tag for this list of groups
164      *
165      * Returns an Etag based on the action name, language
166      * the group id, and timestamps of the first and last
167      * user who has joined the group
168      *
169      * @return string etag
170      */
171     function etag()
172     {
173         if (!empty($this->profiles) && (count($this->profiles) > 0)) {
174
175             $last = count($this->profiles) - 1;
176
177             return '"' . implode(
178                 ':',
179                 array($this->arg('action'),
180                       common_user_cache_hash($this->auth_user),
181                       common_language(),
182                       $this->group->id,
183                       strtotime($this->profiles[0]->created),
184                       strtotime($this->profiles[$last]->created))
185             )
186             . '"';
187         }
188
189         return null;
190     }
191 }