shownotice.php 7.6 KB

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