From 4cb1ed2c9da673ffc0a0d752906ebdee292c5ea4 Mon Sep 17 00:00:00 2001 From: Diogo Cordeiro Date: Fri, 6 Jul 2018 11:47:28 +0100 Subject: [PATCH] Add discovery module --- utils/discovery.php | 65 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 utils/discovery.php diff --git a/utils/discovery.php b/utils/discovery.php new file mode 100644 index 0000000..f1c45da --- /dev/null +++ b/utils/discovery.php @@ -0,0 +1,65 @@ +. + * + * @category Feed + * @package GNUsocial + * @author Daniel Supernault + * @author Diogo Cordeiro + * @copyright 2018 Free Software Foundaction, Inc. + * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 + * @link https://gnu.io/social + */ + +if (!defined('GNUSOCIAL')) { exit(1); } + +class Activitypub_Discovery { + public function lookup ($url) + { + // First check if we already have it locally + if (($actor_profile = Profile::getKV("profileurl", $url)) != false) { + return $actor_profile; + } + + // If that's not the case, grab it + $client = new HTTPClient (); + $headers = array(); + $headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"'; + $headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social'; + $response = $client->get ($url, $headers); + $this->response = json_decode ($response->getBody (), JSON_UNESCAPED_SLASHES); + if (!$response->isOk ()) + ActivityPubReturn::error("Invalid Actor URL", 404); + return $this->storeProfile (); + } + + public function storeProfile () + { + $res = $this->response; + $profile = new Profile; + $profile->profileurl = $res["url"]; + $profile->nickname = $res["nickname"]; + $profile->fulname = $res["display_name"]; + $profile->bio = str_limit($res["summary"], 1000); + $profile->insert (); + + return $profile; + } +}