]> git.mxchange.org Git - friendica.git/commitdiff
Fix comtact-relation follower calculation
authorMichael <heluecht@pirati.ca>
Sat, 17 Feb 2024 21:32:17 +0000 (21:32 +0000)
committerMichael <heluecht@pirati.ca>
Sat, 17 Feb 2024 21:32:17 +0000 (21:32 +0000)
database.sql
doc/database/db_contact-relation.md
src/Content/Widget.php
src/Model/Contact/Relation.php
static/dbstructure.config.php

index f1966a15e7d5673a31233b485adee13c4309e8ea..41ce810cf51e695eeb8866517d2bf35a2178e0b1 100644 (file)
@@ -1,6 +1,6 @@
 -- ------------------------------------------
 -- Friendica 2024.03-rc (Yellow Archangel)
--- DB_UPDATE_VERSION 1552
+-- DB_UPDATE_VERSION 1553
 -- ------------------------------------------
 
 
@@ -539,6 +539,7 @@ CREATE TABLE IF NOT EXISTS `contact-relation` (
        `relation-score` smallint unsigned COMMENT 'score for interactions of relation-cid on cid',
        `thread-score` smallint unsigned COMMENT 'score for interactions of cid on threads of relation-cid',
        `relation-thread-score` smallint unsigned COMMENT 'score for interactions of relation-cid on threads of cid',
+       `post-score` smallint unsigned COMMENT 'score for the amount of posts from cid that can be seen by relation-cid',
         PRIMARY KEY(`cid`,`relation-cid`),
         INDEX `relation-cid` (`relation-cid`),
        FOREIGN KEY (`cid`) REFERENCES `contact` (`id`) ON UPDATE RESTRICT ON DELETE CASCADE,
index f11fd95a0f5458d8ea3020d01886206dcf01f3ce..c83c9b8326eb40be0ab8379b2104665eb164612e 100644 (file)
@@ -6,17 +6,18 @@ Contact relations
 Fields
 ------
 
-| Field                 | Description                                              | Type              | Null | Key | Default             | Extra |
-| --------------------- | -------------------------------------------------------- | ----------------- | ---- | --- | ------------------- | ----- |
-| cid                   | contact the related contact had interacted with          | int unsigned      | NO   | PRI | 0                   |       |
-| relation-cid          | related contact who had interacted with the contact      | int unsigned      | NO   | PRI | 0                   |       |
-| last-interaction      | Date of the last interaction by relation-cid on cid      | datetime          | NO   |     | 0001-01-01 00:00:00 |       |
-| follow-updated        | Date of the last update of the contact relationship      | datetime          | NO   |     | 0001-01-01 00:00:00 |       |
-| follows               | if true, relation-cid follows cid                        | boolean           | NO   |     | 0                   |       |
-| score                 | score for interactions of cid on relation-cid            | smallint unsigned | YES  |     | NULL                |       |
-| relation-score        | score for interactions of relation-cid on cid            | smallint unsigned | YES  |     | NULL                |       |
-| thread-score          | score for interactions of cid on threads of relation-cid | smallint unsigned | YES  |     | NULL                |       |
-| relation-thread-score | score for interactions of relation-cid on threads of cid | smallint unsigned | YES  |     | NULL                |       |
+| Field                 | Description                                                             | Type              | Null | Key | Default             | Extra |
+| --------------------- | ----------------------------------------------------------------------- | ----------------- | ---- | --- | ------------------- | ----- |
+| cid                   | contact the related contact had interacted with                         | int unsigned      | NO   | PRI | 0                   |       |
+| relation-cid          | related contact who had interacted with the contact                     | int unsigned      | NO   | PRI | 0                   |       |
+| last-interaction      | Date of the last interaction by relation-cid on cid                     | datetime          | NO   |     | 0001-01-01 00:00:00 |       |
+| follow-updated        | Date of the last update of the contact relationship                     | datetime          | NO   |     | 0001-01-01 00:00:00 |       |
+| follows               | if true, relation-cid follows cid                                       | boolean           | NO   |     | 0                   |       |
+| score                 | score for interactions of cid on relation-cid                           | smallint unsigned | YES  |     | NULL                |       |
+| relation-score        | score for interactions of relation-cid on cid                           | smallint unsigned | YES  |     | NULL                |       |
+| thread-score          | score for interactions of cid on threads of relation-cid                | smallint unsigned | YES  |     | NULL                |       |
+| relation-thread-score | score for interactions of relation-cid on threads of cid                | smallint unsigned | YES  |     | NULL                |       |
+| post-score            | score for the amount of posts from cid that can be seen by relation-cid | smallint unsigned | YES  |     | NULL                |       |
 
 Indexes
 ------------
index 20d82cbe4c3bb120086ccf29a39ce135ed82a481..1bb2ec2b617e8d401b1bba546eb856c2fdaf83c1 100644 (file)
@@ -103,6 +103,7 @@ class Widget
        {
                // Always hide content from these networks
                $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET, Protocol::TWITTER, Protocol::ZOT];
+               Addon::loadAddons();
 
                if (!Addon::isEnabled("discourse")) {
                        $networks[] = Protocol::DISCOURSE;
index 217b5a896ed12e81674721613400e340e4bb95be..1fd250b3100221a775326234e36fe4b6d82eb69c 100644 (file)
@@ -22,6 +22,7 @@
 namespace Friendica\Model\Contact;
 
 use Exception;
+use Friendica\Content\Widget;
 use Friendica\Core\Logger;
 use Friendica\Core\Protocol;
 use Friendica\Database\Database;
@@ -78,14 +79,14 @@ class Relation
         */
        public static function discoverByUser(int $uid)
        {
-               $contact = Contact::selectFirst(['id', 'url', 'network'], ['uid' => $uid, 'self' => true]);
+               $contact = Contact::selectFirst(['id', 'url', 'network'], ['id' => Contact::getPublicIdByUserId($uid)]);
                if (empty($contact)) {
                        Logger::warning('Self contact for user not found', ['uid' => $uid]);
                        return;
                }
 
-               $followers = self::getContacts($uid, [Contact::FOLLOWER, Contact::FRIEND]);
-               $followings = self::getContacts($uid, [Contact::SHARING, Contact::FRIEND]);
+               $followers  = self::getContacts($uid, [Contact::FOLLOWER, Contact::FRIEND], false);
+               $followings = self::getContacts($uid, [Contact::SHARING, Contact::FRIEND], false);
 
                self::updateFollowersFollowings($contact, $followers, $followings);
        }
@@ -207,10 +208,11 @@ class Relation
         * Fetch contact url list from the given local user
         *
         * @param integer $uid
-        * @param array $rel
+        * @param array   $rel
+        * @param bool    $only_ap
         * @return array contact list
         */
-       private static function getContacts(int $uid, array $rel): array
+       private static function getContacts(int $uid, array $rel, bool $only_ap = true): array
        {
                $list = [];
                $profile = Profile::getByUID($uid);
@@ -219,15 +221,22 @@ class Relation
                }
 
                $condition = [
-                       'rel' => $rel,
-                       'uid' => $uid,
-                       'self' => false,
+                       'rel'     => $rel,
+                       'uid'     => $uid,
+                       'self'    => false,
                        'deleted' => false,
-                       'hidden' => false,
+                       'hidden'  => false,
                        'archive' => false,
                        'pending' => false,
+                       'blocked' => false,
+                       'failed'  => false,
                ];
-               $condition = DBA::mergeConditions($condition, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
+               if ($only_ap) {
+                       $condition = DBA::mergeConditions($condition, ["`url` IN (SELECT `url` FROM `apcontact`)"]);
+               } else {
+                       $networks  = Widget::unavailableNetworks();
+                       $condition = DBA::mergeConditions($condition, array_merge(["NOT `network` IN (" . substr(str_repeat("?, ", count($networks)), 0, -2) . ")"], $networks));
+               }
                $contacts = DBA::select('contact', ['url'], $condition);
                while ($contact = DBA::fetch($contacts)) {
                        $list[] = $contact['url'];
@@ -870,6 +879,20 @@ class Relation
                        DBA::update('contact-relation', ['thread-score' => $score], ['relation-cid' => $contact_id, 'cid' => $interaction['author-id']]);
                }
                DBA::close($interactions);
+
+               $total = DBA::fetchFirst("SELECT count(*) AS `posts` FROM `post-thread-user` WHERE EXISTS(SELECT `cid` FROM `contact-relation` WHERE `cid` = `post-thread-user`.`author-id` AND `relation-cid` = ? AND `follows`) AND `uid` = ? AND `created` > ?",
+                       $contact_id, $uid, DateTimeFormat::utc('now - ' . $days . ' day'));
+
+               Logger::debug('Calculate post-score', ['uid' => $uid, 'total' => $total['posts']]);
+
+               $posts = DBA::p("SELECT `author-id`, count(*) AS `posts` FROM `post-thread-user` WHERE EXISTS(SELECT `cid` FROM `contact-relation` WHERE `cid` = `post-thread-user`.`author-id` AND `relation-cid` = ? AND `follows`) AND `uid` = ? AND `created` > ? GROUP BY `author-id`",
+                       $contact_id, $uid, DateTimeFormat::utc('now - ' . $days . ' day'));
+               while ($post = DBA::fetch($posts)) {
+                       $score = min((int)(($post['posts'] / $total['posts']) * 65535), 65535);
+                       DBA::update('contact-relation', ['post-score' => $score], ['relation-cid' => $contact_id, 'cid' => $post['author-id']]);
+               }
+               DBA::close($posts);
+
                Logger::debug('Calculation - end', ['uid' => $uid]);
        }
 }
index 1b7f7a40deda11011f3e7d7abca22ed0e88b9fd9..7606d7b65f75e313590a72caf8be21db81d9c921 100644 (file)
@@ -56,7 +56,7 @@ use Friendica\Database\DBA;
 
 // This file is required several times during the test in DbaDefinition which justifies this condition
 if (!defined('DB_UPDATE_VERSION')) {
-       define('DB_UPDATE_VERSION', 1552);
+       define('DB_UPDATE_VERSION', 1553);
 }
 
 return [
@@ -598,6 +598,7 @@ return [
                        "relation-score" => ["type" => "smallint unsigned", "comment" => "score for interactions of relation-cid on cid"],
                        "thread-score" => ["type" => "smallint unsigned", "comment" => "score for interactions of cid on threads of relation-cid"],
                        "relation-thread-score" => ["type" => "smallint unsigned", "comment" => "score for interactions of relation-cid on threads of cid"],
+                       "post-score" => ["type" => "smallint unsigned", "comment" => "score for the amount of posts from cid that can be seen by relation-cid"],
                ],
                "indexes" => [
                        "PRIMARY" => ["cid", "relation-cid"],