From 94ef7c55c0ea0612812bca8ac625b6e2b5d3ab2d Mon Sep 17 00:00:00 2001 From: Michael Date: Sun, 20 Oct 2024 17:46:01 +0000 Subject: [PATCH] Issue 14121: Show precise contact counts for own user --- src/Content/Widget.php | 50 ++++++++++++++++++++++++---- src/Factory/Api/Mastodon/Account.php | 29 ++++++++++++++-- 2 files changed, 69 insertions(+), 10 deletions(-) diff --git a/src/Content/Widget.php b/src/Content/Widget.php index 184063a5f9..6f030b640f 100644 --- a/src/Content/Widget.php +++ b/src/Content/Widget.php @@ -91,23 +91,59 @@ class Widget $networks = [Protocol::PHANTOM, Protocol::FACEBOOK, Protocol::APPNET, Protocol::TWITTER, Protocol::ZOT, Protocol::OSTATUS, Protocol::STATUSNET]; Addon::loadAddons(); - if (!Addon::isEnabled("discourse")) { + if (!Addon::isEnabled('discourse')) { $networks[] = Protocol::DISCOURSE; } - if (!Addon::isEnabled("pumpio")) { + if (!Addon::isEnabled('pumpio')) { $networks[] = Protocol::PUMPIO; } - if (!Addon::isEnabled("tumblr")) { + if (!Addon::isEnabled('tumblr')) { $networks[] = Protocol::TUMBLR; } - if (!DI::config()->get("system", "diaspora_enabled")) { + if (!DI::config()->get('system', 'diaspora_enabled')) { $networks[] = Protocol::DIASPORA; } - if (!Addon::isEnabled("pnut")) { + if (!Addon::isEnabled('pnut')) { + $networks[] = Protocol::PNUT; + } + return $networks; + } + + /** + * Return available networks as array + * + * @return array Supported networks + */ + public static function availableNetworks(): array + { + $networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::FEED]; + Addon::loadAddons(); + + if (Addon::isEnabled('discourse')) { + $networks[] = Protocol::DISCOURSE; + } + + if (Addon::isEnabled('pumpio')) { + $networks[] = Protocol::PUMPIO; + } + + if (Addon::isEnabled('tumblr')) { + $networks[] = Protocol::TUMBLR; + } + + if (DI::config()->get('system', 'diaspora_enabled')) { + $networks[] = Protocol::DIASPORA; + } + + if (function_exists('imap_open') && !DI::config()->get('system', 'imap_disabled')) { + $networks[] = Protocol::MAIL; + } + + if (Addon::isEnabled('pnut')) { $networks[] = Protocol::PNUT; } return $networks; @@ -382,7 +418,7 @@ class Widget $tpl = Renderer::getMarkupTemplate('widget/remote_friends_common.tpl'); return Renderer::replaceMacros($tpl, [ - '$desc' => DI::l10n()->tt("%d contact in common", "%d contacts in common", $total), + '$desc' => DI::l10n()->tt('%d contact in common', '%d contacts in common', $total), '$base' => DI::baseUrl(), '$nickname' => $nickname, '$linkmore' => $total > 5 ? 'true' : '', @@ -576,4 +612,4 @@ class Widget $channelname ); } -} \ No newline at end of file +} diff --git a/src/Factory/Api/Mastodon/Account.php b/src/Factory/Api/Mastodon/Account.php index e73cd7c8e9..150082cfb0 100644 --- a/src/Factory/Api/Mastodon/Account.php +++ b/src/Factory/Api/Mastodon/Account.php @@ -10,8 +10,10 @@ namespace Friendica\Factory\Api\Mastodon; use Friendica\App\BaseURL; use Friendica\BaseFactory; use Friendica\Collection\Api\Mastodon\Fields; +use Friendica\Content\Widget; use Friendica\Database\DBA; use Friendica\Model\Contact; +use Friendica\Model\User; use Friendica\Network\HTTPException; use Friendica\Profile\ProfileField\Repository\ProfileField as ProfileFieldRepository; use ImagickException; @@ -75,10 +77,15 @@ class Account extends BaseFactory $fields = new Fields(); if (Contact::isLocal($account['url'])) { - $self_contact = Contact::selectFirst(['uid'], ['nurl' => $account['nurl'], 'self' => true]); - if (!empty($self_contact['uid'])) { - $profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($self_contact['uid']); + $profile_uid = User::getIdForContactId($account['id']); + if ($profile_uid) { + $profileFields = $this->profileFieldRepo->selectPublicFieldsByUserId($profile_uid); $fields = $this->mstdnFieldFactory->createFromProfileFields($profileFields); + + if ($profile_uid == $uid) { + $account['ap-followers_count'] = $this->getContactRelationCountForUid($uid, [Contact::FOLLOWER, Contact::FRIEND]); + $account['ap-following_count'] = $this->getContactRelationCountForUid($uid, [Contact::SHARING, Contact::FRIEND]); + } } } @@ -98,4 +105,20 @@ class Account extends BaseFactory return new \Friendica\Object\Api\Mastodon\Account($this->baseUrl, $account, $fields); } + + private function getContactRelationCountForUid(int $uid, array $rel): int + { + $condition = [ + 'uid' => $uid, + 'rel' => $rel, + 'self' => false, + 'deleted' => false, + 'archive' => false, + 'pending' => false, + 'blocked' => false, + 'network' => Widget::availableNetworks(), + ]; + + return DBA::count('contact', $condition); + } }