]> git.mxchange.org Git - friendica.git/commitdiff
Display interaction data for Diaspora accounts
authorMichael <heluecht@pirati.ca>
Thu, 30 Dec 2021 22:11:52 +0000 (22:11 +0000)
committerMichael <heluecht@pirati.ca>
Thu, 30 Dec 2021 22:11:52 +0000 (22:11 +0000)
database.sql
doc/database/db_fcontact.md
src/Factory/Api/Mastodon/Account.php
src/Model/Contact.php
src/Model/FContact.php
src/Object/Api/Mastodon/Account.php
static/dbstructure.config.php

index 175c48b8c3f88e4c069e3627703c44dedd2cac93..4179932ad8d767ae859db57dd5573d8dcdcdcc5c 100644 (file)
@@ -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)),
index 51cfac7641bca0ce3694022ef1f53310a80c4ba8..77f8e0b85bd21814a6f52ea5daed92e587b6008d 100644 (file)
@@ -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
 ------------
index 7b7eba0ed5b539547e254a13ea6f113a109baebd..66662673a9659f00cae5fd631ae4e22a3ec15c56 100644 (file)
@@ -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);
        }
 
        /**
index 4898ec56783e57368fab52a946457eeacb5f9d1b..8c946502b190fd56c9e4de34bd6300c5320828e3 100644 (file)
@@ -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
         *
index 006a37a608510b3d3453a8ffb0ab4504d25ae94a..09ee3fb3d5dbb1d824e3a9376d8f206c46feb91f 100644 (file)
@@ -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"]];
index 2b95e06877e6401b4d5ed4cd1cc8c960a890446f..d1e26b8db2178d63cd4dbefe77c7b8c39729a097 100644 (file)
@@ -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;
index 3fbce08d40cf2e9a3c2a7f9ae414002ccef0d351..da9b8d38c2e5a363568027dd25ae2587e6df5ee4 100644 (file)
@@ -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"],