2010-12-17 18:12:17 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* StatusNet - the distributed open-source microblogging tool
|
|
|
|
* Copyright (C) 2010, StatusNet, Inc.
|
|
|
|
*
|
|
|
|
* class to import activities as part of a user's timeline
|
2011-01-14 20:41:36 +00:00
|
|
|
*
|
2010-12-17 18:12:17 +00:00
|
|
|
* PHP version 5
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
* @category Cache
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2010 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
|
2014-04-28 13:08:42 +01:00
|
|
|
if (!defined('GNUSOCIAL')) { exit(1); }
|
2010-12-17 18:12:17 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Class comment
|
|
|
|
*
|
|
|
|
* @category General
|
|
|
|
* @package StatusNet
|
|
|
|
* @author Evan Prodromou <evan@status.net>
|
|
|
|
* @copyright 2010 StatusNet, Inc.
|
|
|
|
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
|
|
|
|
* @link http://status.net/
|
|
|
|
*/
|
|
|
|
class ActivityImporter extends QueueHandler
|
|
|
|
{
|
|
|
|
private $trusted = false;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function comment
|
|
|
|
*
|
|
|
|
* @param
|
|
|
|
*
|
|
|
|
* @return
|
|
|
|
*/
|
|
|
|
function handle($data)
|
|
|
|
{
|
|
|
|
list($user, $author, $activity, $trusted) = $data;
|
|
|
|
|
|
|
|
$this->trusted = $trusted;
|
|
|
|
|
2010-12-28 06:28:20 +00:00
|
|
|
$done = null;
|
|
|
|
|
2011-05-30 14:57:49 +01:00
|
|
|
try {
|
|
|
|
if (Event::handle('StartImportActivity',
|
|
|
|
array($user, $author, $activity, $trusted, &$done))) {
|
2010-12-28 06:28:20 +00:00
|
|
|
switch ($activity->verb) {
|
|
|
|
case ActivityVerb::FOLLOW:
|
|
|
|
$this->subscribeProfile($user, $author, $activity);
|
|
|
|
break;
|
|
|
|
case ActivityVerb::JOIN:
|
|
|
|
$this->joinGroup($user, $activity);
|
|
|
|
break;
|
|
|
|
case ActivityVerb::POST:
|
|
|
|
$this->postNote($user, $author, $activity);
|
|
|
|
break;
|
|
|
|
default:
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when using an unknown verb for the activity importer.
|
|
|
|
throw new ClientException(sprintf(_("Unknown verb: \"%s\"."),$activity->verb));
|
2010-12-28 06:28:20 +00:00
|
|
|
}
|
2011-01-14 20:41:36 +00:00
|
|
|
Event::handle('EndImportActivity',
|
2010-12-28 06:28:20 +00:00
|
|
|
array($user, $author, $activity, $trusted));
|
|
|
|
$done = true;
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
2011-05-30 14:57:49 +01:00
|
|
|
} catch (Exception $e) {
|
|
|
|
common_log(LOG_ERR, $e->getMessage());
|
|
|
|
$done = true;
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
2010-12-28 06:28:20 +00:00
|
|
|
return $done;
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
2011-01-14 20:41:36 +00:00
|
|
|
|
2010-12-17 18:12:17 +00:00
|
|
|
function subscribeProfile($user, $author, $activity)
|
|
|
|
{
|
|
|
|
$profile = $user->getProfile();
|
|
|
|
|
|
|
|
if ($activity->objects[0]->id == $author->id) {
|
2010-12-17 21:27:20 +00:00
|
|
|
if (!$this->trusted) {
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when trying to force a subscription for an untrusted user.
|
2011-04-06 00:40:03 +01:00
|
|
|
throw new ClientException(_('Cannot force subscription for untrusted user.'));
|
2010-12-17 21:27:20 +00:00
|
|
|
}
|
|
|
|
|
2010-12-17 18:12:17 +00:00
|
|
|
$other = $activity->actor;
|
2013-08-18 12:04:58 +01:00
|
|
|
$otherUser = User::getKV('uri', $other->id);
|
2011-01-14 20:41:36 +00:00
|
|
|
|
2014-04-28 13:08:42 +01:00
|
|
|
if (!$otherUser instanceof User) {
|
2011-04-06 00:40:03 +01:00
|
|
|
// TRANS: Client exception thrown when trying to force a remote user to subscribe.
|
|
|
|
throw new Exception(_('Cannot force remote user to subscribe.'));
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 13:08:42 +01:00
|
|
|
$otherProfile = $otherUser->getProfile();
|
|
|
|
|
2010-12-17 18:12:17 +00:00
|
|
|
// XXX: don't do this for untrusted input!
|
|
|
|
|
|
|
|
Subscription::start($otherProfile, $profile);
|
2011-01-14 20:41:36 +00:00
|
|
|
} else if (empty($activity->actor)
|
2010-12-17 18:12:17 +00:00
|
|
|
|| $activity->actor->id == $author->id) {
|
|
|
|
|
|
|
|
$other = $activity->objects[0];
|
|
|
|
|
2014-05-26 14:05:14 +01:00
|
|
|
try {
|
|
|
|
$otherProfile = Profile::fromUri($other->id);
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when trying to subscribe to an unknown profile.
|
2014-05-26 14:05:14 +01:00
|
|
|
} catch (UnknownUriException $e) {
|
|
|
|
// Let's convert it to a client exception instead of server.
|
2011-04-06 00:40:03 +01:00
|
|
|
throw new ClientException(_('Unknown profile.'));
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Subscription::start($profile, $otherProfile);
|
|
|
|
} else {
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when trying to import an event not related to the importing user.
|
2011-04-06 00:40:03 +01:00
|
|
|
throw new Exception(_('This activity seems unrelated to our user.'));
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function joinGroup($user, $activity)
|
|
|
|
{
|
|
|
|
// XXX: check that actor == subject
|
|
|
|
|
|
|
|
$uri = $activity->objects[0]->id;
|
|
|
|
|
2013-08-18 12:04:58 +01:00
|
|
|
$group = User_group::getKV('uri', $uri);
|
2010-12-17 18:12:17 +00:00
|
|
|
|
2014-04-28 13:08:42 +01:00
|
|
|
if (!$group instanceof User_group) {
|
2010-12-17 18:12:17 +00:00
|
|
|
$oprofile = Ostatus_profile::ensureActivityObjectProfile($activity->objects[0]);
|
|
|
|
if (!$oprofile->isGroup()) {
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when trying to join a remote group that is not a group.
|
2011-04-06 00:40:03 +01:00
|
|
|
throw new ClientException(_('Remote profile is not a group!'));
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
|
|
|
$group = $oprofile->localGroup();
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(!empty($group));
|
|
|
|
|
2010-12-17 22:37:43 +00:00
|
|
|
if ($user->isMember($group)) {
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when trying to join a group the importing user is already a member of.
|
|
|
|
throw new ClientException(_("User is already a member of this group."));
|
2010-12-17 22:37:43 +00:00
|
|
|
}
|
|
|
|
|
2011-03-21 21:35:29 +00:00
|
|
|
$user->joinGroup($group);
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// XXX: largely cadged from Ostatus_profile::processNote()
|
|
|
|
|
2010-12-17 22:37:43 +00:00
|
|
|
function postNote($user, $author, $activity)
|
2010-12-17 18:12:17 +00:00
|
|
|
{
|
|
|
|
$note = $activity->objects[0];
|
|
|
|
|
|
|
|
$sourceUri = $note->id;
|
|
|
|
|
2013-08-18 12:04:58 +01:00
|
|
|
$notice = Notice::getKV('uri', $sourceUri);
|
2010-12-17 18:12:17 +00:00
|
|
|
|
2014-04-28 13:08:42 +01:00
|
|
|
if ($notice instanceof Notice) {
|
2011-01-14 20:41:36 +00:00
|
|
|
|
2010-12-17 22:37:43 +00:00
|
|
|
common_log(LOG_INFO, "Notice {$sourceUri} already exists.");
|
|
|
|
|
|
|
|
if ($this->trusted) {
|
|
|
|
|
|
|
|
$profile = $notice->getProfile();
|
|
|
|
|
|
|
|
$uri = $profile->getUri();
|
|
|
|
|
2014-04-28 13:08:42 +01:00
|
|
|
if ($uri === $author->id) {
|
|
|
|
common_log(LOG_INFO, sprintf('Updating notice author from %s to %s', $author->id, $user->getUri()));
|
2010-12-17 22:37:43 +00:00
|
|
|
$orig = clone($notice);
|
|
|
|
$notice->profile_id = $user->id;
|
|
|
|
$notice->update($orig);
|
|
|
|
return;
|
|
|
|
} else {
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when trying to import a notice by another user.
|
|
|
|
// TRANS: %1$s is the source URI of the notice, %2$s is the URI of the author.
|
2011-01-31 22:00:22 +00:00
|
|
|
throw new ClientException(sprintf(_('Already know about notice %1$s and '.
|
|
|
|
' it has a different author %2$s.'),
|
2010-12-17 22:37:43 +00:00
|
|
|
$sourceUri, $uri));
|
|
|
|
}
|
2010-12-17 23:56:17 +00:00
|
|
|
} else {
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when trying to overwrite the author information for a non-trusted user during import.
|
2011-04-06 00:40:03 +01:00
|
|
|
throw new ClientException(_('Not overwriting author info for non-trusted user.'));
|
2010-12-17 22:37:43 +00:00
|
|
|
}
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Use summary as fallback for content
|
|
|
|
|
|
|
|
if (!empty($note->content)) {
|
|
|
|
$sourceContent = $note->content;
|
|
|
|
} else if (!empty($note->summary)) {
|
|
|
|
$sourceContent = $note->summary;
|
|
|
|
} else if (!empty($note->title)) {
|
|
|
|
$sourceContent = $note->title;
|
|
|
|
} else {
|
|
|
|
// @fixme fetch from $sourceUrl?
|
2011-01-14 20:41:36 +00:00
|
|
|
// TRANS: Client exception thrown when trying to import a notice without content.
|
|
|
|
// TRANS: %s is the notice URI.
|
2011-04-06 00:40:03 +01:00
|
|
|
throw new ClientException(sprintf(_('No content for notice %s.'),$sourceUri));
|
2010-12-17 18:12:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get (safe!) HTML and text versions of the content
|
|
|
|
|
2015-02-17 23:14:28 +00:00
|
|
|
$rendered = common_purify($sourceContent);
|
2014-07-14 12:52:23 +01:00
|
|
|
$content = common_strip_html($rendered);
|
2010-12-17 18:12:17 +00:00
|
|
|
|
|
|
|
$shortened = $user->shortenLinks($content);
|
|
|
|
|
|
|
|
$options = array('is_local' => Notice::LOCAL_PUBLIC,
|
|
|
|
'uri' => $sourceUri,
|
|
|
|
'rendered' => $rendered,
|
|
|
|
'replies' => array(),
|
|
|
|
'groups' => array(),
|
|
|
|
'tags' => array(),
|
2010-12-17 22:37:43 +00:00
|
|
|
'urls' => array(),
|
|
|
|
'distribute' => false);
|
2010-12-17 18:12:17 +00:00
|
|
|
|
|
|
|
// Check for optional attributes...
|
|
|
|
|
|
|
|
if (!empty($activity->time)) {
|
|
|
|
$options['created'] = common_sql_date($activity->time);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($activity->context) {
|
|
|
|
// Any individual or group attn: targets?
|
|
|
|
|
|
|
|
list($options['groups'], $options['replies']) = $this->filterAttention($activity->context->attention);
|
|
|
|
|
|
|
|
// Maintain direct reply associations
|
|
|
|
// @fixme what about conversation ID?
|
|
|
|
if (!empty($activity->context->replyToID)) {
|
2014-04-28 13:08:42 +01:00
|
|
|
$orig = Notice::getKV('uri', $activity->context->replyToID);
|
|
|
|
if ($orig instanceof Notice) {
|
2010-12-17 18:12:17 +00:00
|
|
|
$options['reply_to'] = $orig->id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$location = $activity->context->location;
|
|
|
|
|
|
|
|
if ($location) {
|
|
|
|
$options['lat'] = $location->lat;
|
|
|
|
$options['lon'] = $location->lon;
|
|
|
|
if ($location->location_id) {
|
|
|
|
$options['location_ns'] = $location->location_ns;
|
|
|
|
$options['location_id'] = $location->location_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Atom categories <-> hashtags
|
|
|
|
|
|
|
|
foreach ($activity->categories as $cat) {
|
|
|
|
if ($cat->term) {
|
|
|
|
$term = common_canonical_tag($cat->term);
|
|
|
|
if ($term) {
|
|
|
|
$options['tags'][] = $term;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Atom enclosures -> attachment URLs
|
|
|
|
foreach ($activity->enclosures as $href) {
|
|
|
|
// @fixme save these locally or....?
|
|
|
|
$options['urls'][] = $href;
|
|
|
|
}
|
|
|
|
|
2010-12-17 22:37:43 +00:00
|
|
|
common_log(LOG_INFO, "Saving notice {$options['uri']}");
|
|
|
|
|
2010-12-17 18:12:17 +00:00
|
|
|
$saved = Notice::saveNew($user->id,
|
|
|
|
$content,
|
|
|
|
'restore', // TODO: restore the actual source
|
|
|
|
$options);
|
|
|
|
|
|
|
|
return $saved;
|
|
|
|
}
|
|
|
|
|
2013-10-28 21:21:14 +00:00
|
|
|
protected function filterAttention(array $attn)
|
2010-12-17 18:12:17 +00:00
|
|
|
{
|
2013-10-28 21:21:14 +00:00
|
|
|
$groups = array(); // TODO: context->attention
|
|
|
|
$replies = array(); // TODO: context->attention
|
2010-12-17 18:12:17 +00:00
|
|
|
|
2013-10-28 21:21:14 +00:00
|
|
|
foreach ($attn as $recipient=>$type) {
|
2010-12-17 18:12:17 +00:00
|
|
|
|
|
|
|
// Is the recipient a local user?
|
|
|
|
|
2013-08-18 12:04:58 +01:00
|
|
|
$user = User::getKV('uri', $recipient);
|
2010-12-17 18:12:17 +00:00
|
|
|
|
2013-10-28 21:21:14 +00:00
|
|
|
if ($user instanceof User) {
|
|
|
|
// TODO: @fixme sender verification, spam etc?
|
2010-12-17 18:12:17 +00:00
|
|
|
$replies[] = $recipient;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the recipient a remote group?
|
|
|
|
$oprofile = Ostatus_profile::ensureProfileURI($recipient);
|
|
|
|
|
|
|
|
if ($oprofile) {
|
|
|
|
if (!$oprofile->isGroup()) {
|
|
|
|
// may be canonicalized or something
|
|
|
|
$replies[] = $oprofile->uri;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is the recipient a local group?
|
2013-10-28 21:21:14 +00:00
|
|
|
// TODO: @fixme uri on user_group isn't reliable yet
|
2013-08-18 12:04:58 +01:00
|
|
|
// $group = User_group::getKV('uri', $recipient);
|
2010-12-17 18:12:17 +00:00
|
|
|
$id = OStatusPlugin::localGroupFromUrl($recipient);
|
|
|
|
|
|
|
|
if ($id) {
|
2013-08-18 12:04:58 +01:00
|
|
|
$group = User_group::getKV('id', $id);
|
2010-12-17 18:12:17 +00:00
|
|
|
if ($group) {
|
|
|
|
// Deliver to all members of this local group if allowed.
|
|
|
|
$profile = $sender->localProfile();
|
|
|
|
if ($profile->isMember($group)) {
|
|
|
|
$groups[] = $group->id;
|
|
|
|
} else {
|
|
|
|
common_log(LOG_INFO, "Skipping reply to local group {$group->nickname} as sender {$profile->id} is not a member");
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
common_log(LOG_INFO, "Skipping reply to bogus group $recipient");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return array($groups, $replies);
|
|
|
|
}
|
|
|
|
}
|