apilistmembers.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * List/add/remove list 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 Shashi Gowda <connect2shashi@gmail.com>
  25. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  26. * @link http://status.net/
  27. */
  28. if (!defined('STATUSNET')) {
  29. exit(1);
  30. }
  31. require_once INSTALLDIR . '/lib/api/apilistusers.php';
  32. class ApiListMembersAction extends ApiListUsersAction
  33. {
  34. /**
  35. * Add a user to a list (tag someone)
  36. *
  37. * @return boolean success
  38. */
  39. function handlePost()
  40. {
  41. if($this->auth_user->id != $this->list->tagger) {
  42. // TRANS: Client error displayed when trying to add members to a list without having the right to do so.
  43. $this->clientError(_('You are not allowed to add members to this list.'), 401);
  44. }
  45. if (!($this->target instanceof Profile)) {
  46. // TRANS: Client error displayed when trying to modify list members without specifying them.
  47. $this->clientError(_('You must specify a member.'));
  48. }
  49. $result = Profile_tag::setTag($this->auth_user->id,
  50. $this->target->id, $this->list->tag);
  51. if(empty($result)) {
  52. // TRANS: Client error displayed when an unknown error occurs viewing list members.
  53. $this->clientError(_('An error occured.'), 500);
  54. }
  55. switch($this->format) {
  56. case 'xml':
  57. $this->showSingleXmlList($this->list);
  58. break;
  59. case 'json':
  60. $this->showSingleJsonList($this->list);
  61. break;
  62. default:
  63. // TRANS: Client error displayed when coming across a non-supported API method.
  64. $this->clientError(_('API method not found.'), 404);
  65. }
  66. }
  67. /**
  68. * Remove a user from a list (untag someone)
  69. *
  70. * @return boolean success
  71. */
  72. function handleDelete()
  73. {
  74. if($this->auth_user->id != $this->list->tagger) {
  75. // TRANS: Client error displayed when trying to remove members from a list without having the right to do so.
  76. $this->clientError(_('You are not allowed to remove members from this list.'), 401);
  77. }
  78. if (!($this->target instanceof Profile)) {
  79. // TRANS: Client error displayed when trying to modify list members without specifying them.
  80. $this->clientError(_('You must specify a member.'));
  81. }
  82. $args = array('tagger' => $this->auth_user->id,
  83. 'tagged' => $this->target->id,
  84. 'tag' => $this->list->tag);
  85. $ptag = Profile_tag::pkeyGet($args);
  86. if (empty($ptag)) {
  87. // TRANS: Client error displayed when trying to remove a list member that is not part of a list.
  88. $this->clientError(_('The user you are trying to remove from the list is not a member.'));
  89. }
  90. if (!$ptag->delete()) {
  91. // TRANS: Client error displayed when an unknown error occurs viewing list members.
  92. $this->clientError(_('An error occured.'), 500);
  93. }
  94. switch($this->format) {
  95. case 'xml':
  96. $this->showSingleXmlList($this->list);
  97. break;
  98. case 'json':
  99. $this->showSingleJsonList($this->list);
  100. break;
  101. default:
  102. // TRANS: Client error displayed when coming across a non-supported API method.
  103. $this->clientError(_('API method not found.'), 404);
  104. }
  105. return true;
  106. }
  107. /**
  108. * List the members of a list (people tagged)
  109. */
  110. function getUsers()
  111. {
  112. $fn = array($this->list, 'getTagged');
  113. list($this->users, $this->next_cursor, $this->prev_cursor) =
  114. Profile_list::getAtCursor($fn, array(), $this->cursor, 20);
  115. }
  116. }