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