GContact discovery added
authorMichael <heluecht@pirati.ca>
Thu, 5 Mar 2020 22:03:24 +0000 (22:03 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 5 Mar 2020 22:03:24 +0000 (22:03 +0000)
src/Model/GContact.php
src/Module/Admin/Site.php
src/Protocol/ActivityPub.php
src/Protocol/ActivityPub/Transmitter.php
src/Worker/UpdateGContact.php
view/templates/admin/site.tpl
view/theme/frio/templates/admin/site.tpl

index 95afc1b..bd2d140 100644 (file)
@@ -28,6 +28,7 @@ use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Core\System;
 use Friendica\Core\Search;
+use Friendica\Core\Worker;
 use Friendica\Database\DBA;
 use Friendica\DI;
 use Friendica\Network\Probe;
@@ -1275,6 +1276,75 @@ class GContact
                }
        }
 
+       /**
+        * Fetches the followers of a given profile and adds them
+        *
+        * @param string $url URL of a profile
+        * @return void
+        */
+       public static function discoverFollowers(string $url)
+       {
+               $apcontact = APContact::getByURL($url);
+
+               if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) {
+                       $followers = ActivityPub::fetchItems($apcontact['followers']);
+                       Logger::info('Discover AP followers', ['url' => $url, 'contacts' => count($followers)]);
+                       foreach ($followers as $follower) {
+                               if (DBA::exists('gcontact', ['nurl' => Strings::normaliseLink(($follower))])) {
+                                       continue;
+                               }
+                               Logger::info('Discover new AP contact', ['url' => $follower]);
+                               Worker::add(PRIORITY_LOW, 'UpdateGContact', $follower);
+                       }
+                       Logger::info('AP followers discovery finished', ['url' => $url]);
+               }
+
+               if (!empty($apcontact['following']) && is_string($apcontact['following'])) {
+                       $followings = ActivityPub::fetchItems($apcontact['following']);
+                       Logger::info('Discover AP followings', ['url' => $url, 'contacts' => count($followings)]);
+                       foreach ($followings as $following) {
+                               if (DBA::exists('gcontact', ['nurl' => Strings::normaliseLink(($following))])) {
+                                       continue;
+                               }
+                               Logger::info('Discover new AP contact', ['url' => $following]);
+                               Worker::add(PRIORITY_LOW, 'UpdateGContact', $following);
+                       }
+                       Logger::info('AP followings discovery finished', ['url' => $url]);
+               }
+
+
+               $data = Probe::uri($url);
+               if (empty($data['poco'])) {
+                       return;
+               }
+
+               $curlResult = Network::curl($data['poco']);
+               if (!$curlResult->isSuccess()) {
+                       return;
+               }
+               $poco = json_decode($curlResult->getBody(), true);
+               if (empty($poco['entry'])) {
+                       return;
+               }
+
+               Logger::info('PoCo Discovery started', ['url' => $url, 'contacts' => count($poco['entry'])]);
+
+               foreach ($poco['entry'] as $entries) {
+                       if (!empty($entries['urls'])) {
+                               foreach ($entries['urls'] as $url) {
+                                       if ($url['type'] == 'profile') {
+                                               if (DBA::exists('gcontact', ['nurl' => Strings::normaliseLink(($url['value']))])) {
+                                                       continue;
+                                               }
+                                               Logger::info('Discover new PoCo contact', ['url' => $$url['value']]);
+                                               Worker::add(PRIORITY_LOW, 'UpdateGContact', $$url['value']);
+                                       }
+                               }
+                       }
+               }
+               Logger::info('PoCo Discovery finished', ['url' => $url]);
+       }
+
        /**
         * Returns a random, global contact of the current node
         *
index ec6f01a..078f169 100644 (file)
@@ -178,6 +178,7 @@ class Site extends BaseAdmin
                $optimize_max_tablesize = (!empty($_POST['optimize_max_tablesize']) ? intval(trim($_POST['optimize_max_tablesize'])) : 100);
                $optimize_fragmentation = (!empty($_POST['optimize_fragmentation']) ? intval(trim($_POST['optimize_fragmentation'])) : 30);
                $poco_completion        = (!empty($_POST['poco_completion'])        ? intval(trim($_POST['poco_completion']))        : false);
+               $gcontact_discovery     = (!empty($_POST['gcontact_discovery'])     ? intval(trim($_POST['gcontact_discovery']))     : false);
                $poco_requery_days      = (!empty($_POST['poco_requery_days'])      ? intval(trim($_POST['poco_requery_days']))      : 7);
                $poco_discovery         = (!empty($_POST['poco_discovery'])         ? intval(trim($_POST['poco_discovery']))         : PortableContact::DISABLED);
                $poco_discovery_since   = (!empty($_POST['poco_discovery_since'])   ? intval(trim($_POST['poco_discovery_since']))   : 30);
@@ -305,6 +306,7 @@ class Site extends BaseAdmin
                DI::config()->set('system', 'optimize_max_tablesize', $optimize_max_tablesize);
                DI::config()->set('system', 'optimize_fragmentation', $optimize_fragmentation);
                DI::config()->set('system', 'poco_completion'       , $poco_completion);
+               DI::config()->set('system', 'gcontact_discovery'    , $gcontact_discovery);
                DI::config()->set('system', 'poco_requery_days'     , $poco_requery_days);
                DI::config()->set('system', 'poco_discovery'        , $poco_discovery);
                DI::config()->set('system', 'poco_discovery_since'  , $poco_discovery_since);
@@ -669,6 +671,7 @@ class Site extends BaseAdmin
                        '$optimize_fragmentation' => ['optimize_fragmentation', DI::l10n()->t('Minimum level of fragmentation'), DI::config()->get('system', 'optimize_fragmentation', 30), DI::l10n()->t('Minimum fragmenation level to start the automatic optimization - default value is 30%.')],
 
                        '$poco_completion'        => ['poco_completion', DI::l10n()->t('Periodical check of global contacts'), DI::config()->get('system', 'poco_completion'), DI::l10n()->t('If enabled, the global contacts are checked periodically for missing or outdated data and the vitality of the contacts and servers.')],
+                       '$gcontact_discovery'     => ['gcontact_discovery', DI::l10n()->t('Discover followers/followings from global contacts'), DI::config()->get('system', 'gcontact_discovery'), DI::l10n()->t('If enabled, the global contacts are checked for new contacts among their followers and following contacts.')],
                        '$poco_requery_days'      => ['poco_requery_days', DI::l10n()->t('Days between requery'), DI::config()->get('system', 'poco_requery_days'), DI::l10n()->t('Number of days after which a server is requeried for his contacts.')],
                        '$poco_discovery'         => ['poco_discovery', DI::l10n()->t('Discover contacts from other servers'), (string)intval(DI::config()->get('system', 'poco_discovery')), DI::l10n()->t('Periodically query other servers for contacts. You can choose between "Users": the users on the remote system, "Global Contacts": active contacts that are known on the system. The fallback is meant for Redmatrix servers and older friendica servers, where global contacts weren\'t available. The fallback increases the server load, so the recommended setting is "Users, Global Contacts".'), $poco_discovery_choices],
                        '$poco_discovery_since'   => ['poco_discovery_since', DI::l10n()->t('Timeframe for fetching global contacts'), (string)intval(DI::config()->get('system', 'poco_discovery_since')), DI::l10n()->t('When the discovery is activated, this value defines the timeframe for the activity of the global contacts that are fetched from other servers.'), $poco_discovery_since_choices],
index c22bbb3..894c7f6 100644 (file)
@@ -231,13 +231,13 @@ class ActivityPub
                        $items = $data['orderedItems'];
                } elseif (!empty($data['first']['orderedItems'])) {
                        $items = $data['first']['orderedItems'];
-               } elseif (!empty($data['first'])) {
+               } elseif (!empty($data['first']) && is_string($data['first'])) {
                        return self::fetchItems($data['first'], $uid);
                } else {
                        $items = [];
                }
 
-               if (!empty($data['next'])) {
+               if (!empty($data['next']) && is_string($data['next'])) {
                        $items = array_merge($items, self::fetchItems($data['next'], $uid));
                }
 
index ead0c84..f6c9311 100644 (file)
@@ -345,7 +345,7 @@ class Transmitter
                                        continue;
                                }
 
-                               if ($receiver == $profile['followers'] && !empty($item_profile['followers'])) {
+                               if (!empty($profile['followers']) && $receiver == $profile['followers'] && !empty($item_profile['followers'])) {
                                        $permissions[$element][] = $item_profile['followers'];
                                } elseif (!in_array($receiver, $exclude)) {
                                        $permissions[$element][] = $receiver;
index 7bdaec4..0d0d85f 100644 (file)
@@ -22,8 +22,8 @@
 namespace Friendica\Worker;
 
 use Friendica\Core\Logger;
+use Friendica\DI;
 use Friendica\Model\GContact;
-use Friendica\Database\DBA;
 
 class UpdateGContact
 {
@@ -39,5 +39,9 @@ class UpdateGContact
                $success = GContact::updateFromProbe($url, $force);
 
                Logger::info('Updated from probe', ['url' => $url, 'force' => $force, 'success' => $success]);
+
+               if ($success && DI::config()->get('system', 'gcontact_discovery')) {
+                       GContact::discoverFollowers($url);
+               }
        }
 }
index 9ccda33..9e601b0 100644 (file)
@@ -98,6 +98,7 @@
 
        <h3>{{$portable_contacts}}</h3>
        {{include file="field_checkbox.tpl" field=$poco_completion}}
+       {{include file="field_checkbox.tpl" field=$gcontact_discovery}}
        {{include file="field_input.tpl" field=$poco_requery_days}}
        {{include file="field_select.tpl" field=$poco_discovery}}
        {{include file="field_select.tpl" field=$poco_discovery_since}}
index e13b296..e4b05d4 100644 (file)
                                <div id="admin-settings-contacts-collapse" class="panel-collapse collapse" role="tabpanel" aria-labelledby="admin-settings-cocontactsrporate">
 
                                        {{include file="field_checkbox.tpl" field=$poco_completion}}
+                                       {{include file="field_checkbox.tpl" field=$gcontact_discovery}}
                                        {{include file="field_input.tpl" field=$poco_requery_days}}
                                        {{include file="field_select.tpl" field=$poco_discovery}}
                                        {{include file="field_select.tpl" field=$poco_discovery_since}}