[TheFreeNetwork] Do not allow lower priority protocols to handle remote actors already handled by the higher ones

This commit is contained in:
Diogo Cordeiro
2020-08-26 23:55:36 +01:00
committed by Diogo Peralta Cordeiro
parent a0a37352c8
commit c834d27dd6
7 changed files with 66 additions and 12 deletions

View File

@@ -1,7 +1,7 @@
StartTFNLookup: tries to locate a duplicated remote profile by URI; federation plugins must trigger this event before profile insertion
@param string $uri URI of the remote profile to be inserted
@param string $class profile class of the federation protocol that triggered the event
@param int|null &$profile_id profile ID associated with the duplicated remote profile found
@param int|null &$profile_id profile ID associated with the duplicated remote profile found, throws AlreadyHandledException to warn that the duplicate has priority
EndTFNLookup: deletes a previous duplicated remote profile found; federation plugins must trigget this event after successfully inserting a new profile
@param string $class profile class of the federation protocol that triggered the event

View File

@@ -4,7 +4,7 @@ Making this possible essentially consists in not allowing duplication of remote
and to ensure that each profile is handled by one and only one federation protocol at a time.
Each newly added federation protocol **must** support all the already supported functionalities by the other federation
protocols, otherwise this module will be moving between FullFeaturedFederationProtocolProfile and LackyFederationProtocolProfile all the time.
protocols, otherwise if the LackyFederationProtocolProfile is preferred, the remote actors using it will be limited.
You **must** feed this Module with the list of preferences for each federation protocol, more on that in the following section.

View File

@@ -42,6 +42,7 @@ class TheFreeNetworkModule extends Module
const MODULE_VERSION = '0.1.0alpha0';
public $protocols = null; // protocols TFN should handle
public $priority = []; // protocols preferences
private $lrdd = false; // whether LRDD plugin is active or not
@@ -56,9 +57,12 @@ class TheFreeNetworkModule extends Module
// require needed classes
$plugin_dir = dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'plugins';
$p = 0;
foreach ($this->protocols as $protocol => $class) {
$this->priority[$class] = $p++;
require_once $plugin_dir . DIRECTORY_SEPARATOR . $protocol . DIRECTORY_SEPARATOR . 'classes' . DIRECTORY_SEPARATOR . $class . '.php';
}
unset($p);
// $lrdd flag
$this->lrdd = PluginList::isPluginActive("LRDD");
@@ -74,25 +78,31 @@ class TheFreeNetworkModule extends Module
* @param string $class profile class that triggered this event
* @param int|null &$profile_id Profile:id associated with the remote entity found
* @return bool hook flag
* @throws AlreadyHandledException Do not allow to create a profile if a preferred protocol already has one
*/
public function onStartTFNLookup(string $uri, string $class, int &$profile_id = null): bool
{
$profile_id = $this->lookup($uri, $class);
[$profile_id, $cls] = $this->lookup($uri, $class);
if (is_null($profile_id)) {
$perf = common_config('performance', 'high');
if (!$perf && $this->lrdd) {
// Force lookup with online resources
$profile_id = $this->lookup($uri, $class, true);
[$profile_id, $cls] = $this->lookup($uri, $class, true);
}
}
// Lower means higher priority
if ($this->priority[$cls] < $this->priority[$class]) {
throw new AlreadyHandledException("TheFreeNetworkModule->AlreadyHandled: $cls is preferred over $class");
}
return false;
}
/**
* A new remote profile was sucessfully added, delete
* A new remote profile was successfully added, delete
* other remotes associated with the same Profile entity.
*
* @param string $class profile class that triggered this event
@@ -146,9 +156,9 @@ class TheFreeNetworkModule extends Module
* @param string $uri
* @param string $class
* @param bool $online
* @return int|null Profile:id associated with the remote entity found
* @return null|array [Profile:id, class] associated with the remote entity found
*/
private function lookup(string $uri, string $class, bool $online = false): ?int
private function lookup(string $uri, string $class, bool $online = false): ?array
{
if ($online) {
$this->log(LOG_INFO, 'Searching with online resources for a remote profile with URI: ' . $uri);
@@ -169,7 +179,7 @@ class TheFreeNetworkModule extends Module
$profile = $cls::getKV('uri', $alias);
if ($profile instanceof $cls) {
$this->log(LOG_INFO, 'Found a remote ' . $cls . ' associated with Profile:' . $profile->getID());
return $profile->getID();
return [$profile->getID(), $cls];
}
}
}