]> git.mxchange.org Git - friendica.git/commitdiff
Fetch local data without HTTP requests
authorMichael <heluecht@pirati.ca>
Sun, 18 Jul 2021 18:54:25 +0000 (18:54 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 18 Jul 2021 18:54:25 +0000 (18:54 +0000)
src/Model/APContact.php
src/Protocol/ActivityPub.php

index e9500f85b89c585dd301a0e54ee3db6f3a54c5c1..a4c97f6232f672c9030449bff3c83136fe4ee20a 100644 (file)
@@ -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'];
                        }
index 134f69f4a66fd9a6d45c39cfdb063e734840471f..0f62ab5376ca192f9cb9ad59ab4f9a74703a1b5c 100644 (file)
@@ -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;