]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/activityimporter.php
move activity importing code to two different queuehandler classes
[quix0rs-gnu-social.git] / lib / activityimporter.php
1 <?php
2 /**
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2010, StatusNet, Inc.
5  *
6  * class to import activities as part of a user's timeline
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  Cache
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  * Class comment
39  *
40  * @category  General
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
48 class ActivityImporter extends QueueHandler
49 {
50     private $trusted = false;
51
52     /**
53      * Function comment
54      *
55      * @param
56      *
57      * @return
58      */
59
60     function handle($data)
61     {
62         list($user, $author, $activity, $trusted) = $data;
63
64         $this->trusted = $trusted;
65
66         try {
67             switch ($activity->verb) {
68             case ActivityVerb::FOLLOW:
69                 $this->subscribeProfile($user, $author, $activity);
70                 break;
71             case ActivityVerb::JOIN:
72                 $this->joinGroup($user, $activity);
73                 break;
74             case ActivityVerb::POST:
75                 $this->postNote($user, $activity);
76                 break;
77             default:
78                 throw new Exception("Unknown verb: {$activity->verb}");
79             }
80         } catch (ClientException $ce) {
81             common_log(LOG_WARNING, $ce->getMessage());
82             return true;
83         } catch (ServerException $se) {
84             common_log(LOG_ERR, $ce->getMessage());
85             return false;
86         } catch (Exception $e) {
87             common_log(LOG_ERR, $ce->getMessage());
88             return false;
89         }
90         return true;
91     }
92     
93     function subscribeProfile($user, $author, $activity)
94     {
95         $profile = $user->getProfile();
96
97         if ($activity->objects[0]->id == $author->id) {
98             $other = $activity->actor;
99             $otherUser = User::staticGet('uri', $other->id);
100             
101             if (!empty($otherUser)) {
102                 $otherProfile = $otherUser->getProfile();
103             } else {
104                 throw new Exception("Can't force remote user to subscribe.");
105             }
106
107             // XXX: don't do this for untrusted input!
108
109             Subscription::start($otherProfile, $profile);
110
111         } else if (empty($activity->actor) 
112                    || $activity->actor->id == $author->id) {
113
114             $other = $activity->objects[0];
115
116             $otherProfile = Profile::fromUri($other->id);
117
118             if (empty($otherProfile)) {
119                 throw new ClientException(_("Unknown profile."));
120             }
121
122             Subscription::start($profile, $otherProfile);
123         } else {
124             throw new Exception("This activity seems unrelated to our user.");
125         }
126     }
127
128     function joinGroup($user, $activity)
129     {
130         // XXX: check that actor == subject
131
132         $uri = $activity->objects[0]->id;
133
134         $group = User_group::staticGet('uri', $uri);
135
136         if (empty($group)) {
137             $oprofile = Ostatus_profile::ensureActivityObjectProfile($activity->objects[0]);
138             if (!$oprofile->isGroup()) {
139                 throw new Exception("Remote profile is not a group!");
140             }
141             $group = $oprofile->localGroup();
142         }
143
144         assert(!empty($group));
145
146         if (Event::handle('StartJoinGroup', array($group, $user))) {
147             Group_member::join($group->id, $user->id);
148             Event::handle('EndJoinGroup', array($group, $user));
149         }
150     }
151
152     // XXX: largely cadged from Ostatus_profile::processNote()
153
154     function postNote($user, $activity)
155     {
156         $note = $activity->objects[0];
157
158         $sourceUri = $note->id;
159
160         $notice = Notice::staticGet('uri', $sourceUri);
161
162         if (!empty($notice)) {
163             // This is weird.
164             $orig = clone($notice);
165             $notice->profile_id = $user->id;
166             $notice->update($orig);
167             return;
168         }
169
170         // Use summary as fallback for content
171
172         if (!empty($note->content)) {
173             $sourceContent = $note->content;
174         } else if (!empty($note->summary)) {
175             $sourceContent = $note->summary;
176         } else if (!empty($note->title)) {
177             $sourceContent = $note->title;
178         } else {
179             // @fixme fetch from $sourceUrl?
180             // @todo i18n FIXME: use sprintf and add i18n.
181             throw new ClientException("No content for notice {$sourceUri}.");
182         }
183
184         // Get (safe!) HTML and text versions of the content
185
186         $rendered = $this->purify($sourceContent);
187         $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
188
189         $shortened = $user->shortenLinks($content);
190
191         $options = array('is_local' => Notice::LOCAL_PUBLIC,
192                          'uri' => $sourceUri,
193                          'rendered' => $rendered,
194                          'replies' => array(),
195                          'groups' => array(),
196                          'tags' => array(),
197                          'urls' => array());
198
199         // Check for optional attributes...
200
201         if (!empty($activity->time)) {
202             $options['created'] = common_sql_date($activity->time);
203         }
204
205         if ($activity->context) {
206             // Any individual or group attn: targets?
207
208             list($options['groups'], $options['replies']) = $this->filterAttention($activity->context->attention);
209
210             // Maintain direct reply associations
211             // @fixme what about conversation ID?
212             if (!empty($activity->context->replyToID)) {
213                 $orig = Notice::staticGet('uri',
214                                           $activity->context->replyToID);
215                 if (!empty($orig)) {
216                     $options['reply_to'] = $orig->id;
217                 }
218             }
219
220             $location = $activity->context->location;
221
222             if ($location) {
223                 $options['lat'] = $location->lat;
224                 $options['lon'] = $location->lon;
225                 if ($location->location_id) {
226                     $options['location_ns'] = $location->location_ns;
227                     $options['location_id'] = $location->location_id;
228                 }
229             }
230         }
231
232         // Atom categories <-> hashtags
233
234         foreach ($activity->categories as $cat) {
235             if ($cat->term) {
236                 $term = common_canonical_tag($cat->term);
237                 if ($term) {
238                     $options['tags'][] = $term;
239                 }
240             }
241         }
242
243         // Atom enclosures -> attachment URLs
244         foreach ($activity->enclosures as $href) {
245             // @fixme save these locally or....?
246             $options['urls'][] = $href;
247         }
248
249         $saved = Notice::saveNew($user->id,
250                                  $content,
251                                  'restore', // TODO: restore the actual source
252                                  $options);
253
254         return $saved;
255     }
256
257     function filterAttention($attn)
258     {
259         $groups = array();
260         $replies = array();
261
262         foreach (array_unique($attn) as $recipient) {
263
264             // Is the recipient a local user?
265
266             $user = User::staticGet('uri', $recipient);
267
268             if ($user) {
269                 // @fixme sender verification, spam etc?
270                 $replies[] = $recipient;
271                 continue;
272             }
273
274             // Is the recipient a remote group?
275             $oprofile = Ostatus_profile::ensureProfileURI($recipient);
276
277             if ($oprofile) {
278                 if (!$oprofile->isGroup()) {
279                     // may be canonicalized or something
280                     $replies[] = $oprofile->uri;
281                 }
282                 continue;
283             }
284
285             // Is the recipient a local group?
286             // @fixme uri on user_group isn't reliable yet
287             // $group = User_group::staticGet('uri', $recipient);
288             $id = OStatusPlugin::localGroupFromUrl($recipient);
289
290             if ($id) {
291                 $group = User_group::staticGet('id', $id);
292                 if ($group) {
293                     // Deliver to all members of this local group if allowed.
294                     $profile = $sender->localProfile();
295                     if ($profile->isMember($group)) {
296                         $groups[] = $group->id;
297                     } else {
298                         common_log(LOG_INFO, "Skipping reply to local group {$group->nickname} as sender {$profile->id} is not a member");
299                     }
300                     continue;
301                 } else {
302                     common_log(LOG_INFO, "Skipping reply to bogus group $recipient");
303                 }
304             }
305         }
306
307         return array($groups, $replies);
308     }
309  
310
311     function purify($content)
312     {
313         $config = array('safe' => 1,
314                         'deny_attribute' => 'id,style,on*');
315         return htmLawed($content, $config);
316     }
317 }