newmessage.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * GNUsocial implementation of Direct Messages
  18. *
  19. * @package GNUsocial
  20. * @author Mikael Nordfeldth <mmn@hethane.se>
  21. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  22. * @copyright 2019 Free Software Foundation, Inc http://www.fsf.org
  23. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  24. */
  25. defined('GNUSOCIAL') || die();
  26. /**
  27. * Action for posting new direct messages
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @author Zach Copley <zach@status.net>
  33. * @author Sarven Capadisli <csarven@status.net>
  34. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  35. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  36. */
  37. class NewmessageAction extends FormAction
  38. {
  39. protected $form = 'Message';
  40. protected $to = null;
  41. protected $content = null;
  42. /**
  43. * Title of the page.
  44. * Note that this usually doesn't get called unless something went wrong.
  45. *
  46. * @return string page title
  47. */
  48. function title() : string
  49. {
  50. // TRANS: Page title for new direct message page.
  51. return _('New message');
  52. }
  53. protected function doPreparation()
  54. {
  55. if ($this->trimmed('to')) {
  56. $this->to = Profile::getKV('id', $this->trimmed('to'));
  57. if (!$this->to instanceof Profile) {
  58. // TRANS: Client error displayed trying to send a direct message to a non-existing user.
  59. $this->clientError(_('No such user.'), 404);
  60. }
  61. $this->formOpts['to'] = $this->to;
  62. }
  63. if ($this->trimmed('content')) {
  64. $this->content = $this->trimmed('content');
  65. $this->formOpts['content'] = $this->content;
  66. }
  67. if ($this->trimmed('to-box')) {
  68. $selected = explode(':', $this->trimmed('to-box'));
  69. if (sizeof($selected) == 2) {
  70. $this->to = Profile::getKV('id', $selected[1]);
  71. // validating later
  72. }
  73. }
  74. }
  75. protected function doPost()
  76. {
  77. assert($this->scoped instanceof Profile); // XXX: maybe an error instead...
  78. if (empty($this->content)) {
  79. // TRANS: Form validator error displayed trying to send a direct message without content.
  80. $this->clientError(_('No content!'));
  81. }
  82. $content_shortened = $this->scoped->shortenLinks($this->content);
  83. if (MessageModel::contentTooLong($content_shortened)) {
  84. // TRANS: Form validation error displayed when message content is too long.
  85. // TRANS: %d is the maximum number of characters for a message.
  86. $this->clientError(sprintf(_m('That\'s too long. Maximum message size is %d character.',
  87. 'That\'s too long. Maximum message size is %d characters.',
  88. MessageModel::maxContent()),
  89. MessageModel::maxContent()));
  90. }
  91. // validate recipients
  92. if (!$this->to instanceof Profile) {
  93. $mentions = common_find_mentions($this->content, $this->scoped);
  94. if (empty($mentions)) {
  95. $this->clientError(_('No recipients specified.'));
  96. }
  97. } else {
  98. // push to-box profile to the content message, will be
  99. // detected during Notice save
  100. try {
  101. if ($this->to->isLocal()) {
  102. $this->content = "@{$this->to->getNickname()} {$this->content}";
  103. } else {
  104. $this->content = '@' . substr($this->to->getAcctUri(), 5) . " {$this->content}";
  105. }
  106. } catch (ProfileNoAcctUriException $e) {
  107. // well, I'm no magician
  108. }
  109. }
  110. $message = MessageModel::saveNew($this->scoped, $this->content);
  111. Event::handle('SendDirectMessage', [$message]);
  112. mail_notify_message($message);
  113. if (GNUsocial::isAjax()) {
  114. // TRANS: Confirmation text after sending a direct message.
  115. // TRANS: %s is the direct message recipient.
  116. return sprintf(_('Direct message to %s sent.'), $this->to->getNickname());
  117. }
  118. $url = common_local_url('outbox', array('nickname' => $this->scoped->getNickname()));
  119. common_redirect($url, 303);
  120. }
  121. function showNoticeForm()
  122. {
  123. // Just don't show a NoticeForm
  124. }
  125. }