From f1d3f60499a325557be866fedd31b46093d477ae Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 17 Oct 2021 20:19:02 +0000 Subject: [PATCH] Remove "fcontact" from suggestions --- database.sql | 8 ++- doc/database/db_intro.md | 28 +++++---- mod/ping.php | 4 +- src/Model/FContact.php | 61 ------------------- .../Notifications/Factory/Introduction.php | 9 ++- src/Protocol/DFRN.php | 53 +++++++++++++++- static/dbstructure.config.php | 5 +- update.php | 15 +++++ 8 files changed, 96 insertions(+), 87 deletions(-) diff --git a/database.sql b/database.sql index b26a9d57c..793aae386 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.12-dev (Siberian Iris) --- DB_UPDATE_VERSION 1438 +-- DB_UPDATE_VERSION 1439 -- ------------------------------------------ @@ -711,8 +711,9 @@ CREATE TABLE IF NOT EXISTS `inbox-status` ( CREATE TABLE IF NOT EXISTS `intro` ( `id` int unsigned NOT NULL auto_increment COMMENT 'sequential ID', `uid` mediumint unsigned NOT NULL DEFAULT 0 COMMENT 'User id', - `fid` int unsigned COMMENT '', + `fid` int unsigned COMMENT 'deprecated', `contact-id` int unsigned NOT NULL DEFAULT 0 COMMENT '', + `suggest-cid` int unsigned COMMENT 'Suggested contact', `knowyou` boolean NOT NULL DEFAULT '0' COMMENT '', `duplex` boolean NOT NULL DEFAULT '0' COMMENT '', `note` text COMMENT '', @@ -724,7 +725,8 @@ CREATE TABLE IF NOT EXISTS `intro` ( INDEX `contact-id` (`contact-id`), INDEX `uid` (`uid`), FOREIGN KEY (`uid`) REFERENCES `user` (`uid`) ON UPDATE RESTRICT ON DELETE CASCADE, - FOREIGN KEY (`contact-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE + FOREIGN KEY (`contact-id`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE, + FOREIGN KEY (`suggest-cid`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE ) DEFAULT COLLATE utf8mb4_general_ci COMMENT=''; -- diff --git a/doc/database/db_intro.md b/doc/database/db_intro.md index 426d8579d..1040402e5 100644 --- a/doc/database/db_intro.md +++ b/doc/database/db_intro.md @@ -6,19 +6,20 @@ Table intro Fields ------ -| Field | Description | Type | Null | Key | Default | Extra | -| ---------- | ------------- | ------------------ | ---- | --- | ------------------- | -------------- | -| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | -| uid | User id | mediumint unsigned | NO | | 0 | | -| fid | | int unsigned | YES | | NULL | | -| contact-id | | int unsigned | NO | | 0 | | -| knowyou | | boolean | NO | | 0 | | -| duplex | | boolean | NO | | 0 | | -| note | | text | YES | | NULL | | -| hash | | varchar(255) | NO | | | | -| datetime | | datetime | NO | | 0001-01-01 00:00:00 | | -| blocked | | boolean | NO | | 1 | | -| ignore | | boolean | NO | | 0 | | +| Field | Description | Type | Null | Key | Default | Extra | +| ----------- | ----------------- | ------------------ | ---- | --- | ------------------- | -------------- | +| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | +| uid | User id | mediumint unsigned | NO | | 0 | | +| fid | deprecated | int unsigned | YES | | NULL | | +| contact-id | | int unsigned | NO | | 0 | | +| suggest-cid | Suggested contact | int unsigned | YES | | NULL | | +| knowyou | | boolean | NO | | 0 | | +| duplex | | boolean | NO | | 0 | | +| note | | text | YES | | NULL | | +| hash | | varchar(255) | NO | | | | +| datetime | | datetime | NO | | 0001-01-01 00:00:00 | | +| blocked | | boolean | NO | | 1 | | +| ignore | | boolean | NO | | 0 | | Indexes ------------ @@ -36,5 +37,6 @@ Foreign Keys |-------|--------------|--------------| | uid | [user](help/database/db_user) | uid | | contact-id | [contact](help/database/db_contact) | id | +| suggest-cid | [contact](help/database/db_contact) | id | Return to [database documentation](help/database) diff --git a/mod/ping.php b/mod/ping.php index 1b4cfb333..3abb2f37a 100644 --- a/mod/ping.php +++ b/mod/ping.php @@ -177,8 +177,8 @@ function ping_init(App $a) $intros1 = DBA::toArray(DBA::p( "SELECT `intro`.`id`, `intro`.`datetime`, - `fcontact`.`name`, `fcontact`.`url`, `fcontact`.`photo` - FROM `intro` INNER JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` + `contact`.`name`, `contact`.`url`, `contact`.`photo` + FROM `intro` INNER JOIN `contact` ON `intro`.`suggest-cid` = `contact`.`id` WHERE `intro`.`uid` = ? AND NOT `intro`.`blocked` AND NOT `intro`.`ignore` AND `intro`.`fid` != 0", local_user() )); diff --git a/src/Model/FContact.php b/src/Model/FContact.php index 1c5c186e6..006a37a60 100644 --- a/src/Model/FContact.php +++ b/src/Model/FContact.php @@ -24,9 +24,7 @@ namespace Friendica\Model; use Friendica\Core\Logger; use Friendica\Core\Protocol; use Friendica\Database\DBA; -use Friendica\DI; use Friendica\Network\Probe; -use Friendica\Protocol\Activity; use Friendica\Util\DateTimeFormat; use Friendica\Util\Strings; @@ -128,63 +126,4 @@ class FContact return null; } - - /** - * Suggest a given contact to a given user from a given contact - * - * @param integer $uid - * @param integer $cid - * @param integer $from_cid - * @return bool Was the adding successful? - */ - public static function addSuggestion(int $uid, int $cid, int $from_cid, string $note = '') - { - $owner = User::getOwnerDataById($uid); - $contact = Contact::getById($cid); - $from_contact = Contact::getById($from_cid); - - if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($contact['url']), 'uid' => $uid])) { - return false; - } - - $fcontact = self::getByURL($contact['url'], null, $contact['network']); - if (empty($fcontact)) { - Logger::warning('FContact had not been found', ['fcontact' => $contact['url']]); - return false; - } - - $fid = $fcontact['id']; - - // Quit if we already have an introduction for this person - if (DBA::exists('intro', ['uid' => $uid, 'fid' => $fid])) { - return false; - } - - $suggest = []; - $suggest['uid'] = $uid; - $suggest['cid'] = $from_cid; - $suggest['url'] = $contact['url']; - $suggest['name'] = $contact['name']; - $suggest['photo'] = $contact['photo']; - $suggest['request'] = $contact['request']; - $suggest['title'] = ''; - $suggest['body'] = $note; - - $hash = Strings::getRandomHex(); - $fields = ['uid' => $suggest['uid'], 'fid' => $fid, 'contact-id' => $suggest['cid'], - 'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false]; - DBA::insert('intro', $fields); - - notification([ - 'type' => Notification\Type::SUGGEST, - 'otype' => Notification\ObjectType::INTRO, - 'verb' => Activity::REQ_FRIEND, - 'uid' => $owner['uid'], - 'cid' => $from_contact['uid'], - 'item' => $suggest, - 'link' => DI::baseUrl().'/notifications/intros', - ]); - - return true; - } } diff --git a/src/Navigation/Notifications/Factory/Introduction.php b/src/Navigation/Notifications/Factory/Introduction.php index 0baf0385c..916ddff67 100644 --- a/src/Navigation/Notifications/Factory/Introduction.php +++ b/src/Navigation/Notifications/Factory/Introduction.php @@ -98,14 +98,13 @@ class Introduction extends BaseFactory $formattedIntroductions = []; try { - /// @todo Fetch contact details by "Contact::getByUrl" instead of queries to contact and fcontact $stmtNotifications = $this->dba->p( "SELECT `intro`.`id` AS `intro_id`, `intro`.*, `contact`.*, - `fcontact`.`name` AS `fname`, `fcontact`.`url` AS `furl`, `fcontact`.`addr` AS `faddr`, - `fcontact`.`photo` AS `fphoto`, `fcontact`.`request` AS `frequest` + `sugggest-contact`.`name` AS `fname`, `sugggest-contact`.`url` AS `furl`, `sugggest-contact`.`addr` AS `faddr`, + `sugggest-contact`.`photo` AS `fphoto`, `sugggest-contact`.`request` AS `frequest` FROM `intro` LEFT JOIN `contact` ON `contact`.`id` = `intro`.`contact-id` - LEFT JOIN `fcontact` ON `intro`.`fid` = `fcontact`.`id` + LEFT JOIN `contact` AS `sugggest-contact` ON `intro`.`suggest-cid` = `sugggest-contact`.`id` WHERE `intro`.`uid` = ? $sql_extra LIMIT ?, ?", $_SESSION['uid'], @@ -121,7 +120,7 @@ class Introduction extends BaseFactory // There are two kind of introduction. Contacts suggested by other contacts and normal connection requests. // We have to distinguish between these two because they use different data. // Contact suggestions - if ($intro['fid'] ?? '') { + if ($intro['suggest-cid'] ?? '') { if (empty($intro['furl'])) { continue; } diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index d73eabc2d..081b7f2a3 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -1330,7 +1330,58 @@ class DFRN $cid = Contact::getIdForURL($url); $note = $xpath->evaluate('string(dfrn:note[1]/text())', $suggestion); - return FContact::addSuggestion($importer['importer_uid'], $cid, $importer['id'], $note); + return self::addSuggestion($importer['importer_uid'], $cid, $importer['id'], $note); + } + + /** + * Suggest a given contact to a given user from a given contact + * + * @param integer $uid + * @param integer $cid + * @param integer $from_cid + * @return bool Was the adding successful? + */ + private static function addSuggestion(int $uid, int $cid, int $from_cid, string $note = '') + { + $owner = User::getOwnerDataById($uid); + $contact = Contact::getById($cid); + $from_contact = Contact::getById($from_cid); + + if (DBA::exists('contact', ['nurl' => Strings::normaliseLink($contact['url']), 'uid' => $uid])) { + return false; + } + + // Quit if we already have an introduction for this person + if (DBA::exists('intro', ['uid' => $uid, 'suggest-cid' => $cid])) { + return false; + } + + $suggest = []; + $suggest['uid'] = $uid; + $suggest['cid'] = $from_cid; + $suggest['url'] = $contact['url']; + $suggest['name'] = $contact['name']; + $suggest['photo'] = $contact['photo']; + $suggest['request'] = $contact['request']; + $suggest['title'] = ''; + $suggest['body'] = $note; + + $hash = Strings::getRandomHex(); + $fields = ['uid' => $suggest['uid'], 'suggest-cid' => $cid, 'contact-id' => $suggest['cid'], + 'note' => $suggest['body'], 'hash' => $hash, 'datetime' => DateTimeFormat::utcNow(), 'blocked' => false]; + DBA::insert('intro', $fields); + + notification([ + 'type' => Notification\Type::SUGGEST, + 'otype' => Notification\ObjectType::INTRO, + 'verb' => Activity::REQ_FRIEND, + 'uid' => $owner['uid'], + 'cid' => $from_contact['uid'], + 'item' => $suggest, + 'link' => DI::baseUrl().'/notifications/intros', + ]); + + return true; } /** diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index e333c136f..6f1986507 100644 --- a/static/dbstructure.config.php +++ b/static/dbstructure.config.php @@ -55,7 +55,7 @@ use Friendica\Database\DBA; if (!defined('DB_UPDATE_VERSION')) { - define('DB_UPDATE_VERSION', 1438); + define('DB_UPDATE_VERSION', 1439); } return [ @@ -774,8 +774,9 @@ return [ "fields" => [ "id" => ["type" => "int unsigned", "not null" => "1", "extra" => "auto_increment", "primary" => "1", "comment" => "sequential ID"], "uid" => ["type" => "mediumint unsigned", "not null" => "1", "default" => "0", "foreign" => ["user" => "uid"], "comment" => "User id"], - "fid" => ["type" => "int unsigned", "relation" => ["fcontact" => "id"], "comment" => ""], + "fid" => ["type" => "int unsigned", "relation" => ["fcontact" => "id"], "comment" => "deprecated"], "contact-id" => ["type" => "int unsigned", "not null" => "1", "default" => "0", "foreign" => ["contact" => "id"], "comment" => ""], + "suggest-cid" => ["type" => "int unsigned", "foreign" => ["contact" => "id"], "comment" => "Suggested contact"], "knowyou" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "duplex" => ["type" => "boolean", "not null" => "1", "default" => "0", "comment" => ""], "note" => ["type" => "text", "comment" => ""], diff --git a/update.php b/update.php index 114401334..6a53c83f1 100644 --- a/update.php +++ b/update.php @@ -1013,3 +1013,18 @@ function update_1438() DBA::update('photo', ['photo-type' => Photo::CONTACT_AVATAR], ["NOT `profile` AND NOT `contact-id` IS NULL AND `contact-id` != ?", 0]); DBA::update('photo', ['photo-type' => Photo::DEFAULT], ["NOT `profile` AND (`contact-id` IS NULL OR `contact-id` = ?) AND `photo-type` IS NULL AND `album` != ?", 0, Photo::CONTACT_PHOTOS]); } + +function update_1439() +{ + $intros = DBA::select('intro', ['id', 'fid'], ["NOT `fid` IS NULL AND `fid` != ?", 0]); + while ($intro = DBA::fetch($intros)) { + $fcontact = DBA::selectFirst('fcontact', ['url'], ['id' => $intro['fid']]); + if (!empty($fcontact['url'])) { + $id = Contact::getIdForURL($fcontact['url']); + if (!empty($id)) { + DBA::update('intro',['suggest-cid' => $id], ['id' => $intro['id']]); + } + } + } + DBA::close($intros); +}