]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/atompubfavoritefeed.php
Merge branch 'master' into 0.9.x
[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
50 class AtompubfavoritefeedAction extends ApiAuthAction
51 {
52     private $_profile = null;
53     private $_faves   = null;
54
55     /**
56      * For initializing members of the class.
57      *
58      * @param array $argarray misc. arguments
59      *
60      * @return boolean true
61      */
62
63     function prepare($argarray)
64     {
65         parent::prepare($argarray);
66
67         $this->_profile = Profile::staticGet('id', $this->trimmed('profile'));
68
69         if (empty($this->_profile)) {
70             throw new ClientException(_('No such profile'), 404);
71         }
72
73         $offset = ($this->page-1) * $this->count;
74         $limit  = $this->count + 1;
75
76         $this->_faves = Fave::byProfile($this->_profile->id,
77                                         $offset,
78                                         $limit);
79         
80         return true;
81     }
82
83     /**
84      * Handler method
85      *
86      * @param array $argarray is ignored since it's now passed in in prepare()
87      *
88      * @return void
89      */
90
91     function handle($argarray=null)
92     {
93         parent::handle($argarray);
94
95         switch ($_SERVER['REQUEST_METHOD']) {
96         case 'HEAD':
97         case 'GET':
98             $this->showFeed();
99             break;
100         case 'POST':
101             $this->addFavorite();
102             break;
103         default:
104             throw new ClientException(_('HTTP method not supported.'), 405);
105             return;
106         }
107
108         return;
109     }
110
111     /**
112      * Show a feed of favorite activity streams objects
113      *
114      * @return void
115      */
116
117     function showFeed()
118     {
119         header('Content-Type: application/atom+xml; charset=utf-8');
120
121         $url = common_local_url('AtomPubFavoriteFeed',
122                                 array('profile' => $this->_profile->id));
123
124         $feed = new Atom10Feed(true);
125
126         $feed->addNamespace('activity',
127                             'http://activitystrea.ms/spec/1.0/');
128
129         $feed->addNamespace('poco',
130                             'http://portablecontacts.net/spec/1.0');
131
132         $feed->addNamespace('media',
133                             'http://purl.org/syndication/atommedia');
134
135         $feed->id = $url;
136
137         $feed->setUpdated('now');
138
139         $feed->addAuthor($this->_profile->getBestName(),
140                          $this->_profile->getURI());
141
142         $feed->setTitle(sprintf(_("%s favorites"),
143                                 $this->_profile->getBestName()));
144
145         $feed->setSubtitle(sprintf(_("Notices %s has favorited to on %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
209     function addFavorite()
210     {
211         // XXX: Refactor this; all the same for atompub
212
213         if (empty($this->auth_user) ||
214             $this->auth_user->id != $this->_profile->id) {
215             throw new ClientException(_("Can't 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 error displayed when not using the POST verb.
238                 // TRANS: Do not translate POST.
239                 throw new ClientException(_('Can only handle Favorite activities.'));
240                 return;
241             }
242
243             $note = $activity->objects[0];
244
245             if (!in_array($note->type, array(ActivityObject::NOTE,
246                                              ActivityObject::BLOGENTRY,
247                                              ActivityObject::STATUS))) {
248                 throw new ClientException(_('Can only fave notices.'));
249                 return;
250             }
251
252             $notice = Notice::staticGet('uri', $note->id);
253
254             if (empty($notice)) {
255                 // XXX: import from listed URL or something
256                 throw new ClientException(_('Unknown note.'));
257             }
258
259             $old = Fave::pkeyGet(array('user_id' => $this->auth_user->id,
260                                        'notice_id' => $notice->id));
261
262             if (!empty($old)) {
263                 throw new ClientException(_('Already a favorite.'));
264             }
265
266             $profile = $this->auth_user->getProfile();
267
268             $fave = Fave::addNew($profile, $notice);
269
270             if (!empty($fave)) {
271                 $this->_profile->blowFavesCache();
272                 $this->notify($fave, $notice, $this->auth_user);
273             }
274
275             Event::handle('EndAtomPubNewActivity', array($activity, $fave));
276         }
277
278         if (!empty($fave)) {
279             $act = $fave->asActivity();
280
281             header('Content-Type: application/atom+xml; charset=utf-8');
282             header('Content-Location: ' . $act->selfLink);
283
284             $this->startXML();
285             $this->raw($act->asString(true, true, true));
286             $this->endXML();
287         }
288     }
289
290     /**
291      * Return true if read only.
292      *
293      * MAY override
294      *
295      * @param array $args other arguments
296      *
297      * @return boolean is read only action?
298      */
299
300     function isReadOnly($args)
301     {
302         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
303             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
304             return true;
305         } else {
306             return false;
307         }
308     }
309
310     /**
311      * Return last modified, if applicable.
312      *
313      * MAY override
314      *
315      * @return string last modified http header
316      */
317     function lastModified()
318     {
319         // For comparison with If-Last-Modified
320         // If not applicable, return null
321         return null;
322     }
323
324     /**
325      * Return etag, if applicable.
326      *
327      * MAY override
328      *
329      * @return string etag http header
330      */
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
343     function requiresAuth()
344     {
345         if ($_SERVER['REQUEST_METHOD'] == 'GET' ||
346             $_SERVER['REQUEST_METHOD'] == 'HEAD') {
347             return false;
348         } else {
349             return true;
350         }
351     }
352
353     /**
354      * Notify the author of the favorite that the user likes their notice
355      *
356      * @param Favorite $fave   the favorite in question
357      * @param Notice   $notice the notice that's been faved
358      * @param User     $user   the user doing the favoriting
359      *
360      * @return void
361      */
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 }