From d9a9876ddd846f583e75c46cd84be9330895770e Mon Sep 17 00:00:00 2001
From: Michael <heluecht@pirati.ca>
Date: Fri, 31 Jul 2020 09:08:51 +0000
Subject: [PATCH] Synchronize contacts with the directory server

---
 src/Module/Admin/Site.php                |  3 +
 src/Worker/AddContact.php                |  7 +++
 src/Worker/Cron.php                      |  5 ++
 src/Worker/PullDirectory.php             | 76 ++++++++++++++++++++++++
 view/templates/admin/site.tpl            |  1 +
 view/theme/frio/templates/admin/site.tpl |  1 +
 6 files changed, 93 insertions(+)
 create mode 100644 src/Worker/PullDirectory.php

diff --git a/src/Module/Admin/Site.php b/src/Module/Admin/Site.php
index c1d4479d9a..3bdcd71147 100644
--- a/src/Module/Admin/Site.php
+++ b/src/Module/Admin/Site.php
@@ -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.')],
diff --git a/src/Worker/AddContact.php b/src/Worker/AddContact.php
index 6bb8a41b59..fbec7abdab 100644
--- a/src/Worker/AddContact.php
+++ b/src/Worker/AddContact.php
@@ -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;
diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php
index 6749b9648d..5bdd22f330 100644
--- a/src/Worker/Cron.php
+++ b/src/Worker/Cron.php
@@ -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
index 0000000000..af11a3fd06
--- /dev/null
+++ b/src/Worker/PullDirectory.php
@@ -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]);
+	}
+}
diff --git a/view/templates/admin/site.tpl b/view/templates/admin/site.tpl
index 49bae09ef7..57e574725d 100644
--- a/view/templates/admin/site.tpl
+++ b/view/templates/admin/site.tpl
@@ -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}}
diff --git a/view/theme/frio/templates/admin/site.tpl b/view/theme/frio/templates/admin/site.tpl
index 5f5df4aac3..61a9d1e500 100644
--- a/view/theme/frio/templates/admin/site.tpl
+++ b/view/theme/frio/templates/admin/site.tpl
@@ -219,6 +219,7 @@
 				<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}}