apidirectmessagenew.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Send a direct message 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 Adrian Lang <mail@adrianlang.de>
  25. * @author Evan Prodromou <evan@status.net>
  26. * @author Robin Millette <robin@millette.info>
  27. * @author Zach Copley <zach@status.net>
  28. * @copyright 2009 StatusNet, Inc.
  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('GNUSOCIAL')) { exit(1); }
  33. /**
  34. * Creates a new direct message from the authenticating user to
  35. * the user specified by id.
  36. *
  37. * @category API
  38. * @package StatusNet
  39. * @author Adrian Lang <mail@adrianlang.de>
  40. * @author Evan Prodromou <evan@status.net>
  41. * @author Robin Millette <robin@millette.info>
  42. * @author Zach Copley <zach@status.net>
  43. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  44. * @link http://status.net/
  45. */
  46. class ApiDirectMessageNewAction extends ApiAuthAction
  47. {
  48. protected $needPost = true;
  49. var $other = null; // Profile we're sending to
  50. var $content = null;
  51. /**
  52. * Take arguments for running
  53. *
  54. * @param array $args $_REQUEST args
  55. *
  56. * @return boolean success flag
  57. */
  58. protected function prepare(array $args=array())
  59. {
  60. parent::prepare($args);
  61. if (!$this->scoped instanceof Profile) {
  62. // TRANS: Client error when user not found for an API direct message action.
  63. $this->clientError(_('No such user.'), 404);
  64. }
  65. $this->content = $this->trimmed('text');
  66. $user_param = $this->trimmed('user');
  67. $user_id = $this->arg('user_id');
  68. $screen_name = $this->trimmed('screen_name');
  69. if (isset($user_param) || isset($user_id) || isset($screen_name)) {
  70. $this->other = $this->getTargetProfile($user_param);
  71. }
  72. return true;
  73. }
  74. /**
  75. * Handle the request
  76. *
  77. * Save the new message
  78. *
  79. * @return void
  80. */
  81. protected function handle()
  82. {
  83. parent::handle();
  84. if (empty($this->content)) {
  85. // TRANS: Client error displayed when no message text was submitted (406).
  86. $this->clientError(_('No message text!'), 406);
  87. } else {
  88. $content_shortened = $this->auth_user->shortenLinks($this->content);
  89. if (Message::contentTooLong($content_shortened)) {
  90. // TRANS: Client error displayed when message content is too long.
  91. // TRANS: %d is the maximum number of characters for a message.
  92. $this->clientError(
  93. sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', Message::maxContent()), Message::maxContent()),
  94. 406);
  95. }
  96. }
  97. if (!$this->other instanceof Profile) {
  98. // TRANS: Client error displayed if a recipient user could not be found (403).
  99. $this->clientError(_('Recipient user not found.'), 403);
  100. } else if (!$this->scoped->mutuallySubscribed($this->other)) {
  101. // TRANS: Client error displayed trying to direct message another user who's not a friend (403).
  102. $this->clientError(_('Cannot send direct messages to users who aren\'t your friend.'), 403);
  103. } else if ($this->scoped->getID() === $this->other->getID()) {
  104. // Note: sending msgs to yourself is allowed by Twitter
  105. // TRANS: Client error displayed trying to direct message self (403).
  106. $this->clientError(_('Do not send a message to yourself; just say it to yourself quietly instead.'), 403);
  107. }
  108. $message = Message::saveNew(
  109. $this->scoped->getID(),
  110. $this->other->getID(),
  111. html_entity_decode($this->content, ENT_NOQUOTES, 'UTF-8'),
  112. $this->source
  113. );
  114. $message->notify();
  115. if ($this->format == 'xml') {
  116. $this->showSingleXmlDirectMessage($message);
  117. } elseif ($this->format == 'json') {
  118. $this->showSingleJsondirectMessage($message);
  119. }
  120. }
  121. }