apifavoritecreate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Add a notice to 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. * @author Mikael Nordfeldth <mmn@hethane.se>
  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://gnu.io/
  32. */
  33. if (!defined('GNUSOCIAL')) { exit(1); }
  34. /**
  35. * Favorites the status specified in the ID parameter as the authenticating user.
  36. * Returns the favorite status when successful.
  37. *
  38. * @category API
  39. * @package StatusNet
  40. * @author Craig Andrews <candrews@integralblue.com>
  41. * @author Evan Prodromou <evan@status.net>
  42. * @author Zach Copley <zach@status.net>
  43. * @author Mikael Nordfeldth <mmn@hethane.se>
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  45. * @link http://gnu.io/
  46. */
  47. class ApiFavoriteCreateAction extends ApiAuthAction
  48. {
  49. var $notice = null;
  50. protected $needPost = true;
  51. protected function prepare(array $args=array())
  52. {
  53. parent::prepare($args);
  54. $this->notice = Notice::getKV($this->arg('id'));
  55. if (!empty($this->notice->repeat_of)) {
  56. common_log(LOG_DEBUG, 'Trying to Fave '.$this->notice->id.', repeat of '.$this->notice->repeat_of);
  57. common_log(LOG_DEBUG, 'Will Fave '.$this->notice->repeat_of.' instead');
  58. $real_notice_id = $this->notice->repeat_of;
  59. $this->notice = Notice::getKV($real_notice_id);
  60. }
  61. return true;
  62. }
  63. protected function handle()
  64. {
  65. parent::handle();
  66. if (!in_array($this->format, array('xml', 'json'))) {
  67. $this->clientError(
  68. // TRANS: Client error displayed when coming across a non-supported API method.
  69. _('API method not found.'),
  70. 404,
  71. $this->format
  72. );
  73. }
  74. if (empty($this->notice)) {
  75. $this->clientError(
  76. // TRANS: Client error displayed when requesting a status with a non-existing ID.
  77. _('No status found with that ID.'),
  78. 404,
  79. $this->format
  80. );
  81. }
  82. try {
  83. $stored = Fave::addNew($this->scoped, $this->notice);
  84. } catch (AlreadyFulfilledException $e) {
  85. // Note: Twitter lets you fave things repeatedly via API.
  86. $this->clientError($e->getMessage(), 403);
  87. }
  88. if ($this->format == 'xml') {
  89. $this->showSingleXmlStatus($this->notice);
  90. } elseif ($this->format == 'json') {
  91. $this->show_single_json_status($this->notice);
  92. }
  93. }
  94. }