From c364a77d630271ee7dd8ad00ef92a223439ba011 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 21 Sep 2019 12:39:07 +0000 Subject: [PATCH 1/4] Always use direct DFRN transport on local contacts --- src/Model/Contact.php | 31 ++++++++++++++++++------ src/Protocol/ActivityPub/Transmitter.php | 8 ++++++ src/Worker/Notifier.php | 5 ++++ 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 6f0de5acd9..1ea6704bb9 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -290,6 +290,19 @@ class Contact extends BaseObject return ''; } + /** + * Check if the given contact url is on the same machine + * + * @param string $url The contact link + * + * @return boolean Is it the same machine? + */ + public static function isLocal($url) + { + return Strings::compareLink(self::getBasepath($url), System::baseUrl()); + + } + /** * Returns the public contact id of the given user id * @@ -2486,6 +2499,9 @@ class Contact extends BaseObject ['id' => $contact['id'], 'uid' => $importer['uid']]); } + // Ensure to always have the correct network type, independent from the connection request method + self::updateFromProbe($contact['id'], '', true); + return true; } else { // send email notification to owner? @@ -2511,15 +2527,14 @@ class Contact extends BaseObject 'writable' => 1, ]); - $contact_record = [ - 'id' => DBA::lastInsertId(), - 'network' => $network, - 'name' => $name, - 'url' => $url, - 'photo' => $photo - ]; + $contact_id = DBA::lastInsertId(); - Contact::updateAvatar($photo, $importer["uid"], $contact_record["id"], true); + // Ensure to always have the correct network type, independent from the connection request method + self::updateFromProbe($contact_id, '', true); + + Contact::updateAvatar($photo, $importer["uid"], $contact_id, true); + + $contact_record = DBA::selectFirst('contact', ['id', 'network', 'name', 'url', 'photo'], ['id' => $contact_id]); /// @TODO Encapsulate this into a function/method $fields = ['uid', 'username', 'email', 'page-flags', 'notify-flags', 'language']; diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index e44ae1cf66..19c0cc28ed 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -544,6 +544,10 @@ class Transmitter $contacts = DBA::select('contact', ['url', 'network', 'protocol'], $condition); while ($contact = DBA::fetch($contacts)) { + if (Contact::isLocal($contact['url'])) { + continue; + } + if (!in_array($contact['network'], $networks) && ($contact['protocol'] != Protocol::ACTIVITYPUB)) { continue; } @@ -611,6 +615,10 @@ class Transmitter if ($receiver == $item_profile['followers']) { $inboxes = array_merge($inboxes, self::fetchTargetInboxesforUser($uid, $personal)); } else { + if (Contact::isLocal($receiver)) { + continue; + } + $profile = APContact::getByURL($receiver, false); if (!empty($profile)) { if (empty($profile['sharedinbox']) || $personal || $blindcopy) { diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 154a638502..8755a59b54 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -568,6 +568,11 @@ class Notifier */ private static function skipDFRN($contact, $item, $cmd) { + // Use DFRN if we are on the same site + if (Contact::isLocal($contact['url'])) { + return false; + } + // Don't skip when author or owner don't have AP profiles if ((!empty($item['author-link']) && empty(APContact::getByURL($item['author-link'], false))) || (!empty($item['owner-link']) && empty(APContact::getByURL($item['owner-link'], false)))) { return false; From cf569425c6df85ddc8190a502c6bd3be96081175 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 21 Sep 2019 13:00:53 +0000 Subject: [PATCH 2/4] Avoid a notice --- src/Worker/Notifier.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index 8755a59b54..cb2d83af6d 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -569,7 +569,7 @@ class Notifier private static function skipDFRN($contact, $item, $cmd) { // Use DFRN if we are on the same site - if (Contact::isLocal($contact['url'])) { + if (!empty($contact['url']) && Contact::isLocal($contact['url'])) { return false; } From b24ef7247dca299e4805cb0c3e1e815873cbc47e Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 21 Sep 2019 13:17:33 +0000 Subject: [PATCH 3/4] Don't update the basepath on local check --- src/Model/Contact.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 1ea6704bb9..2e8cb3d57b 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -270,14 +270,17 @@ class Contact extends BaseObject * @param string $url The contact 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) + public static function getBasepath($url, $dont_update = false) { $contact = DBA::selectFirst('contact', ['baseurl'], ['uid' => 0, 'nurl' => Strings::normaliseLink($url)]); if (!empty($contact['baseurl'])) { return $contact['baseurl']; + } elseif ($dont_update) { + return ''; } self::updateFromProbeByURL($url, true); @@ -299,8 +302,7 @@ class Contact extends BaseObject */ public static function isLocal($url) { - return Strings::compareLink(self::getBasepath($url), System::baseUrl()); - + return Strings::compareLink(self::getBasepath($url, true), System::baseUrl()); } /** From a2105c5dfec5cbc08a2cd76b122b57f5202059c1 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 21 Sep 2019 13:19:00 +0000 Subject: [PATCH 4/4] Changed documentation --- src/Model/Contact.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 2e8cb3d57b..798c9cd209 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -294,11 +294,11 @@ class Contact extends BaseObject } /** - * Check if the given contact url is on the same machine + * Check if the given contact url is on the same server * * @param string $url The contact link * - * @return boolean Is it the same machine? + * @return boolean Is it the same server? */ public static function isLocal($url) {