shownotice.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Show a single notice
  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 Personal
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2008-2011 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) {
  30. exit(1);
  31. }
  32. require_once INSTALLDIR . '/lib/notices/noticelist.php';
  33. /**
  34. * Show a single notice
  35. *
  36. * @category Personal
  37. * @package StatusNet
  38. * @author Evan Prodromou <evan@status.net>
  39. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  40. * @link http://status.net/
  41. */
  42. class ShownoticeAction extends ManagedAction
  43. {
  44. public $target;
  45. public $user;
  46. protected $redirectAfterLogin = true;
  47. /**
  48. * Notice object to show
  49. */
  50. public $notice = null;
  51. /**
  52. * Profile of the notice object
  53. */
  54. public $profile = null;
  55. /**
  56. * Avatar of the profile of the notice object
  57. */
  58. public $avatar = null;
  59. /**
  60. * Load attributes based on database arguments
  61. *
  62. * Loads all the DB stuff
  63. *
  64. * @param array $args $_REQUEST array
  65. *
  66. * @return success flag
  67. */
  68. protected function prepare(array $args=[])
  69. {
  70. parent::prepare($args);
  71. if ($this->boolean('ajax')) {
  72. GNUsocial::setApi(true);
  73. }
  74. $this->notice = $this->getNotice();
  75. $this->target = $this->notice;
  76. if (!$this->notice->inScope($this->scoped)) {
  77. // TRANS: Client exception thrown when trying a view a notice the user has no access to.
  78. throw new ClientException(_('Access restricted.'), 403);
  79. }
  80. $this->profile = $this->notice->getProfile();
  81. if (!$this->profile instanceof Profile) {
  82. // TRANS: Server error displayed trying to show a notice without a connected profile.
  83. $this->serverError(_('Notice has no profile.'), 500);
  84. }
  85. try {
  86. $this->user = $this->profile->getUser();
  87. } catch (NoSuchUserException $e) {
  88. // FIXME: deprecate $this->user stuff in extended classes
  89. $this->user = null;
  90. }
  91. try {
  92. $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
  93. } catch (Exception $e) {
  94. $this->avatar = null;
  95. }
  96. return true;
  97. }
  98. /**
  99. * Fetch the notice to show. This may be overridden by child classes to
  100. * customize what we fetch without duplicating all of the prepare() method.
  101. *
  102. * @return Notice
  103. */
  104. protected function getNotice()
  105. {
  106. $id = $this->arg('notice');
  107. $notice = null;
  108. try {
  109. $notice = Notice::getByID($id);
  110. // Alright, got it!
  111. return $notice;
  112. } catch (NoResultException $e) {
  113. // Hm, not found.
  114. $deleted = null;
  115. Event::handle('IsNoticeDeleted', [$id, &$deleted]);
  116. if ($deleted === true) {
  117. // TRANS: Client error displayed trying to show a deleted notice.
  118. throw new ClientException(_('Notice deleted.'), 410);
  119. }
  120. }
  121. // TRANS: Client error displayed trying to show a non-existing notice.
  122. throw new ClientException(_('No such notice.'), 404);
  123. }
  124. /**
  125. * Is this action read-only?
  126. *
  127. * @return boolean true
  128. */
  129. public function isReadOnly($args)
  130. {
  131. return true;
  132. }
  133. /**
  134. * Last-modified date for page
  135. *
  136. * When was the content of this page last modified? Based on notice,
  137. * profile, avatar.
  138. *
  139. * @return int last-modified date as unix timestamp
  140. */
  141. public function lastModified()
  142. {
  143. return max(strtotime($this->notice->modified),
  144. strtotime($this->profile->modified),
  145. ($this->avatar) ? strtotime($this->avatar->modified) : 0);
  146. }
  147. /**
  148. * An entity tag for this page
  149. *
  150. * Shows the ETag for the page, based on the notice ID and timestamps
  151. * for the notice, profile, and avatar. It's weak, since we change
  152. * the date text "one hour ago", etc.
  153. *
  154. * @return string etag
  155. */
  156. public function etag()
  157. {
  158. $avtime = ($this->avatar) ?
  159. strtotime($this->avatar->modified) : 0;
  160. return 'W/"' . implode(':', [$this->arg('action'),
  161. common_user_cache_hash(),
  162. common_language(),
  163. $this->notice->id,
  164. strtotime($this->notice->created),
  165. strtotime($this->profile->modified),
  166. $avtime]) . '"';
  167. }
  168. /**
  169. * Title of the page
  170. *
  171. * @return string title of the page
  172. */
  173. public function title()
  174. {
  175. return $this->notice->getTitle();
  176. }
  177. /**
  178. * Fill the content area of the page
  179. *
  180. * Shows a single notice list item.
  181. *
  182. * @return void
  183. */
  184. public function showContent()
  185. {
  186. $this->elementStart('ol', ['class' => 'notices xoxo']);
  187. $nli = new NoticeListItem($this->notice, $this);
  188. $nli->show();
  189. $this->elementEnd('ol');
  190. }
  191. /**
  192. * Don't show page notice
  193. *
  194. * @return void
  195. */
  196. public function showPageNoticeBlock()
  197. {
  198. }
  199. public function getFeeds()
  200. {
  201. return [
  202. new Feed(Feed::JSON,
  203. common_local_url('ApiStatusesShow',
  204. ['id' => $this->target->getID(),
  205. 'format' => 'json']),
  206. // TRANS: Title for link to single notice representation.
  207. // TRANS: %s is a user nickname.
  208. sprintf(_('Single notice (JSON)'))
  209. ),
  210. new Feed(Feed::ATOM,
  211. common_local_url('ApiStatusesShow',
  212. ['id' => $this->target->getID(),
  213. 'format' => 'atom']),
  214. // TRANS: Title for link to notice feed.
  215. // TRANS: %s is a user nickname.
  216. sprintf(_('Single notice (Atom)'))
  217. )
  218. ];
  219. }
  220. /**
  221. * Extra <head> content
  222. *
  223. * Facebook OpenGraph metadata.
  224. *
  225. * @return void
  226. */
  227. public function extraHead()
  228. {
  229. // Extras to aid in sharing notices to Facebook
  230. $avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
  231. $this->element('meta', ['property' => 'og:image',
  232. 'content' => $avatarUrl]);
  233. $this->element('meta', ['property' => 'og:description',
  234. 'content' => $this->notice->content]);
  235. }
  236. }