apifavoritedestroy.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Remote a notice from a user's list of favorite notices via the 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 Craig Andrews <candrews@integralblue.com>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Zach Copley <zach@status.net>
  27. * @copyright 2009 StatusNet, Inc.
  28. * @copyright 2009 Free Software Foundation, Inc http://www.fsf.org
  29. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  30. * @link http://status.net/
  31. */
  32. if (!defined('STATUSNET')) {
  33. exit(1);
  34. }
  35. /**
  36. * Un-favorites the status specified in the ID parameter as the authenticating user.
  37. * Returns the un-favorited status in the requested format when successful.
  38. *
  39. * @category API
  40. * @package StatusNet
  41. * @author Craig Andrews <candrews@integralblue.com>
  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 ApiFavoriteDestroyAction extends ApiAuthAction
  48. {
  49. var $notice = null;
  50. /**
  51. * Take arguments for running
  52. *
  53. * @param array $args $_REQUEST args
  54. *
  55. * @return boolean success flag
  56. */
  57. function prepare($args)
  58. {
  59. parent::prepare($args);
  60. $this->user = $this->auth_user;
  61. $this->notice = Notice::getKV($this->arg('id'));
  62. if ($this->notice->repeat_of != '' ) {
  63. common_log(LOG_DEBUG, 'Trying to unFave '.$this->notice->id);
  64. common_log(LOG_DEBUG, 'Will unFave '.$this->notice->repeat_of.' instead');
  65. $real_notice_id = $this->notice->repeat_of;
  66. $this->notice = Notice::getKV($real_notice_id);
  67. }
  68. return true;
  69. }
  70. /**
  71. * Handle the request
  72. *
  73. * Check the format and show the user info
  74. *
  75. * @param array $args $_REQUEST data (unused)
  76. *
  77. * @return void
  78. */
  79. function handle($args)
  80. {
  81. parent::handle($args);
  82. if ($_SERVER['REQUEST_METHOD'] != 'POST') {
  83. $this->clientError(
  84. // TRANS: Client error. POST is a HTTP command. It should not be translated.
  85. _('This method requires a POST.'),
  86. 400,
  87. $this->format
  88. );
  89. return;
  90. }
  91. if (!in_array($this->format, array('xml', 'json'))) {
  92. $this->clientError(
  93. // TRANS: Client error displayed when coming across a non-supported API method.
  94. _('API method not found.'),
  95. 404,
  96. $this->format
  97. );
  98. return;
  99. }
  100. if (empty($this->notice)) {
  101. $this->clientError(
  102. // TRANS: Client error displayed when trying to remove a favourite with an invalid ID.
  103. _('No status found with that ID.'),
  104. 404,
  105. $this->format
  106. );
  107. return;
  108. }
  109. $fave = new Fave();
  110. $fave->user_id = $this->user->id;
  111. $fave->notice_id = $this->notice->id;
  112. if (!$fave->find(true)) {
  113. $this->clientError(
  114. // TRANS: Client error displayed when trying to remove a favourite that was not a favourite.
  115. _('That status is not a favorite.'),
  116. 403,
  117. $this->favorite
  118. );
  119. return;
  120. }
  121. $result = $fave->delete();
  122. if (!$result) {
  123. common_log_db_error($fave, 'DELETE', __FILE__);
  124. $this->clientError(
  125. // TRANS: Client error displayed when removing a favourite has failed.
  126. _('Could not delete favorite.'),
  127. 404,
  128. $this->format
  129. );
  130. return;
  131. }
  132. Fave::blowCacheForProfileId($this->user->id);
  133. if ($this->format == 'xml') {
  134. $this->showSingleXmlStatus($this->notice);
  135. } elseif ($this->format == 'json') {
  136. $this->show_single_json_status($this->notice);
  137. }
  138. }
  139. }