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