Merge pull request #8953 from annando/sync-directory

Synchronize contacts with the directory server
This commit is contained in:
Hypolite Petovan 2020-07-31 05:56:25 -04:00 committed by GitHub
commit 152842633f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 0 deletions

View 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.')],

View 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;

View 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']);

View File

@ -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]);
}
}

View 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}}

View File

@ -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}}