showmessage.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 showing a single message
  28. *
  29. * @category Plugin
  30. * @package GNUsocial
  31. * @author Evan Prodromou <evan@status.net>
  32. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  33. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  34. */
  35. class ShowmessageAction extends Action
  36. {
  37. protected $message = null;
  38. protected $from = null;
  39. protected $attentions = null;
  40. protected $user = null;
  41. /**
  42. * Load attributes based on database arguments.
  43. *
  44. * @param array $args $_REQUEST array
  45. * @return bool success flag
  46. */
  47. function prepare($args = [])
  48. {
  49. parent::prepare($args);
  50. if (!$this->trimmed('message')) {
  51. return true;
  52. }
  53. $this->message = Notice::getKV('id', $this->trimmed('message'));
  54. if (!$this->message instanceof Notice) {
  55. // TRANS: Client error displayed requesting a single message that does not exist.
  56. $this->clientError(_m('No such message.'), 404);
  57. }
  58. $this->from = $this->message->getProfile();
  59. $this->attentions = $this->message->getAttentionProfiles();
  60. $this->user = common_current_user();
  61. if (empty($this->user) || $this->user->getID() != $this->from->getID()) {
  62. $receiver = false;
  63. foreach ($this->attentions as $attention) {
  64. if ($this->user->getID() == $attention->getID()) {
  65. $receiver = true;
  66. break;
  67. }
  68. }
  69. if (!$receiver) {
  70. // TRANS: Client error displayed requesting a single direct message the requesting user was not a party in.
  71. throw new ClientException(_m('Only the sender and recipients may read this message.'), 403);
  72. }
  73. }
  74. return true;
  75. }
  76. /**
  77. * Handler method.
  78. *
  79. * @return void
  80. */
  81. function handle()
  82. {
  83. $this->showPage();
  84. }
  85. /**
  86. * Title of the page.
  87. *
  88. * @return string page title
  89. */
  90. function title() : string
  91. {
  92. if ($this->user->getID() == $this->from->getID()) {
  93. if (sizeof($this->attentions) > 1) {
  94. return sprintf(_m('Message to many on %1$s'), common_exact_date($this->message->getCreated()));
  95. } else {
  96. $to = Profile::getKV('id', $this->attentions[0]->getID());
  97. // @todo FIXME: Might be nice if the timestamp could be localised.
  98. // TRANS: Page title for single direct message display when viewing user is the sender.
  99. // TRANS: %1$s is the addressed user's nickname, $2$s is a timestamp.
  100. return sprintf(_m('Message to %1$s on %2$s'),
  101. $to->getBestName(),
  102. common_exact_date($this->message->getCreated()));
  103. }
  104. } else {
  105. // @todo FIXME: Might be nice if the timestamp could be localised.
  106. // TRANS: Page title for single message display.
  107. // TRANS: %1$s is the sending user's nickname, $2$s is a timestamp.
  108. return sprintf(_m('Message from %1$s on %2$s'),
  109. $this->from->getBestName(),
  110. common_exact_date($this->message->getCreated()));
  111. }
  112. }
  113. /**
  114. * Show content.
  115. *
  116. * @return void
  117. */
  118. function showContent()
  119. {
  120. $this->elementStart('ul', 'notices messages');
  121. $ml = new ShowMessageListItem($this, $this->message, $this->user, $this->from, $this->attentions);
  122. $ml->show();
  123. $this->elementEnd('ul');
  124. }
  125. /**
  126. * Is this action read-only?
  127. *
  128. * @param array $args other arguments
  129. * @return bool true if read-only action, false otherwise
  130. */
  131. function isReadOnly($args) : bool
  132. {
  133. return true;
  134. }
  135. /**
  136. * Don't show aside
  137. *
  138. * @return void
  139. */
  140. function showAside() {
  141. }
  142. }
  143. /**
  144. * showmessage action's MessageListItem widget.
  145. *
  146. * @category Plugin
  147. * @package GNUsocial
  148. * @author Evan Prodromou <evan@status.net>
  149. * @author Bruno Casteleiro <brunoccast@fc.up.pt>
  150. * @license https://www.gnu.org/licenses/agpl.html GNU AGPL v3 or later
  151. */
  152. class ShowMessageListItem extends MessageListItem
  153. {
  154. protected $user;
  155. protected $from;
  156. protected $attentions;
  157. function __construct($out, $message, $user, $from, $attentions)
  158. {
  159. parent::__construct($out, $message);
  160. $this->user = $user;
  161. $this->from = $from;
  162. $this->attentions = $attentions;
  163. }
  164. function getMessageProfile() : ?Profile
  165. {
  166. return $this->user->getID() == $this->from->getID() ?
  167. $this->attentions[0] : $this->from;
  168. }
  169. }