apidirectmessagenew.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Direct messaging implementation for GNU social
  18. *
  19. * @package GNUsocial
  20. * @author Adrian Lang <mail@adrianlang.de>
  21. * @author Evan Prodromou <evan@status.net>
  22. * @author Robin Millette <robin@millette.info>
  23. * @author Zach Copley <zach@status.net>
  24. * @copyright 2009, 2020 Free Software Foundation, Inc http://www.fsf.org
  25. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  26. */
  27. defined('GNUSOCIAL') || die();
  28. /**
  29. * Creates a new direct message from the authenticating user to
  30. * the user specified by id
  31. *
  32. * @category Plugin
  33. * @package GNUsocial
  34. * @author Adrian Lang <mail@adrianlang.de>
  35. * @author Evan Prodromou <evan@status.net>
  36. * @author Robin Millette <robin@millette.info>
  37. * @author Zach Copley <zach@status.net>
  38. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  39. */
  40. class ApiDirectMessageNewAction extends ApiAuthAction
  41. {
  42. protected $needPost = true;
  43. public $other = null; // Profile we're sending to
  44. public $content = null;
  45. /**
  46. * Take arguments for running
  47. *
  48. * @param array $args $_REQUEST args
  49. *
  50. * @return boolean success flag
  51. */
  52. protected function prepare(array $args = [])
  53. {
  54. parent::prepare($args);
  55. if (!$this->scoped instanceof Profile) {
  56. // TRANS: Client error when user not found for an API direct message action.
  57. $this->clientError(_('No such user.'), 404);
  58. }
  59. $this->content = $this->trimmed('text');
  60. $user_param = $this->trimmed('user');
  61. $user_id = $this->arg('user_id');
  62. $screen_name = $this->trimmed('screen_name');
  63. if (isset($user_param) || isset($user_id) || isset($screen_name)) {
  64. $this->other = $this->getTargetProfile($user_param);
  65. }
  66. return true;
  67. }
  68. /**
  69. * Handle the request
  70. *
  71. * Save the new message
  72. *
  73. * @return void
  74. */
  75. protected function handle()
  76. {
  77. parent::handle();
  78. if (empty($this->content)) {
  79. // TRANS: Client error displayed when no message text was submitted (406).
  80. $this->clientError(_('No message text!'), 406);
  81. } else {
  82. $content_shortened = $this->auth_user->shortenLinks($this->content);
  83. if (MessageModel::contentTooLong($content_shortened)) {
  84. // TRANS: Client error displayed when message content is too long.
  85. // TRANS: %d is the maximum number of characters for a message.
  86. $this->clientError(
  87. sprintf(_m('That\'s too long. Maximum message size is %d character.', 'That\'s too long. Maximum message size is %d characters.', MessageModel::maxContent()), MessageModel::maxContent()),
  88. 406
  89. );
  90. }
  91. }
  92. if (!$this->other instanceof Profile) {
  93. // TRANS: Client error displayed if a recipient user could not be found (403).
  94. $this->clientError(_('Recipient user not found.'), 403);
  95. } elseif (
  96. $this->other->isLocal()
  97. && !$this->scoped->mutuallySubscribed($this->other)
  98. ) {
  99. // TRANS: Client error displayed trying to direct message another user who's not a friend (403).
  100. $this->clientError(_('Cannot send direct messages to users who aren\'t your friend.'), 403);
  101. } elseif ($this->scoped->getID() === $this->other->getID()) {
  102. // Note: sending msgs to yourself is allowed by Twitter
  103. // TRANS: Client error displayed trying to direct message self (403).
  104. $this->clientError(_('Do not send a message to yourself; just say it to yourself quietly instead.'), 403);
  105. }
  106. // push other profile to the content, it will be
  107. // detected during Notice save
  108. if ($this->other->isLocal()) {
  109. $this->content = "@{$this->other->getNickname()} {$this->content}";
  110. } else {
  111. $this->content = '@' . substr($this->other->getAcctUri(), 5) . $this->content;
  112. }
  113. $message = MessageModel::saveNew(
  114. $this->scoped->getID(),
  115. $this->content,
  116. $this->source
  117. );
  118. Event::handle('SendDirectMessage', [$message]);
  119. mail_notify_message($message);
  120. if ($this->format == 'xml') {
  121. $this->showSingleXmlDirectMessage($message);
  122. } elseif ($this->format == 'json') {
  123. $this->showSingleJsondirectMessage($message);
  124. }
  125. }
  126. }