X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FProtocol%2FActivityPub%2FProcessor.php;h=d57842c2b226362eae4c16cccc9fa3e4eaf842c2;hb=24c32cff0dcd38d5aa509208f5f17abb05a8b140;hp=668b8a5b89b5e56d07f879e8d45fe12589aacd5e;hpb=45729f01070082c7e93926b19ceb64a67ad0e1f5;p=friendica.git diff --git a/src/Protocol/ActivityPub/Processor.php b/src/Protocol/ActivityPub/Processor.php index 668b8a5b89..d57842c2b2 100644 --- a/src/Protocol/ActivityPub/Processor.php +++ b/src/Protocol/ActivityPub/Processor.php @@ -1,6 +1,6 @@ $item['thr-parent']])) { - Logger::info('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]); + if (empty($conversation) && empty($activity['directmessage']) && ($item['gravity'] != Item::GRAVITY_PARENT) && !Post::exists(['uri' => $item['thr-parent']])) { + Logger::notice('Parent not found, message will be discarded.', ['thr-parent' => $item['thr-parent']]); if (!$fetch_parents) { Queue::remove($activity); } @@ -431,7 +431,7 @@ class Processor $item['owner-id'] = $item['author-id']; } else { $actor = APContact::getByURL($item['owner-link'], false); - $item['isForum'] = ($actor['type'] == 'Group'); + $item['isForum'] = ($actor['type'] ?? 'Person') == 'Group'; } $item['uri'] = $activity['id']; @@ -552,7 +552,7 @@ class Processor Logger::notice('Fetching is done by worker.', ['parent' => $activity['reply-to-id'], 'recursion-depth' => $recursion_depth]); Fetch::add($activity['reply-to-id']); $activity['recursion-depth'] = 0; - $wid = Worker::add(PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO); + $wid = Worker::add(Worker::PRIORITY_HIGH, 'FetchMissingActivity', $activity['reply-to-id'], $activity, '', Receiver::COMPLETION_AUTO); Fetch::setWorkerId($activity['reply-to-id'], $wid); } else { Logger::debug('Activity will already be fetched via a worker.', ['url' => $activity['reply-to-id']]); @@ -657,12 +657,13 @@ class Processor $activity['reply-to-id'] = $activity['object_id']; $item = self::createItem($activity, false); if (empty($item)) { + Logger::debug('Activity was not prepared', ['id' => $activity['object_id']]); return; } $item['verb'] = $verb; $item['thr-parent'] = $activity['object_id']; - $item['gravity'] = GRAVITY_ACTIVITY; + $item['gravity'] = Item::GRAVITY_ACTIVITY; unset($item['post-type']); $item['object-type'] = Activity\ObjectType::NOTE; @@ -803,7 +804,7 @@ class Processor private static function processContent(array $activity, array $item) { if (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/markdown')) { - $item['title'] = strip_tags($activity['name']); + $item['title'] = strip_tags($activity['name'] ?? ''); $content = Markdown::toBBCode($activity['content']); } elseif (!empty($activity['mediatype']) && ($activity['mediatype'] == 'text/bbcode')) { $item['title'] = $activity['name']; @@ -826,13 +827,29 @@ class Processor $content = self::addMentionLinks($content, $activity['tags']); + if (!empty($activity['quote-url'])) { + $id = Item::fetchByLink($activity['quote-url']); + if ($id) { + $shared_item = Post::selectFirst(['uri-id'], ['id' => $id]); + $item['quote-uri-id'] = $shared_item['uri-id']; + } else { + Logger::info('Quote was not fetched', ['guid' => $item['guid'], 'uri-id' => $item['uri-id'], 'quote' => $activity['quote-url']]); + } + } + if (!empty($activity['source'])) { $item['body'] = $activity['source']; $item['raw-body'] = $content; - $item['body'] = Item::improveSharedDataInBody($item); + + $quote_uri_id = Item::getQuoteUriId($item['body']); + if (empty($item['quote-uri-id']) && !empty($quote_uri_id)) { + $item['quote-uri-id'] = $quote_uri_id; + } + + $item['body'] = BBCode::removeSharedData($item['body']); } else { $parent_uri = $item['parent-uri'] ?? $item['thr-parent']; - if (empty($activity['directmessage']) && ($parent_uri != $item['uri']) && ($item['gravity'] == GRAVITY_COMMENT)) { + if (empty($activity['directmessage']) && ($parent_uri != $item['uri']) && ($item['gravity'] == Item::GRAVITY_COMMENT)) { $parent = Post::selectFirst(['id', 'uri-id', 'private', 'author-link', 'alias'], ['uri' => $parent_uri]); if (!DBA::isResult($parent)) { Logger::warning('Unknown parent item.', ['uri' => $parent_uri]); @@ -932,7 +949,7 @@ class Processor return true; } - if ($item['gravity'] != GRAVITY_PARENT) { + if ($item['gravity'] != Item::GRAVITY_PARENT) { // We cannot reliably check at this point if a comment or activity belongs to an accepted post or needs to be fetched // This can possibly be improved in the future. Logger::debug('Message is no parent - accepted', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]); @@ -940,7 +957,7 @@ class Processor } $tags = array_column(Tag::getByURIId($item['uri-id'], [Tag::HASHTAG]), 'name'); - if (Relay::isSolicitedPost($tags, $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB)) { + if (Relay::isSolicitedPost($tags, $item['body'], $item['author-id'], $item['uri'], Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0)) { Logger::debug('Post is accepted because of the relay settings', ['uri-id' => $item['uri-id'], 'guid' => $item['guid'], 'url' => $item['uri']]); return true; } else { @@ -979,6 +996,14 @@ class Processor continue; } + if (($receiver != 0) && empty($item['parent-uri-id']) && !empty($item['thr-parent-id'])) { + $parent = Post::selectFirst(['parent-uri-id', 'parent-uri'], ['uri-id' => $item['thr-parent-id'], 'uid' => [0, $receiver]]); + if (!empty($parent['parent-uri-id'])) { + $item['parent-uri-id'] = $parent['parent-uri-id']; + $item['parent-uri'] = $parent['parent-uri']; + } + } + $item['uid'] = $receiver; $type = $activity['reception_type'][$receiver] ?? Receiver::TARGET_UNKNOWN; @@ -1018,6 +1043,11 @@ class Processor } elseif (!empty($activity['push'])) { $item['post-reason'] = Item::PR_PUSHED; } + } elseif (($item['post-reason'] == Item::PR_FOLLOWER) && !empty($activity['from-relay'])) { + // When a post arrives via a relay and we follow the author, we have to override the causer. + // Otherwise the system assumes that we follow the relay. (See "addRowInformation") + Logger::debug('Relay post for follower', ['receiver' => $receiver, 'guid' => $item['guid'], 'relay' => $activity['from-relay']]); + $item['causer-id'] = ($item['gravity'] == Item::GRAVITY_PARENT) ? $item['owner-id'] : $item['author-id']; } if ($item['isForum'] ?? false) { @@ -1035,7 +1065,7 @@ class Processor continue; } - if (($receiver != 0) && ($item['gravity'] == GRAVITY_PARENT) && !in_array($item['post-reason'], [Item::PR_FOLLOWER, Item::PR_TAG, item::PR_TO, Item::PR_CC])) { + if (($receiver != 0) && ($item['gravity'] == Item::GRAVITY_PARENT) && !in_array($item['post-reason'], [Item::PR_FOLLOWER, Item::PR_TAG, item::PR_TO, Item::PR_CC])) { if (!($item['isForum'] ?? false)) { if ($item['post-reason'] == Item::PR_BCC) { Logger::info('Top level post via BCC from a non sharer, ignoring', ['uid' => $receiver, 'contact' => $item['contact-id'], 'url' => $item['uri']]); @@ -1070,7 +1100,7 @@ class Processor continue; } - if (($item['gravity'] != GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) { + if (($item['gravity'] != Item::GRAVITY_ACTIVITY) && ($activity['object_type'] == 'as:Event')) { $event_id = self::createEvent($activity, $item); $item = Event::getItemArrayForImportedId($event_id, $item); @@ -1100,7 +1130,7 @@ class Processor } // Store send a follow request for every reshare - but only when the item had been stored - if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == GRAVITY_PARENT) && !empty($item['author-link']) && ($item['author-link'] != $item['owner-link'])) { + if ($stored && ($item['private'] != Item::PRIVATE) && ($item['gravity'] == Item::GRAVITY_PARENT) && !empty($item['author-link']) && ($item['author-link'] != $item['owner-link'])) { $author = APContact::getByURL($item['owner-link'], false); // We send automatic follow requests for reshared messages. (We don't need though for forum posts) if ($author['type'] != 'Group') { @@ -1120,18 +1150,42 @@ class Processor */ private static function hasParents(array $item, int $receiver) { - if (($receiver == 0) || ($item['gravity'] == GRAVITY_PARENT)) { + if (($receiver == 0) || ($item['gravity'] == Item::GRAVITY_PARENT)) { return true; } $fields = ['causer-id' => $item['causer-id'] ?? $item['author-id'], 'post-reason' => Item::PR_FETCHED]; + $add_parent = true; + + if ($item['verb'] != Activity::ANNOUNCE) { + switch (DI::pConfig()->get($receiver, 'system', 'accept_only_sharer')) { + case Item::COMPLETION_COMMENT: + $add_parent = ($item['gravity'] != Item::GRAVITY_ACTIVITY); + break; + + case Item::COMPLETION_NONE: + $add_parent = false; + break; + } + } + + if ($add_parent) { + $add_parent = Contact::isSharing($fields['causer-id'], $receiver); + if (!$add_parent && ($item['author-id'] != $fields['causer-id'])) { + $add_parent = Contact::isSharing($item['author-id'], $receiver); + } + if (!$add_parent && !in_array($item['owner-id'], [$fields['causer-id'], $item['author-id']])) { + $add_parent = Contact::isSharing($item['owner-id'], $receiver); + } + } + $has_parents = false; if (!empty($item['parent-uri-id'])) { if (Post::exists(['uri-id' => $item['parent-uri-id'], 'uid' => $receiver])) { $has_parents = true; - } elseif (Post::exists(['uri-id' => $item['parent-uri'], 'uid' => 0])) { + } elseif ($add_parent && Post::exists(['uri-id' => $item['parent-uri-id'], 'uid' => 0])) { $stored = Item::storeForUserByUriId($item['parent-uri-id'], $receiver, $fields); $has_parents = (bool)$stored; if ($stored) { @@ -1140,13 +1194,17 @@ class Processor Logger::notice('Parent could not be added.', ['uid' => $receiver, 'uri' => $item['uri'], 'parent' => $item['parent-uri']]); return false; } + } elseif ($add_parent) { + Logger::debug('Parent does not exist.', ['uid' => $receiver, 'uri' => $item['uri'], 'parent' => $item['parent-uri']]); + } else { + Logger::debug('Parent should not be added.', ['uid' => $receiver, 'gravity' => $item['gravity'], 'verb' => $item['verb'], 'guid' => $item['guid'], 'uri' => $item['uri'], 'parent' => $item['parent-uri']]); } } if (empty($item['parent-uri-id']) || ($item['thr-parent-id'] != $item['parent-uri-id'])) { if (Post::exists(['uri-id' => $item['thr-parent-id'], 'uid' => $receiver])) { $has_parents = true; - } elseif (Post::exists(['uri-id' => $item['thr-parent-id'], 'uid' => 0])) { + } elseif (($has_parents || $add_parent) && Post::exists(['uri-id' => $item['thr-parent-id'], 'uid' => 0])) { $stored = Item::storeForUserByUriId($item['thr-parent-id'], $receiver, $fields); $has_parents = $has_parents || (bool)$stored; if ($stored) { @@ -1154,6 +1212,10 @@ class Processor } else { Logger::notice('Thread parent could not be added.', ['uid' => $receiver, 'uri' => $item['uri'], 'thread-parent' => $item['thr-parent']]); } + } elseif ($add_parent) { + Logger::debug('Thread parent does not exist.', ['uid' => $receiver, 'uri' => $item['uri'], 'thread-parent' => $item['thr-parent']]); + } else { + Logger::debug('Thread parent should not be added.', ['uid' => $receiver, 'gravity' => $item['gravity'], 'verb' => $item['verb'], 'guid' => $item['guid'], 'uri' => $item['uri'], 'thread-parent' => $item['thr-parent']]); } } @@ -1211,8 +1273,11 @@ class Processor foreach ($receivers[$element] as $receiver) { if ($receiver == ActivityPub::PUBLIC_COLLECTION) { $name = Receiver::PUBLIC_COLLECTION; + } elseif ($path = parse_url($receiver, PHP_URL_PATH)) { + $name = trim($path, '/'); } else { - $name = trim(parse_url($receiver, PHP_URL_PATH), '/'); + Logger::warning('Unable to coerce name from receiver', ['receiver' => $receiver]); + $name = ''; } $target = Tag::getTargetType($receiver); @@ -1233,7 +1298,7 @@ class Processor */ private static function postMail(array $activity, array $item) { - if (($item['gravity'] != GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) { + if (($item['gravity'] != Item::GRAVITY_PARENT) && !DBA::exists('mail', ['uri' => $item['thr-parent'], 'uid' => $item['uid']])) { Logger::info('Parent not found, mail will be discarded.', ['uid' => $item['uid'], 'uri' => $item['thr-parent']]); return false; } @@ -1304,7 +1369,7 @@ class Processor $pcid = Contact::getIdForURL($url, 0, false); if (empty($pcid)) { - Logger::info('Contact not found', ['contact' => $url]); + Logger::notice('Contact not found', ['contact' => $url]); return; } @@ -1364,7 +1429,7 @@ class Processor public static function fetchCachedActivity(string $url, int $uid): array { - $cachekey = self::CACHEKEY_FETCH_ACTIVITY . $uid . ':' . $url; + $cachekey = self::CACHEKEY_FETCH_ACTIVITY . $uid . ':' . hash('sha256', $url); $object = DI::cache()->get($cachekey); if (!is_null($object)) { @@ -1465,6 +1530,12 @@ class Processor $ldactivity['recursion-depth'] = !empty($child['recursion-depth']) ? $child['recursion-depth'] + 1 : 0; + if ($object_actor != $actor) { + Contact::updateByUrlIfNeeded($object_actor); + } + + Contact::updateByUrlIfNeeded($actor); + if (!empty($relay_actor)) { $ldactivity['thread-completion'] = $ldactivity['from-relay'] = Contact::getIdForURL($relay_actor); $ldactivity['completion-mode'] = Receiver::COMPLETION_RELAY; @@ -1536,7 +1607,7 @@ class Processor } } - return Relay::isSolicitedPost($messageTags, $body, $authorid, $id, Protocol::ACTIVITYPUB); + return Relay::isSolicitedPost($messageTags, $body, $authorid, $id, Protocol::ACTIVITYPUB, $activity['thread-completion'] ?? 0); } /** @@ -1615,9 +1686,9 @@ class Processor } if (DI::config()->get('system', 'bulk_delivery')) { Post\Delivery::add($post['uri-id'], $uid, $inbox, $post['created'], Delivery::POST, [$cid]); - Worker::add(PRIORITY_HIGH, 'APDelivery', '', 0, $inbox, 0); + Worker::add(Worker::PRIORITY_HIGH, 'APDelivery', '', 0, $inbox, 0); } else { - Worker::add(PRIORITY_HIGH, 'APDelivery', Delivery::POST, $post['id'], $inbox, $uid, [$cid], $post['uri-id']); + Worker::add(Worker::PRIORITY_HIGH, 'APDelivery', Delivery::POST, $post['id'], $inbox, $uid, [$cid], $post['uri-id']); } } } @@ -1650,11 +1721,13 @@ class Processor { if (empty($activity['object_id']) || empty($activity['actor'])) { Logger::info('Empty object id or actor.'); + Queue::remove($activity); return; } if ($activity['object_id'] != $activity['actor']) { Logger::info('Object id does not match actor.'); + Queue::remove($activity); return; } @@ -1668,6 +1741,42 @@ class Processor Queue::remove($activity); } + /** + * Add moved contacts as followers for all subscribers of the old contact + * + * @param array $activity + * @return void + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + */ + public static function movePerson(array $activity) + { + if (empty($activity['target_id']) || empty($activity['object_id'])) { + Queue::remove($activity); + return; + } + + if ($activity['object_id'] != $activity['actor']) { + Logger::notice('Object is not the actor', ['activity' => $activity]); + Queue::remove($activity); + return; + } + + $from = Contact::getByURL($activity['object_id'], false, ['uri-id']); + if (empty($from['uri-id'])) { + Logger::info('Object not found', ['activity' => $activity]); + Queue::remove($activity); + return; + } + + $contacts = DBA::select('contact', ['uid', 'url'], ["`uri-id` = ? AND `uid` != ? AND `rel` IN (?, ?)", $from['uri-id'], 0, Contact::FRIEND, Contact::SHARING]); + while ($from_contact = DBA::fetch($contacts)) { + $result = Contact::createFromProbeForUser($from_contact['uid'], $activity['target_id']); + Logger::debug('Follower added', ['from' => $from_contact, 'result' => $result]); + } + DBA::close($contacts); + Queue::remove($activity); + } + /** * Blocks the user by the contact * @@ -1718,6 +1827,43 @@ class Processor Queue::remove($activity); } + /** + * Report a user + * + * @param array $activity + * @return void + * @throws \Exception + */ + public static function ReportAccount(array $activity) + { + $account_id = Contact::getIdForURL($activity['object_id']); + if (empty($account_id)) { + Logger::info('Unknown account', ['activity' => $activity]); + Queue::remove($activity); + return; + } + + $reporter_id = Contact::getIdForURL($activity['actor']); + if (empty($reporter_id)) { + Logger::info('Unknown actor', ['activity' => $activity]); + Queue::remove($activity); + return; + } + + $uri_ids = []; + foreach ($activity['object_ids'] as $status_id) { + $post = Post::selectFirst(['uri-id'], ['uri' => $status_id]); + if (!empty($post['uri-id'])) { + $uri_ids[] = $post['uri-id']; + } + } + + $report = DI::reportFactory()->createFromReportsRequest($reporter_id, $account_id, $activity['content'], null, '', false, $uri_ids); + DI::report()->save($report); + + Logger::info('Stored report', ['reporter' => $reporter_id, 'account_id' => $account_id, 'comment' => $activity['content'], 'object_ids' => $activity['object_ids']]); + } + /** * Accept a follow request * @@ -1727,17 +1873,38 @@ class Processor */ public static function acceptFollowUser(array $activity) { - $uid = User::getIdForURL($activity['object_actor']); + if (!empty($activity['object_actor'])) { + $uid = User::getIdForURL($activity['object_actor']); + $check_id = false; + } elseif (!empty($activity['receiver']) && (count($activity['receiver']) == 1)) { + $uid = array_shift($activity['receiver']); + $check_id = true; + } + if (empty($uid)) { + Logger::notice('User could not be detected', ['activity' => $activity]); + Queue::remove($activity); return; } $cid = Contact::getIdForURL($activity['actor'], $uid); if (empty($cid)) { - Logger::info('No contact found', ['actor' => $activity['actor']]); + Logger::notice('No contact found', ['actor' => $activity['actor']]); + Queue::remove($activity); return; } + $id = Transmitter::activityIDFromContact($cid); + if ($id == $activity['object_id']) { + Logger::info('Successful id check', ['uid' => $uid, 'cid' => $cid]); + } else { + Logger::info('Unsuccessful id check', ['uid' => $uid, 'cid' => $cid, 'id' => $id, 'object_id' => $activity['object_id']]); + if ($check_id) { + Queue::remove($activity); + return; + } + } + self::switchContact($cid); $fields = ['pending' => false]; @@ -1809,7 +1976,7 @@ class Processor return; } - Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => GRAVITY_ACTIVITY]); + Item::markForDeletion(['uri' => $activity['object_id'], 'author-id' => $author_id, 'gravity' => Item::GRAVITY_ACTIVITY]); Queue::remove($activity); } @@ -1965,7 +2132,9 @@ class Processor $name = $tag['name']; } - $body = str_replace($tag['name'], $hash . '[url=' . $tag['href'] . ']' . $name . '[/url]', $body); + if (Network::isValidHttpUrl($tag['href'])) { + $body = str_replace($tag['name'], $hash . '[url=' . $tag['href'] . ']' . $name . '[/url]', $body); + } } return $body;