1
1
Fork 0

New contact field "updated", fix warnings

This commit is contained in:
Michael 2019-04-09 05:15:23 +00:00
parent a2ca14def4
commit 90eea919a4
3 changed files with 37 additions and 15 deletions

View file

@ -34,7 +34,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1309); define('DB_UPDATE_VERSION', 1310);
} }
return [ return [
@ -180,6 +180,7 @@ return [
"id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "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"], "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" => ""], "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"], "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" => ""], "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"], "rel" => ["type" => "tinyint unsigned", "not null" => "1", "default" => "0", "comment" => "The kind of the relation between the user and the contact"],

View file

@ -599,7 +599,10 @@ class Contact extends BaseObject
} }
if ($update) { if ($update) {
if ($fields['name'] != $self['name']) {
$fields['name-date'] = DateTimeFormat::utcNow(); $fields['name-date'] = DateTimeFormat::utcNow();
}
$fields['updated'] = DateTimeFormat::utcNow();
DBA::update('contact', $fields, ['id' => $self['id']]); DBA::update('contact', $fields, ['id' => $self['id']]);
// Update the public contact as well // Update the public contact as well
@ -1132,7 +1135,7 @@ class Contact extends BaseObject
'priority', 'batch', 'request', 'confirm', 'poco']; 'priority', 'batch', 'request', 'confirm', 'poco'];
$data = DBA::selectFirst('contact', $fields, ['nurl' => Strings::normaliseLink($url)]); $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]]; $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
$data = DBA::selectFirst('contact', $fields, $condition); $data = DBA::selectFirst('contact', $fields, $condition);
} }
@ -1147,7 +1150,7 @@ class Contact extends BaseObject
'photo', 'keywords', 'location', 'about', 'network']; 'photo', 'keywords', 'location', 'about', 'network'];
$data = DBA::selectFirst('gcontact', $fields, ['nurl' => Strings::normaliseLink($url)]); $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]]; $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
$data = DBA::selectFirst('contact', $fields, $condition); $data = DBA::selectFirst('contact', $fields, $condition);
} }
@ -1172,7 +1175,7 @@ class Contact extends BaseObject
'photo', 'network', 'priority', 'batch', 'request', 'confirm']; 'photo', 'network', 'priority', 'batch', 'request', 'confirm'];
$data = DBA::selectFirst('fcontact', $fields, ['url' => $url]); $data = DBA::selectFirst('fcontact', $fields, ['url' => $url]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($data)) {
$condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]]; $condition = ['alias' => [$url, Strings::normaliseLink($url), $ssl_url]];
$data = DBA::selectFirst('contact', $fields, $condition); $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 /// @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 // 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) // Then the addr (nick@server.tld)
if (!DBA::isResult($contact)) { 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) // 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 // The link could be provided as http although we stored it as https
$ssl_url = str_replace('http://', 'https://', $url); $ssl_url = str_replace('http://', 'https://', $url);
$condition = ['`alias` IN (?, ?, ?) AND `uid` = ? AND NOT `deleted`', $url, Strings::normaliseLink($url), $ssl_url, $uid]; $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)) { if (DBA::isResult($contact)) {
$contact_id = $contact["id"]; $contact_id = $contact["id"];
// Update the contact every 7 days // 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 // We force the update if the avatar is empty
if (empty($contact['avatar'])) { if (empty($contact['avatar'])) {
$update_contact = true; $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) { if (!$update_contact || $no_update) {
return $contact_id; return $contact_id;
} }
@ -1291,13 +1299,16 @@ class Contact extends BaseObject
$contact = $default; $contact = $default;
} else { } else {
$contact = self::getProbeDataFromDatabase($url); $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) { if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url) && !$in_loop) {
@ -1433,7 +1444,7 @@ class Contact extends BaseObject
$updated['name-date'] = DateTimeFormat::utcNow(); $updated['name-date'] = DateTimeFormat::utcNow();
} }
$updated['avatar-date'] = DateTimeFormat::utcNow(); $updated['updated'] = DateTimeFormat::utcNow();
DBA::update('contact', $updated, ['id' => $contact_id], $contact); DBA::update('contact', $updated, ['id' => $contact_id], $contact);
@ -1732,6 +1743,7 @@ class Contact extends BaseObject
} }
$ret['nurl'] = Strings::normaliseLink($ret['url']); $ret['nurl'] = Strings::normaliseLink($ret['url']);
$ret['updated'] = DateTimeFormat::utcNow();
self::updateAvatar($ret['photo'], $uid, $id, true); self::updateAvatar($ret['photo'], $uid, $id, true);

View file

@ -8,12 +8,21 @@ namespace Friendica\Worker;
use Friendica\Core\Logger; use Friendica\Core\Logger;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Util\DateTimeFormat;
use Friendica\Database\DBA;
class UpdateContact class UpdateContact
{ {
public static function execute($contact_id) public static function execute($contact_id)
{ {
$success = Contact::updateFromProbe($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]); Logger::info('Updated from probe', ['id' => $contact_id, 'success' => $success]);
} }
} }