X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=src%2FModel%2FAPContact.php;h=7a63e413cab3ee5f6f26d6ad8c5b8acfe1c4a629;hb=a24febb8b656217a7cd88db205007420cb64485e;hp=9fc72ac5a49d7780a1c4fef68976e323b0c9ef1c;hpb=aa0b485f3dca72c5448076e913fa54d948cd7731;p=friendica.git diff --git a/src/Model/APContact.php b/src/Model/APContact.php index 9fc72ac5a4..7a63e413ca 100644 --- a/src/Model/APContact.php +++ b/src/Model/APContact.php @@ -1,6 +1,6 @@ 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); - if (empty($compacted['@id'])) { return $fetched_contact; } + // Detect multiple fast repeating request to the same address + // See https://github.com/friendica/friendica/issues/9303 + $cachekey = 'apcontact:getByURL:' . $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]); + } else { + DI::cache()->set($cachekey, System::callstack(20), Duration::FIVE_MINUTES); + } + $apcontact['url'] = $compacted['@id']; $apcontact['uuid'] = JsonLD::fetchElement($compacted, 'diaspora:guid', '@value'); $apcontact['type'] = str_replace('as:', '', JsonLD::fetchElement($compacted, '@type')); @@ -192,8 +218,11 @@ class APContact } // Quit if none of the basic values are set - if (empty($apcontact['url']) || empty($apcontact['inbox']) || empty($apcontact['type'])) { + if (empty($apcontact['url']) || empty($apcontact['type']) || (($apcontact['type'] != 'Tombstone') && empty($apcontact['inbox']))) { return $fetched_contact; + } elseif ($apcontact['type'] == 'Tombstone') { + // The "inbox" field must have a content + $apcontact['inbox'] = ''; } // Quit if this doesn't seem to be an account at all @@ -206,7 +235,7 @@ class APContact unset($parts['path']); if (empty($apcontact['addr'])) { - if (!empty($apcontact['nick'])) { + if (!empty($apcontact['nick']) && is_array($parts)) { $apcontact['addr'] = $apcontact['nick'] . '@' . str_replace('//', '', Network::unparseURL($parts)); } else { $apcontact['addr'] = ''; @@ -229,29 +258,23 @@ class APContact } if (!empty($apcontact['following'])) { - $data = ActivityPub::fetchContent($apcontact['following']); - if (!empty($data)) { - if (!empty($data['totalItems'])) { - $apcontact['following_count'] = $data['totalItems']; - } + $following = ActivityPub::fetchContent($apcontact['following']); + if (!empty($following['totalItems'])) { + $apcontact['following_count'] = $following['totalItems']; } } if (!empty($apcontact['followers'])) { - $data = ActivityPub::fetchContent($apcontact['followers']); - if (!empty($data)) { - if (!empty($data['totalItems'])) { - $apcontact['followers_count'] = $data['totalItems']; - } + $followers = ActivityPub::fetchContent($apcontact['followers']); + if (!empty($followers['totalItems'])) { + $apcontact['followers_count'] = $followers['totalItems']; } } if (!empty($apcontact['outbox'])) { - $data = ActivityPub::fetchContent($apcontact['outbox']); - if (!empty($data)) { - if (!empty($data['totalItems'])) { - $apcontact['statuses_count'] = $data['totalItems']; - } + $outbox = ActivityPub::fetchContent($apcontact['outbox']); + if (!empty($outbox['totalItems'])) { + $apcontact['statuses_count'] = $outbox['totalItems']; } } @@ -304,7 +327,7 @@ class APContact if (empty($apcontact['subscribe'])) { $apcontact['subscribe'] = null; - } + } if (!empty($apcontact['baseurl']) && empty($fetched_contact['gsid'])) { $apcontact['gsid'] = GServer::getID($apcontact['baseurl']); @@ -320,22 +343,71 @@ class APContact $apcontact['updated'] = DateTimeFormat::utcNow(); - DBA::update('apcontact', $apcontact, ['url' => $url], true); - // We delete the old entry when the URL is changed - if (($url != $apcontact['url']) && DBA::exists('apcontact', ['url' => $url]) && DBA::exists('apcontact', ['url' => $apcontact['url']])) { + if ($url != $apcontact['url']) { + Logger::info('Delete changed profile url', ['old' => $url, 'new' => $apcontact['url']]); DBA::delete('apcontact', ['url' => $url]); } + if (DBA::exists('apcontact', ['url' => $apcontact['url']])) { + DBA::update('apcontact', $apcontact, ['url' => $apcontact['url']]); + } else { + DBA::replace('apcontact', $apcontact); + } + Logger::info('Updated profile', ['url' => $url]); return $apcontact; } + /** + * Mark the given AP Contact as "to archive" + * + * @param array $apcontact + * @return void + */ + public static function markForArchival(array $apcontact) + { + if (!empty($apcontact['inbox'])) { + Logger::info('Set inbox status to failure', ['inbox' => $apcontact['inbox']]); + HTTPSignature::setInboxStatus($apcontact['inbox'], false); + } + + if (!empty($apcontact['sharedinbox'])) { + // Check if there are any available inboxes + $available = DBA::exists('apcontact', ["`sharedinbox` = ? AnD `inbox` IN (SELECT `url` FROM `inbox-status` WHERE `success` > `failure`)", + $apcontact['sharedinbox']]); + if (!$available) { + // If all known personal inboxes are failing then set their shared inbox to failure as well + Logger::info('Set shared inbox status to failure', ['sharedinbox' => $apcontact['sharedinbox']]); + HTTPSignature::setInboxStatus($apcontact['sharedinbox'], false, true); + } + } + } + + /** + * Unmark the given AP Contact as "to archive" + * + * @param array $apcontact + * @return void + */ + public static function unmarkForArchival(array $apcontact) + { + if (!empty($apcontact['inbox'])) { + Logger::info('Set inbox status to success', ['inbox' => $apcontact['inbox']]); + HTTPSignature::setInboxStatus($apcontact['inbox'], true); + } + if (!empty($apcontact['sharedinbox'])) { + Logger::info('Set shared inbox status to success', ['sharedinbox' => $apcontact['sharedinbox']]); + HTTPSignature::setInboxStatus($apcontact['sharedinbox'], true, true); + } + } + /** * Unarchive inboxes * - * @param string $url inbox url + * @param string $url inbox url + * @param boolean $shared Shared Inbox */ private static function unarchiveInbox($url, $shared) { @@ -343,15 +415,6 @@ class APContact return; } - $now = DateTimeFormat::utcNow(); - - $fields = ['archive' => false, 'success' => $now, 'shared' => $shared]; - - if (!DBA::exists('inbox-status', ['url' => $url])) { - $fields = array_merge($fields, ['url' => $url, 'created' => $now]); - DBA::insert('inbox-status', $fields); - } else { - DBA::update('inbox-status', $fields, ['url' => $url]); - } + HTTPSignature::setInboxStatus($url, true, $shared); } }