3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2010, StatusNet, Inc.
6 * class to import activities as part of a user's timeline
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.
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.
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/>.
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/
31 if (!defined('STATUSNET')) {
32 // This check helps protect against security problems;
33 // your code file can't be executed directly from the web.
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/
47 class ActivityImporter extends QueueHandler
49 private $trusted = false;
58 function handle($data)
60 list($user, $author, $activity, $trusted) = $data;
62 $this->trusted = $trusted;
66 if (Event::handle('StartImportActivity',
67 array($user, $author, $activity, $trusted, &$done))) {
69 switch ($activity->verb) {
70 case ActivityVerb::FOLLOW:
71 $this->subscribeProfile($user, $author, $activity);
73 case ActivityVerb::JOIN:
74 $this->joinGroup($user, $activity);
76 case ActivityVerb::POST:
77 $this->postNote($user, $author, $activity);
80 // TRANS: Client exception thrown when using an unknown verb for the activity importer.
81 throw new ClientException(sprintf(_("Unknown verb: \"%s\"."),$activity->verb));
83 Event::handle('EndImportActivity',
84 array($user, $author, $activity, $trusted));
86 } catch (ClientException $ce) {
87 common_log(LOG_WARNING, $ce->getMessage());
89 } catch (ServerException $se) {
90 common_log(LOG_ERR, $se->getMessage());
92 } catch (Exception $e) {
93 common_log(LOG_ERR, $e->getMessage());
100 function subscribeProfile($user, $author, $activity)
102 $profile = $user->getProfile();
104 if ($activity->objects[0]->id == $author->id) {
105 if (!$this->trusted) {
106 // TRANS: Client exception thrown when trying to force a subscription for an untrusted user.
107 throw new ClientException(_('Cannot force subscription for untrusted user.'));
110 $other = $activity->actor;
111 $otherUser = User::staticGet('uri', $other->id);
113 if (!empty($otherUser)) {
114 $otherProfile = $otherUser->getProfile();
116 // TRANS: Client exception thrown when trying to force a remote user to subscribe.
117 throw new Exception(_('Cannot force remote user to subscribe.'));
120 // XXX: don't do this for untrusted input!
122 Subscription::start($otherProfile, $profile);
123 } else if (empty($activity->actor)
124 || $activity->actor->id == $author->id) {
126 $other = $activity->objects[0];
128 $otherProfile = Profile::fromUri($other->id);
130 if (empty($otherProfile)) {
131 // TRANS: Client exception thrown when trying to subscribe to an unknown profile.
132 throw new ClientException(_('Unknown profile.'));
135 Subscription::start($profile, $otherProfile);
137 // TRANS: Client exception thrown when trying to import an event not related to the importing user.
138 throw new Exception(_('This activity seems unrelated to our user.'));
142 function joinGroup($user, $activity)
144 // XXX: check that actor == subject
146 $uri = $activity->objects[0]->id;
148 $group = User_group::staticGet('uri', $uri);
151 $oprofile = Ostatus_profile::ensureActivityObjectProfile($activity->objects[0]);
152 if (!$oprofile->isGroup()) {
153 // TRANS: Client exception thrown when trying to join a remote group that is not a group.
154 throw new ClientException(_('Remote profile is not a group!'));
156 $group = $oprofile->localGroup();
159 assert(!empty($group));
161 if ($user->isMember($group)) {
162 // TRANS: Client exception thrown when trying to join a group the importing user is already a member of.
163 throw new ClientException(_("User is already a member of this group."));
166 $user->joinGroup($group);
169 // XXX: largely cadged from Ostatus_profile::processNote()
171 function postNote($user, $author, $activity)
173 $note = $activity->objects[0];
175 $sourceUri = $note->id;
177 $notice = Notice::staticGet('uri', $sourceUri);
179 if (!empty($notice)) {
181 common_log(LOG_INFO, "Notice {$sourceUri} already exists.");
183 if ($this->trusted) {
185 $profile = $notice->getProfile();
187 $uri = $profile->getUri();
189 if ($uri == $author->id) {
190 common_log(LOG_INFO, "Updating notice author from $author->id to $user->uri");
191 $orig = clone($notice);
192 $notice->profile_id = $user->id;
193 $notice->update($orig);
196 // TRANS: Client exception thrown when trying to import a notice by another user.
197 // TRANS: %1$s is the source URI of the notice, %2$s is the URI of the author.
198 throw new ClientException(sprintf(_('Already know about notice %1$s and '.
199 ' it has a different author %2$s.'),
203 // TRANS: Client exception thrown when trying to overwrite the author information for a non-trusted user during import.
204 throw new ClientException(_('Not overwriting author info for non-trusted user.'));
208 // Use summary as fallback for content
210 if (!empty($note->content)) {
211 $sourceContent = $note->content;
212 } else if (!empty($note->summary)) {
213 $sourceContent = $note->summary;
214 } else if (!empty($note->title)) {
215 $sourceContent = $note->title;
217 // @fixme fetch from $sourceUrl?
218 // TRANS: Client exception thrown when trying to import a notice without content.
219 // TRANS: %s is the notice URI.
220 throw new ClientException(sprintf(_('No content for notice %s.'),$sourceUri));
223 // Get (safe!) HTML and text versions of the content
225 $rendered = $this->purify($sourceContent);
226 $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
228 $shortened = $user->shortenLinks($content);
230 $options = array('is_local' => Notice::LOCAL_PUBLIC,
232 'rendered' => $rendered,
233 'replies' => array(),
237 'distribute' => false);
239 // Check for optional attributes...
241 if (!empty($activity->time)) {
242 $options['created'] = common_sql_date($activity->time);
245 if ($activity->context) {
246 // Any individual or group attn: targets?
248 list($options['groups'], $options['replies']) = $this->filterAttention($activity->context->attention);
250 // Maintain direct reply associations
251 // @fixme what about conversation ID?
252 if (!empty($activity->context->replyToID)) {
253 $orig = Notice::staticGet('uri',
254 $activity->context->replyToID);
256 $options['reply_to'] = $orig->id;
260 $location = $activity->context->location;
263 $options['lat'] = $location->lat;
264 $options['lon'] = $location->lon;
265 if ($location->location_id) {
266 $options['location_ns'] = $location->location_ns;
267 $options['location_id'] = $location->location_id;
272 // Atom categories <-> hashtags
274 foreach ($activity->categories as $cat) {
276 $term = common_canonical_tag($cat->term);
278 $options['tags'][] = $term;
283 // Atom enclosures -> attachment URLs
284 foreach ($activity->enclosures as $href) {
285 // @fixme save these locally or....?
286 $options['urls'][] = $href;
289 common_log(LOG_INFO, "Saving notice {$options['uri']}");
291 $saved = Notice::saveNew($user->id,
293 'restore', // TODO: restore the actual source
299 function filterAttention($attn)
304 foreach (array_unique($attn) as $recipient) {
306 // Is the recipient a local user?
308 $user = User::staticGet('uri', $recipient);
311 // @fixme sender verification, spam etc?
312 $replies[] = $recipient;
316 // Is the recipient a remote group?
317 $oprofile = Ostatus_profile::ensureProfileURI($recipient);
320 if (!$oprofile->isGroup()) {
321 // may be canonicalized or something
322 $replies[] = $oprofile->uri;
327 // Is the recipient a local group?
328 // @fixme uri on user_group isn't reliable yet
329 // $group = User_group::staticGet('uri', $recipient);
330 $id = OStatusPlugin::localGroupFromUrl($recipient);
333 $group = User_group::staticGet('id', $id);
335 // Deliver to all members of this local group if allowed.
336 $profile = $sender->localProfile();
337 if ($profile->isMember($group)) {
338 $groups[] = $group->id;
340 common_log(LOG_INFO, "Skipping reply to local group {$group->nickname} as sender {$profile->id} is not a member");
344 common_log(LOG_INFO, "Skipping reply to bogus group $recipient");
349 return array($groups, $replies);
353 function purify($content)
355 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
357 $config = array('safe' => 1,
358 'deny_attribute' => 'id,style,on*');
360 return htmLawed($content, $config);