From 424a85bb9424d5b826734147add52f4d196f13c9 Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 18 Jul 2021 18:54:25 +0000 Subject: [PATCH] Fetch local data without HTTP requests --- src/Model/APContact.php | 55 +++++++++++++++++++++++++----------- src/Protocol/ActivityPub.php | 2 -- 2 files changed, 39 insertions(+), 18 deletions(-) diff --git a/src/Model/APContact.php b/src/Model/APContact.php index e9500f85b..a4c97f623 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -31,11 +31,13 @@ use Friendica\DI; use Friendica\Network\Probe; use Friendica\Protocol\ActivityNamespace; use Friendica\Protocol\ActivityPub; +use Friendica\Protocol\ActivityPub\Transmitter; use Friendica\Util\Crypto; use Friendica\Util\DateTimeFormat; use Friendica\Util\HTTPSignature; use Friendica\Util\JsonLD; use Friendica\Util\Network; +use Friendica\Util\Strings; class APContact { @@ -161,22 +163,31 @@ class APContact DI::cache()->set($cachekey, System::callstack(20), Duration::FIVE_MINUTES); } - $curlResult = HTTPSignature::fetchRaw($url); - $failed = empty($curlResult) || empty($curlResult->getBody()) || - (!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410)); - - if (!$failed) { - $data = json_decode($curlResult->getBody(), true); - $failed = empty($data) || !is_array($data); + if (Network::isLocalLink($url) && ($local_uid = User::getIdForURL($url))) { + $data = Transmitter::getProfile($local_uid); + $local_owner = User::getOwnerDataById($local_uid); } - if (!$failed && ($curlResult->getReturnCode() == 410)) { - $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone']; - } + if (empty($data)) { + $local_owner = []; - if ($failed) { - self::markForArchival($fetched_contact ?: []); - return $fetched_contact; + $curlResult = HTTPSignature::fetchRaw($url); + $failed = empty($curlResult) || empty($curlResult->getBody()) || + (!$curlResult->isSuccess() && ($curlResult->getReturnCode() != 410)); + + if (!$failed) { + $data = json_decode($curlResult->getBody(), true); + $failed = empty($data) || !is_array($data); + } + + if (!$failed && ($curlResult->getReturnCode() == 410)) { + $data = ['@context' => ActivityPub::CONTEXT, 'id' => $url, 'type' => 'Tombstone']; + } + + if ($failed) { + self::markForArchival($fetched_contact ?: []); + return $fetched_contact; + } } $compacted = JsonLD::compact($data); @@ -267,7 +278,11 @@ class APContact } if (!empty($apcontact['following'])) { - $following = ActivityPub::fetchContent($apcontact['following']); + if (!empty($local_owner)) { + $following = ActivityPub\Transmitter::getContacts($local_owner, [Contact::SHARING, Contact::FRIEND], 'following'); + } else { + $following = ActivityPub::fetchContent($apcontact['following']); + } if (!empty($following['totalItems'])) { // Mastodon seriously allows for this condition? // Jul 14 2021 - See https://mastodon.social/@BLUW for a negative following count @@ -279,7 +294,11 @@ class APContact } if (!empty($apcontact['followers'])) { - $followers = ActivityPub::fetchContent($apcontact['followers']); + if (!empty($local_owner)) { + $followers = ActivityPub\Transmitter::getContacts($local_owner, [Contact::FOLLOWER, Contact::FRIEND], 'followers'); + } else { + $followers = ActivityPub::fetchContent($apcontact['followers']); + } if (!empty($followers['totalItems'])) { // Mastodon seriously allows for this condition? // Jul 14 2021 - See https://mastodon.online/@goes11 for a negative followers count @@ -291,7 +310,11 @@ class APContact } if (!empty($apcontact['outbox'])) { - $outbox = ActivityPub::fetchContent($apcontact['outbox']); + if (!empty($local_owner)) { + $outbox = ActivityPub\Transmitter::getOutbox($local_owner); + } else { + $outbox = ActivityPub::fetchContent($apcontact['outbox']); + } if (!empty($outbox['totalItems'])) { $apcontact['statuses_count'] = $outbox['totalItems']; } diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index 134f69f4a..0f62ab537 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -22,8 +22,6 @@ namespace Friendica\Protocol; use Friendica\Core\Protocol; -use Friendica\Database\DBA; -use Friendica\DI; use Friendica\Model\APContact; use Friendica\Model\User; use Friendica\Util\HTTPSignature;