apifriendshipsdestroy.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Unsubscribe to a user via API
  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 Dan Moore <dan@moore.cx>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Zach Copley <zach@status.net>
  27. * @copyright 2009-2010 StatusNet, Inc.
  28. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  29. * @link http://status.net/
  30. */
  31. if (!defined('STATUSNET')) {
  32. exit(1);
  33. }
  34. /**
  35. * Allows the authenticating users to unfollow (unsubscribe) the user specified in
  36. * the ID parameter. Returns the unfollowed user in the requested format when
  37. * successful. Returns a string describing the failure condition when unsuccessful.
  38. *
  39. * @category API
  40. * @package StatusNet
  41. * @author Dan Moore <dan@moore.cx>
  42. * @author Evan Prodromou <evan@status.net>
  43. * @author Zach Copley <zach@status.net>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://status.net/
  46. */
  47. class ApiFriendshipsDestroyAction extends ApiAuthAction
  48. {
  49. var $other = null;
  50. /**
  51. * Take arguments for running
  52. *
  53. * @param array $args $_REQUEST args
  54. *
  55. * @return boolean success flag
  56. *
  57. */
  58. function prepare($args)
  59. {
  60. parent::prepare($args);
  61. $this->user = $this->auth_user;
  62. $this->other = $this->getTargetProfile($this->arg('id'));
  63. return true;
  64. }
  65. /**
  66. * Handle the request
  67. *
  68. * Check the format and show the user info
  69. *
  70. * @param array $args $_REQUEST data (unused)
  71. *
  72. * @return void
  73. */
  74. function handle($args)
  75. {
  76. parent::handle($args);
  77. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  78. $this->clientError(
  79. // TRANS: Client error. POST is a HTTP command. It should not be translated.
  80. _('This method requires a POST.'),
  81. 400,
  82. $this->format
  83. );
  84. return;
  85. }
  86. if (!in_array($this->format, array('xml', 'json'))) {
  87. $this->clientError(
  88. // TRANS: Client error displayed when coming across a non-supported API method.
  89. _('API method not found.'),
  90. 404,
  91. $this->format
  92. );
  93. return;
  94. }
  95. if (empty($this->other)) {
  96. $this->clientError(
  97. // TRANS: Client error displayed when trying to unfollow a user that cannot be found.
  98. _('Could not unfollow user: User not found.'),
  99. 403,
  100. $this->format
  101. );
  102. return;
  103. }
  104. // Don't allow unsubscribing from yourself!
  105. if ($this->user->id == $this->other->id) {
  106. $this->clientError(
  107. // TRANS: Client error displayed when trying to unfollow self.
  108. _("You cannot unfollow yourself."),
  109. 403,
  110. $this->format
  111. );
  112. return;
  113. }
  114. // throws an exception on error
  115. Subscription::cancel($this->user->getProfile(), $this->other);
  116. $this->initDocument($this->format);
  117. $this->showProfile($this->other, $this->format);
  118. $this->endDocument($this->format);
  119. }
  120. }