Moving functionality to the contact model
authorMichael <heluecht@pirati.ca>
Mon, 24 Jun 2019 03:25:01 +0000 (03:25 +0000)
committerMichael <heluecht@pirati.ca>
Mon, 24 Jun 2019 03:25:01 +0000 (03:25 +0000)
src/Model/Contact.php
src/Worker/Cron.php
src/Worker/OnePoll.php
src/Worker/UpdateContact.php

index 7a3731e..70a152d 100644 (file)
@@ -1755,6 +1755,42 @@ class Contact extends BaseObject
                return $data;
        }
 
+        /**
+        * @brief Helper function for "updateFromProbe". Updates personal and public contact
+        *
+        * @param array $contact The personal contact entry
+        * @param array $fields  The fields that are updated
+        * @throws \Exception
+        */
+       private static function updateContact($id, $uid, $url, array $fields)
+       {
+               DBA::update('contact', $fields, ['id' => $id]);
+
+               if ($uid != 0) {
+                       return;
+               }
+
+               $condition = ['self' => false, 'nurl' => Strings::normaliseLink($url),
+                       'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]];
+
+               // These contacts are sharing with us, we don't poll them.
+               // This means that we don't set the update fields in "OnePoll.php".
+               $condition['rel'] = self::SHARING;
+               DBA::update('contact', $fields, $condition);
+
+               unset($fields['last-update']);
+               unset($fields['success_update']);
+               unset($fields['failure_update']);
+
+               if (empty($fields)) {
+                       return;
+               }
+
+               // We are polling these contacts, so we mustn't set the update fields here.
+               $condition['rel'] = [self::FOLLOWER, self::FRIEND];
+               DBA::update('contact', $fields, $condition);
+       }
+
        /**
         * @param integer $id      contact id
         * @param string  $network Optional network we are probing for
@@ -1767,7 +1803,7 @@ class Contact extends BaseObject
        {
                /*
                  Warning: Never ever fetch the public key via Probe::uri and write it into the contacts.
-                 This will reliably kill your communication with Friendica contacts.
+                 This will reliably kill your communication with old Friendica contacts.
                 */
 
                $fields = ['avatar', 'uid', 'name', 'nick', 'url', 'addr', 'batch', 'notify',
@@ -1785,12 +1821,14 @@ class Contact extends BaseObject
 
                $ret = Probe::uri($contact['url'], $network, $uid, !$force);
 
-               // If Probe::uri fails the network code will be different (mostly "feed" or "unkn")
-               if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) {
-                       return false;
-               }
+               $updated = DateTimeFormat::utcNow();
 
-               if (!in_array($ret['network'], Protocol::NATIVE_SUPPORT)) {
+               // If Probe::uri fails the network code will be different (mostly "feed" or "unkn")
+               if (!in_array($ret['network'], Protocol::NATIVE_SUPPORT) ||
+                       (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network']))) {
+                       if ($force && ($uid == 0)) {
+                               self::updateContact($id, $uid, $ret['url'], ['last-update' => $updated, 'failure_update' => $updated]);
+                       }
                        return false;
                }
 
@@ -1810,19 +1848,23 @@ class Contact extends BaseObject
                self::updateAvatar($ret['photo'], $uid, $id, $update || $force);
 
                if (!$update) {
+                       if ($force && ($uid == 0)) {
+                               self::updateContact($id, $uid, $ret['url'], ['last-update' => $updated, 'success_update' => $updated]);
+                       }
                        return true;
                }
 
                $ret['nurl'] = Strings::normaliseLink($ret['url']);
-               $ret['updated'] = DateTimeFormat::utcNow();
+               $ret['updated'] = $updated;
+
+               if ($force && ($uid == 0)) {
+                       $ret['last-update'] = $updated;
+                       $ret['success_update'] = $updated;
+               }
 
                unset($ret['photo']);
-               DBA::update('contact', $ret, ['id' => $id]);
 
-               // Updating all similar contacts when we are updating the public contact
-               if (($uid == 0) && in_array($ret['network'], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS])) {
-                       DBA::update('contact', $ret, ['self' => false, 'nurl' => $ret['nurl']]);
-               }
+               self::updateContact($id, $uid, $ret['url'], $ret);
 
                // Update the corresponding gcontact entry
                PortableContact::lastUpdated($ret["url"]);
index 8d29069..d20bfd1 100644 (file)
@@ -133,7 +133,7 @@ class Cron
         */
        private static function updatePublicContacts() {
                $count = 0;
-               $last_updated = DateTimeFormat::utc('now - 1 months');
+               $last_updated = DateTimeFormat::utc('now - 1 week');
                $condition = ["`network` IN (?, ?, ?, ?) AND `uid` = ? AND NOT `self` AND `last-update` < ?",
                        Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, 0, $last_updated];
 
index a605ee9..f5da683 100644 (file)
@@ -709,6 +709,6 @@ class OnePoll
        private static function updateContact(array $contact, array $fields)
        {
                DBA::update('contact', $fields, ['id' => $contact['id']]);
-               DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $contact['nurl']]);
+//             DBA::update('contact', $fields, ['uid' => 0, 'nurl' => $contact['nurl']]);
        }
 }
index 7679431..f23c5c0 100644 (file)
@@ -20,19 +20,5 @@ class UpdateContact
                $success = Contact::updateFromProbe($contact_id, '', $force);
 
                Logger::info('Updated from probe', ['id' => $contact_id, 'force' => $force, 'success' => $success]);
-
-               // Update the update date fields only when we are forcing the update
-               if (!$force) {
-                       return;
-               }
-
-               // Update the "last-update", "success_update" and "failure_update" field only when it is a public contact.
-               // These fields are set in OnePoll for all non public contacts.
-               $updated = DateTimeFormat::utcNow();
-               if ($success) {
-                       DBA::update('contact', ['last-update' => $updated, 'success_update' => $updated], ['id' => $contact_id, 'uid' => 0]);
-               } else {
-                       DBA::update('contact', ['last-update' => $updated, 'failure_update' => $updated], ['id' => $contact_id, 'uid' => 0]);
-               }
        }
 }