From bca1dc1ca45375eb68e9d276a398d595eca5e68f Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 24 Jun 2019 03:25:01 +0000 Subject: [PATCH] Moving functionality to the contact model --- src/Model/Contact.php | 66 +++++++++++++++++++++++++++++------- src/Worker/Cron.php | 2 +- src/Worker/OnePoll.php | 2 +- src/Worker/UpdateContact.php | 14 -------- 4 files changed, 56 insertions(+), 28 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 7a3731e537..70a152d431 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -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"]); diff --git a/src/Worker/Cron.php b/src/Worker/Cron.php index 8d290698d6..d20bfd133e 100644 --- a/src/Worker/Cron.php +++ b/src/Worker/Cron.php @@ -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]; diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index a605ee92ed..f5da683b13 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -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']]); } } diff --git a/src/Worker/UpdateContact.php b/src/Worker/UpdateContact.php index 7679431fd9..f23c5c0a07 100644 --- a/src/Worker/UpdateContact.php +++ b/src/Worker/UpdateContact.php @@ -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]); - } } } -- 2.39.5