Merge pull request #11128 from annando/diaspora-contact-details

Display interaction data for Diaspora accounts
This commit is contained in:
Hypolite Petovan 2021-12-31 00:40:32 +01:00 committed by GitHub
commit 0a4dc51bdc
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 82 additions and 36 deletions

View file

@ -1,6 +1,6 @@
-- ------------------------------------------ -- ------------------------------------------
-- Friendica 2021.12-rc (Siberian Iris) -- 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 '', `alias` varchar(255) NOT NULL DEFAULT '' COMMENT '',
`pubkey` text COMMENT '', `pubkey` text COMMENT '',
`updated` datetime NOT NULL DEFAULT '0001-01-01 00:00:00' 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`), PRIMARY KEY(`id`),
INDEX `addr` (`addr`(32)), INDEX `addr` (`addr`(32)),
UNIQUE INDEX `url` (`url`(190)), UNIQUE INDEX `url` (`url`(190)),

View file

@ -7,7 +7,7 @@ Fields
------ ------
| Field | Description | Type | Null | Key | Default | Extra | | Field | Description | Type | Null | Key | Default | Extra |
| -------- | ------------------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- | | ----------------- | ------------------------------------------------------------- | ---------------- | ---- | --- | ------------------- | -------------- |
| id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment | | id | sequential ID | int unsigned | NO | PRI | NULL | auto_increment |
| guid | unique id | varchar(255) | NO | | | | | guid | unique id | varchar(255) | NO | | | |
| url | | varchar(255) | NO | | | | | url | | varchar(255) | NO | | | |
@ -26,6 +26,9 @@ Fields
| alias | | varchar(255) | NO | | | | | alias | | varchar(255) | NO | | | |
| pubkey | | text | YES | | NULL | | | pubkey | | text | YES | | NULL | |
| updated | | datetime | NO | | 0001-01-01 00:00:00 | | | 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 Indexes
------------ ------------

View file

@ -26,6 +26,7 @@ use Friendica\BaseFactory;
use Friendica\Collection\Api\Mastodon\Fields; use Friendica\Collection\Api\Mastodon\Fields;
use Friendica\Model\APContact; use Friendica\Model\APContact;
use Friendica\Model\Contact; use Friendica\Model\Contact;
use Friendica\Model\FContact;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;
use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository; use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository;
use ImagickException; use ImagickException;
@ -73,6 +74,7 @@ class Account extends BaseFactory
} }
$apcontact = APContact::getByURL($publicContact['url'], false); $apcontact = APContact::getByURL($publicContact['url'], false);
$fcontact = FContact::getByURL($publicContact['url'], false);
$self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]); $self_contact = Contact::selectFirst(['uid'], ['nurl' => $publicContact['nurl'], 'self' => true]);
if (!empty($self_contact['uid'])) { if (!empty($self_contact['uid'])) {
@ -82,7 +84,7 @@ class Account extends BaseFactory
$fields = new Fields(); $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);
} }
/** /**

View file

@ -211,6 +211,19 @@ class Contact
return DBA::selectFirst('contact', $fields, ['id' => $id]); 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 * Fetches a contact by a given url
* *
@ -1228,6 +1241,10 @@ class Contact
Logger::info('Contact will be updated', ['url' => $url, 'uid' => $uid, 'update' => $update, 'cid' => $contact_id]); 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); self::updateFromProbeArray($contact_id, $data);
// Don't return a number for a deleted account // Don't return a number for a deleted account
@ -2063,6 +2080,11 @@ class Contact
} }
$ret = Probe::uri($contact['url'], $network, $contact['uid']); $ret = Probe::uri($contact['url'], $network, $contact['uid']);
if ($ret['network'] == Protocol::DIASPORA) {
FContact::updateFromProbeArray($ret);
}
return self::updateFromProbeArray($id, $ret); return self::updateFromProbeArray($id, $ret);
} }

View file

@ -40,12 +40,12 @@ class FContact
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @throws \Friendica\Network\HTTPException\InternalServerErrorException
* @throws \ImagickException * @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)) { if (!DBA::isResult($person)) {
$urls = [$handle, str_replace('http://', 'https://', $handle), Strings::normaliseLink($handle)]; $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)) { if (DBA::isResult($person)) {
@ -70,14 +70,14 @@ class FContact
if ($update) { if ($update) {
Logger::info('create or refresh', ['handle' => $handle]); 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" // Note that Friendica contacts will return a "Diaspora person"
// if Diaspora connectivity is enabled on their server // if Diaspora connectivity is enabled on their server
if ($r && ($r["network"] === $network)) { if ($data['network'] ?? '' === Protocol::DIASPORA) {
self::updateFContact($r); self::updateFromProbeArray($data);
$person = self::getByURL($handle, false, $network); $person = self::getByURL($handle, false);
} }
} }
@ -90,15 +90,27 @@ class FContact
* @param array $arr The fcontact data * @param array $arr The fcontact data
* @throws \Exception * @throws \Exception
*/ */
private static function updateFContact($arr) public static function updateFromProbeArray($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"], $fields = ['name' => $arr["name"], 'photo' => $arr["photo"],
'request' => $arr["request"], 'nick' => $arr["nick"], 'request' => $arr["request"], 'nick' => $arr["nick"],
'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"], 'addr' => strtolower($arr["addr"]), 'guid' => $arr["guid"],
'batch' => $arr["batch"], 'notify' => $arr["notify"], 'batch' => $arr["batch"], 'notify' => $arr["notify"],
'poll' => $arr["poll"], 'confirm' => $arr["confirm"], 'poll' => $arr["poll"], 'confirm' => $arr["confirm"],
'alias' => $arr["alias"], 'pubkey' => $arr["pubkey"], '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()]; 'updated' => DateTimeFormat::utcNow()];
$condition = ['url' => $arr["url"], 'network' => $arr["network"]]; $condition = ['url' => $arr["url"], 'network' => $arr["network"]];

View file

@ -89,9 +89,10 @@ class Account extends BaseDataTransferObject
* @param array $publicContact Full contact table record with uid = 0 * @param array $publicContact Full contact table record with uid = 0
* @param array $apcontact Optional full apcontact table record * @param array $apcontact Optional full apcontact table record
* @param array $userContact Optional full contact table record with uid != 0 * @param array $userContact Optional full contact table record with uid != 0
* @param array $fcontact Optional full fcontact table record
* @throws \Friendica\Network\HTTPException\InternalServerErrorException * @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->id = (string)$publicContact['id'];
$this->username = $publicContact['nick']; $this->username = $publicContact['nick'];
@ -117,9 +118,9 @@ class Account extends BaseDataTransferObject
$this->avatar_static = $this->avatar; $this->avatar_static = $this->avatar;
$this->header = Contact::getHeaderUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], '', $userContact['updated'] ?? '' ?: $publicContact['updated']); $this->header = Contact::getHeaderUrlForId($userContact['id'] ?? 0 ?: $publicContact['id'], '', $userContact['updated'] ?? '' ?: $publicContact['updated']);
$this->header_static = $this->header; $this->header_static = $this->header;
$this->followers_count = $apcontact['followers_count'] ?? 0; $this->followers_count = $apcontact['followers_count'] ?? $fcontact['interacted_count'] ?? 0;
$this->following_count = $apcontact['following_count'] ?? 0; $this->following_count = $apcontact['following_count'] ?? $fcontact['interacting_count'] ?? 0;
$this->statuses_count = $apcontact['statuses_count'] ?? 0; $this->statuses_count = $apcontact['statuses_count'] ?? $fcontact['post_count'] ?? 0;
$publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME; $publicContactLastItem = $publicContact['last-item'] ?: DBA::NULL_DATETIME;
$userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME; $userContactLastItem = $userContact['last-item'] ?? DBA::NULL_DATETIME;

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', 1447); define('DB_UPDATE_VERSION', 1448);
} }
return [ return [
@ -662,6 +662,9 @@ return [
"alias" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""], "alias" => ["type" => "varchar(255)", "not null" => "1", "default" => "", "comment" => ""],
"pubkey" => ["type" => "text", "comment" => ""], "pubkey" => ["type" => "text", "comment" => ""],
"updated" => ["type" => "datetime", "not null" => "1", "default" => DBA::NULL_DATETIME, "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" => [ "indexes" => [
"PRIMARY" => ["id"], "PRIMARY" => ["id"],