apigroupmembership.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. if (!defined('STATUSNET')) {
  34. exit(1);
  35. }
  36. /**
  37. * List 20 newest members of the group specified by name or ID.
  38. *
  39. * @category API
  40. * @package StatusNet
  41. * @author Craig Andrews <candrews@integralblue.com>
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Jeffery To <jeffery.to@gmail.com>
  44. * @author Zach Copley <zach@status.net>
  45. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  46. * @link http://status.net/
  47. */
  48. class ApiGroupMembershipAction extends ApiPrivateAuthAction
  49. {
  50. var $group = null;
  51. var $profiles = null;
  52. /**
  53. * Take arguments for running
  54. *
  55. * @param array $args $_REQUEST args
  56. *
  57. * @return boolean success flag
  58. */
  59. protected function prepare(array $args=array())
  60. {
  61. parent::prepare($args);
  62. $this->group = $this->getTargetGroup($this->arg('id'));
  63. if (empty($this->group)) {
  64. // TRANS: Client error displayed trying to show group membership on a non-existing group.
  65. $this->clientError(_('Group not found.'), 404);
  66. }
  67. $this->profiles = $this->getProfiles();
  68. return true;
  69. }
  70. /**
  71. * Handle the request
  72. *
  73. * Show the members of the group
  74. *
  75. * @return void
  76. */
  77. protected function handle()
  78. {
  79. parent::handle();
  80. // XXX: RSS and Atom
  81. switch($this->format) {
  82. case 'xml':
  83. $this->showTwitterXmlUsers($this->profiles);
  84. break;
  85. case 'json':
  86. $this->showJsonUsers($this->profiles);
  87. break;
  88. default:
  89. // TRANS: Client error displayed when coming across a non-supported API method.
  90. $this->clientError(_('API method not found.'), 404);
  91. }
  92. }
  93. /**
  94. * Fetch the members of a group
  95. *
  96. * @return array $profiles list of profiles
  97. */
  98. function getProfiles()
  99. {
  100. $profiles = array();
  101. $profile = $this->group->getMembers(
  102. ($this->page - 1) * $this->count,
  103. $this->count,
  104. $this->since_id,
  105. $this->max_id
  106. );
  107. while ($profile->fetch()) {
  108. $profiles[] = clone($profile);
  109. }
  110. return $profiles;
  111. }
  112. /**
  113. * Is this action read only?
  114. *
  115. * @param array $args other arguments
  116. *
  117. * @return boolean true
  118. */
  119. function isReadOnly($args)
  120. {
  121. return true;
  122. }
  123. /**
  124. * When was this list of profiles last modified?
  125. *
  126. * @return string datestamp of the lastest profile in the group
  127. */
  128. function lastModified()
  129. {
  130. if (!empty($this->profiles) && (count($this->profiles) > 0)) {
  131. return strtotime($this->profiles[0]->created);
  132. }
  133. return null;
  134. }
  135. /**
  136. * An entity tag for this list of groups
  137. *
  138. * Returns an Etag based on the action name, language
  139. * the group id, and timestamps of the first and last
  140. * user who has joined the group
  141. *
  142. * @return string etag
  143. */
  144. function etag()
  145. {
  146. if (!empty($this->profiles) && (count($this->profiles) > 0)) {
  147. $last = count($this->profiles) - 1;
  148. return '"' . implode(
  149. ':',
  150. array($this->arg('action'),
  151. common_user_cache_hash($this->auth_user),
  152. common_language(),
  153. $this->group->id,
  154. strtotime($this->profiles[0]->created),
  155. strtotime($this->profiles[$last]->created))
  156. )
  157. . '"';
  158. }
  159. return null;
  160. }
  161. }