From 7b0a9ffd8923a26f416ce67a285b692c8f84ad3f Mon Sep 17 00:00:00 2001 From: Michael Date: Wed, 3 Jul 2019 05:46:35 +0000 Subject: [PATCH] Update gcontact when public contact is updated --- src/Model/Contact.php | 29 ++++++------- src/Model/GContact.php | 30 +++++++++++++- src/Worker/OnePoll.php | 2 - src/Worker/UpdateGContact.php | 78 +++-------------------------------- 4 files changed, 46 insertions(+), 93 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 77726e6d42..c980482dee 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -792,6 +792,7 @@ class Contact extends BaseObject */ DBA::update('contact', ['archive' => 1], ['id' => $contact['id']]); DBA::update('contact', ['archive' => 1], ['nurl' => Strings::normaliseLink($contact['url']), 'self' => false]); + GContact::updateFromPublicContactURL($contact['url']); } } } @@ -829,6 +830,7 @@ class Contact extends BaseObject $fields = ['term-date' => DBA::NULL_DATETIME, 'archive' => false]; DBA::update('contact', $fields, ['id' => $contact['id']]); DBA::update('contact', $fields, ['nurl' => Strings::normaliseLink($contact['url'])]); + GContact::updateFromPublicContactURL($contact['url']); if (!empty($contact['batch'])) { $condition = ['batch' => $contact['batch'], 'contact-type' => self::TYPE_RELAY]; @@ -965,7 +967,7 @@ class Contact extends BaseObject if ((empty($profile["addr"]) || empty($profile["name"])) && (defaults($profile, "gid", 0) != 0) && in_array($profile["network"], Protocol::FEDERATED) ) { - Worker::add(PRIORITY_LOW, "UpdateGContact", $profile["gid"]); + Worker::add(PRIORITY_LOW, "UpdateGContact", $url); } // Show contact details of Diaspora contacts only if connected @@ -1339,10 +1341,14 @@ class Contact extends BaseObject return 0; } - // When we don't want to update, we look if we know this contact in any way if ($no_update && empty($default)) { + // When we don't want to update, we look if we know this contact in any way $data = self::getProbeDataFromDatabase($url, $contact_id); $background_update = true; + } elseif ($no_update && !empty($default)) { + // If there are default values, take these + $data = $default; + $background_update = false; } else { $data = []; $background_update = false; @@ -1357,18 +1363,9 @@ class Contact extends BaseObject } } - // Last try in gcontact for unsupported networks - if (!in_array($data["network"], [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::OSTATUS, Protocol::DIASPORA, Protocol::PUMPIO, Protocol::MAIL, Protocol::FEED])) { - if ($uid != 0) { - return 0; - } - - $contact = array_merge(self::getProbeDataFromDatabase($url, $contact_id), $default); - if (empty($contact)) { - return 0; - } - - $data = array_merge($data, $contact); + // Take the default values when probing failed + if (!empty($default) && !in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) { + $data = array_merge($data, $default); } if (empty($data)) { @@ -1518,7 +1515,7 @@ class Contact extends BaseObject if (!$background_update && ($uid == 0)) { // Update the gcontact entry - GContact::updateFromPublicContact($contact_id); + GContact::updateFromPublicContactID($contact_id); } return $contact_id; @@ -1776,7 +1773,7 @@ class Contact extends BaseObject } // Update the corresponding gcontact entry - GContact::updateFromPublicContact($id); + GContact::updateFromPublicContactID($id); // Archive or unarchive the contact. We only need to do this for the public contact. // The archive/unarchive function will update the personal contacts by themselves. diff --git a/src/Model/GContact.php b/src/Model/GContact.php index 60cea8b5a2..4fb766193f 100644 --- a/src/Model/GContact.php +++ b/src/Model/GContact.php @@ -864,12 +864,38 @@ class GContact * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function updateFromPublicContact($cid) + public static function updateFromPublicContactID($cid) + { + self::updateFromPublicContact(['id' => $cid]); + } + + /** + * @brief Updates the gcontact entry from a given public contact url + * + * @param string $url contact url + * @return void + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + public static function updateFromPublicContactURL($url) + { + self::updateFromPublicContact(['nurl' => Strings::normaliseLink($url)]); + } + + /** + * @brief Helper function for updateFromPublicContactID and updateFromPublicContactURL + * + * @param array $condition contact condition + * @return void + * @throws \Friendica\Network\HTTPException\InternalServerErrorException + * @throws \ImagickException + */ + private static function updateFromPublicContact($condition) { $fields = ['name', 'nick', 'url', 'nurl', 'location', 'about', 'keywords', 'gender', 'bd', 'contact-type', 'network', 'addr', 'notify', 'alias', 'archive', 'term-date', 'created', 'updated', 'avatar', 'success_update', 'failure_update', 'forum', 'prv']; - $contact = DBA::selectFirst('contact', $fields, ['id' => $cid, 'uid' => 0, 'network' => Protocol::FEDERATED]); + $contact = DBA::selectFirst('contact', $fields, array_merge($condition, ['uid' => 0, 'network' => Protocol::FEDERATED])); if (!DBA::isResult($contact)) { return; } diff --git a/src/Worker/OnePoll.php b/src/Worker/OnePoll.php index 33a9a51a18..9af4be9bed 100644 --- a/src/Worker/OnePoll.php +++ b/src/Worker/OnePoll.php @@ -191,11 +191,9 @@ class OnePoll } self::updateContact($contact, ['last-update' => $updated, 'success_update' => $updated]); - DBA::update('gcontact', ['last_contact' => $updated], ['nurl' => $contact['nurl']]); Contact::unmarkForArchival($contact); } elseif (in_array($contact["network"], [Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED])) { self::updateContact($contact, ['last-update' => $updated, 'failure_update' => $updated]); - DBA::update('gcontact', ['last_failure' => $updated], ['nurl' => $contact['nurl']]); Contact::markForArchival($contact); } else { self::updateContact($contact, ['last-update' => $updated]); diff --git a/src/Worker/UpdateGContact.php b/src/Worker/UpdateGContact.php index dd344baf91..aacebcb80a 100644 --- a/src/Worker/UpdateGContact.php +++ b/src/Worker/UpdateGContact.php @@ -7,85 +7,17 @@ namespace Friendica\Worker; use Friendica\Core\Logger; -use Friendica\Core\Protocol; +use Friendica\Model\GContact; use Friendica\Database\DBA; -use Friendica\Network\Probe; -use Friendica\Protocol\PortableContact; -use Friendica\Util\DateTimeFormat; -use Friendica\Util\Strings; class UpdateGContact { - public static function execute($contact_id) + public static function execute($url, $command = '') { - Logger::log('update_gcontact: start'); + $force = ($command == "force"); - if (empty($contact_id)) { - Logger::log('update_gcontact: no contact'); - return; - } + $success = GContact::updateFromProbe($url, $force); - $r = q("SELECT * FROM `gcontact` WHERE `id` = %d", intval($contact_id)); - - if (!DBA::isResult($r)) { - return; - } - - if (!in_array($r[0]["network"], Protocol::FEDERATED)) { - return; - } - - $data = Probe::uri($r[0]["url"]); - - if (!in_array($data["network"], Protocol::FEDERATED)) { - if ($r[0]["server_url"] != "") { - PortableContact::checkServer($r[0]["server_url"], $r[0]["network"]); - } - - q("UPDATE `gcontact` SET `last_failure` = '%s' WHERE `id` = %d", - DBA::escape(DateTimeFormat::utcNow()), intval($contact_id)); - return; - } - - if (($data["name"] == "") && ($r[0]['name'] != "")) { - $data["name"] = $r[0]['name']; - } - - if (($data["nick"] == "") && ($r[0]['nick'] != "")) { - $data["nick"] = $r[0]['nick']; - } - - if (($data["addr"] == "") && ($r[0]['addr'] != "")) { - $data["addr"] = $r[0]['addr']; - } - - if (($data["photo"] == "") && ($r[0]['photo'] != "")) { - $data["photo"] = $r[0]['photo']; - } - - - q("UPDATE `gcontact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `photo` = '%s' - WHERE `id` = %d", - DBA::escape($data["name"]), - DBA::escape($data["nick"]), - DBA::escape($data["addr"]), - DBA::escape($data["photo"]), - intval($contact_id) - ); - - q("UPDATE `contact` SET `name` = '%s', `nick` = '%s', `addr` = '%s', `photo` = '%s' - WHERE `uid` = 0 AND `addr` = '' AND `nurl` = '%s'", - DBA::escape($data["name"]), - DBA::escape($data["nick"]), - DBA::escape($data["addr"]), - DBA::escape($data["photo"]), - DBA::escape(Strings::normaliseLink($data["url"])) - ); - - q("UPDATE `contact` SET `addr` = '%s' - WHERE `uid` != 0 AND `addr` = '' AND `nurl` = '%s'", - DBA::escape($data["addr"]), - DBA::escape(Strings::normaliseLink($data["url"])) - ); + Logger::info('Updated from probe', ['url' => $url, 'force' => $force, 'success' => $success]); } }