From f387d85cdbbe181964015cfeb85582871f2b5e1c Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 Apr 2019 20:41:18 +0000 Subject: [PATCH 01/10] Fetch profile data from different contact tables when we won't or can't probe via network --- src/Model/Contact.php | 122 +++++++++++++++++++++++------------ src/Protocol/ActivityPub.php | 7 +- 2 files changed, 85 insertions(+), 44 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index fe373ae94a..2072044b30 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1114,6 +1114,81 @@ class Contact extends BaseObject )", intval($uid), intval($uid)); } + /** + * Have a look at all contact tables for a given profile url. + * This function works as a replacement for probing the contact. + * + * @param string $url Contact URL + * + * @return array Contact array in the "probe" structure + */ + private static function getProbeDataFromDatabase($url) + { + // The link could be provided as http although we stored it as https + $ssl_url = str_replace('http://', 'https://', $url); + + $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', + 'photo', 'keywords', 'location', 'about', 'network', + 'priority', 'batch', 'request', 'confirm', 'poco']; + $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); + + if (!DBA::isResult($contact)) { + $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; + $data = DBA::selectFirst('contact', $fields, $condition); + } + + if (DBA::isResult($data)) { + // For security reasons we don't fetch key data from our users + $data["pubkey"] = ''; + return $data; + } + + $fields = ['url', 'addr', 'alias', 'notify', 'name', 'nick', + 'photo', 'keywords', 'location', 'about', 'network']; + $data = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]); + + if (!DBA::isResult($contact)) { + $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; + $data = DBA::selectFirst('contact', $fields, $condition); + } + + if (DBA::isResult($data)) { + $data["pubkey"] = ''; + $data["poll"] = ''; + $data["priority"] = 0; + $data["batch"] = ''; + $data["request"] = ''; + $data["confirm"] = ''; + $data["poco"] = ''; + return $data; + } + + $data = ActivityPub::probeProfile($url, false); + if (!empty($data)) { + return $data; + } + + $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', + 'photo', 'network', 'priority', 'batch', 'request', 'confirm']; + $data = DBA::selectFirst('fcontact', $fields, ['url' => $url]); + + if (!DBA::isResult($contact)) { + $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; + $data = DBA::selectFirst('contact', $fields, $condition); + } + + if (DBA::isResult($data)) { + $data["pubkey"] = ''; + $data["keywords"] = ''; + $data["location"] = ''; + $data["about"] = ''; + $data["poco"] = ''; + return $data; + } + + return []; + } + /** * @brief Fetch the contact id for a given URL and user * @@ -1187,17 +1262,9 @@ class Contact extends BaseObject return 0; } - // When we don't want to update, we look if some of our users already know this contact - if ($no_update) { - $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', - 'photo', 'keywords', 'location', 'about', 'network', - 'priority', 'batch', 'request', 'confirm', 'poco']; - $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); - - if (DBA::isResult($data)) { - // For security reasons we don't fetch key data from our users - $data["pubkey"] = ''; - } + // When we don't want to update, we look if we know this contact in any way + if ($no_update && empty($default)) { + $data = self::getProbeDataFromDatabase($url); } else { $data = []; } @@ -1217,40 +1284,13 @@ class Contact extends BaseObject return 0; } - // Get data from the gcontact table - $fields = ['name', 'nick', 'url', 'photo', 'addr', 'alias', 'network']; - $contact = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]); - if (!DBA::isResult($contact)) { - $contact = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); - } - - if (!DBA::isResult($contact)) { - $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', - 'photo', 'keywords', 'location', 'about', 'network', - 'priority', 'batch', 'request', 'confirm', 'poco']; - $contact = DBA::selectFirst('contact', $fields, ['addr' => $url]); - } - - // The link could be provided as http although we stored it as https - $ssl_url = str_replace('http://', 'https://', $url); - - if (!DBA::isResult($contact)) { - $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; - $contact = DBA::selectFirst('contact', $fields, $condition); - } - - if (!DBA::isResult($contact)) { - $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', - 'photo', 'network', 'priority', 'batch', 'request', 'confirm']; - $condition = ['url' => [$url, Strings::normaliseLink($url), $ssl_url]]; - $contact = DBA::selectFirst('fcontact', $fields, $condition); - } - if (!empty($default)) { $contact = $default; + } else { + $contact = self::getProbeDataFromDatabase($url); } - if (!DBA::isResult($contact)) { + if (!empty($contact)) { return 0; } else { $data = array_merge($data, $contact); diff --git a/src/Protocol/ActivityPub.php b/src/Protocol/ActivityPub.php index bf8fed94c6..22073a5028 100644 --- a/src/Protocol/ActivityPub.php +++ b/src/Protocol/ActivityPub.php @@ -89,14 +89,15 @@ class ActivityPub /** * Fetches a profile from the given url into an array that is compatible to Probe::uri * - * @param string $url profile url + * @param string $url profile url + * @param boolean $update Update the profile * @return array * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function probeProfile($url) + public static function probeProfile($url, $update = true) { - $apcontact = APContact::getByURL($url, true); + $apcontact = APContact::getByURL($url, $update); if (empty($apcontact)) { return false; } From c088249e1bb85323fccafe314a433fe2a422f43e Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 Apr 2019 21:05:33 +0000 Subject: [PATCH 02/10] Update the contact in the background --- src/Model/Contact.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 2072044b30..2916cf1bb2 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1254,6 +1254,12 @@ class Contact extends BaseObject if (empty($contact['avatar'])) { $update_contact = true; } + + // Update the contact in the background if needed + if ($update_contact && $no_update) { + Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id); + } + if (!$update_contact || $no_update) { return $contact_id; } @@ -1265,8 +1271,10 @@ class Contact extends BaseObject // When we don't want to update, we look if we know this contact in any way if ($no_update && empty($default)) { $data = self::getProbeDataFromDatabase($url); + $background_update = true; } else { $data = []; + $background_update = false; } if (empty($data)) { @@ -1345,6 +1353,11 @@ class Contact extends BaseObject $contact_id = $contacts[0]["id"]; + // Update in the background when we fetched the data solely from the database + if ($background_update) { + Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id); + } + // Update the newly created contact from data in the gcontact table $gcontact = DBA::selectFirst('gcontact', ['location', 'about', 'keywords', 'gender'], ['nurl' => Strings::normaliseLink($data["url"])]); if (DBA::isResult($gcontact)) { From a25fd7713b0ddf0badd76e225edd881127d42a83 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 Apr 2019 21:06:15 +0000 Subject: [PATCH 03/10] Added Worker --- src/Worker/UpdateContact.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 src/Worker/UpdateContact.php diff --git a/src/Worker/UpdateContact.php b/src/Worker/UpdateContact.php new file mode 100644 index 0000000000..fab97a927b --- /dev/null +++ b/src/Worker/UpdateContact.php @@ -0,0 +1,19 @@ + $contact_id, 'success' => $success]); + } +} From a2ca14def48485b44cec701ae255838253196538 Mon Sep 17 00:00:00 2001 From: Michael Date: Mon, 8 Apr 2019 21:35:00 +0000 Subject: [PATCH 04/10] Removed a worker call - this needs more work --- src/Model/Contact.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 2916cf1bb2..47b2a8b1e6 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1255,11 +1255,6 @@ class Contact extends BaseObject $update_contact = true; } - // Update the contact in the background if needed - if ($update_contact && $no_update) { - Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id); - } - if (!$update_contact || $no_update) { return $contact_id; } From 90eea919a42c6d2dc40afd967a9dcf6ebae4e22a Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 Apr 2019 05:15:23 +0000 Subject: [PATCH 05/10] New contact field "updated", fix warnings --- config/dbstructure.config.php | 3 ++- src/Model/Contact.php | 40 +++++++++++++++++++++++------------ src/Worker/UpdateContact.php | 9 ++++++++ 3 files changed, 37 insertions(+), 15 deletions(-) diff --git a/config/dbstructure.config.php b/config/dbstructure.config.php index 8ae14ad41f..e82c69664a 100755 --- a/config/dbstructure.config.php +++ b/config/dbstructure.config.php @@ -34,7 +34,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1309); + define('DB_UPDATE_VERSION', 1310); } return [ @@ -180,6 +180,7 @@ return [ "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "relation" => ["user" => "uid"], "comment" => "Owner User id"], "created" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], + "updated" => ["type" => "datetime", "default" => DBA::NULL_DATETIME, "comment" => "Date of last contact update"], "self" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => "1 if the contact is the user him/her self"], "remote_self" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "rel" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "The kind of the relation between the user and the contact"], diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 47b2a8b1e6..1b38fde2ed 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -599,7 +599,10 @@ class Contact extends BaseObject } if ($update) { - $fields['name-date'] = DateTimeFormat::utcNow(); + if ($fields['name'] != $self['name']) { + $fields['name-date'] = DateTimeFormat::utcNow(); + } + $fields['updated'] = DateTimeFormat::utcNow(); DBA::update('contact', $fields, ['id' => $self['id']]); // Update the public contact as well @@ -1132,7 +1135,7 @@ class Contact extends BaseObject 'priority', 'batch', 'request', 'confirm', 'poco']; $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); - if (!DBA::isResult($contact)) { + if (!DBA::isResult($data)) { $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; $data = DBA::selectFirst('contact', $fields, $condition); } @@ -1147,7 +1150,7 @@ class Contact extends BaseObject 'photo', 'keywords', 'location', 'about', 'network']; $data = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]); - if (!DBA::isResult($contact)) { + if (!DBA::isResult($data)) { $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; $data = DBA::selectFirst('contact', $fields, $condition); } @@ -1172,7 +1175,7 @@ class Contact extends BaseObject 'photo', 'network', 'priority', 'batch', 'request', 'confirm']; $data = DBA::selectFirst('fcontact', $fields, ['url' => $url]); - if (!DBA::isResult($contact)) { + if (!DBA::isResult($data)) { $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; $data = DBA::selectFirst('contact', $fields, $condition); } @@ -1229,11 +1232,11 @@ class Contact extends BaseObject /// @todo Verify if we can't use Contact::getDetailsByUrl instead of the following // We first try the nurl (http://server.tld/nick), most common case - $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false]); + $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated'], ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false]); // Then the addr (nick@server.tld) if (!DBA::isResult($contact)) { - $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], ['addr' => $url, 'uid' => $uid, 'deleted' => false]); + $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated'], ['addr' => $url, 'uid' => $uid, 'deleted' => false]); } // Then the alias (which could be anything) @@ -1241,20 +1244,25 @@ class Contact extends BaseObject // The link could be provided as http although we stored it as https $ssl_url = str_replace('http://', 'https://', $url); $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid]; - $contact = DBA::selectFirst('contact', ['id', 'avatar', 'avatar-date'], $condition); + $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated'], $condition); } if (DBA::isResult($contact)) { $contact_id = $contact["id"]; // Update the contact every 7 days - $update_contact = ($contact['avatar-date'] < DateTimeFormat::utc('now -7 days')); + $update_contact = ($contact['updated'] < DateTimeFormat::utc('now -7 days')); // We force the update if the avatar is empty if (empty($contact['avatar'])) { $update_contact = true; } + // Update the contact in the background if needed but it is called by the frontend + if ($update_contact && $no_update) { + Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id); + } + if (!$update_contact || $no_update) { return $contact_id; } @@ -1291,13 +1299,16 @@ class Contact extends BaseObject $contact = $default; } else { $contact = self::getProbeDataFromDatabase($url); + if (empty($contact)) { + return 0; + } } - if (!empty($contact)) { - return 0; - } else { - $data = array_merge($data, $contact); - } + $data = array_merge($data, $contact); + } + + if (empty($data)) { + return 0; } if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url) && !$in_loop) { @@ -1433,7 +1444,7 @@ class Contact extends BaseObject $updated['name-date'] = DateTimeFormat::utcNow(); } - $updated['avatar-date'] = DateTimeFormat::utcNow(); + $updated['updated'] = DateTimeFormat::utcNow(); DBA::update('contact', $updated, ['id' => $contact_id], $contact); @@ -1732,6 +1743,7 @@ class Contact extends BaseObject } $ret['nurl'] = Strings::normaliseLink($ret['url']); + $ret['updated'] = DateTimeFormat::utcNow(); self::updateAvatar($ret['photo'], $uid, $id, true); diff --git a/src/Worker/UpdateContact.php b/src/Worker/UpdateContact.php index fab97a927b..ae3b06b506 100644 --- a/src/Worker/UpdateContact.php +++ b/src/Worker/UpdateContact.php @@ -8,12 +8,21 @@ namespace Friendica\Worker; use Friendica\Core\Logger; use Friendica\Model\Contact; +use Friendica\Util\DateTimeFormat; +use Friendica\Database\DBA; class UpdateContact { public static function execute($contact_id) { $success = Contact::updateFromProbe($contact_id); + // Update the "updated" field if the contact could be probed. + // We don't do this in the function above, since we don't want to + // update the contact whenever that function is called from anywhere. + if ($success) { + DBA::update('contact', ['updated' => DateTimeFormat::utcNow()], ['id' => $contact_id]); + } + Logger::info('Updated from probe', ['id' => $contact_id, 'success' => $success]); } } From e268b508906443e2e04d1de23f722e92ddca5034 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 Apr 2019 05:50:51 +0000 Subject: [PATCH 06/10] Updated database structure file --- config/dbstructure.config.php | 2 +- database.sql | 5 +++-- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/config/dbstructure.config.php b/config/dbstructure.config.php index e82c69664a..3996205db0 100755 --- a/config/dbstructure.config.php +++ b/config/dbstructure.config.php @@ -188,7 +188,7 @@ return [ "network" => ["type" => "char(4)", "not null" => "1", "default" => "", "comment" => "Network protocol of the contact"], "name" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "Name that this contact is known by"], "nick" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => "Nick- and user name of the contact"], - "location" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], + "location" => ["type" => "varchar(255)", "default" => "", "comment" => ""], "about" => ["type" => "text", "comment" => ""], "keywords" => ["type" => "text", "comment" => "public keywords (interests) of the contact"], "gender" => ["type" => "varchar(32)", "not null" => "1", "default" => "", "comment" => ""], diff --git a/database.sql b/database.sql index 9c4af9a43f..b9e70097ee 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2019.06-dev (Dalmatian Bellflower) --- DB_UPDATE_VERSION 1309 +-- DB_UPDATE_VERSION 1310 -- ------------------------------------------ @@ -138,6 +138,7 @@ CREATE TABLE IF NOT EXISTS `contact` ( `id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID', `uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'Owner User id', `created` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', + `updated` datetime DEFAULT '0001-01-01 00:00:00' COMMENT 'Date of last contact update', `self` boolean NOT NULL DEFAULT '0' COMMENT '1 if the contact is the user him/her self', `remote_self` boolean NOT NULL DEFAULT '0' COMMENT '', `rel` tinyint unsigned NOT NULL DEFAULT 0 COMMENT 'The kind of the relation between the user and the contact', @@ -145,7 +146,7 @@ CREATE TABLE IF NOT EXISTS `contact` ( `network` char(4) NOT NULL DEFAULT '' COMMENT 'Network protocol of the contact', `name` varchar(255) NOT NULL DEFAULT '' COMMENT 'Name that this contact is known by', `nick` varchar(255) NOT NULL DEFAULT '' COMMENT 'Nick- and user name of the contact', - `location` varchar(255) NOT NULL DEFAULT '' COMMENT '', + `location` varchar(255) DEFAULT '' COMMENT '', `about` text COMMENT '', `keywords` text COMMENT 'public keywords (interests) of the contact', `gender` varchar(32) NOT NULL DEFAULT '' COMMENT '', From bca7419987bb3f3c5782b9f8f80d5fa3b614c963 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 Apr 2019 08:35:29 +0000 Subject: [PATCH 07/10] Avoid overwriting existing contacts with bad data --- src/Model/Contact.php | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 1b38fde2ed..51264d4697 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1295,13 +1295,9 @@ class Contact extends BaseObject return 0; } - if (!empty($default)) { - $contact = $default; - } else { - $contact = self::getProbeDataFromDatabase($url); - if (empty($contact)) { - return 0; - } + $contact = array_merge(self::getProbeDataFromDatabase($url), $default); + if (empty($contact)) { + return 0; } $data = array_merge($data, $contact); From 84a6e390ab78d99e0688a5cbe4696e33de130b4c Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 Apr 2019 08:47:57 +0000 Subject: [PATCH 08/10] Fetch data from the given contact number, if already present --- src/Model/Contact.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 51264d4697..ecbf7411c3 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1121,11 +1121,12 @@ class Contact extends BaseObject * Have a look at all contact tables for a given profile url. * This function works as a replacement for probing the contact. * - * @param string $url Contact URL + * @param string $url Contact URL + * @param integer $cid Contact ID * * @return array Contact array in the "probe" structure */ - private static function getProbeDataFromDatabase($url) + private static function getProbeDataFromDatabase($url, $cid) { // The link could be provided as http although we stored it as https $ssl_url = str_replace('http://', 'https://', $url); @@ -1133,6 +1134,14 @@ class Contact extends BaseObject $fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick', 'photo', 'keywords', 'location', 'about', 'network', 'priority', 'batch', 'request', 'confirm', 'poco']; + + if (!empty($cid)) { + $data = DBA::selectFirst('contact', $fields, ['id' => $cid]); + if (DBA::isResult($data)) { + return $data; + } + } + $data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); if (!DBA::isResult($data)) { @@ -1273,7 +1282,7 @@ class Contact extends BaseObject // When we don't want to update, we look if we know this contact in any way if ($no_update && empty($default)) { - $data = self::getProbeDataFromDatabase($url); + $data = self::getProbeDataFromDatabase($url, $contact_id); $background_update = true; } else { $data = []; @@ -1295,7 +1304,7 @@ class Contact extends BaseObject return 0; } - $contact = array_merge(self::getProbeDataFromDatabase($url), $default); + $contact = array_merge(self::getProbeDataFromDatabase($url, $contact_id), $default); if (empty($contact)) { return 0; } From d58147413f0833f40ce1f4ca524cea30a68134f8 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 Apr 2019 11:28:45 +0000 Subject: [PATCH 09/10] Avoid update of non native contacts, fix most warnings --- src/Model/Contact.php | 74 +++++++++++++++++++++++-------------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index ecbf7411c3..94d42945cd 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1241,11 +1241,11 @@ class Contact extends BaseObject /// @todo Verify if we can't use Contact::getDetailsByUrl instead of the following // We first try the nurl (http://server.tld/nick), most common case - $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated'], ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false]); + $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated', 'network'], ['nurl' => Strings::normaliseLink($url), 'uid' => $uid, 'deleted' => false]); // Then the addr (nick@server.tld) if (!DBA::isResult($contact)) { - $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated'], ['addr' => $url, 'uid' => $uid, 'deleted' => false]); + $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated', 'network'], ['addr' => $url, 'uid' => $uid, 'deleted' => false]); } // Then the alias (which could be anything) @@ -1253,7 +1253,7 @@ class Contact extends BaseObject // The link could be provided as http although we stored it as https $ssl_url = str_replace('http://', 'https://', $url); $condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid]; - $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated'], $condition); + $contact = DBA::selectFirst('contact', ['id', 'avatar', 'updated', 'network'], $condition); } if (DBA::isResult($contact)) { @@ -1268,7 +1268,7 @@ class Contact extends BaseObject } // Update the contact in the background if needed but it is called by the frontend - if ($update_contact && $no_update) { + if ($update_contact && $no_update && in_array($contact['network'], Protocol::NATIVE_SUPPORT)) { Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id); } @@ -1324,26 +1324,26 @@ class Contact extends BaseObject $fields = [ 'uid' => $uid, 'created' => DateTimeFormat::utcNow(), - 'url' => $data["url"], - 'nurl' => Strings::normaliseLink($data["url"]), - 'addr' => $data["addr"], - 'alias' => $data["alias"], - 'notify' => $data["notify"], - 'poll' => $data["poll"], - 'name' => $data["name"], - 'nick' => $data["nick"], - 'photo' => $data["photo"], - 'keywords' => $data["keywords"], - 'location' => $data["location"], - 'about' => $data["about"], - 'network' => $data["network"], - 'pubkey' => $data["pubkey"], + 'url' => $data['url'], + 'nurl' => Strings::normaliseLink($data['url']), + 'addr' => defaults($data, 'addr', ''), + 'alias' => defaults($data, 'alias', ''), + 'notify' => defaults($data, 'notify', ''), + 'poll' => defaults($data, 'poll', ''), + 'name' => defaults($data, 'name', ''), + 'nick' => defaults($data, 'nick', ''), + 'photo' => defaults($data, 'photo', ''), + 'keywords' => defaults($data, 'keywords', ''), + 'location' => defaults($data, 'location', ''), + 'about' => defaults($data, 'about', ''), + 'network' => $data['network'], + 'pubkey' => defaults($data, 'pubkey', ''), 'rel' => self::SHARING, - 'priority' => $data["priority"], - 'batch' => $data["batch"], - 'request' => $data["request"], - 'confirm' => $data["confirm"], - 'poco' => $data["poco"], + 'priority' => defaults($data, 'priority', 0), + 'batch' => defaults($data, 'batch', ''), + 'request' => defaults($data, 'request', ''), + 'confirm' => defaults($data, 'confirm', ''), + 'poco' => defaults($data, 'poco', ''), 'name-date' => DateTimeFormat::utcNow(), 'uri-date' => DateTimeFormat::utcNow(), 'avatar-date' => DateTimeFormat::utcNow(), @@ -1373,13 +1373,13 @@ class Contact extends BaseObject $gcontact = DBA::selectFirst('gcontact', ['location', 'about', 'keywords', 'gender'], ['nurl' => Strings::normaliseLink($data["url"])]); if (DBA::isResult($gcontact)) { // Only use the information when the probing hadn't fetched these values - if ($data['keywords'] != '') { + if (!empty($data['keywords'])) { unset($gcontact['keywords']); } - if ($data['location'] != '') { + if (!empty($data['location'])) { unset($gcontact['location']); } - if ($data['about'] != '') { + if (!empty($data['about'])) { unset($gcontact['about']); } DBA::update('contact', $gcontact, ['id' => $contact_id]); @@ -1410,30 +1410,30 @@ class Contact extends BaseObject 'name' => $data['name'], 'nick' => $data['nick']]; - if ($data['keywords'] != '') { + if (!empty($data['keywords'])) { $updated['keywords'] = $data['keywords']; } - if ($data['location'] != '') { + if (!empty($data['location'])) { $updated['location'] = $data['location']; } // Update the technical stuff as well - if filled - if ($data['notify'] != '') { + if (!empty($data['notify'])) { $updated['notify'] = $data['notify']; } - if ($data['poll'] != '') { + if (!empty($data['poll'])) { $updated['poll'] = $data['poll']; } - if ($data['batch'] != '') { + if (!empty($data['batch'])) { $updated['batch'] = $data['batch']; } - if ($data['request'] != '') { + if (!empty($data['request'])) { $updated['request'] = $data['request']; } - if ($data['confirm'] != '') { + if (!empty($data['confirm'])) { $updated['confirm'] = $data['confirm']; } - if ($data['poco'] != '') { + if (!empty($data['poco'])) { $updated['poco'] = $data['poco']; } @@ -1726,7 +1726,11 @@ 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'])) { + if (in_array($ret['network'], [Protocol::FEED, Protocol::PHANTOM]) && ($ret['network'] != $contact['network'])) { + return false; + } + + if (!in_array($ret['network'], Protocol::NATIVE_SUPPORT)) { return false; } From 8748aff110053ca741c73d88f80ba1c0260507c4 Mon Sep 17 00:00:00 2001 From: Michael Date: Tue, 9 Apr 2019 11:44:35 +0000 Subject: [PATCH 10/10] Avoid some more warning --- src/Model/Contact.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 94d42945cd..a866df91c5 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1393,7 +1393,9 @@ class Contact extends BaseObject } } - self::updateAvatar($data["photo"], $uid, $contact_id); + if (!empty($data['photo'])) { + self::updateAvatar($data['photo'], $uid, $contact_id); + } $fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey']; $contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]);