]> git.mxchange.org Git - friendica.git/commitdiff
Issue 14121: Show precise contact counts for own user
authorMichael <heluecht@pirati.ca>
Sun, 20 Oct 2024 17:46:01 +0000 (17:46 +0000)
committerMichael <heluecht@pirati.ca>
Sun, 20 Oct 2024 17:46:01 +0000 (17:46 +0000)
src/Content/Widget.php
src/Factory/Api/Mastodon/Account.php

index 184063a5f9ed56dc46c01b2ac83f2bca7e480e9d..6f030b640f6a2c1804bd04d93205febbb40a2006 100644 (file)
@@ -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
+}
index e73cd7c8e923e8103fec650f96c232dc1ce5cf3f..150082cfb0e287fb801c59cf7c818b6050ee3109 100644 (file)
@@ -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);
+       }
 }