]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/APContact.php
Changed to null-coalscing style (??) as sugguested by @MrPetovan
[friendica.git] / src / Model / APContact.php
index b519655e13bbe97e57377a5e12ae3505957bac40..69f9be5135095d3df00c0eb35dcc58073dc94c50 100644 (file)
@@ -28,6 +28,7 @@ use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
+use Friendica\Network\HTTPClient\Client\HttpClientAccept;
 use Friendica\Network\HTTPException;
 use Friendica\Network\Probe;
 use Friendica\Protocol\ActivityNamespace;
@@ -38,7 +39,6 @@ use Friendica\Util\DateTimeFormat;
 use Friendica\Util\HTTPSignature;
 use Friendica\Util\JsonLD;
 use Friendica\Util\Network;
-use Friendica\Util\Strings;
 
 class APContact
 {
@@ -71,10 +71,10 @@ class APContact
 
                $data = ['addr' => $addr];
                $template = 'https://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
-               $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), 'application/jrd+json');
+               $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClientAccept::JRD_JSON);
                if (empty($webfinger['links'])) {
                        $template = 'http://' . $addr_parts[1] . '/.well-known/webfinger?resource=acct:' . urlencode($addr);
-                       $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), 'application/jrd+json');
+                       $webfinger = Probe::webfinger(str_replace('{uri}', urlencode($addr), $template), HttpClientAccept::JRD_JSON);
                        if (empty($webfinger['links'])) {
                                return [];
                        }
@@ -168,7 +168,7 @@ class APContact
 
                // Detect multiple fast repeating request to the same address
                // See https://github.com/friendica/friendica/issues/9303
-               $cachekey = 'apcontact:getByURL:' . $url;
+               $cachekey = 'apcontact:' . ItemURI::getIdByURI($url);
                $result = DI::cache()->get($cachekey);
                if (!is_null($result)) {
                        Logger::notice('Multiple requests for the address', ['url' => $url, 'update' => $update, 'callstack' => System::callstack(20), 'result' => $result]);
@@ -231,6 +231,9 @@ class APContact
                        self::unarchiveInbox($apcontact['sharedinbox'], true);
                }
 
+               $apcontact['featured']      = JsonLD::fetchElement($compacted, 'toot:featured', '@id');
+               $apcontact['featured-tags'] = JsonLD::fetchElement($compacted, 'toot:featuredTags', '@id');
+
                $apcontact['nick'] = JsonLD::fetchElement($compacted, 'as:preferredUsername', '@value') ?? '';
                $apcontact['name'] = JsonLD::fetchElement($compacted, 'as:name', '@value');
 
@@ -238,9 +241,11 @@ class APContact
                        $apcontact['name'] = $apcontact['nick'];
                }
 
-               $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary', '@value'));
+               $aboutHtml = JsonLD::fetchElement($compacted, 'as:summary', '@value');
+               $apcontact['about'] = (!empty($aboutHtml) ? HTML::toBBCode($aboutHtml) : '');
 
                $ims = JsonLD::fetchElementArray($compacted, 'vcard:hasInstantMessage');
+
                if (!empty($ims)) {
                        foreach ($ims as $link) {
                                if (substr($link, 0, 5) == 'xmpp:') {
@@ -362,7 +367,7 @@ class APContact
                // To-Do
 
                // Unhandled
-               // tag, attachment, image, nomadicLocations, signature, featured, movedTo, liked
+               // tag, attachment, image, nomadicLocations, signature, movedTo, liked
 
                // Unhandled from Misskey
                // sharedInbox, isCat
@@ -428,6 +433,30 @@ class APContact
                        $apcontact['uri-id'] = ItemURI::insert(['uri' => $apcontact['url'], 'guid' => $apcontact['uuid']]);
                }
 
+               foreach (APContact\Endpoint::ENDPOINT_NAMES as $type => $name) {
+                       $value = JsonLD::fetchElement($compacted, $name, '@id');
+                       if (empty($value)) {
+                               continue;
+                       }
+                       APContact\Endpoint::update($apcontact['uri-id'], $type, $value);
+               }
+
+               if (!empty($compacted['as:endpoints'])) {
+                       foreach ($compacted['as:endpoints'] as $name => $endpoint) {
+                               if (empty($endpoint['@id']) || !is_string($endpoint['@id'])) {
+                                       continue;
+                               }
+
+                               if (in_array($name, APContact\Endpoint::ENDPOINT_NAMES)) {
+                                       $key = array_search($name, APContact\Endpoint::ENDPOINT_NAMES);
+                                       APContact\Endpoint::update($apcontact['uri-id'], $key, $endpoint['@id']);
+                                       Logger::debug('Store endpoint', ['key' => $key, 'name' => $name, 'endpoint' => $endpoint['@id']]);
+                               } elseif (!in_array($name, ['as:sharedInbox', 'as:uploadMedia', 'as:oauthTokenEndpoint', 'as:oauthAuthorizationEndpoint', 'litepub:oauthRegistrationEndpoint'])) {
+                                       Logger::debug('Unknown endpoint', ['name' => $name, 'endpoint' => $endpoint['@id']]);
+                               }
+                       }
+               }
+
                $apcontact['updated'] = DateTimeFormat::utcNow();
 
                // We delete the old entry when the URL is changed