]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubfavoritefeed.php
Merge branch '0.9.x' into testing
[quix0rs-gnu-social.git] / actions / atompubfavoritefeed.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * Feed of ActivityStreams 'favorite' actions
7  *
8  * PHP version 5
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU Affero General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Affero General Public License for more details.
19  *
20  * You should have received a copy of the GNU Affero General Public License
21  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
22  *
23  * @category  AtomPub
24  * @package   StatusNet
25  * @author    Evan Prodromou <evan@status.net>
26  * @copyright 2010 StatusNet, Inc.
27  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
28  * @link      http://status.net/
29  */
30
31 if (!defined('STATUSNET')) {
32     // This check helps protect against security problems;
33     // your code file can't be executed directly from the web.
34     exit(1);
35 }
36
37 require_once INSTALLDIR . '/lib/apiauth.php';
38
39 /**
40  * Feed of ActivityStreams 'favorite' actions
41  *
42  * @category  AtomPub
43  * @package   StatusNet
44  * @author    Evan Prodromou <evan@status.net>
45  * @copyright 2010 StatusNet, Inc.
46  * @license   http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
47  * @link      http://status.net/
48  */
49 class AtompubfavoritefeedAction extends ApiAuthAction
50 {
51     private $_profile = null;
52     private $_faves   = null;
53
54     /**
55      * For initializing members of the class.
56      *
57      * @param array $argarray misc. arguments
58      *
59      * @return boolean true
60      */
61     function prepare($argarray)
62     {
63         parent::prepare($argarray);
64
65         $this->_profile = Profile::staticGet('id', $this->trimmed('profile'));
66
67         if (empty($this->_profile)) {
68             // TRANS: Client exception thrown when requesting a favorite feed for a non-existing profile.
69             throw new ClientException(_('No such profile.'), 404);
70         }
71
72         $offset = ($this->page-1) * $this->count;
73         $limit  = $this->count + 1;
74
75         $this->_faves = Fave::byProfile($this->_profile->id,
76                                         $offset,
77                                         $limit);
78
79         return true;
80     }
81
82     /**
83      * Handler method
84      *
85      * @param array $argarray is ignored since it's now passed in in prepare()
86      *
87      * @return void
88      */
89     function handle($argarray=null)
90     {
91         parent::handle($argarray);
92
93         switch ($_SERVER['REQUEST_METHOD']) {
94         case 'HEAD':
95         case 'GET':
96             $this->showFeed();
97             break;
98         case 'POST':
99             $this->addFavorite();
100             break;
101         default:
102             // TRANS: Client exception thrown when using an unsupported HTTP method.
103             throw new ClientException(_('HTTP method not supported.'), 405);
104             return;
105         }
106
107         return;
108     }
109
110     /**
111      * Show a feed of favorite activity streams objects
112      *
113      * @return void
114      */
115     function showFeed()
116     {
117         header('Content-Type: application/atom+xml; charset=utf-8');
118
119         $url = common_local_url('AtomPubFavoriteFeed',
120                                 array('profile' => $this->_profile->id));
121
122         $feed = new Atom10Feed(true);
123
124         $feed->addNamespace('activity',
125                             'http://activitystrea.ms/spec/1.0/');
126
127         $feed->addNamespace('poco',
128                             'http://portablecontacts.net/spec/1.0');
129
130         $feed->addNamespace('media',
131                             'http://purl.org/syndication/atommedia');
132
133         $feed->id = $url;
134
135         $feed->setUpdated('now');
136
137         $feed->addAuthor($this->_profile->getBestName(),
138                          $this->_profile->getURI());
139
140         // TRANS: Title for Atom favorites feed.
141         // TRANS: %s is a user nickname.
142         $feed->setTitle(sprintf(_("%s favorites"),
143                                 $this->_profile->getBestName()));
144
145         // TRANS: Subtitle for Atom favorites feed.
146         // TRANS: %1$s is a user nickname, %2$s is the StatusNet sitename.
147         $feed->setSubtitle(sprintf(_("Notices %1$s has favorited on %2$s"),
148                                    $this->_profile->getBestName(),
149                                    common_config('site', 'name')));
150
151         $feed->addLink(common_local_url('showfavorites',
152                                         array('nickname' =>
153                                               $this->_profile->nickname)));
154
155         $feed->addLink($url,
156                        array('rel' => 'self',
157                              'type' => 'application/atom+xml'));
158
159         // If there's more...
160
161         if ($this->page > 1) {
162             $feed->addLink($url,
163                            array('rel' => 'first',
164                                  'type' => 'application/atom+xml'));
165
166             $feed->addLink(common_local_url('AtomPubFavoriteFeed',
167                                             array('profile' =>
168                                                   $this->_profile->id),
169                                             array('page' =>
170                                                   $this->page - 1)),
171                            array('rel' => 'prev',
172                                  'type' => 'application/atom+xml'));
173         }
174
175         if ($this->_faves->N > $this->count) {
176
177             $feed->addLink(common_local_url('AtomPubFavoriteFeed',
178                                             array('profile' =>
179                                                   $this->_profile->id),
180                                             array('page' =>
181                                                   $this->page + 1)),
182                            array('rel' => 'next',
183                                  'type' => 'application/atom+xml'));
184         }
185
186         $i = 0;
187
188         while ($this->_faves->fetch()) {
189
190             // We get one more than needed; skip that one
191
192             $i++;
193
194             if ($i > $this->count) {
195                 break;
196             }
197
198             $act = $this->_faves->asActivity();
199             $feed->addEntryRaw($act->asString(false, false, false));
200         }
201
202         $this->raw($feed->getString());
203     }
204
205     /**
206      * add a new favorite
207      *
208      * @return void
209      */
210     function addFavorite()
211     {
212         // XXX: Refactor this; all the same for atompub
213
214         if (empty($this->auth_user) ||
215             $this->auth_user->id != $this->_profile->id) {
216             // TRANS: Client exception thrown when trying to set a favorite for another user.
217             throw new ClientException(_("Cannot add someone else's".
218                                         " subscription."), 403);
219         }
220
221         $xml = file_get_contents('php://input');
222
223         $dom = DOMDocument::loadXML($xml);
224
225         if ($dom->documentElement->namespaceURI != Activity::ATOM ||
226             $dom->documentElement->localName != 'entry') {
227             // TRANS: Client error displayed when not using an Atom entry.
228             throw new ClientException(_('Atom post must be an Atom entry.'));
229             return;
230         }
231
232         $activity = new Activity($dom->documentElement);
233
234         $fave = null;
235
236         if (Event::handle('StartAtomPubNewActivity', array(&$activity))) {
237
238             if ($activity->verb != ActivityVerb::FAVORITE) {
239                 // TRANS: Client exception thrown when trying use an incorrect activity verb for the Atom pub method.
240                 throw new ClientException(_('Can only handle favorite activities.'));
241                 return;
242             }
243
244             $note = $activity->objects[0];
245
246             if (!in_array($note->type, array(ActivityObject::NOTE,
247                                              ActivityObject::BLOGENTRY,
248                                              ActivityObject::STATUS))) {
249                 // TRANS: Client exception thrown when trying favorite an object that is not a notice.
250                 throw new ClientException(_('Can only fave notices.'));
251                 return;
252             }
253
254             $notice = Notice::staticGet('uri', $note->id);
255
256             if (empty($notice)) {
257                 // XXX: import from listed URL or something
258                 // TRANS: Client exception thrown when trying favorite a notice without content.
259                 throw new ClientException(_('Unknown note.'));
260             }
261
262             $old = Fave::pkeyGet(array('user_id' => $this->auth_user->id,
263                                        'notice_id' => $notice->id));
264
265             if (!empty($old)) {
266                 // TRANS: Client exception thrown when trying favorite an already favorited notice.
267                 throw new ClientException(_('Already a favorite.'));
268             }
269
270             $profile = $this->auth_user->getProfile();
271
272             $fave = Fave::addNew($profile, $notice);
273
274             if (!empty($fave)) {
275                 $this->_profile->blowFavesCache();
276                 $this->notify($fave, $notice, $this->auth_user);
277             }
278
279             Event::handle('EndAtomPubNewActivity', array($activity, $fave));
280         }
281
282         if (!empty($fave)) {
283             $act = $fave->asActivity();
284
285             header('Content-Type: application/atom+xml; charset=utf-8');
286             header('Content-Location: ' . $act->selfLink);
287
288             $this->startXML();
289             $this->raw($act->asString(true, true, true));
290             $this->endXML();
291         }
292     }
293
294     /**
295      * Return true if read only.
296      *
297      * MAY override
298      *
299      * @param array $args other arguments
300      *
301      * @return boolean is read only action?
302      */
303     function isReadOnly($args)
304     {
305         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
306             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
307             return true;
308         } else {
309             return false;
310         }
311     }
312
313     /**
314      * Return last modified, if applicable.
315      *
316      * MAY override
317      *
318      * @return string last modified http header
319      */
320     function lastModified()
321     {
322         // For comparison with If-Last-Modified
323         // If not applicable, return null
324         return null;
325     }
326
327     /**
328      * Return etag, if applicable.
329      *
330      * MAY override
331      *
332      * @return string etag http header
333      */
334     function etag()
335     {
336         return null;
337     }
338
339     /**
340      * Does this require authentication?
341      *
342      * @return boolean true if delete, else false
343      */
344     function requiresAuth()
345     {
346         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
347             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
348             return false;
349         } else {
350             return true;
351         }
352     }
353
354     /**
355      * Notify the author of the favorite that the user likes their notice
356      *
357      * @param Favorite $fave   the favorite in question
358      * @param Notice   $notice the notice that's been faved
359      * @param User     $user   the user doing the favoriting
360      *
361      * @return void
362      */
363     function notify($fave, $notice, $user)
364     {
365         $other = User::staticGet('id', $notice->profile_id);
366         if ($other && $other->id != $user->id) {
367             if ($other->email && $other->emailnotifyfav) {
368                 mail_notify_fave($other, $user, $notice);
369             }
370             // XXX: notify by IM
371             // XXX: notify by SMS
372         }
373     }
374 }