Add and fix some code documentation

This commit is contained in:
Diogo Cordeiro 2018-07-10 00:17:18 +01:00
parent cc69c33113
commit cb70a8f6e8

View File

@ -5,9 +5,7 @@ use Profile;
/** /**
* GNU social - a federating social network * GNU social - a federating social network
* *
* An activity * ActivityPubPlugin implementation for GNU Social
*
* PHP version 5
* *
* LICENCE: This program is free software: you can redistribute it and/or modify * LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by * it under the terms of the GNU Affero General Public License as published by
@ -22,27 +20,54 @@ use Profile;
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
* *
* @category Feed * @category Plugin
* @package GNUsocial * @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com> * @author Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt> * @author Diogo Cordeiro <diogo@fc.up.pt>
* @copyright 2018 Free Software Foundaction, Inc. * @copyright 2018 Free Software Foundation http://fsf.org
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0 * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link https://gnu.io/social * @link https://www.gnu.org/software/social/
*/
if (!defined ('GNUSOCIAL')) {
exit(1);
}
/**
* @category Plugin
* @package GNUsocial
* @author Daniel Supernault <danielsupernault@gmail.com>
* @author Diogo Cordeiro <diogo@fc.up.pt>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://www.gnu.org/software/social/
*/ */
if (!defined('GNUSOCIAL')) { exit(1); } class Activitypub_Discovery
{
class Activitypub_Discovery {
private $discovered_actor_profiles = array (); private $discovered_actor_profiles = array ();
/**
* Get every profile from the given URL
* This function cleans the $this->discovered_actor_profiles array
* so that there is no erroneous data
*
* @param string $url User's url
* @return array of \Profile objects
*/
public function lookup ($url) public function lookup ($url)
{ {
$this->discovered_actor_profiles = array (); $this->discovered_actor_profiles = array ();
return $this->_lookup ($url);; return $this->_lookup ($url);
} }
/**
* Get every profile from the given URL
* This is a recursive function that will accumulate the results on
* $discovered_actor_profiles array
*
* @param string $url User's url
* @return array of \Profile objects
*/
private function _lookup ($url) private 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
@ -52,16 +77,25 @@ class Activitypub_Discovery {
return $this->discovered_actor_profiles; return $this->discovered_actor_profiles;
} }
/**
* Get a local user profiles from its URL and joins it on
* $this->discovered_actor_profiles
*
* @param string $url User's url
* @return boolean success state
*/
private function grab_local_user ($url) private function grab_local_user ($url)
{ {
if (($actor_profile = Profile::getKV ("profileurl", $url)) != false) { if (($actor_profile = Profile::getKV ("profileurl", $url)) != false) {
$this->discovered_actor_profiles[]= $actor_profile; $this->discovered_actor_profiles[]= $actor_profile;
return true; return true;
} else { // XXX: } else {
// Sometimes it is not true that the user is not locally available, /******************************** XXX: ********************************
// mostly when it is a local user and URLs slightly changed * Sometimes it is not true that the user is not locally available, *
// e.g.: GS instance owner changed from standard urls to pretty urls * mostly when it is a local user and URLs slightly changed *
// (not sure if this is necessary, but anyway) * e.g.: GS instance owner changed from standard urls to pretty urls *
* (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 ());
@ -76,8 +110,14 @@ class Activitypub_Discovery {
return false; return false;
} }
private function grab_remote_user ($url) /**
{ * Get a remote user(s) profile(s) from its URL and joins it on
* $this->discovered_actor_profiles
*
* @param string $url User's url
* @return boolean success state
*/
private function grab_remote_user ($url) {
$client = new HTTPClient (); $client = new HTTPClient ();
$headers = array(); $headers = array();
$headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"'; $headers[] = 'Accept: application/ld+json; profile="https://www.w3.org/ns/activitystreams"';
@ -106,6 +146,12 @@ class Activitypub_Discovery {
return false; return false;
} }
/**
* Save remote user profile in local instance
*
* @param array $res remote response
* @return \Profile remote Profile object
*/
private function store_profile ($res) { private function store_profile ($res) {
$profile = new Profile; $profile = new Profile;
$profile->profileurl = $res["url"]; $profile->profileurl = $res["url"];
@ -117,8 +163,14 @@ class Activitypub_Discovery {
return $profile; return $profile;
} }
private function validate_remote_response ($res) /**
{ * Validates a remote response in order to determine whether this
* response is a valid profile or not
*
* @param array $res remote response
* @return boolean success state
*/
private function validate_remote_response ($res) {
if (!isset ($res["url"], $res["nickname"], $res["display_name"], $res["summary"])) { if (!isset ($res["url"], $res["nickname"], $res["display_name"], $res["summary"])) {
return false; return false;
} }