]> git.mxchange.org Git - friendica.git/commitdiff
Combine getFollowers and getFollowing into getContacts in ActivityPub\Transmitter
authorHypolite Petovan <hypolite@mrpetovan.com>
Wed, 6 May 2020 02:32:45 +0000 (22:32 -0400)
committerHypolite Petovan <hypolite@mrpetovan.com>
Wed, 6 May 2020 02:32:45 +0000 (22:32 -0400)
src/Module/Followers.php
src/Module/Following.php
src/Protocol/ActivityPub/Transmitter.php

index 8e683e5623c777ea87b2ed717412c030444fbb1d..bcf4bb78291d992f0473734ae7574b88ff44ad5f 100644 (file)
@@ -22,8 +22,8 @@
 namespace Friendica\Module;
 
 use Friendica\BaseModule;
-use Friendica\Core\System;
 use Friendica\DI;
+use Friendica\Model\Contact;
 use Friendica\Model\User;
 use Friendica\Protocol\ActivityPub;
 
@@ -49,7 +49,7 @@ class Followers extends BaseModule
 
                $page = $_REQUEST['page'] ?? null;
 
-               $followers = ActivityPub\Transmitter::getFollowers($owner, $page);
+               $followers = ActivityPub\Transmitter::getContacts($owner, [Contact::FOLLOWER, Contact::FRIEND], 'followers', $page);
 
                header('Content-Type: application/activity+json');
                echo json_encode($followers);
index 30f47b5986746fe794602bb9738e3c394e739e79..c2a765d74c4f80a533c3c552c6d02740d9be98ca 100644 (file)
@@ -22,8 +22,8 @@
 namespace Friendica\Module;
 
 use Friendica\BaseModule;
-use Friendica\Core\System;
 use Friendica\DI;
+use Friendica\Model\Contact;
 use Friendica\Model\User;
 use Friendica\Protocol\ActivityPub;
 
@@ -49,10 +49,10 @@ class Following extends BaseModule
 
                $page = $_REQUEST['page'] ?? null;
 
-               $Following = ActivityPub\Transmitter::getFollowing($owner, $page);
+               $following = ActivityPub\Transmitter::getContacts($owner, [Contact::SHARING, Contact::FRIEND], 'following', $page);
 
                header('Content-Type: application/activity+json');
-               echo json_encode($Following);
+               echo json_encode($following);
                exit();
        }
 }
index 16b7b039a9cfdab343e03d6ea5087a4103dcfc9c..b393abd9e1798d331120b84bc72d07cd0185fa4b 100644 (file)
@@ -62,72 +62,26 @@ require_once 'mod/share.php';
 class Transmitter
 {
        /**
-        * collects the lost of followers of the given owner
+        * Collects a list of contacts of the given owner
         *
-        * @param array   $owner Owner array
-        * @param integer $page  Page number
+        * @param array     $owner  Owner array
+        * @param int|array $rel    The relevant value(s) contact.rel should match
+        * @param string    $module The name of the relevant AP endpoint module (followers|following)
+        * @param integer   $page   Page number
         *
         * @return array of owners
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
+        * @throws \Exception
         */
-       public static function getFollowers($owner, $page = null)
+       public static function getContacts($owner, $rel, $module, $page = null)
        {
-               $condition = ['rel' => [Contact::FOLLOWER, Contact::FRIEND], 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'],
+               $condition = ['rel' => $rel, 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'],
                        'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
                $count = DBA::count('contact', $condition);
 
-               $data = ['@context' => ActivityPub::CONTEXT];
-               $data['id'] = DI::baseUrl() . '/followers/' . $owner['nickname'];
-               $data['type'] = 'OrderedCollection';
-               $data['totalItems'] = $count;
-
-               // When we hide our friends we will only show the pure number but don't allow more.
-               $profile = Profile::getByUID($owner['uid']);
-               if (!empty($profile['hide-friends'])) {
-                       return $data;
-               }
-
-               if (empty($page)) {
-                       $data['first'] = DI::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1';
-               } else {
-                       $data['type'] = 'OrderedCollectionPage';
-                       $list = [];
-
-                       $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]);
-                       while ($contact = DBA::fetch($contacts)) {
-                               $list[] = $contact['url'];
-                       }
-                       DBA::close($contacts);
-
-                       if (!empty($list)) {
-                               $data['next'] = DI::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1);
-                       }
-
-                       $data['partOf'] = DI::baseUrl() . '/followers/' . $owner['nickname'];
-
-                       $data['orderedItems'] = $list;
-               }
-
-               return $data;
-       }
-
-       /**
-        * Create list of following contacts
-        *
-        * @param array   $owner Owner array
-        * @param integer $page  Page numbe
-        *
-        * @return array of following contacts
-        * @throws \Friendica\Network\HTTPException\InternalServerErrorException
-        */
-       public static function getFollowing($owner, $page = null)
-       {
-               $condition = ['rel' => [Contact::SHARING, Contact::FRIEND], 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'],
-                       'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false];
-               $count = DBA::count('contact', $condition);
+               $modulePath = '/' . $module . '/';
 
                $data = ['@context' => ActivityPub::CONTEXT];
-               $data['id'] = DI::baseUrl() . '/following/' . $owner['nickname'];
+               $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname'];
                $data['type'] = 'OrderedCollection';
                $data['totalItems'] = $count;
 
@@ -138,7 +92,7 @@ class Transmitter
                }
 
                if (empty($page)) {
-                       $data['first'] = DI::baseUrl() . '/following/' . $owner['nickname'] . '?page=1';
+                       $data['first'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=1';
                } else {
                        $data['type'] = 'OrderedCollectionPage';
                        $list = [];
@@ -150,10 +104,10 @@ class Transmitter
                        DBA::close($contacts);
 
                        if (!empty($list)) {
-                               $data['next'] = DI::baseUrl() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1);
+                               $data['next'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=' . ($page + 1);
                        }
 
-                       $data['partOf'] = DI::baseUrl() . '/following/' . $owner['nickname'];
+                       $data['partOf'] = DI::baseUrl() . $modulePath . $owner['nickname'];
 
                        $data['orderedItems'] = $list;
                }