From 73b82d1455f1c1bdc74eb31ebd3d5653df50576e Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 1 Jan 2020 17:54:36 +0000 Subject: [PATCH 1/2] Don't add contacts when not needed --- src/Model/Contact.php | 20 ++++++++++++++------ src/Model/GContact.php | 34 +++++++++++++++++++++++++++++++++- src/Model/GServer.php | 2 +- src/Protocol/Diaspora.php | 6 ------ src/Worker/SearchDirectory.php | 3 +-- src/Worker/UpdateGContacts.php | 4 ++-- 6 files changed, 51 insertions(+), 18 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index d0d5d68666..a9b1820aac 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -277,21 +277,29 @@ class Contact */ public static function getBasepath($url, $dont_update = false) { - $contact = DBA::selectFirst('contact', ['baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]); + $contact = DBA::selectFirst('contact', ['id', 'baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]); + if (!DBA::isResult($contact)) { + return ''; + } + if (!empty($contact['baseurl'])) { return $contact['baseurl']; } elseif ($dont_update) { return ''; } - self::updateFromProbeByURL($url, true); + // Update the existing contact + self::updateFromProbe($contact['id'], '', true); - $contact = DBA::selectFirst('contact', ['baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]); - if (!empty($contact['baseurl'])) { - return $contact['baseurl']; + // And fetch the result + $contact = DBA::selectFirst('contact', ['baseurl'], ['id' => $contact['id']]); + if (empty($contact['baseurl'])) { + Logger::info('No baseurl for contact', ['url' => $url]); + return ''; } - return ''; + Logger::info('Found baseurl for contact', ['url' => $url, 'baseurl' => $contact['baseurl']]); + return $contact['baseurl']; } /** diff --git a/src/Model/GContact.php b/src/Model/GContact.php index 61e44b4106..e546b8c1ce 100644 --- a/src/Model/GContact.php +++ b/src/Model/GContact.php @@ -216,7 +216,7 @@ class GContact if (empty($gcontact['server_url'])) { // We check the server url to be sure that it is a real one - $server_url = Contact::getBasepath($gcontact['url']); + $server_url = self::getBasepath($gcontact['url']); // We are now sure that it is a correct URL. So we use it in the future if ($server_url != '') { @@ -1128,6 +1128,38 @@ class GContact self::update($gcontact); } + /** + * @brief Get the basepath for a given contact link + * + * @param string $url The gcontact link + * + * @return string basepath + * @return boolean $dont_update Don't update the contact + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function getBasepath($url, $dont_update = false) + { + $gcontact = DBA::selectFirst('gcontact', ['server_url'], ['nurl' => Strings::normaliseLink($url)]); + if (!empty($gcontact['server_url'])) { + return $gcontact['server_url']; + } elseif ($dont_update) { + return ''; + } + + self::updateFromProbe($url, true); + + // Fetch the result + $gcontact = DBA::selectFirst('gcontact', ['server_url'], ['nurl' => Strings::normaliseLink($url)]); + if (empty($gcontact['server_url'])) { + Logger::info('No baseurl for gcontact', ['url' => $url]); + return ''; + } + + Logger::info('Found baseurl for gcontact', ['url' => $url, 'baseurl' => $gcontact['server_url']]); + return $gcontact['server_url']; + } + /** * @brief Fetches users of given GNU Social server * diff --git a/src/Model/GServer.php b/src/Model/GServer.php index f17265a3a3..de8a3c1a1a 100644 --- a/src/Model/GServer.php +++ b/src/Model/GServer.php @@ -44,7 +44,7 @@ class GServer public static function reachable(string $profile, string $server = '', string $network = '', bool $force = false) { if ($server == '') { - $server = Contact::getBasepath($profile); + $server = GContact::getBasepath($profile); } if ($server == '') { diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 7704d3f07b..5dd8e6c98b 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -1132,12 +1132,6 @@ class Diaspora private static function contactByHandle($uid, $handle) { $cid = Contact::getIdForURL($handle, $uid); - if (!$cid) { - $handle_parts = explode("@", $handle); - $nurl_sql = "%%://" . $handle_parts[1] . "%%/profile/" . $handle_parts[0]; - $cid = Contact::getIdForURL($nurl_sql, $uid); - } - if (!$cid) { Logger::log("Haven't found a contact for user " . $uid . " and handle " . $handle, Logger::DEBUG); return false; diff --git a/src/Worker/SearchDirectory.php b/src/Worker/SearchDirectory.php index 4fd6d44d50..d489acb7fb 100644 --- a/src/Worker/SearchDirectory.php +++ b/src/Worker/SearchDirectory.php @@ -10,7 +10,6 @@ use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Database\DBA; use Friendica\Model\GContact; -use Friendica\Model\Contact; use Friendica\Model\GServer; use Friendica\Network\Probe; use Friendica\Util\Network; @@ -55,7 +54,7 @@ class SearchDirectory continue; } - $server_url = Contact::getBasepath($jj->url); + $server_url = GContact::getBasepath($jj->url, true); if ($server_url != '') { if (!GServer::check($server_url)) { Logger::info("Friendica server doesn't answer.", ['server' => $server_url]); diff --git a/src/Worker/UpdateGContacts.php b/src/Worker/UpdateGContacts.php index 1d9d86bcf6..edee112eef 100644 --- a/src/Worker/UpdateGContacts.php +++ b/src/Worker/UpdateGContacts.php @@ -9,7 +9,7 @@ use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Core\Worker; use Friendica\Database\DBA; -use Friendica\Model\Contact; +use Friendica\Model\GContact; use Friendica\Model\GServer; use Friendica\Util\DateTimeFormat; use Friendica\Util\Strings; @@ -53,7 +53,7 @@ class UpdateGContacts continue; } - $server_url = Contact::getBasepath($contact['url']); + $server_url = GContact::getBasepath($contact['url'], true); $force_update = false; if (!empty($contact['server_url'])) { From bd6fb64c162035e066b39eabe0903a1200bffa19 Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 1 Jan 2020 18:57:55 +0000 Subject: [PATCH 2/2] Fixed documentation --- src/Model/Contact.php | 2 +- src/Model/GContact.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index a9b1820aac..e63aa7aec1 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -269,9 +269,9 @@ class Contact * @brief Get the basepath for a given contact link * * @param string $url The contact link + * @param boolean $dont_update Don't update the contact * * @return string basepath - * @return boolean $dont_update Don't update the contact * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ diff --git a/src/Model/GContact.php b/src/Model/GContact.php index e546b8c1ce..22447aab47 100644 --- a/src/Model/GContact.php +++ b/src/Model/GContact.php @@ -1128,13 +1128,13 @@ class GContact self::update($gcontact); } - /** + /** * @brief Get the basepath for a given contact link * * @param string $url The gcontact link + * @param boolean $dont_update Don't update the contact * * @return string basepath - * @return boolean $dont_update Don't update the contact * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */