]> git.mxchange.org Git - friendica.git/commitdiff
Synchronize contacts with the directory server
authorMichael <heluecht@pirati.ca>
Fri, 31 Jul 2020 09:08:51 +0000 (09:08 +0000)
committerMichael <heluecht@pirati.ca>
Fri, 31 Jul 2020 09:08:51 +0000 (09:08 +0000)
src/Module/Admin/Site.php
src/Worker/AddContact.php
src/Worker/Cron.php
src/Worker/PullDirectory.php [new file with mode: 0644]
view/templates/admin/site.tpl
view/theme/frio/templates/admin/site.tpl

index c1d4479d9ae09703f53a4575743b0b04c710626e..3bdcd71147c34dd4f986243bbc90afed4e92b48c 100644 (file)
@@ -179,6 +179,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);
                $contact_discovery      = (!empty($_POST['contact_discovery'])      ? intval(trim($_POST['contact_discovery']))      : ContactRelation::DISCOVERY_NONE);
+               $synchronize_directory  = (!empty($_POST['synchronize_directory'])  ? intval(trim($_POST['synchronize_directory']))  : false);
                $poco_completion        = (!empty($_POST['poco_completion'])        ? intval(trim($_POST['poco_completion']))        : 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);
@@ -309,6 +310,7 @@ class Site extends BaseAdmin
                DI::config()->set('system', 'optimize_fragmentation', $optimize_fragmentation);
                DI::config()->set('system', 'poco_completion'       , $poco_completion);
                DI::config()->set('system', 'contact_discovery'     , $contact_discovery);
+               DI::config()->set('system', 'synchronize_directory' , $synchronize_directory);
                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);
@@ -684,6 +686,7 @@ class Site extends BaseAdmin
                                '<li>' . DI::l10n()->t('Local contacts - contacts of our local contacts are discovered for their followers/followings.') . '</li>' .
                                '<li>' . DI::l10n()->t('Interactors - contacts of our local contacts and contacts who interacted on locally visible postings are discovered for their followers/followings.') . '</li></ul>',
                                $discovery_choices],
+                       '$synchronize_directory'  => ['synchronize_directory', DI::l10n()->t('Synchronize the contacts with the directory server'), DI::config()->get('system', 'synchronize_directory'), DI::l10n()->t('if enabled, the system will check periodically for new contacts on the defined directory server.')],
 
                        '$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.')],
                        '$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.')],
index 6bb8a41b59ce3fa50928785249b09e1d53df3253..fbec7abdab9a9b8473e965c96df0969652029a3d 100644 (file)
@@ -34,6 +34,13 @@ class AddContact
         */
        public static function execute(int $uid, string $url)
        {
+               if ($uid == 0) {
+                       // Adding public contact
+                       $result = Contact::getIdForURL($url);
+                       Logger::info('Added public contact', ['url' => $url, 'result' => $result]);
+                       return;
+               }
+
                $user = User::getById($uid);
                if (empty($user)) {
                        return;
index 6749b9648d667b6e5e57f68ac2ea6e039f1f3fc3..5bdd22f330b0d707e21853066155ff96915d80fa 100644 (file)
@@ -105,6 +105,11 @@ class Cron
                // Hourly cron calls
                if (DI::config()->get('system', 'last_cron_hourly', 0) + 3600 < time()) {
 
+                       // Search for new contacts in the directory
+                       if (DI::config()->get('system', 'synchronize_directory')) {
+                               Worker::add(PRIORITY_LOW, 'PullDirectory');
+                       }
+
                        // Delete all done workerqueue entries
                        DBA::delete('workerqueue', ['`done` AND `executed` < UTC_TIMESTAMP() - INTERVAL 1 HOUR']);
 
diff --git a/src/Worker/PullDirectory.php b/src/Worker/PullDirectory.php
new file mode 100644 (file)
index 0000000..af11a3f
--- /dev/null
@@ -0,0 +1,76 @@
+<?php
+/**
+ * @copyright Copyright (C) 2020, Friendica
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * 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 the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
+
+namespace Friendica\Worker;
+
+use Friendica\Core\Logger;
+use Friendica\Core\Worker;
+use Friendica\DI;
+use Friendica\Model\Contact;
+
+class PullDirectory
+{
+       /**
+        * Pull contacts from a directory server
+        */
+       public static function execute()
+       {
+               if (!DI::config()->get('system', 'synchronize_directory')) {
+                       Logger::info('Synchronization deactivated');
+                       return;
+               }
+
+               $directory = DI::config()->get('system', 'directory');
+               if (empty($directory)) {
+                       Logger::info('No directory configured');
+                       return;
+               }
+
+               $now = (int)DI::config()->get('system', 'last-directory-sync', 0);
+
+               Logger::info('Synchronization started.', ['now' => $now, 'directory' => $directory]);
+
+               $result = DI::httpRequest()->fetch($directory . '/sync/pull/since/' . $now);
+               if (empty($result)) {
+                       Logger::info('Directory server return empty result.', ['directory' => $directory]);
+                       return;
+               }
+
+               $contacts = json_decode($result, true);
+               if (empty($contacts['results'])) {
+                       Logger::info('No results fetched.', ['directory' => $directory]);
+                       return;
+               }
+
+               $now = $contacts['now'] ?? 0;
+               $count = $contacts['count'] ?? 0;
+               $added = 0;
+               foreach ($contacts['results'] as $url) {
+                       if (empty(Contact::getByURL($url, false, ['id']))) {
+                               Worker::add(PRIORITY_LOW, 'AddContact', 0, $url);
+                               ++$added;
+                       }
+               }
+               DI::config()->set('system', 'last-directory-sync', $now);
+
+               Logger::info('Synchronization ended.', ['now' => $now, 'count' => $count, 'added' => $added, 'directory' => $directory]);
+       }
+}
index 49bae09ef772e6bddbc388e86d5d0833c0efc14a..57e574725d94593c43f92435903136e5ce2a7677 100644 (file)
@@ -98,6 +98,7 @@
 
                <h2>{{$portable_contacts}}</h2>
                {{include file="field_select.tpl" field=$contact_discovery}}
+               {{include file="field_checkbox.tpl" field=$synchronize_directory}}
                {{include file="field_checkbox.tpl" field=$poco_completion}}
                {{include file="field_input.tpl" field=$poco_requery_days}}
                {{include file="field_select.tpl" field=$poco_discovery}}
index 5f5df4aac397872d7aab663ffc4786eb1910bc15..61a9d1e500c2342eb0f3a8c00b26ccb2dea83456 100644 (file)
                                <div id="admin-settings-contacts-collapse" class="panel-collapse collapse" role="tabpanel" aria-labelledby="admin-settings-cocontactsrporate">
                                        <div class="panel-body">
                                                {{include file="field_select.tpl" field=$contact_discovery}}
+                                               {{include file="field_checkbox.tpl" field=$synchronize_directory}}
                                                {{include file="field_checkbox.tpl" field=$poco_completion}}
                                                {{include file="field_input.tpl" field=$poco_requery_days}}
                                                {{include file="field_select.tpl" field=$poco_discovery}}