From 264936100f06d95a3db7399a8d5a107c8169a7a8 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 11:09:12 +0000 Subject: [PATCH 1/2] New functions to check if a contact supports that protocol --- src/Protocol/ActivityPub.php | 14 ++++++++++ src/Protocol/DFRN.php | 16 +++++++++++ src/Protocol/Diaspora.php | 53 ++++++++++++++++++++++++------------ src/Protocol/OStatus.php | 15 ++++++++++ 4 files changed, 81 insertions(+), 17 deletions(-) diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index 6c7bbb474..14fca625a 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -194,4 +194,18 @@ class ActivityPub ActivityPub\Receiver::processActivity($ldactivity, '', $uid, true); } } + + /** + * Checks if the given contact url does support ActivityPub + * + * @param string $url profile url + * @param boolean $update Update the profile + * @return boolean + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function isSupportedByContactUrl($url, $update = null) + { + return !empty(APContact::getByURL($url, $update)); + } } diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 5fceab326..e8196172e 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -29,6 +29,7 @@ use Friendica\Model\Mail; use Friendica\Model\PermissionSet; use Friendica\Model\Profile; use Friendica\Model\User; +use Friendica\Network\Probe; use Friendica\Object\Image; use Friendica\Util\BaseURL; use Friendica\Util\Crypto; @@ -3041,4 +3042,19 @@ class DFRN return (strcmp($existing_edited, $update_edited) < 0); } + + /** + * Checks if the given contact url does support DFRN + * + * @param string $url profile url + * @param boolean $update Update the profile + * @return boolean + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function isSupportedByContactUrl($url, $update = false) + { + $probe = Probe::uri($url, Protocol::DFRN, 0, !$update); + return $probe['network'] == Protocol::DFRN; + } } diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 18dcd759a..119ce72f1 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -942,31 +942,41 @@ class Diaspora * @brief Fetches data for a given handle * * @param string $handle The handle + * @param boolean $update Update the profile * * @return array the queried data * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function personByHandle($handle) + public static function personByHandle($handle, $update = null) { - $update = false; - $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); + if (!DBA::isResult($person)) { + $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)]; + $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]); + } + if (DBA::isResult($person)) { Logger::debug("In cache " . print_r($person, true)); - // update record occasionally so it doesn't get stale - $d = strtotime($person["updated"]." +00:00"); - if ($d < strtotime("now - 14 days")) { - $update = true; - } + if (is_null($update)) { + // update record occasionally so it doesn't get stale + $d = strtotime($person["updated"]." +00:00"); + if ($d < strtotime("now - 14 days")) { + $update = true; + } - if ($person["guid"] == "") { - $update = true; + if ($person["guid"] == "") { + $update = true; + } } + } elseif (is_null($update)) { + $update = !DBA::isResult($person); + } else { + $person = []; } - if (!DBA::isResult($person) || $update) { + if ($update) { Logger::log("create or refresh", Logger::DEBUG); $r = Probe::uri($handle, Protocol::DIASPORA); @@ -975,12 +985,7 @@ class Diaspora if ($r && ($r["network"] === Protocol::DIASPORA)) { self::updateFContact($r); - // Fetch the updated or added contact - $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); - if (!DBA::isResult($person)) { - $person = $r; - $person['id'] = 0; - } + $person = self::personByHandle($handle, false); } } @@ -1117,6 +1122,20 @@ class Diaspora return $contact; } + /** + * Checks if the given contact url does support ActivityPub + * + * @param string $url profile url + * @param boolean $update Update the profile + * @return boolean + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function isSupportedByContactUrl($url, $update = null) + { + return !empty(self::personByHandle($url, $update)); + } + /** * @brief Check if posting is allowed for this contact * diff --git a/src/Protocol/OStatus.php b/src/Protocol/OStatus.php index b3685a147..054eaf51b 100644 --- a/src/Protocol/OStatus.php +++ b/src/Protocol/OStatus.php @@ -2302,4 +2302,19 @@ class OStatus return trim($doc->saveXML()); } + + /** + * Checks if the given contact url does support OStatus + * + * @param string $url profile url + * @param boolean $update Update the profile + * @return boolean + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function isSupportedByContactUrl($url, $update = false) + { + $probe = Probe::uri($url, Protocol::OSTATUS, 0, !$update); + return $probe['network'] == Protocol::OSTATUS; + } } From f5cb6148cfa37b7471c15dcb070f2f34ee7d904c Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 27 Jul 2019 21:45:36 +0000 Subject: [PATCH 2/2] Improved documentation --- src/Protocol/ActivityPub.php | 2 +- src/Protocol/Diaspora.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index 14fca625a..3a8a5e5b1 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -199,7 +199,7 @@ class ActivityPub * Checks if the given contact url does support ActivityPub * * @param string $url profile url - * @param boolean $update Update the profile + * @param boolean $update true = always update, false = never update, null = update when not found or outdated * @return boolean * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException diff --git a/src/Protocol/Diaspora.php b/src/Protocol/Diaspora.php index 119ce72f1..325debe7b 100644 --- a/src/Protocol/Diaspora.php +++ b/src/Protocol/Diaspora.php @@ -942,7 +942,7 @@ class Diaspora * @brief Fetches data for a given handle * * @param string $handle The handle - * @param boolean $update Update the profile + * @param boolean $update true = always update, false = never update, null = update when not found or outdated * * @return array the queried data * @throws \Friendica\Network\HTTPException\InternalServerErrorException @@ -1126,7 +1126,7 @@ class Diaspora * Checks if the given contact url does support ActivityPub * * @param string $url profile url - * @param boolean $update Update the profile + * @param boolean $update true = always update, false = never update, null = update when not found or outdated * @return boolean * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException