[ActivityPub] Consistent headers in explorer requests

Minor indentation fixes
This commit is contained in:
Diogo Cordeiro 2019-10-11 16:00:11 +01:00
parent f1717bde51
commit d9b5ef1cee
2 changed files with 70 additions and 83 deletions

View File

@ -424,7 +424,7 @@ class ActivityPubPlugin extends Plugin
if ($aprofile instanceof Activitypub_profile) { if ($aprofile instanceof Activitypub_profile) {
$url = common_local_url('userbyid', ['id' => $aprofile->getID()], null, null, false); $url = common_local_url('userbyid', ['id' => $aprofile->getID()], null, null, false);
common_redirect($url, 303); common_redirect($url, 303);
return true; return false;
} }
} elseif (filter_var($query, FILTER_VALIDATE_URL)) { // URL found! } elseif (filter_var($query, FILTER_VALIDATE_URL)) { // URL found!
/* Is this an ActivityPub notice? */ /* Is this an ActivityPub notice? */

View File

@ -40,6 +40,8 @@ class Activitypub_explorer
{ {
private $discovered_actor_profiles = []; private $discovered_actor_profiles = [];
private $temp_res; // global variable to hold a temporary http response private $temp_res; // global variable to hold a temporary http response
private static $headers = ['Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"',
'User-Agent: GNUsocialBot ' . GNUSOCIAL_VERSION . ' - https://gnusocial.network'];
/** /**
* Shortcut function to get a single profile from its URL. * Shortcut function to get a single profile from its URL.
@ -120,18 +122,15 @@ class Activitypub_explorer
/** /**
* This ensures that we are using a valid ActivityPub URI * This ensures that we are using a valid ActivityPub URI
* *
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param string $url * @param string $url
* @return bool success state (related to the response) * @return bool success state (related to the response)
* @throws Exception (If the HTTP request fails) * @throws Exception (If the HTTP request fails)
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/ */
private function ensure_proper_remote_uri($url) private function ensure_proper_remote_uri($url)
{ {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$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);
$res = json_decode($response->getBody(), true); $res = json_decode($response->getBody(), true);
if (self::validate_remote_response($res)) { if (self::validate_remote_response($res)) {
$this->temp_res = $res; $this->temp_res = $res;
@ -224,10 +223,7 @@ class Activitypub_explorer
common_debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $url); common_debug('ActivityPub Explorer: Trying to grab a remote actor for ' . $url);
if (!isset($this->temp_res)) { if (!isset($this->temp_res)) {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$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);
$res = json_decode($response->getBody(), true); $res = json_decode($response->getBody(), true);
} else { } else {
$res = $this->temp_res; $res = $this->temp_res;
@ -294,11 +290,11 @@ class Activitypub_explorer
/** /**
* Download and update given avatar image * Download and update given avatar image
* *
* @author GNU social
* @param Profile $profile * @param Profile $profile
* @param string $url * @param string $url
* @return Avatar The Avatar we have on disk. * @return Avatar The Avatar we have on disk.
* @throws Exception in various failure cases * @throws Exception in various failure cases
* @author GNU social
*/ */
public static function update_avatar(Profile $profile, $url) public static function update_avatar(Profile $profile, $url)
{ {
@ -361,9 +357,9 @@ class Activitypub_explorer
* Validates a remote response in order to determine whether this * Validates a remote response in order to determine whether this
* response is a valid profile or not * response is a valid profile or not
* *
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param array $res remote response * @param array $res remote response
* @return bool success state * @return bool success state
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/ */
public static function validate_remote_response($res) public static function validate_remote_response($res)
{ {
@ -380,9 +376,9 @@ class Activitypub_explorer
* potential ActivityPub remote profiles, as so it is important to use * potential ActivityPub remote profiles, as so it is important to use
* this hacky workaround (at least for now) * this hacky workaround (at least for now)
* *
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @param string $v URL * @param string $v URL
* @return bool|Activitypub_profile false if fails | Aprofile object if successful * @return bool|Activitypub_profile false if fails | Aprofile object if successful
* @author Diogo Cordeiro <diogo@fc.up.pt>
*/ */
public static function get_aprofile_by_url($v) public static function get_aprofile_by_url($v)
{ {
@ -412,10 +408,7 @@ class Activitypub_explorer
public static function get_actor_inboxes_uri($url) public static function get_actor_inboxes_uri($url)
{ {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
$headers[] = 'User-Agent: GNUSocialBot ' . GNUSOCIAL_VERSION . ' - https://gnu.io/social';
$response = $client->get($url, $headers);
if (!$response->isOk()) { if (!$response->isOk()) {
throw new Exception('Invalid Actor URL.'); throw new Exception('Invalid Actor URL.');
} }
@ -443,10 +436,7 @@ class Activitypub_explorer
private function travel_collection($url) private function travel_collection($url)
{ {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$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);
$res = json_decode($response->getBody(), true); $res = json_decode($response->getBody(), true);
if (!isset($res['orderedItems'])) { if (!isset($res['orderedItems'])) {
@ -479,10 +469,7 @@ class Activitypub_explorer
public static function get_remote_user_activity($url) public static function get_remote_user_activity($url)
{ {
$client = new HTTPClient(); $client = new HTTPClient();
$headers = []; $response = $client->get($url, self::$headers);
$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);
$res = json_decode($response->getBody(), true); $res = json_decode($response->getBody(), true);
if (Activitypub_explorer::validate_remote_response($res)) { if (Activitypub_explorer::validate_remote_response($res)) {
common_debug('ActivityPub Explorer: Found a valid remote actor for ' . $url); common_debug('ActivityPub Explorer: Found a valid remote actor for ' . $url);