]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Profile/Contacts.php
Merge remote-tracking branch 'upstream/2023.03-rc' into npf2
[friendica.git] / src / Module / Profile / Contacts.php
index 938dbd3d05ec943d2bf7a28679ae3d2d0a38ae07..dde138d8cc93c9a60a956d4c2628da4363533f74 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2023, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
 
 namespace Friendica\Module\Profile;
 
+use Friendica\App;
 use Friendica\Content\Nav;
 use Friendica\Content\Pager;
+use Friendica\Core\Config\Capability\IManageConfigValues;
+use Friendica\Core\L10n;
 use Friendica\Core\Protocol;
 use Friendica\Core\Renderer;
-use Friendica\Core\Session;
-use Friendica\Database\DBA;
-use Friendica\DI;
+use Friendica\Core\Session\Capability\IHandleUserSessions;
+use Friendica\Database\Database;
 use Friendica\Model;
 use Friendica\Module;
+use Friendica\Module\Response;
 use Friendica\Network\HTTPException;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
 
 class Contacts extends Module\BaseProfile
 {
-       public static function content(array $parameters = [])
+       /** @var IManageConfigValues */
+       private $config;
+       /** @var IHandleUserSessions */
+       private $userSession;
+       /** @var App */
+       private $app;
+       /** @var Database */
+       private $database;
+
+       public function __construct(Database $database, App $app, IHandleUserSessions $userSession, IManageConfigValues $config, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
        {
-               if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
-                       throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
-               }
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
 
-               $a = DI::app();
+               $this->config      = $config;
+               $this->userSession = $userSession;
+               $this->app         = $app;
+               $this->database    = $database;
+       }
 
-               $nickname = $parameters['nickname'];
-               $type = $parameters['type'] ?? 'all';
+       protected function content(array $request = []): string
+       {
+               if ($this->config->get('system', 'block_public') && !$this->userSession->isAuthenticated()) {
+                       throw new HTTPException\NotFoundException($this->t('User not found.'));
+               }
 
-               Model\Profile::load($a, $nickname);
+               $nickname = $this->parameters['nickname'];
+               $type     = $this->parameters['type'] ?? 'all';
 
-               if (empty($a->profile)) {
-                       throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
+               $profile = Model\Profile::load($this->app, $nickname);
+               if (empty($profile)) {
+                       throw new HTTPException\NotFoundException($this->t('User not found.'));
                }
 
-               if (!empty($a->profile['hide-friends'])) {
-                       throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
+               $is_owner = $profile['uid'] == $this->userSession->getLocalUserId();
+
+               if ($profile['hide-friends'] && !$is_owner) {
+                       throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
                }
 
                Nav::setSelected('home');
 
-               $is_owner = $a->profile['uid'] == local_user();
+               $o = self::getTabsHTML('contacts', $is_owner, $profile['nickname'], $profile['hide-friends']);
 
-               $o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname);
+               $tabs = self::getContactFilterTabs('profile/' . $nickname, $type, $this->userSession->isAuthenticated() && $profile['uid'] != $this->userSession->getLocalUserId());
 
                $condition = [
-                       'uid'     => $a->profile['uid'],
+                       'uid'     => $profile['uid'],
                        'blocked' => false,
                        'pending' => false,
                        'hidden'  => false,
                        'archive' => false,
+                       'failed'  => false,
                        'self'    => false,
-                       'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
+                       'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]
                ];
 
                switch ($type) {
-                       case 'followers': $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND]; break;
-                       case 'following': $condition['rel'] = [Model\Contact::SHARING,  Model\Contact::FRIEND]; break;
-                       case 'mutuals':   $condition['rel'] = Model\Contact::FRIEND; break;
+                       case 'followers':
+                               $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND];
+                               break;
+                       case 'following':
+                               $condition['rel'] = [Model\Contact::SHARING, Model\Contact::FRIEND];
+                               break;
+                       case 'mutuals':
+                               $condition['rel'] = Model\Contact::FRIEND;
+                               break;
                }
 
-               $total = DBA::count('contact', $condition);
+               $total = $this->database->count('contact', $condition);
 
-               $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
+               $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
 
                $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
 
+               // Contact list is obtained from the visited profile user, but the contact display is visitor dependent
                $contacts = array_map(
-                       [Module\Contact::class, 'getContactTemplateVars'],
-                       Model\Contact::selectToArray([], $condition, $params)
+                       function ($contact) {
+                               $contact = Model\Contact::selectFirst(
+                                       [],
+                                       ['uri-id' => $contact['uri-id'], 'uid' => [0, $this->userSession->getLocalUserId()]],
+                                       ['order' => ['uid' => 'DESC']]
+                               );
+                               return Module\Contact::getContactTemplateVars($contact);
+                       },
+                       Model\Contact::selectToArray(['uri-id'], $condition, $params)
                );
 
                $desc = '';
                switch ($type) {
                        case 'followers':
-                               $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
+                               $title = $this->tt('Follower (%s)', 'Followers (%s)', $total);
                                break;
                        case 'following':
-                               $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total);
+                               $title = $this->tt('Following (%s)', 'Following (%s)', $total);
                                break;
                        case 'mutuals':
-                               $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
-                               $desc = DI::l10n()->t(
+                               $title = $this->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
+                               $desc  = $this->t(
                                        'These contacts both follow and are followed by <strong>%s</strong>.',
-                                       htmlentities($a->profile['name'], ENT_COMPAT, 'UTF-8')
+                                       htmlentities($profile['name'], ENT_COMPAT, 'UTF-8')
                                );
                                break;
                        case 'all':
                        default:
-                               $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total);
+                               $title = $this->tt('Contact (%s)', 'Contacts (%s)', $total);
                                break;
                }
 
                $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
-               $o .= Renderer::replaceMacros($tpl, [
-                       '$title'    => $title,
-                       '$desc'     => $desc,
-                       '$nickname' => $nickname,
-                       '$type'     => $type,
-                       '$displayCommonTab' => Session::isAuthenticated() && $a->profile['uid'] != local_user(),
-
-                       '$all_label'       => DI::l10n()->t('All contacts'),
-                       '$followers_label' => DI::l10n()->t('Followers'),
-                       '$following_label' => DI::l10n()->t('Following'),
-                       '$mutuals_label'   => DI::l10n()->t('Mutual friends'),
-                       '$common_label'    => DI::l10n()->t('Common'),
-                       '$noresult_label'  => DI::l10n()->t('No contacts.'),
+               $o   .= Renderer::replaceMacros($tpl, [
+                       '$title' => $title,
+                       '$desc'  => $desc,
+                       '$tabs'  => $tabs,
+
+                       '$noresult_label' => $this->t('No contacts.'),
 
                        '$contacts' => $contacts,
                        '$paginate' => $pager->renderFull($total),