apidirectmessagenew.php 4.8 KB

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