]> git.mxchange.org Git - friendica.git/blobdiff - src/Model/Contact/Relation.php
Unneeded variable removed
[friendica.git] / src / Model / Contact / Relation.php
index 5616148fa0183e2d774d3c70f585c80218faced9..d0be33cca6c2cf3f833e0d8445cdca81802ddb32 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2010-2022, the Friendica project
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -68,6 +68,26 @@ class Relation
                DBA::insert('contact-relation', ['last-interaction' => $interaction_date, 'cid' => $target, 'relation-cid' => $actor], Database::INSERT_UPDATE);
        }
 
+       /**
+        * Fetch the followers of a given user
+        *
+        * @param integer $uid User ID
+        * @return void
+        */
+       public static function discoverByUser(int $uid)
+       {
+               $contact = Contact::selectFirst(['id', 'url', 'network'], ['uid' => $uid, 'self' => true]);
+               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]);
+
+               self::updateFollowersFollowings($contact, $followers, $followings);
+       }
+
        /**
         * Fetches the followers of a given profile and adds them
         *
@@ -113,13 +133,27 @@ class Relation
                        $followings = [];
                }
 
+               self::updateFollowersFollowings($contact, $followers, $followings);
+       }
+
+       /**
+        * Update followers and followings for the given contact
+        *
+        * @param array $contact
+        * @param array $followers
+        * @param array $followings
+        * @return void
+        */
+       private static function updateFollowersFollowings(array $contact, array $followers, array $followings)
+       {
                if (empty($followers) && empty($followings)) {
                        Contact::update(['last-discovery' => DateTimeFormat::utcNow()], ['id' => $contact['id']]);
-                       Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $url, 'network' => $contact['network']]);
+                       Logger::info('The contact does not offer discoverable data', ['id' => $contact['id'], 'url' => $contact['url'], 'network' => $contact['network']]);
                        return;
                }
 
                $target = $contact['id'];
+               $url    = $contact['url'];
 
                if (!empty($followers)) {
                        // Clear the follower list, since it will be recreated in the next step
@@ -396,7 +430,7 @@ class Relation
                        ["`nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` = ?)
                        AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)
                        AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
-                       $uid, Contact::FOLLOWER, 0, 
+                       $uid, Contact::FOLLOWER, 0,
                        Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid],
                        ['order' => ['last-item' => true], 'limit' => $totallimit]
                );
@@ -417,7 +451,7 @@ class Relation
                        ["NOT `nurl` IN (SELECT `nurl` FROM `contact` WHERE `uid` = ? AND `rel` IN (?, ?) AND `nurl` = `nurl`)
                        AND NOT `hidden` AND `uid` = ? AND `network` IN (?, ?, ?, ?)
                        AND NOT `uri-id` IN (SELECT `uri-id` FROM `account-suggestion` WHERE `uri-id` = `contact`.`uri-id` AND `uid` = ?)",
-                       $uid, Contact::FRIEND, Contact::SHARING, 0, 
+                       $uid, Contact::FRIEND, Contact::SHARING, 0,
                        Protocol::ACTIVITYPUB, Protocol::DFRN, $diaspora, $ostatus, $uid],
                        ['order' => ['last-item' => true], 'limit' => $totallimit]
                );
@@ -443,7 +477,7 @@ class Relation
        public static function countFollows(int $cid, array $condition = []): int
        {
                $condition = DBA::mergeConditions($condition, [
-                       '`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)', 
+                       '`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
                        $cid,
                ]);
 
@@ -464,7 +498,7 @@ class Relation
        public static function listFollows(int $cid, array $condition = [], int $count = 30, int $offset = 0, bool $shuffle = false)
        {
                $condition = DBA::mergeConditions($condition,
-                       ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)', 
+                       ['`id` IN (SELECT `relation-cid` FROM `contact-relation` WHERE `cid` = ? AND `follows`)',
                        $cid]
                );