use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) {
- define('DB_UPDATE_VERSION', 1309);
+ define('DB_UPDATE_VERSION', 1310);
}
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"],
"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" => ""],
}
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
)", 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
+ * @param integer $cid Contact ID
+ *
+ * @return array Contact array in the "probe" structure
+ */
+ 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);
+
+ $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)) {
+ $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($data)) {
+ $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($data)) {
+ $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
*
/// @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)
// 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;
}
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, $contact_id);
+ $background_update = true;
} else {
$data = [];
+ $background_update = false;
}
if (empty($data)) {
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);
+ $contact = array_merge(self::getProbeDataFromDatabase($url, $contact_id), $default);
+ if (empty($contact)) {
+ return 0;
}
- if (!empty($default)) {
- $contact = $default;
- }
+ $data = array_merge($data, $contact);
+ }
- if (!DBA::isResult($contact)) {
- return 0;
- } else {
- $data = array_merge($data, $contact);
- }
+ if (empty($data)) {
+ return 0;
}
if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url) && !$in_loop) {
$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)) {
$updated['name-date'] = DateTimeFormat::utcNow();
}
- $updated['avatar-date'] = DateTimeFormat::utcNow();
+ $updated['updated'] = DateTimeFormat::utcNow();
DBA::update('contact', $updated, ['id' => $contact_id], $contact);
}
$ret['nurl'] = Strings::normaliseLink($ret['url']);
+ $ret['updated'] = DateTimeFormat::utcNow();
self::updateAvatar($ret['photo'], $uid, $id, true);