]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/APContact.php
doc/themes.md,FAQ-admin: point to live friendica-themes.com mirror
[friendica.git] / src / Model / APContact.php
index 7bdde60c65ca31be4446c4009378bb1a9446d3f5..e068ecefe2b9d534c6c10c5639f27cee90ddfab0 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2021, the Friendica project
+ * @copyright Copyright (C) 2010-2022, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 namespace Friendica\Model;
 
 use Friendica\Content\Text\HTML;
-use Friendica\Core\Cache\Duration;
+use Friendica\Core\Cache\Enum\Duration;
 use Friendica\Core\Logger;
 use Friendica\Core\System;
 use Friendica\Database\DBA;
 use Friendica\Database\DBStructure;
 use Friendica\DI;
+use Friendica\Network\HTTPClient\Client\HttpClient;
+use Friendica\Network\HTTPException;
 use Friendica\Network\Probe;
 use Friendica\Protocol\ActivityNamespace;
 use Friendica\Protocol\ActivityPub;
@@ -37,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
 {
@@ -70,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), HttpClient::ACCEPT_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), HttpClient::ACCEPT_JRD_JSON);
                        if (empty($webfinger['links'])) {
                                return [];
                        }
@@ -118,7 +119,8 @@ class APContact
         */
        public static function getByURL($url, $update = null)
        {
-               if (empty($url)) {
+               if (empty($url) || Network::isUrlBlocked($url)) {
+                       Logger::info('Domain is blocked', ['url' => $url]);
                        return [];
                }
 
@@ -166,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]);
@@ -178,8 +180,12 @@ class APContact
                }
 
                if (Network::isLocalLink($url) && ($local_uid = User::getIdForURL($url))) {
-                       $data  = Transmitter::getProfile($local_uid);
-                       $local_owner = User::getOwnerDataById($local_uid);
+                       try {
+                               $data = Transmitter::getProfile($local_uid);
+                               $local_owner = User::getOwnerDataById($local_uid);
+                       } catch(HTTPException\NotFoundException $e) {
+                               $data = null;
+                       }
                }
 
                if (empty($data)) {
@@ -225,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');
 
@@ -234,6 +243,18 @@ class APContact
 
                $apcontact['about'] = HTML::toBBCode(JsonLD::fetchElement($compacted, 'as:summary', '@value'));
 
+               $ims = JsonLD::fetchElementArray($compacted, 'vcard:hasInstantMessage');
+               if (!empty($ims)) {
+                       foreach ($ims as $link) {
+                               if (substr($link, 0, 5) == 'xmpp:') {
+                                       $apcontact['xmpp'] = substr($link, 5);
+                               }
+                               if (substr($link, 0, 7) == 'matrix:') {
+                                       $apcontact['matrix'] = substr($link, 7);
+                               }
+                       }
+               }
+
                $apcontact['photo'] = JsonLD::fetchElement($compacted, 'as:icon', '@id');
                if (is_array($apcontact['photo']) || !empty($compacted['as:icon']['as:url']['@id'])) {
                        $apcontact['photo'] = JsonLD::fetchElement($compacted['as:icon'], 'as:url', '@id');
@@ -330,6 +351,11 @@ class APContact
                                $outbox = ActivityPub::fetchContent($apcontact['outbox']);
                        }
                        if (!empty($outbox['totalItems'])) {
+                               // Mastodon seriously allows for this condition?
+                               // Jul 20 2021 - See https://chaos.social/@m11 for a negative posts count
+                               if ($outbox['totalItems'] < 0) {
+                                       $outbox['totalItems'] = 0;
+                               }
                                $apcontact['statuses_count'] = $outbox['totalItems'];
                        }
                }