]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/shownotice.php
Enable events for showing Attachment representation
[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 Action
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             StatusNet::setApi(true);
74         }
75
76         $this->notice = $this->getNotice();
77
78         if (!$this->notice->inScope($this->scoped)) {
79             // TRANS: Client exception thrown when trying a view a notice the user has no access to.
80             throw new ClientException(_('Not available.'), 403);
81         }
82
83         $this->profile = $this->notice->getProfile();
84
85         if (!$this->profile instanceof Profile) {
86             // TRANS: Server error displayed trying to show a notice without a connected profile.
87             $this->serverError(_('Notice has no profile.'), 500);
88         }
89
90         try {
91             $this->user = $this->profile->getUser();
92         } catch (NoSuchUserException $e) {
93             // FIXME: deprecate $this->user stuff in extended classes
94             $this->user = null;
95         }
96
97         try {
98             $this->avatar = $this->profile->getAvatar(AVATAR_PROFILE_SIZE);
99         } catch (Exception $e) {
100             $this->avatar = null;
101         }
102
103         return true;
104     }
105
106     /**
107      * Fetch the notice to show. This may be overridden by child classes to
108      * customize what we fetch without duplicating all of the prepare() method.
109      *
110      * @return Notice
111      */
112     protected function getNotice()
113     {
114         $id = $this->arg('notice');
115
116         $notice = Notice::getKV('id', $id);
117
118         if (!$notice instanceof Notice) {
119             // Did we used to have it, and it got deleted?
120             $deleted = Deleted_notice::getKV($id);
121             if ($deleted instanceof Deleted_notice) {
122                 // TRANS: Client error displayed trying to show a deleted notice.
123                 $this->clientError(_('Notice deleted.'), 410);
124             } else {
125                 // TRANS: Client error displayed trying to show a non-existing notice.
126                 $this->clientError(_('No such notice.'), 404);
127             }
128             return false;
129         }
130         return $notice;
131     }
132
133     /**
134      * Is this action read-only?
135      *
136      * @return boolean true
137      */
138     function isReadOnly($args)
139     {
140         return true;
141     }
142
143     /**
144      * Last-modified date for page
145      *
146      * When was the content of this page last modified? Based on notice,
147      * profile, avatar.
148      *
149      * @return int last-modified date as unix timestamp
150      */
151     function lastModified()
152     {
153         return max(strtotime($this->notice->modified),
154                    strtotime($this->profile->modified),
155                    ($this->avatar) ? strtotime($this->avatar->modified) : 0);
156     }
157
158     /**
159      * An entity tag for this page
160      *
161      * Shows the ETag for the page, based on the notice ID and timestamps
162      * for the notice, profile, and avatar. It's weak, since we change
163      * the date text "one hour ago", etc.
164      *
165      * @return string etag
166      */
167     function etag()
168     {
169         $avtime = ($this->avatar) ?
170           strtotime($this->avatar->modified) : 0;
171
172         return 'W/"' . implode(':', array($this->arg('action'),
173                                           common_user_cache_hash(),
174                                           common_language(),
175                                           $this->notice->id,
176                                           strtotime($this->notice->created),
177                                           strtotime($this->profile->modified),
178                                           $avtime)) . '"';
179     }
180
181     /**
182      * Title of the page
183      *
184      * @return string title of the page
185      */
186     function title()
187     {
188         $base = $this->profile->getFancyName();
189
190         // TRANS: Title of the page that shows a notice.
191         // TRANS: %1$s is a user name, %2$s is the notice creation date/time.
192         return sprintf(_('%1$s\'s status on %2$s'),
193                        $base,
194                        common_exact_date($this->notice->created));
195     }
196
197     /**
198      * Handle input
199      *
200      * Only handles get, so just show the page.
201      *
202      * @param array $args $_REQUEST data (unused)
203      *
204      * @return void
205      */
206     protected function handle()
207     {
208         parent::handle();
209
210         if (StatusNet::isAjax()) {
211             $this->showAjax();
212         } else {
213             $this->showPage();
214         }
215     }
216
217     /**
218      * Fill the content area of the page
219      *
220      * Shows a single notice list item.
221      *
222      * @return void
223      */
224     function showContent()
225     {
226         $this->elementStart('ol', array('class' => 'notices xoxo'));
227         $nli = new SingleNoticeItem($this->notice, $this);
228         $nli->show();
229         $this->elementEnd('ol');
230     }
231
232     function showAjax()
233     {
234         $this->startHTML('text/xml;charset=utf-8');
235         $this->elementStart('head');
236         // TRANS: Title for page that shows a notice.
237         $this->element('title', null, _m('TITLE','Notice'));
238         $this->elementEnd('head');
239         $this->elementStart('body');
240         $nli = new NoticeListItem($this->notice, $this);
241         $nli->show();
242         $this->elementEnd('body');
243         $this->endHTML();
244     }
245
246     /**
247      * Don't show page notice
248      *
249      * @return void
250      */
251     function showPageNoticeBlock()
252     {
253     }
254
255     /**
256      * Don't show aside
257      *
258      * @return void
259      */
260     function showAside() {
261     }
262
263     /**
264      * Extra <head> content
265      *
266      * We show the microid(s) for the author, if any.
267      *
268      * @return void
269      */
270     function extraHead()
271     {
272         $user = User::getKV($this->profile->id);
273
274         if (!$user) {
275             return;
276         }
277
278         if ($user->emailmicroid && $user->email && $this->notice->uri) {
279             $id = new Microid('mailto:'. $user->email,
280                               $this->notice->uri);
281             $this->element('meta', array('name' => 'microid',
282                                          'content' => $id->toString()));
283         }
284
285         $this->element('link',array('rel'=>'alternate',
286             'type'=>'application/json+oembed',
287             'href'=>common_local_url(
288                 'oembed',
289                 array(),
290                 array('format'=>'json','url'=>$this->notice->getUrl())),
291             'title'=>'oEmbed'),null);
292         $this->element('link',array('rel'=>'alternate',
293             'type'=>'text/xml+oembed',
294             'href'=>common_local_url(
295                 'oembed',
296                 array(),
297                 array('format'=>'xml','url'=>$this->notice->getUrl())),
298             'title'=>'oEmbed'),null);
299
300         // Extras to aid in sharing notices to Facebook
301         $avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
302         $this->element('meta', array('property' => 'og:image',
303                                      'content' => $avatarUrl));
304         $this->element('meta', array('property' => 'og:description',
305                                      'content' => $this->notice->content));
306     }
307 }
308
309 // @todo FIXME: Class documentation missing.
310 class SingleNoticeItem extends DoFollowListItem
311 {
312     function avatarSize()
313     {
314         return AVATAR_STREAM_SIZE;
315     }
316 }