apifriendshipscreate.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Subscribe to a user 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 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 follow (subscribe) the user specified in
  36. * the ID parameter. Returns the befriended 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 ApiFriendshipsCreateAction extends ApiAuthAction
  48. {
  49. protected $needPost = true;
  50. var $other = null;
  51. /**
  52. * Take arguments for running
  53. *
  54. * @param array $args $_REQUEST args
  55. *
  56. * @return boolean success flag
  57. *
  58. */
  59. protected function prepare(array $args=array())
  60. {
  61. parent::prepare($args);
  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. * @return void
  71. */
  72. protected function handle()
  73. {
  74. parent::handle();
  75. if (!in_array($this->format, array('xml', 'json'))) {
  76. // TRANS: Client error displayed when coming across a non-supported API method.
  77. $this->clientError(_('API method not found.'), 404);
  78. }
  79. if (empty($this->other)) {
  80. // TRANS: Client error displayed when trying follow who's profile could not be found.
  81. $this->clientError(_('Could not follow user: profile not found.'), 403);
  82. }
  83. if ($this->user->isSubscribed($this->other)) {
  84. $errmsg = sprintf(
  85. // TRANS: Client error displayed when trying to follow a user that's already being followed.
  86. // TRANS: %s is the nickname of the user that is already being followed.
  87. _('Could not follow user: %s is already on your list.'),
  88. $this->other->nickname
  89. );
  90. $this->clientError($errmsg, 403);
  91. }
  92. try {
  93. Subscription::start($this->user->getProfile(), $this->other);
  94. } catch (Exception $e) {
  95. $this->clientError($e->getMessage(), 403);
  96. }
  97. $this->initDocument($this->format);
  98. $this->showProfile($this->other, $this->format);
  99. $this->endDocument($this->format);
  100. }
  101. }