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