Simplify the contact update in "getIdForURL"

This commit is contained in:
Michael 2019-07-12 21:07:47 +00:00
parent c34204cf50
commit 7dfadf7e7e
2 changed files with 63 additions and 95 deletions

View file

@ -1418,50 +1418,37 @@ class Contact extends BaseObject
$condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false]; $condition = ['nurl' => Strings::normaliseLink($data["url"]), 'uid' => $uid, 'deleted' => false];
// This does an insert but should most likely prevent duplicates
DBA::update('contact', $fields, $condition, true); DBA::update('contact', $fields, $condition, true);
$s = DBA::select('contact', ['id'], $condition, ['order' => ['id'], 'limit' => 2]); $contact = DBA::selectFirst('contact', ['id'], $condition, ['order' => ['id']]);
$contacts = DBA::toArray($s); if (!DBA::isResult($contact)) {
if (!DBA::isResult($contacts)) { // Shouldn't happen
return 0; return 0;
} }
$contact_id = $contacts[0]["id"]; $contact_id = $contact["id"];
// Update in the background when we fetched the data solely from the database
if ($background_update) {
Worker::add(PRIORITY_LOW, "UpdateContact", $contact_id, ($uid == 0 ? 'force' : ''));
}
// 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)) {
// Only use the information when the probing hadn't fetched these values
if (!empty($data['keywords'])) {
unset($gcontact['keywords']);
}
if (!empty($data['location'])) {
unset($gcontact['location']);
}
if (!empty($data['about'])) {
unset($gcontact['about']);
}
DBA::update('contact', $gcontact, ['id' => $contact_id]);
}
if (count($contacts) > 1 && $uid == 0 && $contact_id != 0 && $data["url"] != "") {
$condition = ["`nurl` = ? AND `uid` = ? AND `id` != ? AND NOT `self`",
Strings::normaliseLink($data["url"]), 0, $contact_id];
Logger::log('Deleting duplicate contact ' . json_encode($condition), Logger::DEBUG);
DBA::delete('contact', $condition);
}
} }
if (!empty($data['photo']) && ($data['network'] != Protocol::FEED)) { if (!empty($data['photo']) && ($data['network'] != Protocol::FEED)) {
self::updateAvatar($data['photo'], $uid, $contact_id); self::updateAvatar($data['photo'], $uid, $contact_id);
} }
$fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'pubkey', 'baseurl']; if (in_array($data["network"], array_merge(Protocol::NATIVE_SUPPORT, [Protocol::PUMPIO]))) {
if ($background_update) {
// Update in the background when we fetched the data solely from the database
Worker::add(PRIORITY_MEDIUM, "UpdateContact", $contact_id, ($uid == 0 ? 'force' : ''));
} else {
// Else do a direct update
self::updateFromProbe($contact_id, '', false);
// Update the gcontact entry
if ($uid == 0) {
GContact::updateFromPublicContactID($contact_id);
}
}
} else {
$fields = ['url', 'nurl', 'addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'avatar-date', 'baseurl'];
$contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]); $contact = DBA::selectFirst('contact', $fields, ['id' => $contact_id]);
// This condition should always be true // This condition should always be true
@ -1470,63 +1457,26 @@ class Contact extends BaseObject
} }
$updated = [ $updated = [
'addr' => $data['addr'] ?? '',
'alias' => defaults($data, 'alias', ''),
'url' => $data['url'], 'url' => $data['url'],
'nurl' => Strings::normaliseLink($data['url']), 'nurl' => Strings::normaliseLink($data['url']),
'name' => $data['name'], 'updated' => DateTimeFormat::utcNow()
'nick' => $data['nick']
]; ];
if (!empty($data['baseurl'])) { $fields = ['addr', 'alias', 'name', 'nick', 'keywords', 'location', 'about', 'baseurl'];
$updated['baseurl'] = $data['baseurl'];
}
if (!empty($data['keywords'])) {
$updated['keywords'] = $data['keywords'];
}
if (!empty($data['location'])) {
$updated['location'] = $data['location'];
}
// Update the technical stuff as well - if filled foreach ($fields as $field) {
if (!empty($data['notify'])) { $updated[$field] = defaults($data, $field, $contact[$fields]);
$updated['notify'] = $data['notify'];
}
if (!empty($data['poll'])) {
$updated['poll'] = $data['poll'];
}
if (!empty($data['batch'])) {
$updated['batch'] = $data['batch'];
}
if (!empty($data['request'])) {
$updated['request'] = $data['request'];
}
if (!empty($data['confirm'])) {
$updated['confirm'] = $data['confirm'];
}
if (!empty($data['poco'])) {
$updated['poco'] = $data['poco'];
}
// Only fill the pubkey if it had been empty before. We have to prevent identity theft.
if (empty($contact['pubkey']) && !empty($data['pubkey'])) {
$updated['pubkey'] = $data['pubkey'];
} }
if (($updated['addr'] != $contact['addr']) || (!empty($data['alias']) && ($data['alias'] != $contact['alias']))) { if (($updated['addr'] != $contact['addr']) || (!empty($data['alias']) && ($data['alias'] != $contact['alias']))) {
$updated['uri-date'] = DateTimeFormat::utcNow(); $updated['uri-date'] = DateTimeFormat::utcNow();
} }
if (($data["name"] != $contact["name"]) || ($data["nick"] != $contact["nick"])) {
if (($data['name'] != $contact['name']) || ($data['nick'] != $contact['nick'])) {
$updated['name-date'] = DateTimeFormat::utcNow(); $updated['name-date'] = DateTimeFormat::utcNow();
} }
$updated['updated'] = DateTimeFormat::utcNow();
DBA::update('contact', $updated, ['id' => $contact_id], $contact); DBA::update('contact', $updated, ['id' => $contact_id], $contact);
if (!$background_update && ($uid == 0)) {
// Update the gcontact entry
GContact::updateFromPublicContactID($contact_id);
} }
return $contact_id; return $contact_id;
@ -1854,9 +1804,9 @@ class Contact extends BaseObject
Logger::info('Handling duplicate', ['search' => $dup_id, 'replace' => $first]); Logger::info('Handling duplicate', ['search' => $dup_id, 'replace' => $first]);
// Search and replace // Search and replace
DBA::update('item',['author-id' => $first], ['author-id' => $dup_id]); DBA::update('item', ['author-id' => $first], ['author-id' => $dup_id]);
DBA::update('item',['owner-id' => $first], ['owner-id' => $dup_id]); DBA::update('item', ['owner-id' => $first], ['owner-id' => $dup_id]);
DBA::update('item',['contact-id' => $first], ['contact-id' => $dup_id]); DBA::update('item', ['contact-id' => $first], ['contact-id' => $dup_id]);
// Remove the duplicate // Remove the duplicate
DBA::delete('contact', ['id' => $dup_id]); DBA::delete('contact', ['id' => $dup_id]);
@ -1885,7 +1835,7 @@ class Contact extends BaseObject
$fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'gender', $fields = ['uid', 'avatar', 'name', 'nick', 'location', 'keywords', 'about', 'gender',
'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco', 'unsearchable', 'url', 'addr', 'batch', 'notify', 'poll', 'request', 'confirm', 'poco',
'network', 'alias', 'baseurl', 'forum', 'prv', 'contact-type']; 'network', 'alias', 'baseurl', 'forum', 'prv', 'contact-type', 'pubkey'];
$contact = DBA::selectFirst('contact', $fields, ['id' => $id]); $contact = DBA::selectFirst('contact', $fields, ['id' => $id]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
return false; return false;
@ -1894,6 +1844,9 @@ class Contact extends BaseObject
$uid = $contact['uid']; $uid = $contact['uid'];
unset($contact['uid']); unset($contact['uid']);
$pubkey = $contact['pubkey'];
unset($contact['pubkey']);
$contact['photo'] = $contact['avatar']; $contact['photo'] = $contact['avatar'];
unset($contact['avatar']); unset($contact['avatar']);
@ -1927,6 +1880,8 @@ class Contact extends BaseObject
} }
} }
$new_pubkey = $ret['pubkey'];
$update = false; $update = false;
// make sure to not overwrite existing values with blank entries except some technical fields // make sure to not overwrite existing values with blank entries except some technical fields
@ -1955,6 +1910,19 @@ class Contact extends BaseObject
$ret['nurl'] = Strings::normaliseLink($ret['url']); $ret['nurl'] = Strings::normaliseLink($ret['url']);
$ret['updated'] = $updated; $ret['updated'] = $updated;
// Only fill the pubkey if it had been empty before. We have to prevent identity theft.
if (empty($pubkey) && !empty($new_pubkey)) {
$ret['pubkey'] = $new_pubkey;
}
if (($ret['addr'] != $contact['addr']) || (!empty($ret['alias']) && ($ret['alias'] != $contact['alias']))) {
$ret['uri-date'] = DateTimeFormat::utcNow();
}
if (($ret['name'] != $contact['name']) || ($ret['nick'] != $contact['nick'])) {
$ret['name-date'] = $updated;
}
if ($force && ($uid == 0)) { if ($force && ($uid == 0)) {
$ret['last-update'] = $updated; $ret['last-update'] = $updated;
$ret['success_update'] = $updated; $ret['success_update'] = $updated;

View file

@ -348,7 +348,7 @@ class Probe
if (!self::$istimeout) { if (!self::$istimeout) {
$ap_profile = ActivityPub::probeProfile($uri); $ap_profile = ActivityPub::probeProfile($uri);
if (!empty($ap_profile) && empty($network) && (defaults($data, 'network', '') != Protocol::DFRN)) { if (empty($data) || (!empty($ap_profile) && empty($network) && (defaults($data, 'network', '') != Protocol::DFRN))) {
$data = $ap_profile; $data = $ap_profile;
} elseif (!empty($ap_profile)) { } elseif (!empty($ap_profile)) {
$ap_profile['batch'] = ''; $ap_profile['batch'] = '';