shownotice.php 7.5 KB

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