]> git.mxchange.org Git - friendica.git/commitdiff
Query the same contact only once a month
authorMichael <heluecht@pirati.ca>
Sat, 7 Mar 2020 05:31:03 +0000 (05:31 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 7 Mar 2020 05:31:03 +0000 (05:31 +0000)
database.sql
src/Model/GContact.php
src/Module/Admin/Site.php
src/Worker/UpdateGContact.php
static/dbstructure.config.php

index a5e94f3f542280714feefacf35c5a52e6730ae8d..b4c65fb89acdfda495ef88e6cedfb74bf4d5d10b 100644 (file)
@@ -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';
index ae1bd665e33c1255737b287257729f25cbc0d7d6..ee30cf3c590028eca55cf1ba27ed5b2f69af0ff2 100644 (file)
@@ -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]);
        }
 
index 078f16929978cc93cd098368b48c9114b40ee32c..846280c4f12915ca6e457d01dfd7846df7da75d1 100644 (file)
@@ -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],
index 0d0d85fc01d57f172fb0cc0dc8c5f205f4a148b8..fda1a650b4e7824ef7c6c8e20963f918615dcb5f 100644 (file)
@@ -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);
                }
        }
 }
index 3c9bc50fb9d8f2578e3f00946da170e6ed90e82e..8cd01b4aeb3a71e056ea5bb2a681d6298c8d9ad4 100755 (executable)
@@ -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" => ""],