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

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2021.09-dev (Siberian Iris) -- Friendica 2021.09-rc (Siberian Iris)
-- DB_UPDATE_VERSION 1434 -- DB_UPDATE_VERSION 1435
-- ------------------------------------------ -- ------------------------------------------
@ -1512,13 +1512,16 @@ CREATE TABLE IF NOT EXISTS `userd` (
CREATE TABLE IF NOT EXISTS `user-contact` ( CREATE TABLE IF NOT EXISTS `user-contact` (
`cid` int unsigned NOT NULL DEFAULT 0 COMMENT 'Contact id of the linked public contact', `cid` int unsigned NOT NULL DEFAULT 0 COMMENT 'Contact id of the linked public contact',
`uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id', `uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id',
`uri-id` int unsigned COMMENT 'Id of the item-uri table entry that contains the contact url',
`blocked` boolean COMMENT 'Contact is completely blocked for this user', `blocked` boolean COMMENT 'Contact is completely blocked for this user',
`ignored` boolean COMMENT 'Posts from this contact are ignored', `ignored` boolean COMMENT 'Posts from this contact are ignored',
`collapsed` boolean COMMENT 'Posts from this contact are collapsed', `collapsed` boolean COMMENT 'Posts from this contact are collapsed',
PRIMARY KEY(`uid`,`cid`), PRIMARY KEY(`uid`,`cid`),
INDEX `cid` (`cid`), INDEX `cid` (`cid`),
UNIQUE INDEX `uri-id_uid` (`uri-id`,`uid`),
FOREIGN KEY (`cid`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, FOREIGN KEY (`cid`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE,
FOREIGN KEY (`uri-id`) REFERENCES `item-uri` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE
) DEFAULT COLLATE utf8mb4_general_ci COMMENT='User specific public contact data'; ) DEFAULT COLLATE utf8mb4_general_ci COMMENT='User specific public contact data';
-- --

View File

@ -7,9 +7,10 @@ Fields
------ ------
| Field | Description | Type | Null | Key | Default | Extra | | Field | Description | Type | Null | Key | Default | Extra |
| --------- | ------------------------------------------- | ------------------ | ---- | --- | ------- | ----- | | --------- | ------------------------------------------------------------ | ------------------ | ---- | --- | ------- | ----- |
| cid | Contact id of the linked public contact | int unsigned | NO | PRI | 0 | | | cid | Contact id of the linked public contact | int unsigned | NO | PRI | 0 | |
| uid | User id | mediumint unsigned | NO | PRI | 0 | | | uid | User id | mediumint unsigned | NO | PRI | 0 | |
| uri-id | Id of the item-uri table entry that contains the contact url | int unsigned | YES | | NULL | |
| blocked | Contact is completely blocked for this user | boolean | YES | | NULL | | | blocked | Contact is completely blocked for this user | boolean | YES | | NULL | |
| ignored | Posts from this contact are ignored | boolean | YES | | NULL | | | ignored | Posts from this contact are ignored | boolean | YES | | NULL | |
| collapsed | Posts from this contact are collapsed | boolean | YES | | NULL | | | collapsed | Posts from this contact are collapsed | boolean | YES | | NULL | |
@ -18,9 +19,10 @@ Indexes
------------ ------------
| Name | Fields | | Name | Fields |
| ------- | -------- | | ---------- | ------------------- |
| PRIMARY | uid, cid | | PRIMARY | uid, cid |
| cid | cid | | cid | cid |
| uri-id_uid | UNIQUE, uri-id, uid |
Foreign Keys Foreign Keys
------------ ------------
@ -29,5 +31,6 @@ Foreign Keys
|-------|--------------|--------------| |-------|--------------|--------------|
| cid | [contact](help/database/db_contact) | id | | cid | [contact](help/database/db_contact) | id |
| uid | [user](help/database/db_user) | uid | | uid | [user](help/database/db_user) | uid |
| uri-id | [item-uri](help/database/db_item-uri) | id |
Return to [database documentation](help/database) Return to [database documentation](help/database)

View File

@ -175,7 +175,7 @@ class Contact
* @param array $fields field array * @param array $fields field array
* @param int $duplicate_mode Do an update on a duplicate entry * @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 * @throws \Exception
*/ */
public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT) public static function insert(array $fields, int $duplicate_mode = Database::INSERT_DEFAULT)
@ -190,17 +190,22 @@ class Contact
$fields['created'] = DateTimeFormat::utcNow(); $fields['created'] = DateTimeFormat::utcNow();
} }
$ret = DBA::insert('contact', $fields, $duplicate_mode); DBA::insert('contact', $fields, $duplicate_mode);
$contact = DBA::selectFirst('contact', ['nurl', 'uid'], ['id' => DBA::lastInsertId()]); $contact = DBA::selectFirst('contact', [], ['id' => DBA::lastInsertId()]);
if (!DBA::isResult($contact)) { if (!DBA::isResult($contact)) {
// Shouldn't happen // 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 Contact\User::insertForContactArray($contact);
self::removeDuplicates($contact['nurl'], $contact['uid']);
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 // Only create the entry if it doesn't exist yet
if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) { if (!DBA::exists('contact', ['uid' => $uid, 'self' => true])) {
$return = DBA::insert('contact', $contact); $return = (bool)self::insert($contact);
} }
// Create the public contact // Create the public contact
@ -659,7 +664,7 @@ class Contact
$contact['uid'] = 0; $contact['uid'] = 0;
$contact['prvkey'] = null; $contact['prvkey'] = null;
DBA::insert('contact', $contact, Database::INSERT_IGNORE); self::insert($contact, Database::INSERT_IGNORE);
} }
return $return; return $return;
@ -1209,8 +1214,7 @@ class Contact
$contact_id = $contact['id']; $contact_id = $contact['id'];
Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]); Logger::notice('Contact had been created (shortly) before', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]);
} else { } else {
DBA::insert('contact', $fields); $contact_id = self::insert($fields);
$contact_id = DBA::lastInsertId();
if ($contact_id) { if ($contact_id) {
Logger::info('Contact inserted', ['id' => $contact_id, 'url' => $url, 'uid' => $uid]); 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) 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); $count = DBA::count('contact', $condition);
if ($count <= 1) { if ($count <= 1) {
return false; return false;
@ -2684,7 +2688,7 @@ class Contact
} }
// create contact record // create contact record
DBA::insert('contact', [ $contact_id = self::insert([
'uid' => $importer['uid'], 'uid' => $importer['uid'],
'created' => DateTimeFormat::utcNow(), 'created' => DateTimeFormat::utcNow(),
'url' => $url, 'url' => $url,
@ -2699,8 +2703,6 @@ class Contact
'writable' => 1, 'writable' => 1,
]); ]);
$contact_id = DBA::lastInsertId();
// Ensure to always have the correct network type, independent from the connection request method // Ensure to always have the correct network type, independent from the connection request method
self::updateFromProbe($contact_id); self::updateFromProbe($contact_id);

View File

@ -21,14 +21,56 @@
namespace Friendica\Model\Contact; namespace Friendica\Model\Contact;
use Friendica\Core\Logger;
use Friendica\Core\System;
use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\ItemURI;
/** /**
* This class provides information about user related contacts based on the "user-contact" table. * This class provides information about user related contacts based on the "user-contact" table.
*/ */
class User 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 * Block contact id for user id
* *

View File

@ -234,7 +234,7 @@ class User
$system['closeness'] = 0; $system['closeness'] = 0;
$system['baseurl'] = DI::baseUrl(); $system['baseurl'] = DI::baseUrl();
$system['gsid'] = GServer::getID($system['baseurl']); $system['gsid'] = GServer::getID($system['baseurl']);
DBA::insert('contact', $system); Contact::insert($system);
} }
/** /**

View File

@ -184,6 +184,7 @@ class ExpirePosts
AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`) AND NOT EXISTS(SELECT `external-id` FROM `post-user` WHERE `external-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`) AND NOT EXISTS(SELECT `uri-id` FROM `mail` WHERE `uri-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `uri-id` FROM `event` WHERE `uri-id` = `item-uri`.`id`) AND NOT EXISTS(SELECT `uri-id` FROM `event` WHERE `uri-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `uri-id` FROM `user-contact` WHERE `uri-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `uri-id` FROM `contact` WHERE `uri-id` = `item-uri`.`id`) AND NOT EXISTS(SELECT `uri-id` FROM `contact` WHERE `uri-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `uri-id` FROM `apcontact` WHERE `uri-id` = `item-uri`.`id`) AND NOT EXISTS(SELECT `uri-id` FROM `apcontact` WHERE `uri-id` = `item-uri`.`id`)
AND NOT EXISTS(SELECT `uri-id` FROM `fcontact` WHERE `uri-id` = `item-uri`.`id`) AND NOT EXISTS(SELECT `uri-id` FROM `fcontact` WHERE `uri-id` = `item-uri`.`id`)

View File

@ -55,7 +55,7 @@
use Friendica\Database\DBA; use Friendica\Database\DBA;
if (!defined('DB_UPDATE_VERSION')) { if (!defined('DB_UPDATE_VERSION')) {
define('DB_UPDATE_VERSION', 1434); define('DB_UPDATE_VERSION', 1435);
} }
return [ return [
@ -1531,6 +1531,7 @@ return [
"fields" => [ "fields" => [
"cid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "primary" => "1", "foreign" => ["contact" => "id"], "comment" => "Contact id of the linked public contact"], "cid" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "primary" => "1", "foreign" => ["contact" => "id"], "comment" => "Contact id of the linked public contact"],
"uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "primary" => "1", "foreign" => ["user" => "uid"], "comment" => "User id"], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "primary" => "1", "foreign" => ["user" => "uid"], "comment" => "User id"],
"uri-id" => ["type" => "int unsigned", "foreign" => ["item-uri" => "id"], "comment" => "Id of the item-uri table entry that contains the contact url"],
"blocked" => ["type" => "boolean", "comment" => "Contact is completely blocked for this user"], "blocked" => ["type" => "boolean", "comment" => "Contact is completely blocked for this user"],
"ignored" => ["type" => "boolean", "comment" => "Posts from this contact are ignored"], "ignored" => ["type" => "boolean", "comment" => "Posts from this contact are ignored"],
"collapsed" => ["type" => "boolean", "comment" => "Posts from this contact are collapsed"] "collapsed" => ["type" => "boolean", "comment" => "Posts from this contact are collapsed"]
@ -1538,6 +1539,7 @@ return [
"indexes" => [ "indexes" => [
"PRIMARY" => ["uid", "cid"], "PRIMARY" => ["uid", "cid"],
"cid" => ["cid"], "cid" => ["cid"],
"uri-id_uid" => ["UNIQUE", "uri-id", "uid"],
] ]
], ],
"worker-ipc" => [ "worker-ipc" => [