Insert a user-contact for every contact

This commit is contained in:
Michael 2021-09-10 13:05:16 +00:00
parent 225d368139
commit a4b0ab90b1
7 changed files with 87 additions and 34 deletions

View file

@ -175,7 +175,7 @@ class Contact
* @param array $fields field array
* @param int $duplicate_mode Do an update on a duplicate entry
*
* @return boolean was the insert successful?
* @return int id of the created contact
* @throws \Exception
*/
public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT)
@ -190,17 +190,22 @@ class Contact
$fields['created'] = DateTimeFormat::utcNow();
}
$ret = DBA::insert('contact', $fields, $duplicate_mode);
$contact = DBA::selectFirst('contact', ['nurl', 'uid'], ['id' => DBA::lastInsertId()]);
DBA::insert('contact', $fields, $duplicate_mode);
$contact = DBA::selectFirst('contact', [], ['id' => DBA::lastInsertId()]);
if (!DBA::isResult($contact)) {
// Shouldn't happen
return $ret;
Logger::warning('Created contact could not be found', ['fields' => $fields]);
return 0;
}
// Search for duplicated contacts and get rid of them
self::removeDuplicates($contact['nurl'], $contact['uid']);
Contact\User::insertForContactArray($contact);
return $ret;
// Search for duplicated contacts and get rid of them
if (!$contact['self']) {
self::removeDuplicates($contact['nurl'], $contact['uid']);
}
return $contact['id'];
}
/**
@ -650,7 +655,7 @@ class Contact
// Only create the entry if it doesn't exist yet
if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
$return = DBA::insert('contact', $contact);
$return = (bool)self::insert($contact);
}
// Create the public contact
@ -659,7 +664,7 @@ class Contact
$contact['uid'] = 0;
$contact['prvkey'] = null;
DBA::insert('contact', $contact, Database::INSERT_IGNORE);
self::insert($contact, Database::INSERT_IGNORE);
}
return $return;
@ -1209,8 +1214,7 @@ class Contact
$contact_id = $contact['id'];
Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
} else {
DBA::insert('contact', $fields);
$contact_id = DBA::lastInsertId();
$contact_id = self::insert($fields);
if ($contact_id) {
Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
}
@ -1991,7 +1995,7 @@ class Contact
*/
public static function removeDuplicates(string $nurl, int $uid)
{
$condition = ['nurl' => $nurl, 'uid' => $uid, 'deleted' => false, 'network' => Protocol::FEDERATED];
$condition = ['nurl' => $nurl, 'uid' => $uid, 'self' => false, 'deleted' => false, 'network' => Protocol::FEDERATED];
$count = DBA::count('contact', $condition);
if ($count <= 1) {
return false;
@ -2355,7 +2359,7 @@ class Contact
$probed = false;
$ret = $arr['contact'];
} else {
$probed = true;
$probed = true;
$ret = Probe::uri($url, $network, $uid);
}
@ -2684,7 +2688,7 @@ class Contact
}
// create contact record
DBA::insert('contact', [
$contact_id = self::insert([
'uid' => $importer['uid'],
'created' => DateTimeFormat::utcNow(),
'url' => $url,
@ -2699,8 +2703,6 @@ class Contact
'writable' => 1,
]);
$contact_id = DBA::lastInsertId();
// Ensure to always have the correct network type, independent from the connection request method
self::updateFromProbe($contact_id);

View file

@ -21,14 +21,56 @@
namespace Friendica\Model\Contact;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\Database\DBA;
use Friendica\Model\Contact;
use Friendica\Model\ItemURI;
/**
* This class provides information about user related contacts based on the "user-contact" table.
*/
class User
{
/**
* Insert a user-contact for a given contact array
*
* @param array $contact
* @return void
*/
public static function insertForContactArray(array $contact)
{
if (!isset($contact['uid']) || (empty($contact['uri-id']) && empty($contact['url']))) {
Logger::info('Missing contact details', ['contact' => $contact, 'callstack' => System::callstack(20)]);
return false;
}
if (empty($contact['uri-id'])) {
$contact['uri-id'] = ItemURI::getIdByURI($contact['url']);
}
$pcontact = Contact::selectFirst(['id'], ['uri-id' => $contact['uri-id'], 'uid' => 0]);
if (!DBA::isResult($pcontact)) {
Logger::info('Public contact for user not found', ['uri-id' => $contact['uri-id'], 'uid' => $contact['uid'], 'cid' => $pcontact['id']]);
return false;
}
$fields = [
'cid' => $pcontact['id'],
'uid' => $contact['uid'],
'uri-id' => $contact['uri-id'],
'blocked' => $contact['blocked'] ?? false,
'ignored' => $contact['readonly'] ?? false,
];
$ret = DBA::insert('user-contact', $fields, Database::INSERT_IGNORE);
Logger::info('Inserted user contact', ['uid' => $contact['uid'], 'cid' => $pcontact['id'], 'uri-id' => $contact['uri-id'], 'ret' => $ret]);
return $ret;
}
/**
* Block contact id for user id
*

View file

@ -234,7 +234,7 @@ class User
$system['closeness'] = 0;
$system['baseurl'] = DI::baseUrl();
$system['gsid'] = GServer::getID($system['baseurl']);
DBA::insert('contact', $system);
Contact::insert($system);
}
/**
@ -1585,8 +1585,8 @@ class User
/**
* Check if the given user id has delegations or is delegated
*
* @param int $uid
* @return bool
* @param int $uid
* @return bool
*/
public static function hasIdentities(int $uid):bool
{