From 29be22ee6a866381257e8b8d68ee7005da30911b Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 30 Dec 2021 22:11:52 +0000 Subject: [PATCH 1/2] Display interaction data for Diaspora accounts --- database.sql | 5 +++- doc/database/db_fcontact.md | 43 +++++++++++++++------------- src/Factory/Api/Mastodon/Account.php | 4 ++- src/Model/Contact.php | 13 +++++++++ src/Model/FContact.php | 28 ++++++++++++------ src/Object/Api/Mastodon/Account.php | 9 +++--- static/dbstructure.config.php | 5 +++- 7 files changed, 72 insertions(+), 35 deletions(-) diff --git a/database.sql b/database.sql index 175c48b8c3..4179932ad8 100644 --- a/database.sql +++ b/database.sql @@ -1,6 +1,6 @@ -- ------------------------------------------ -- Friendica 2021.12-rc (Siberian Iris) --- DB_UPDATE_VERSION 1447 +-- DB_UPDATE_VERSION 1448 -- ------------------------------------------ @@ -601,6 +601,9 @@ CREATE TABLE IF NOT EXISTS `fcontact` ( `alias` varchar(255) NOT NULL DEFAULT '' COMMENT '', `pubkey` text COMMENT '', `updated` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' COMMENT '', + `interacting_count` int unsigned DEFAULT 0 COMMENT 'Number of contacts this contact interactes with', + `interacted_count` int unsigned DEFAULT 0 COMMENT 'Number of contacts that interacted with this contact', + `post_count` int unsigned DEFAULT 0 COMMENT 'Number of posts and comments', PRIMARY KEY(`id`), INDEX `addr` (`addr`(32)), UNIQUE INDEX `url` (`url`(190)), diff --git a/doc/database/db_fcontact.md b/doc/database/db_fcontact.md index 51cfac7641..77f8e0b85b 100644 --- a/doc/database/db_fcontact.md +++ b/doc/database/db_fcontact.md @@ -6,26 +6,29 @@ Diaspora compatible contacts - used in the Diaspora implementation Fields ------ -| Field | Description | Type | Null | Key | Default | Extra | -| -------- | ------------------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- | -| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | -| guid | unique id | varchar(255) | NO | | | | -| url | | varchar(255) | NO | | | | -| uri-id | Id of the item-uri table entry that contains the fcontact url | int unsigned | YES | | NULL | | -| name | | varchar(255) | NO | | | | -| photo | | varchar(255) | NO | | | | -| request | | varchar(255) | NO | | | | -| nick | | varchar(255) | NO | | | | -| addr | | varchar(255) | NO | | | | -| batch | | varchar(255) | NO | | | | -| notify | | varchar(255) | NO | | | | -| poll | | varchar(255) | NO | | | | -| confirm | | varchar(255) | NO | | | | -| priority | | tinyint unsigned | NO | | 0 | | -| network | | char(4) | NO | | | | -| alias | | varchar(255) | NO | | | | -| pubkey | | text | YES | | NULL | | -| updated | | datetime | NO | | 0001-01-01 00:00:00 | | +| Field | Description | Type | Null | Key | Default | Extra | +| ----------------- | ------------------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- | +| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | +| guid | unique id | varchar(255) | NO | | | | +| url | | varchar(255) | NO | | | | +| uri-id | Id of the item-uri table entry that contains the fcontact url | int unsigned | YES | | NULL | | +| name | | varchar(255) | NO | | | | +| photo | | varchar(255) | NO | | | | +| request | | varchar(255) | NO | | | | +| nick | | varchar(255) | NO | | | | +| addr | | varchar(255) | NO | | | | +| batch | | varchar(255) | NO | | | | +| notify | | varchar(255) | NO | | | | +| poll | | varchar(255) | NO | | | | +| confirm | | varchar(255) | NO | | | | +| priority | | tinyint unsigned | NO | | 0 | | +| network | | char(4) | NO | | | | +| alias | | varchar(255) | NO | | | | +| pubkey | | text | YES | | NULL | | +| updated | | datetime | NO | | 0001-01-01 00:00:00 | | +| interacting_count | Number of contacts this contact interactes with | int unsigned | YES | | 0 | | +| interacted_count | Number of contacts that interacted with this contact | int unsigned | YES | | 0 | | +| post_count | Number of posts and comments | int unsigned | YES | | 0 | | Indexes ------------ diff --git a/src/Factory/Api/Mastodon/Account.php b/src/Factory/Api/Mastodon/Account.php index 7b7eba0ed5..66662673a9 100644 --- a/src/Factory/Api/Mastodon/Account.php +++ b/src/Factory/Api/Mastodon/Account.php @@ -26,6 +26,7 @@ use Friendica\BaseFactory; use Friendica\Collection\Api\Mastodon\Fields; use Friendica\Model\APContact; use Friendica\Model\Contact; +use Friendica\Model\FContact; use Friendica\Network\HTTPException; use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository; use ImagickException; @@ -73,6 +74,7 @@ class Account extends BaseFactory } $apcontact = APContact::getByURL($publicContact['url'], false); + $fcontact = FContact::getByURL($publicContact['url'], false); $self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]); if (!empty($self_contact['uid'])) { @@ -82,7 +84,7 @@ class Account extends BaseFactory $fields = new Fields(); } - return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact, $userContact); + return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $publicContact, $fields, $apcontact, $userContact, $fcontact); } /** diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 4898ec5678..8c946502b1 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -211,6 +211,19 @@ class Contact return DBA::selectFirst('contact', $fields, ['id' => $id]); } + /** + * Fetch the first contact with the provided uri-id. + * + * @param integer $uri_id uri-id of the contact + * @param array $fields Array of selected fields, empty for all + * @return array|boolean Contact record if it exists, false otherwise + * @throws \Exception + */ + public static function getByUriId($uri_id, $fields = []) + { + return DBA::selectFirst('contact', $fields, ['uri-id' => $uri_id], ['order' => ['uid']]); + } + /** * Fetches a contact by a given url * diff --git a/src/Model/FContact.php b/src/Model/FContact.php index 006a37a608..09ee3fb3d5 100644 --- a/src/Model/FContact.php +++ b/src/Model/FContact.php @@ -40,12 +40,12 @@ class FContact * @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \ImagickException */ - public static function getByURL($handle, $update = null, $network = Protocol::DIASPORA) + public static function getByURL($handle, $update = null) { - $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'addr' => $handle]); + $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'addr' => $handle]); if (!DBA::isResult($person)) { $urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)]; - $person = DBA::selectFirst('fcontact', [], ['network' => $network, 'url' => $urls]); + $person = DBA::selectFirst('fcontact', [], ['network' => Protocol::DIASPORA, 'url' => $urls]); } if (DBA::isResult($person)) { @@ -70,14 +70,14 @@ class FContact if ($update) { Logger::info('create or refresh', ['handle' => $handle]); - $r = Probe::uri($handle, $network); + $data = Probe::uri($handle, Protocol::DIASPORA); // Note that Friendica contacts will return a "Diaspora person" // if Diaspora connectivity is enabled on their server - if ($r && ($r["network"] === $network)) { - self::updateFContact($r); + if ($data['network'] ?? '' === Protocol::DIASPORA) { + self::updateFContact($data); - $person = self::getByURL($handle, false, $network); + $person = self::getByURL($handle, false); } } @@ -92,13 +92,25 @@ class FContact */ private static function updateFContact($arr) { + $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]); + + $contact = Contact::getByUriId($uriid, ['id']); + if (!empty($contact['id'])) { + $last_interaction = DateTimeFormat::utc('now - 180 days'); + + $interacted = DBA::count('contact-relation', ["`cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]); + $interacting = DBA::count('contact-relation', ["`relation-cid` = ? AND NOT `follows` AND `last-interaction` > ?", $contact['id'], $last_interaction]); + $posts = Post::countPosts(['author-id' => $contact['id'], 'gravity' => [GRAVITY_PARENT, GRAVITY_COMMENT]]); + } + $fields = ['name' => $arr["name"], 'photo' => $arr["photo"], 'request' => $arr["request"], 'nick' => $arr["nick"], 'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"], 'batch' => $arr["batch"], 'notify' => $arr["notify"], 'poll' => $arr["poll"], 'confirm' => $arr["confirm"], 'alias' => $arr["alias"], 'pubkey' => $arr["pubkey"], - 'uri-id' => ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]), + 'uri-id' => $uriid, 'interacting_count' => $interacting ?? 0, + 'interacted_count' => $interacted ?? 0, 'post_count' => $posts ?? 0, 'updated' => DateTimeFormat::utcNow()]; $condition = ['url' => $arr["url"], 'network' => $arr["network"]]; diff --git a/src/Object/Api/Mastodon/Account.php b/src/Object/Api/Mastodon/Account.php index 2b95e06877..d1e26b8db2 100644 --- a/src/Object/Api/Mastodon/Account.php +++ b/src/Object/Api/Mastodon/Account.php @@ -89,9 +89,10 @@ class Account extends BaseDataTransferObject * @param array $publicContact Full contact table record with uid = 0 * @param array $apcontact Optional full apcontact table record * @param array $userContact Optional full contact table record with uid != 0 + * @param array $fcontact Optional full fcontact table record * @throws \Friendica\Network\HTTPException\InternalServerErrorException */ - public function __construct(BaseURL $baseUrl, array $publicContact, Fields $fields, array $apcontact = [], array $userContact = []) + public function __construct(BaseURL $baseUrl, array $publicContact, Fields $fields, array $apcontact = [], array $userContact = [], array $fcontact = []) { $this->id = (string)$publicContact['id']; $this->username = $publicContact['nick']; @@ -117,9 +118,9 @@ class Account extends BaseDataTransferObject $this->avatar_static = $this->avatar; $this->header = Contact::getHeaderUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], '', $userContact['updated'] ?? '' ?: $publicContact['updated']); $this->header_static = $this->header; - $this->followers_count = $apcontact['followers_count'] ?? 0; - $this->following_count = $apcontact['following_count'] ?? 0; - $this->statuses_count = $apcontact['statuses_count'] ?? 0; + $this->followers_count = $apcontact['followers_count'] ?? $fcontact['interacted_count'] ?? 0; + $this->following_count = $apcontact['following_count'] ?? $fcontact['interacting_count'] ?? 0; + $this->statuses_count = $apcontact['statuses_count'] ?? $fcontact['post_count'] ?? 0; $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME; $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME; diff --git a/static/dbstructure.config.php b/static/dbstructure.config.php index 3fbce08d40..da9b8d38c2 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', 1447); + define('DB_UPDATE_VERSION', 1448); } return [ @@ -662,6 +662,9 @@ return [ "alias" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "pubkey" => ["type" => "text", "comment" => ""], "updated" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "comment" => ""], + "interacting_count" => ["type" => "int unsigned", "default" => 0, "comment" => "Number of contacts this contact interactes with"], + "interacted_count" => ["type" => "int unsigned", "default" => 0, "comment" => "Number of contacts that interacted with this contact"], + "post_count" => ["type" => "int unsigned", "default" => 0, "comment" => "Number of posts and comments"], ], "indexes" => [ "PRIMARY" => ["id"], From ce174cad917ccfc38fbc385baf401ffb33de3d7e Mon Sep 17 00:00:00 2001 From: Michael Date: Thu, 30 Dec 2021 22:40:52 +0000 Subject: [PATCH 2/2] More often update Diaspora contacts --- src/Model/Contact.php | 9 +++++++++ src/Model/FContact.php | 4 ++-- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/src/Model/Contact.php b/src/Model/Contact.php index 8c946502b1..96ffc53468 100644 --- a/src/Model/Contact.php +++ b/src/Model/Contact.php @@ -1241,6 +1241,10 @@ class Contact Logger::info('Contact will be updated', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]); } + if ($data['network'] == Protocol::DIASPORA) { + FContact::updateFromProbeArray($data); + } + self::updateFromProbeArray($contact_id, $data); // Don't return a number for a deleted account @@ -2076,6 +2080,11 @@ class Contact } $ret = Probe::uri($contact['url'], $network, $contact['uid']); + + if ($ret['network'] == Protocol::DIASPORA) { + FContact::updateFromProbeArray($ret); + } + return self::updateFromProbeArray($id, $ret); } diff --git a/src/Model/FContact.php b/src/Model/FContact.php index 09ee3fb3d5..f78da626cb 100644 --- a/src/Model/FContact.php +++ b/src/Model/FContact.php @@ -75,7 +75,7 @@ class FContact // Note that Friendica contacts will return a "Diaspora person" // if Diaspora connectivity is enabled on their server if ($data['network'] ?? '' === Protocol::DIASPORA) { - self::updateFContact($data); + self::updateFromProbeArray($data); $person = self::getByURL($handle, false); } @@ -90,7 +90,7 @@ class FContact * @param array $arr The fcontact data * @throws \Exception */ - private static function updateFContact($arr) + public static function updateFromProbeArray($arr) { $uriid = ItemURI::insert(['uri' => $arr['url'], 'guid' => $arr['guid']]);