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/
48 class ActivityImporter extends QueueHandler
50 private $trusted = false;
60 function handle($data)
62 list($user, $author, $activity, $trusted) = $data;
64 $this->trusted = $trusted;
67 switch ($activity->verb) {
68 case ActivityVerb::FOLLOW:
69 $this->subscribeProfile($user, $author, $activity);
71 case ActivityVerb::JOIN:
72 $this->joinGroup($user, $activity);
74 case ActivityVerb::POST:
75 $this->postNote($user, $author, $activity);
78 throw new Exception("Unknown verb: {$activity->verb}");
80 } catch (ClientException $ce) {
81 common_log(LOG_WARNING, $ce->getMessage());
83 } catch (ServerException $se) {
84 common_log(LOG_ERR, $se->getMessage());
86 } catch (Exception $e) {
87 common_log(LOG_ERR, $e->getMessage());
93 function subscribeProfile($user, $author, $activity)
95 $profile = $user->getProfile();
97 if ($activity->objects[0]->id == $author->id) {
99 if (!$this->trusted) {
100 throw new ClientException(_("Can't force subscription for untrusted user."));
103 $other = $activity->actor;
104 $otherUser = User::staticGet('uri', $other->id);
106 if (!empty($otherUser)) {
107 $otherProfile = $otherUser->getProfile();
109 throw new Exception("Can't force remote user to subscribe.");
112 // XXX: don't do this for untrusted input!
114 Subscription::start($otherProfile, $profile);
116 } else if (empty($activity->actor)
117 || $activity->actor->id == $author->id) {
119 $other = $activity->objects[0];
121 $otherProfile = Profile::fromUri($other->id);
123 if (empty($otherProfile)) {
124 throw new ClientException(_("Unknown profile."));
127 Subscription::start($profile, $otherProfile);
129 throw new Exception("This activity seems unrelated to our user.");
133 function joinGroup($user, $activity)
135 // XXX: check that actor == subject
137 $uri = $activity->objects[0]->id;
139 $group = User_group::staticGet('uri', $uri);
142 $oprofile = Ostatus_profile::ensureActivityObjectProfile($activity->objects[0]);
143 if (!$oprofile->isGroup()) {
144 throw new ClientException("Remote profile is not a group!");
146 $group = $oprofile->localGroup();
149 assert(!empty($group));
151 if ($user->isMember($group)) {
152 throw new ClientException("User is already a member of this group.");
155 if (Event::handle('StartJoinGroup', array($group, $user))) {
156 Group_member::join($group->id, $user->id);
157 Event::handle('EndJoinGroup', array($group, $user));
161 // XXX: largely cadged from Ostatus_profile::processNote()
163 function postNote($user, $author, $activity)
165 $note = $activity->objects[0];
167 $sourceUri = $note->id;
169 $notice = Notice::staticGet('uri', $sourceUri);
171 if (!empty($notice)) {
173 common_log(LOG_INFO, "Notice {$sourceUri} already exists.");
175 if ($this->trusted) {
177 $profile = $notice->getProfile();
179 $uri = $profile->getUri();
181 if ($uri == $author->id) {
182 common_log(LOG_INFO, "Updating notice author from $author->id to $user->uri");
183 $orig = clone($notice);
184 $notice->profile_id = $user->id;
185 $notice->update($orig);
188 throw new ClientException(sprintf(_("Already know about notice %s and ".
189 " it's got a different author %s."),
193 throw new ClientException("Not overwriting author info for non-trusted user.");
197 // Use summary as fallback for content
199 if (!empty($note->content)) {
200 $sourceContent = $note->content;
201 } else if (!empty($note->summary)) {
202 $sourceContent = $note->summary;
203 } else if (!empty($note->title)) {
204 $sourceContent = $note->title;
206 // @fixme fetch from $sourceUrl?
207 // @todo i18n FIXME: use sprintf and add i18n.
208 throw new ClientException("No content for notice {$sourceUri}.");
211 // Get (safe!) HTML and text versions of the content
213 $rendered = $this->purify($sourceContent);
214 $content = html_entity_decode(strip_tags($rendered), ENT_QUOTES, 'UTF-8');
216 $shortened = $user->shortenLinks($content);
218 $options = array('is_local' => Notice::LOCAL_PUBLIC,
220 'rendered' => $rendered,
221 'replies' => array(),
225 'distribute' => false);
227 // Check for optional attributes...
229 if (!empty($activity->time)) {
230 $options['created'] = common_sql_date($activity->time);
233 if ($activity->context) {
234 // Any individual or group attn: targets?
236 list($options['groups'], $options['replies']) = $this->filterAttention($activity->context->attention);
238 // Maintain direct reply associations
239 // @fixme what about conversation ID?
240 if (!empty($activity->context->replyToID)) {
241 $orig = Notice::staticGet('uri',
242 $activity->context->replyToID);
244 $options['reply_to'] = $orig->id;
248 $location = $activity->context->location;
251 $options['lat'] = $location->lat;
252 $options['lon'] = $location->lon;
253 if ($location->location_id) {
254 $options['location_ns'] = $location->location_ns;
255 $options['location_id'] = $location->location_id;
260 // Atom categories <-> hashtags
262 foreach ($activity->categories as $cat) {
264 $term = common_canonical_tag($cat->term);
266 $options['tags'][] = $term;
271 // Atom enclosures -> attachment URLs
272 foreach ($activity->enclosures as $href) {
273 // @fixme save these locally or....?
274 $options['urls'][] = $href;
277 common_log(LOG_INFO, "Saving notice {$options['uri']}");
279 $saved = Notice::saveNew($user->id,
281 'restore', // TODO: restore the actual source
287 function filterAttention($attn)
292 foreach (array_unique($attn) as $recipient) {
294 // Is the recipient a local user?
296 $user = User::staticGet('uri', $recipient);
299 // @fixme sender verification, spam etc?
300 $replies[] = $recipient;
304 // Is the recipient a remote group?
305 $oprofile = Ostatus_profile::ensureProfileURI($recipient);
308 if (!$oprofile->isGroup()) {
309 // may be canonicalized or something
310 $replies[] = $oprofile->uri;
315 // Is the recipient a local group?
316 // @fixme uri on user_group isn't reliable yet
317 // $group = User_group::staticGet('uri', $recipient);
318 $id = OStatusPlugin::localGroupFromUrl($recipient);
321 $group = User_group::staticGet('id', $id);
323 // Deliver to all members of this local group if allowed.
324 $profile = $sender->localProfile();
325 if ($profile->isMember($group)) {
326 $groups[] = $group->id;
328 common_log(LOG_INFO, "Skipping reply to local group {$group->nickname} as sender {$profile->id} is not a member");
332 common_log(LOG_INFO, "Skipping reply to bogus group $recipient");
337 return array($groups, $replies);
341 function purify($content)
343 require_once INSTALLDIR.'/extlib/htmLawed/htmLawed.php';
345 $config = array('safe' => 1,
346 'deny_attribute' => 'id,style,on*');
348 return htmLawed($content, $config);