]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/shownotice.php
Stronger typing for NoticeListItem and so
[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         if ($notice instanceof Notice) {
118             // Alright, got it!
119             return $notice;
120         }
121
122         // Did we use to have it, and it got deleted?
123         $deleted = Deleted_notice::getKV('id', $id);
124         if ($deleted instanceof Deleted_notice) {
125             // TRANS: Client error displayed trying to show a deleted notice.
126             $this->clientError(_('Notice deleted.'), 410);
127         }
128         // TRANS: Client error displayed trying to show a non-existing notice.
129         $this->clientError(_('No such notice.'), 404);
130     }
131
132     /**
133      * Is this action read-only?
134      *
135      * @return boolean true
136      */
137     function isReadOnly($args)
138     {
139         return true;
140     }
141
142     /**
143      * Last-modified date for page
144      *
145      * When was the content of this page last modified? Based on notice,
146      * profile, avatar.
147      *
148      * @return int last-modified date as unix timestamp
149      */
150     function lastModified()
151     {
152         return max(strtotime($this->notice->modified),
153                    strtotime($this->profile->modified),
154                    ($this->avatar) ? strtotime($this->avatar->modified) : 0);
155     }
156
157     /**
158      * An entity tag for this page
159      *
160      * Shows the ETag for the page, based on the notice ID and timestamps
161      * for the notice, profile, and avatar. It's weak, since we change
162      * the date text "one hour ago", etc.
163      *
164      * @return string etag
165      */
166     function etag()
167     {
168         $avtime = ($this->avatar) ?
169           strtotime($this->avatar->modified) : 0;
170
171         return 'W/"' . implode(':', array($this->arg('action'),
172                                           common_user_cache_hash(),
173                                           common_language(),
174                                           $this->notice->id,
175                                           strtotime($this->notice->created),
176                                           strtotime($this->profile->modified),
177                                           $avtime)) . '"';
178     }
179
180     /**
181      * Title of the page
182      *
183      * @return string title of the page
184      */
185     function title()
186     {
187         $base = $this->profile->getFancyName();
188
189         // TRANS: Title of the page that shows a notice.
190         // TRANS: %1$s is a user name, %2$s is the notice creation date/time.
191         return sprintf(_('%1$s\'s status on %2$s'),
192                        $base,
193                        common_exact_date($this->notice->created));
194     }
195
196     /**
197      * Handle input
198      *
199      * Only handles get, so just show the page.
200      *
201      * @param array $args $_REQUEST data (unused)
202      *
203      * @return void
204      */
205     protected function handle()
206     {
207         parent::handle();
208
209         if (StatusNet::isAjax()) {
210             $this->showAjax();
211         } else {
212             $this->showPage();
213         }
214     }
215
216     /**
217      * Fill the content area of the page
218      *
219      * Shows a single notice list item.
220      *
221      * @return void
222      */
223     function showContent()
224     {
225         $this->elementStart('ol', array('class' => 'notices xoxo'));
226         $nli = new SingleNoticeItem($this->notice, $this);
227         $nli->show();
228         $this->elementEnd('ol');
229     }
230
231     function showAjax()
232     {
233         $this->startHTML('text/xml;charset=utf-8');
234         $this->elementStart('head');
235         // TRANS: Title for page that shows a notice.
236         $this->element('title', null, _m('TITLE','Notice'));
237         $this->elementEnd('head');
238         $this->elementStart('body');
239         $nli = new NoticeListItem($this->notice, $this);
240         $nli->show();
241         $this->elementEnd('body');
242         $this->endHTML();
243     }
244
245     /**
246      * Don't show page notice
247      *
248      * @return void
249      */
250     function showPageNoticeBlock()
251     {
252     }
253
254     /**
255      * Don't show aside
256      *
257      * @return void
258      */
259     function showAside() {
260     }
261
262     /**
263      * Extra <head> content
264      *
265      * We show the microid(s) for the author, if any.
266      *
267      * @return void
268      */
269     function extraHead()
270     {
271         $user = User::getKV($this->profile->id);
272
273         if (!$user) {
274             return;
275         }
276
277         if ($user->emailmicroid && $user->email && $this->notice->uri) {
278             $id = new Microid('mailto:'. $user->email,
279                               $this->notice->uri);
280             $this->element('meta', array('name' => 'microid',
281                                          'content' => $id->toString()));
282         }
283
284         // Extras to aid in sharing notices to Facebook
285         $avatarUrl = $this->profile->avatarUrl(AVATAR_PROFILE_SIZE);
286         $this->element('meta', array('property' => 'og:image',
287                                      'content' => $avatarUrl));
288         $this->element('meta', array('property' => 'og:description',
289                                      'content' => $this->notice->content));
290     }
291 }
292
293 // @todo FIXME: Class documentation missing.
294 class SingleNoticeItem extends DoFollowListItem
295 {
296     function avatarSize()
297     {
298         return AVATAR_STREAM_SIZE;
299     }
300 }