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('GNUSOCIAL')) { exit(1); }
38 * @author Evan Prodromou <evan@status.net>
39 * @copyright 2010 StatusNet, Inc.
40 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
41 * @link http://status.net/
43 class ActivityImporter extends QueueHandler
45 private $trusted = false;
54 function handle($data)
56 list($user, $author, $activity, $trusted) = $data;
58 $this->trusted = $trusted;
63 if (Event::handle('StartImportActivity',
64 array($user, $author, $activity, $trusted, &$done))) {
65 switch ($activity->verb) {
66 case ActivityVerb::FOLLOW:
67 $this->subscribeProfile($user, $author, $activity);
69 case ActivityVerb::JOIN:
70 $this->joinGroup($user, $activity);
72 case ActivityVerb::POST:
73 $this->postNote($user, $author, $activity);
76 // TRANS: Client exception thrown when using an unknown verb for the activity importer.
77 throw new ClientException(sprintf(_("Unknown verb: \"%s\"."),$activity->verb));
79 Event::handle('EndImportActivity',
80 array($user, $author, $activity, $trusted));
83 } catch (Exception $e) {
84 common_log(LOG_ERR, $e->getMessage());
90 function subscribeProfile($user, $author, $activity)
92 $profile = $user->getProfile();
94 if ($activity->objects[0]->id == $author->id) {
95 if (!$this->trusted) {
96 // TRANS: Client exception thrown when trying to force a subscription for an untrusted user.
97 throw new ClientException(_('Cannot force subscription for untrusted user.'));
100 $other = $activity->actor;
101 $otherUser = User::getKV('uri', $other->id);
103 if (!$otherUser instanceof User) {
104 // TRANS: Client exception thrown when trying to force a remote user to subscribe.
105 throw new Exception(_('Cannot force remote user to subscribe.'));
108 $otherProfile = $otherUser->getProfile();
110 // XXX: don't do this for untrusted input!
112 Subscription::start($otherProfile, $profile);
113 } else if (empty($activity->actor)
114 || $activity->actor->id == $author->id) {
116 $other = $activity->objects[0];
118 $otherProfile = Profile::fromUri($other->id);
120 if (empty($otherProfile)) {
121 // TRANS: Client exception thrown when trying to subscribe to an unknown profile.
122 throw new ClientException(_('Unknown profile.'));
125 Subscription::start($profile, $otherProfile);
127 // TRANS: Client exception thrown when trying to import an event not related to the importing user.
128 throw new Exception(_('This activity seems unrelated to our user.'));
132 function joinGroup($user, $activity)
134 // XXX: check that actor == subject
136 $uri = $activity->objects[0]->id;
138 $group = User_group::getKV('uri', $uri);
140 if (!$group instanceof User_group) {
141 $oprofile = Ostatus_profile::ensureActivityObjectProfile($activity->objects[0]);
142 if (!$oprofile->isGroup()) {
143 // TRANS: Client exception thrown when trying to join a remote group that is not a group.
144 throw new ClientException(_('Remote profile is not a group!'));
146 $group = $oprofile->localGroup();
149 assert(!empty($group));
151 if ($user->isMember($group)) {
152 // TRANS: Client exception thrown when trying to join a group the importing user is already a member of.
153 throw new ClientException(_("User is already a member of this group."));
156 $user->joinGroup($group);
159 // XXX: largely cadged from Ostatus_profile::processNote()
161 function postNote($user, $author, $activity)
163 $note = $activity->objects[0];
165 $sourceUri = $note->id;
167 $notice = Notice::getKV('uri', $sourceUri);
169 if ($notice instanceof Notice) {
171 common_log(LOG_INFO, "Notice {$sourceUri} already exists.");
173 if ($this->trusted) {
175 $profile = $notice->getProfile();
177 $uri = $profile->getUri();
179 if ($uri === $author->id) {
180 common_log(LOG_INFO, sprintf('Updating notice author from %s to %s', $author->id, $user->getUri()));
181 $orig = clone($notice);
182 $notice->profile_id = $user->id;
183 $notice->update($orig);
186 // TRANS: Client exception thrown when trying to import a notice by another user.
187 // TRANS: %1$s is the source URI of the notice, %2$s is the URI of the author.
188 throw new ClientException(sprintf(_('Already know about notice %1$s and '.
189 ' it has a different author %2$s.'),
193 // TRANS: Client exception thrown when trying to overwrite the author information for a non-trusted user during import.
194 throw new ClientException(_('Not overwriting author info for non-trusted user.'));
198 // Use summary as fallback for content
200 if (!empty($note->content)) {
201 $sourceContent = $note->content;
202 } else if (!empty($note->summary)) {
203 $sourceContent = $note->summary;
204 } else if (!empty($note->title)) {
205 $sourceContent = $note->title;
207 // @fixme fetch from $sourceUrl?
208 // TRANS: Client exception thrown when trying to import a notice without content.
209 // TRANS: %s is the notice URI.
210 throw new ClientException(sprintf(_('No content for notice %s.'),$sourceUri));
213 // Get (safe!) HTML and text versions of the content
215 $rendered = $this->purify($sourceContent);
216 $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
218 $shortened = $user->shortenLinks($content);
220 $options = array('is_local' => Notice::LOCAL_PUBLIC,
222 'rendered' => $rendered,
223 'replies' => array(),
227 'distribute' => false);
229 // Check for optional attributes...
231 if (!empty($activity->time)) {
232 $options['created'] = common_sql_date($activity->time);
235 if ($activity->context) {
236 // Any individual or group attn: targets?
238 list($options['groups'], $options['replies']) = $this->filterAttention($activity->context->attention);
240 // Maintain direct reply associations
241 // @fixme what about conversation ID?
242 if (!empty($activity->context->replyToID)) {
243 $orig = Notice::getKV('uri', $activity->context->replyToID);
244 if ($orig instanceof Notice) {
245 $options['reply_to'] = $orig->id;
249 $location = $activity->context->location;
252 $options['lat'] = $location->lat;
253 $options['lon'] = $location->lon;
254 if ($location->location_id) {
255 $options['location_ns'] = $location->location_ns;
256 $options['location_id'] = $location->location_id;
261 // Atom categories <-> hashtags
263 foreach ($activity->categories as $cat) {
265 $term = common_canonical_tag($cat->term);
267 $options['tags'][] = $term;
272 // Atom enclosures -> attachment URLs
273 foreach ($activity->enclosures as $href) {
274 // @fixme save these locally or....?
275 $options['urls'][] = $href;
278 common_log(LOG_INFO, "Saving notice {$options['uri']}");
280 $saved = Notice::saveNew($user->id,
282 'restore', // TODO: restore the actual source
288 protected function filterAttention(array $attn)
290 $groups = array(); // TODO: context->attention
291 $replies = array(); // TODO: context->attention
293 foreach ($attn as $recipient=>$type) {
295 // Is the recipient a local user?
297 $user = User::getKV('uri', $recipient);
299 if ($user instanceof User) {
300 // TODO: @fixme sender verification, spam etc?
301 $replies[] = $recipient;
305 // Is the recipient a remote group?
306 $oprofile = Ostatus_profile::ensureProfileURI($recipient);
309 if (!$oprofile->isGroup()) {
310 // may be canonicalized or something
311 $replies[] = $oprofile->uri;
316 // Is the recipient a local group?
317 // TODO: @fixme uri on user_group isn't reliable yet
318 // $group = User_group::getKV('uri', $recipient);
319 $id = OStatusPlugin::localGroupFromUrl($recipient);
322 $group = User_group::getKV('id', $id);
324 // Deliver to all members of this local group if allowed.
325 $profile = $sender->localProfile();
326 if ($profile->isMember($group)) {
327 $groups[] = $group->id;
329 common_log(LOG_INFO, "Skipping reply to local group {$group->nickname} as sender {$profile->id} is not a member");
333 common_log(LOG_INFO, "Skipping reply to bogus group $recipient");
338 return array($groups, $replies);
342 function purify($content)
344 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
346 $config = array('safe' => 1,
347 'deny_attribute' => 'id,style,on*');
349 return htmLawed($content, $config);