Minor corrections

This commit is contained in:
Diogo Cordeiro 2018-07-09 00:07:14 +01:00
parent 167f88a602
commit f533ef2d32

View File

@ -1,4 +1,7 @@
<?php <?php
use Profile;
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
@ -34,18 +37,19 @@ class Activitypub_Discovery {
public function lookup ($url) public function lookup ($url)
{ {
// First check if we already have it locally and, if so, return it // First check if we already have it locally and, if so, return it
if (($actor_profile = Profile::getKV("profileurl", $url)) != false) { if (($actor_profile = Profile::getKV ("profileurl", $url)) != false) {
return $actor_profile; return $actor_profile;
} else { } else {
// Sometimes it is not true that the user is not locally available, // Sometimes it is not true that the user is not locally available,
// mostly when it is a local user and URLs slightly changed // mostly when it is a local user and URLs slightly changed
// e.g.: GS instance owner changed from standard urls to pretty urls
// (not sure if this is necessary, but anyway) // (not sure if this is necessary, but anyway)
// Iff we really are in the same instance // Iff we really are in the same instance
$root_url_len = strlen (common_root_url ()); $root_url_len = strlen (common_root_url ());
if (substr ($url, 0, $root_url_len) == common_root_url ()) { if (substr ($url, 0, $root_url_len) == common_root_url ()) {
// Grab the nickname and try to get the user // Grab the nickname and try to get the user
if (($actor_profile = Profile::getKV("nickname", substr ($url, $root_url_len))) != false) { if (($actor_profile = Profile::getKV ("nickname", substr ($url, $root_url_len))) != false) {
return $actor_profile; return $actor_profile;
} }
} }
@ -58,14 +62,20 @@ class Activitypub_Discovery {
$headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social'; $headers[] = 'User-Agent: GNUSocialBot v0.1 - https://gnu.io/social';
$response = $client->get ($url, $headers); $response = $client->get ($url, $headers);
$this->response = json_decode ($response->getBody (), JSON_UNESCAPED_SLASHES); $this->response = json_decode ($response->getBody (), JSON_UNESCAPED_SLASHES);
if (!$response->isOk ()) if (!$response->isOk()) {
ActivityPubReturn::error ("Invalid Actor URL", 404); throw new NoResultException ("Invalid Actor URL.");
}
return $this->storeProfile (); return $this->storeProfile ();
} }
public function storeProfile () private function storeProfile ()
{ {
$res = $this->response; $res = $this->response;
// Validate response
if (!isset ($res["url"], $res["nickname"], $res["display_name"], $res["summary"])) {
throw new NoProfileException("Invalid Actor URL.");
}
$profile = new Profile; $profile = new Profile;
$profile->profileurl = $res["url"]; $profile->profileurl = $res["url"];
$profile->nickname = $res["nickname"]; $profile->nickname = $res["nickname"];