From d6905e29cff4bf8968fb88d8dae4c673f3095517 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 7 Mar 2020 05:31:03 +0000 Subject: [PATCH] Query the same contact only once a month --- database.sql | 2 ++ src/Model/GContact.php | 44 +++++++++++++++++++++++++++++++---- src/Module/Admin/Site.php | 2 +- src/Worker/UpdateGContact.php | 10 ++++---- static/dbstructure.config.php | 1 + 5 files changed, 49 insertions(+), 10 deletions(-) diff --git a/database.sql b/database.sql index a5e94f3f54..b4c65fb89a 100644 --- a/database.sql +++ b/database.sql @@ -393,6 +393,7 @@ CREATE TABLE IF NOT EXISTS `gcontact` ( `updated` datetime DEFAULT '0001-01-01 00:00:00' COMMENT '', `last_contact` datetime DEFAULT '0001-01-01 00:00:00' COMMENT '', `last_failure` datetime DEFAULT '0001-01-01 00:00:00' COMMENT '', + `last_discovery` datetime DEFAULT '0001-01-01 00:00:00' COMMENT 'Date of the last contact discovery', `archive_date` datetime DEFAULT '0001-01-01 00:00:00' COMMENT '', `archived` boolean NOT NULL DEFAULT '0' COMMENT '', `location` varchar(255) NOT NULL DEFAULT '' COMMENT '', @@ -425,6 +426,7 @@ CREATE TABLE IF NOT EXISTS `gcontact` ( CREATE TABLE IF NOT EXISTS `gfollower` ( `gcid` int unsigned NOT NULL DEFAULT 0 COMMENT 'global contact', `follower-gcid` int unsigned NOT NULL DEFAULT 0 COMMENT 'global contact of the follower', + `deleted` boolean NOT NULL DEFAULT '0' COMMENT '1 indicates that the connection has been deleted', PRIMARY KEY(`gcid`,`follower-gcid`), INDEX `follower-gcid` (`follower-gcid`) ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='Followers of global contacts'; diff --git a/src/Model/GContact.php b/src/Model/GContact.php index ae1bd665e3..ee30cf3c59 100644 --- a/src/Model/GContact.php +++ b/src/Model/GContact.php @@ -1282,8 +1282,30 @@ class GContact * @param string $url URL of a profile * @return void */ - public static function discoverFollowers(string $url) + public static function discoverFollowers(string $url, int $following_gcid = 0, int $follower_gcid = 0) { + $gcontact = DBA::selectFirst('gcontact', ['id', 'last_discovery'], ['nurl' => Strings::normaliseLink(($url))]); + if (!DBA::isResult($gcontact)) { + return; + } + + if ($gcontact['last_discovery'] > DateTimeFormat::utc('now - 1 month')) { + Logger::info('Last discovery was less then a month before.', ['url' => $url, 'discovery' => $gcontact['last_discovery']]); + return; + } + + $gcid = $gcontact['id']; + + if (!empty($following_gcid)) { + $fields = ['gcid' => $following_gcid, 'follower-gcid' => $gcid]; + Logger::info('Set relation for followed gcontact', $fields); + DBA::update('gfollower', ['deleted' => false], $fields, true); + } elseif (!empty($follower_gcid)) { + $fields = ['gcid' => $gcid, 'follower-gcid' => $follower_gcid]; + Logger::info('Set relation for following gcontact', $fields); + DBA::update('gfollower', ['deleted' => false], $fields, true); + } + $apcontact = APContact::getByURL($url); if (!empty($apcontact['followers']) && is_string($apcontact['followers'])) { @@ -1299,8 +1321,6 @@ class GContact } if (!empty($followers) || !empty($followings)) { - $gcontact = DBA::selectFirst('gcontact', ['id'], ['nurl' => Strings::normaliseLink(($url))]); - $gcid = $gcontact['id']; if (!empty($followers)) { // Clear the follower list, since it will be recreated in the next step DBA::update('gfollower', ['deleted' => true], ['gcid' => $gcid]); @@ -1320,14 +1340,26 @@ class GContact DBA::update('gfollower', ['deleted' => false], $fields, true); continue; } + + $follower_gcid = 0; + $following_gcid = 0; + + if (in_array($contact, $followers)) { + $following_gcid = $gcid; + } elseif (in_array($contact, $followings)) { + $follower_gcid = $gcid; + } + Logger::info('Discover new AP contact', ['url' => $contact]); - Worker::add(PRIORITY_LOW, 'UpdateGContact', $contact); + Worker::add(PRIORITY_LOW, 'UpdateGContact', $contact, '', $following_gcid, $follower_gcid); } if (!empty($followers)) { // Delete all followers that aren't undeleted DBA::delete('gfollower', ['gcid' => $gcid, 'deleted' => true]); } - Logger::info('AP contacts discovery finished', ['url' => $url]); + + DBA::update('gcontact', ['last_discovery' => DateTimeFormat::utcNow()], ['id' => $gcid]); + Logger::info('AP contacts discovery finished, last discovery set', ['url' => $url]); return; } @@ -1360,6 +1392,8 @@ class GContact } } } + + DBA::update('gcontact', ['last_discovery' => DateTimeFormat::utcNow()], ['id' => $gcid]); Logger::info('PoCo Discovery finished', ['url' => $url]); } diff --git a/src/Module/Admin/Site.php b/src/Module/Admin/Site.php index 078f169299..846280c4f1 100644 --- a/src/Module/Admin/Site.php +++ b/src/Module/Admin/Site.php @@ -671,7 +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.')], + '$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. This option will create huge masses of jobs, so it should only be activated on powerful machines.')], '$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], diff --git a/src/Worker/UpdateGContact.php b/src/Worker/UpdateGContact.php index 0d0d85fc01..fda1a650b4 100644 --- a/src/Worker/UpdateGContact.php +++ b/src/Worker/UpdateGContact.php @@ -29,10 +29,12 @@ class UpdateGContact { /** * Update global contact via probe - * @param string $url Global contact url - * @param string $command + * @param string $url Global contact url + * @param string $command + * @param integer $following_gcid gcontact ID of the contact that is followed by this one + * @param integer $follower_gcid gcontact ID of the contact that is following this one */ - public static function execute($url, $command = '') + public static function execute(string $url, string $command = '', int $following_gcid = 0, int $follower_gcid = 0) { $force = ($command == "force"); @@ -41,7 +43,7 @@ class UpdateGContact Logger::info('Updated from probe', ['url' => $url, 'force' => $force, 'success' => $success]); if ($success && DI::config()->get('system', 'gcontact_discovery')) { - GContact::discoverFollowers($url); + GContact::discoverFollowers($url, $following_gcid, $follower_gcid); } } } diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 3c9bc50fb9..8cd01b4aeb 100755 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -462,6 +462,7 @@ return [ "updated" => ["type" => "datetime", "default" => DBA::NULL_DATETIME, "comment" => ""], "last_contact" => ["type" => "datetime", "default" => DBA::NULL_DATETIME, "comment" => ""], "last_failure" => ["type" => "datetime", "default" => DBA::NULL_DATETIME, "comment" => ""], + "last_discovery" => ["type" => "datetime", "default" => DBA::NULL_DATETIME, "comment" => "Date of the last contact discovery"], "archive_date" => ["type" => "datetime", "default" => DBA::NULL_DATETIME, "comment" => ""], "archived" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "location" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], -- 2.39.5