123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- <?php
- if (!defined('GNUSOCIAL')) { exit(1); }
- class ActivityImporter extends QueueHandler
- {
- private $trusted = false;
-
- function handle($data) : bool
- {
- list($user, $author, $activity, $trusted) = $data;
- $this->trusted = $trusted;
- $done = null;
- try {
- if (Event::handle('StartImportActivity',
- array($user, $author, $activity, $trusted, &$done))) {
- 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:
-
- throw new ClientException(sprintf(_("Unknown verb: \"%s\"."),$activity->verb));
- }
- Event::handle('EndImportActivity',
- array($user, $author, $activity, $trusted));
- $done = true;
- }
- } catch (Exception $e) {
- common_log(LOG_ERR, $e->getMessage());
- $done = true;
- }
- return $done;
- }
- function subscribeProfile($user, $author, $activity)
- {
- $profile = $user->getProfile();
- if ($activity->objects[0]->id == $author->id) {
- if (!$this->trusted) {
-
- throw new ClientException(_('Cannot force subscription for untrusted user.'));
- }
- $other = $activity->actor;
- $otherUser = User::getKV('uri', $other->id);
- if (!$otherUser instanceof User) {
-
- throw new Exception(_('Cannot force remote user to subscribe.'));
- }
- $otherProfile = $otherUser->getProfile();
-
- Subscription::ensureStart($otherProfile, $profile);
- } else if (empty($activity->actor)
- || $activity->actor->id == $author->id) {
- $other = $activity->objects[0];
- try {
- $otherProfile = Profile::fromUri($other->id);
-
- } catch (UnknownUriException $e) {
-
- throw new ClientException(_('Unknown profile.'));
- }
- Subscription::ensureStart($profile, $otherProfile);
- } else {
-
- throw new Exception(_('This activity seems unrelated to our user.'));
- }
- }
- function joinGroup($user, $activity)
- {
-
- $uri = $activity->objects[0]->id;
- $group = User_group::getKV('uri', $uri);
- if (!$group instanceof User_group) {
- $oprofile = Ostatus_profile::ensureActivityObjectProfile($activity->objects[0]);
- if (!$oprofile->isGroup()) {
-
- throw new ClientException(_('Remote profile is not a group!'));
- }
- $group = $oprofile->localGroup();
- }
- assert(!empty($group));
- if ($user->isMember($group)) {
-
- throw new ClientException(_("User is already a member of this group."));
- }
- $user->joinGroup($group);
- }
-
- function postNote($user, $author, $activity)
- {
- $note = $activity->objects[0];
- $sourceUri = $note->id;
- $notice = Notice::getKV('uri', $sourceUri);
- if ($notice instanceof Notice) {
- common_log(LOG_INFO, "Notice {$sourceUri} already exists.");
- if ($this->trusted) {
- $profile = $notice->getProfile();
- $uri = $profile->getUri();
- if ($uri === $author->id) {
- common_log(LOG_INFO, sprintf('Updating notice author from %s to %s', $author->id, $user->getUri()));
- $orig = clone($notice);
- $notice->profile_id = $user->id;
- $notice->update($orig);
- return;
- } else {
-
-
- throw new ClientException(sprintf(_('Already know about notice %1$s and '.
- ' it has a different author %2$s.'),
- $sourceUri, $uri));
- }
- } else {
-
- throw new ClientException(_('Not overwriting author info for non-trusted user.'));
- }
- }
-
- if (!empty($note->content)) {
- $sourceContent = $note->content;
- } else if (!empty($note->summary)) {
- $sourceContent = $note->summary;
- } else if (!empty($note->title)) {
- $sourceContent = $note->title;
- } else {
-
-
-
- throw new ClientException(sprintf(_('No content for notice %s.'),$sourceUri));
- }
-
- $rendered = common_purify($sourceContent);
- $content = common_strip_html($rendered);
- $shortened = $user->shortenLinks($content);
- $options = array('is_local' => Notice::LOCAL_PUBLIC,
- 'uri' => $sourceUri,
- 'rendered' => $rendered,
- 'replies' => array(),
- 'groups' => array(),
- 'tags' => array(),
- 'urls' => array(),
- 'distribute' => false);
-
- if (!empty($activity->time)) {
- $options['created'] = common_sql_date($activity->time);
- }
- if ($activity->context) {
-
- list($options['groups'], $options['replies']) = $this->filterAttention($activity->context->attention);
-
-
- if (!empty($activity->context->replyToID)) {
- $orig = Notice::getKV('uri', $activity->context->replyToID);
- if ($orig instanceof Notice) {
- $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;
- }
- }
- }
-
- foreach ($activity->categories as $cat) {
- if ($cat->term) {
- $term = common_canonical_tag($cat->term);
- if ($term) {
- $options['tags'][] = $term;
- }
- }
- }
-
- foreach ($activity->enclosures as $href) {
-
- $options['urls'][] = $href;
- }
- common_log(LOG_INFO, "Saving notice {$options['uri']}");
- $saved = Notice::saveNew($user->id,
- $content,
- 'restore',
- $options);
- return $saved;
- }
- protected function filterAttention(array $attn)
- {
- $groups = array();
- $replies = array();
- foreach ($attn as $recipient=>$type) {
-
- $user = User::getKV('uri', $recipient);
- if ($user instanceof User) {
-
- $replies[] = $recipient;
- continue;
- }
-
- $oprofile = Ostatus_profile::ensureProfileURI($recipient);
- if ($oprofile) {
- if (!$oprofile->isGroup()) {
-
- $replies[] = $oprofile->uri;
- }
- continue;
- }
-
-
-
- $id = OStatusPlugin::localGroupFromUrl($recipient);
- if ($id) {
- $group = User_group::getKV('id', $id);
- if ($group) {
-
- $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);
- }
- }
|