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