From ccb69414d26e2d645396ff9ca522d4cb9c7583d6 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 5 May 2020 22:32:45 -0400 Subject: [PATCH 1/3] Combine getFollowers and getFollowing into getContacts in ActivityPub\Transmitter --- src/Module/Followers.php | 4 +- src/Module/Following.php | 6 +- src/Protocol/ActivityPub/Transmitter.php | 74 +++++------------------- 3 files changed, 19 insertions(+), 65 deletions(-) diff --git a/src/Module/Followers.php b/src/Module/Followers.php index 8e683e5623..bcf4bb7829 100644 --- a/src/Module/Followers.php +++ b/src/Module/Followers.php @@ -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); diff --git a/src/Module/Following.php b/src/Module/Following.php index 30f47b5986..c2a765d74c 100644 --- a/src/Module/Following.php +++ b/src/Module/Following.php @@ -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(); } } diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 16b7b039a9..b393abd9e1 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -62,22 +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); + $modulePath = '/' . $module . '/'; + $data = ['@context' => ActivityPub::CONTEXT]; - $data['id'] = DI::baseUrl() . '/followers/' . $owner['nickname']; + $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname']; $data['type'] = 'OrderedCollection'; $data['totalItems'] = $count; @@ -88,7 +92,7 @@ class Transmitter } if (empty($page)) { - $data['first'] = DI::baseUrl() . '/followers/' . $owner['nickname'] . '?page=1'; + $data['first'] = DI::baseUrl() . $modulePath . $owner['nickname'] . '?page=1'; } else { $data['type'] = 'OrderedCollectionPage'; $list = []; @@ -100,60 +104,10 @@ class Transmitter DBA::close($contacts); if (!empty($list)) { - $data['next'] = DI::baseUrl() . '/followers/' . $owner['nickname'] . '?page=' . ($page + 1); + $data['next'] = DI::baseUrl() . $modulePath . $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); - - $data = ['@context' => ActivityPub::CONTEXT]; - $data['id'] = DI::baseUrl() . '/following/' . $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() . '/following/' . $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() . '/following/' . $owner['nickname'] . '?page=' . ($page + 1); - } - - $data['partOf'] = DI::baseUrl() . '/following/' . $owner['nickname']; + $data['partOf'] = DI::baseUrl() . $modulePath . $owner['nickname']; $data['orderedItems'] = $list; } From 4e579e77f5738a2f39924a12c1cf4c7a2e148bf0 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Tue, 5 May 2020 22:33:26 -0400 Subject: [PATCH 2/3] Only output ActivityPub contacts in /followers and /following - Join contact table with apcontact to weed out non-AP contacts --- src/Protocol/ActivityPub/Transmitter.php | 33 ++++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index b393abd9e1..84a6d61be6 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -74,16 +74,30 @@ class Transmitter */ public static function getContacts($owner, $rel, $module, $page = null) { - $condition = ['rel' => $rel, 'network' => Protocol::FEDERATED, 'uid' => $owner['uid'], - 'self' => false, 'deleted' => false, 'hidden' => false, 'archive' => false, 'pending' => false]; - $count = DBA::count('contact', $condition); + $parameters = [ + 'rel' => $rel, + 'uid' => $owner['uid'], + 'self' => false, + 'deleted' => false, + 'hidden' => false, + 'archive' => false, + 'pending' => false + ]; + $condition = DBA::buildCondition($parameters); + + $sql = "SELECT COUNT(*) as `count` + FROM `contact` + JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url` + " . $condition; + + $contacts = DBA::fetchFirst($sql, ...$parameters); $modulePath = '/' . $module . '/'; $data = ['@context' => ActivityPub::CONTEXT]; $data['id'] = DI::baseUrl() . $modulePath . $owner['nickname']; $data['type'] = 'OrderedCollection'; - $data['totalItems'] = $count; + $data['totalItems'] = $contacts['count']; // When we hide our friends we will only show the pure number but don't allow more. $profile = Profile::getByUID($owner['uid']); @@ -97,7 +111,16 @@ class Transmitter $data['type'] = 'OrderedCollectionPage'; $list = []; - $contacts = DBA::select('contact', ['url'], $condition, ['limit' => [($page - 1) * 100, 100]]); + $sql = "SELECT `contact`.`url` + FROM `contact` + JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url` + " . $condition . " + LIMIT ?, ?"; + + $parameters[] = ($page - 1) * 100; + $parameters[] = 100; + + $contacts = DBA::p($sql, ...$parameters); while ($contact = DBA::fetch($contacts)) { $list[] = $contact['url']; } From 63e799689171b645ab616916e29725969be78bb1 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Thu, 7 May 2020 03:47:45 -0400 Subject: [PATCH 3/3] Improve formatting in ActivityPub\Transmitter --- src/Protocol/ActivityPub/Transmitter.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index 84a6d61be6..0dfef3ebde 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -86,9 +86,9 @@ class Transmitter $condition = DBA::buildCondition($parameters); $sql = "SELECT COUNT(*) as `count` - FROM `contact` - JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url` - " . $condition; + FROM `contact` + JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url` + " . $condition; $contacts = DBA::fetchFirst($sql, ...$parameters); @@ -112,10 +112,10 @@ class Transmitter $list = []; $sql = "SELECT `contact`.`url` - FROM `contact` - JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url` - " . $condition . " - LIMIT ?, ?"; + FROM `contact` + JOIN `apcontact` ON `apcontact`.`url` = `contact`.`url` + " . $condition . " + LIMIT ?, ?"; $parameters[] = ($page - 1) * 100; $parameters[] = 100;