showgroupmessage.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Show a single group message
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category GroupPrivateMessage
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Show a single private group message
  37. *
  38. * @category GroupPrivateMessage
  39. * @package StatusNet
  40. * @author Evan Prodromou <evan@status.net>
  41. * @copyright 2011 StatusNet, Inc.
  42. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  43. * @link http://status.net/
  44. */
  45. class ShowgroupmessageAction extends Action
  46. {
  47. var $gm;
  48. var $group;
  49. var $sender;
  50. var $user;
  51. /**
  52. * For initializing members of the class.
  53. *
  54. * @param array $args misc. arguments
  55. *
  56. * @return boolean true
  57. * @throws ClientException
  58. * @throws ServerException
  59. */
  60. function prepare(array $args = [])
  61. {
  62. parent::prepare($args);
  63. $this->user = common_current_user();
  64. if (empty($this->user)) {
  65. // TRANS: Client exception thrown when trying to view group private messages without being logged in.
  66. throw new ClientException(_m('Only logged-in users can view private messages.'),
  67. 403);
  68. }
  69. $id = $this->trimmed('id');
  70. $this->gm = Group_message::getKV('id', $id);
  71. if (empty($this->gm)) {
  72. // TRANS: Client exception thrown when trying to view a non-existing group private message.
  73. throw new ClientException(_m('No such message.'), 404);
  74. }
  75. $this->group = User_group::getKV('id', $this->gm->to_group);
  76. if (empty($this->group)) {
  77. // TRANS: Server exception thrown when trying to view group private messages for a non-exsting group.
  78. throw new ServerException(_m('Group not found.'));
  79. }
  80. if (!$this->user->isMember($this->group)) {
  81. // TRANS: Client exception thrown when trying to view a group private message without being a group member.
  82. throw new ClientException(_m('Cannot read message.'), 403);
  83. }
  84. $this->sender = Profile::getKV('id', $this->gm->from_profile);
  85. if (empty($this->sender)) {
  86. // TRANS: Server exception thrown when trying to view a group private message without a sender.
  87. throw new ServerException(_m('No sender found.'));
  88. }
  89. return true;
  90. }
  91. /**
  92. * Handler method
  93. *
  94. * @return void
  95. */
  96. function handle()
  97. {
  98. $this->showPage();
  99. }
  100. /**
  101. * Title of the page
  102. */
  103. function title()
  104. {
  105. // TRANS: Title for private group message.
  106. // TRANS: %1$s is the sender name, %2$s is the group name, %3$s is a timestamp.
  107. return sprintf(_m('Message from %1$s to group %2$s on %3$s'),
  108. $this->sender->nickname,
  109. $this->group->nickname,
  110. common_exact_date($this->gm->created));
  111. }
  112. /**
  113. * Show the content area.
  114. */
  115. function showContent()
  116. {
  117. $this->elementStart('ul', 'notices messages');
  118. $gmli = new GroupMessageListItem($this, $this->gm);
  119. $gmli->show();
  120. $this->elementEnd('ul');
  121. }
  122. /**
  123. * Return true if read only.
  124. *
  125. * MAY override
  126. *
  127. * @param array $args other arguments
  128. *
  129. * @return boolean is read only action?
  130. */
  131. function isReadOnly($args)
  132. {
  133. return true;
  134. }
  135. /**
  136. * Return last modified, if applicable.
  137. *
  138. * MAY override
  139. *
  140. * @return string last modified http header
  141. */
  142. function lastModified()
  143. {
  144. return max(strtotime($this->group->modified),
  145. strtotime($this->sender->modified),
  146. strtotime($this->gm->modified));
  147. }
  148. /**
  149. * Return etag, if applicable.
  150. *
  151. * MAY override
  152. *
  153. * @return string etag http header
  154. */
  155. function etag()
  156. {
  157. try {
  158. $avatar = $this->sender->getAvatar(AVATAR_STREAM_SIZE);
  159. $avtime = strtotime($avatar->modified);
  160. } catch (Exception $e) {
  161. $avtime = 0;
  162. }
  163. return 'W/"' . implode(':', array($this->arg('action'),
  164. common_user_cache_hash(),
  165. common_language(),
  166. $this->gm->id,
  167. strtotime($this->sender->modified),
  168. strtotime($this->group->modified),
  169. $avtime)) . '"';
  170. }
  171. }