]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Profile/Contacts.php
Merge pull request #10987 from annando/api4
[friendica.git] / src / Module / Profile / Contacts.php
index 4cd97b4097e914ea3e8ca24099e6e0c249be8ccc..e20fd3f2d5451d0628c7694975b3d16d6cd567be 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 /**
- * @copyright Copyright (C) 2020, Friendica
+ * @copyright Copyright (C) 2010-2021, the Friendica project
  *
  * @license GNU AGPL version 3 or any later version
  *
@@ -21,7 +21,6 @@
 
 namespace Friendica\Module\Profile;
 
-use Friendica\Content\ContactSelector;
 use Friendica\Content\Nav;
 use Friendica\Content\Pager;
 use Friendica\Core\Protocol;
@@ -29,120 +28,96 @@ use Friendica\Core\Renderer;
 use Friendica\Core\Session;
 use Friendica\Database\DBA;
 use Friendica\DI;
-use Friendica\Model\Contact;
-use Friendica\Model\Profile;
-use Friendica\Module\BaseProfile;
-use Friendica\Util\Proxy as ProxyUtils;
+use Friendica\Model;
+use Friendica\Module;
+use Friendica\Network\HTTPException;
 
-class Contacts extends BaseProfile
+class Contacts extends Module\BaseProfile
 {
-       public static function content(array $parameters = [])
+       public function content(): string
        {
                if (DI::config()->get('system', 'block_public') && !Session::isAuthenticated()) {
-                       throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
+                       throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
                }
 
                $a = DI::app();
 
-               //@TODO: Get value from router parameters
-               $nickname = $a->argv[1];
-               $type = ($a->argv[3] ?? '') ?: 'all';
+               $nickname = $this->parameters['nickname'];
+               $type = $this->parameters['type'] ?? 'all';
 
-               Nav::setSelected('home');
-
-               $user = DBA::selectFirst('user', [], ['nickname' => $nickname, 'blocked' => false]);
-               if (!DBA::isResult($user)) {
-                       throw new \Friendica\Network\HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
+               $profile = Model\Profile::load($a, $nickname);
+               if (empty($profile)) {
+                       throw new HTTPException\NotFoundException(DI::l10n()->t('User not found.'));
                }
 
-               $a->profile_uid  = $user['uid'];
+               $is_owner = $profile['uid'] == local_user();
 
-               Profile::load($a, $nickname);
+               if ($profile['hide-friends'] && !$is_owner) {
+                       throw new HTTPException\ForbiddenException(DI::l10n()->t('Permission denied.'));
+               }
 
-               $is_owner = $a->profile['uid'] == local_user();
+               Nav::setSelected('home');
 
-               $o = self::getTabsHTML($a, 'contacts', $is_owner, $nickname);
+               $o = self::getTabsHTML($a, 'contacts', $is_owner, $profile['nickname'], $profile['hide-friends']);
 
-               if (!count($a->profile) || $a->profile['hide-friends']) {
-                       notice(DI::l10n()->t('Permission denied.'));
-                       return $o;
-               }
+               $tabs = self::getContactFilterTabs('profile/' . $nickname, $type, Session::isAuthenticated() && $profile['uid'] != local_user());
 
                $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]
                ];
 
                switch ($type) {
-                       case 'followers': $condition['rel'] = [1, 3]; break;
-                       case 'following': $condition['rel'] = [2, 3]; break;
-                       case 'mutuals': $condition['rel'] = 3; 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);
 
-               $pager = new Pager(DI::l10n(), DI::args()->getQueryString());
+               $pager = new Pager(DI::l10n(), DI::args()->getQueryString(), 30);
 
                $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
 
-               $contacts_stmt = DBA::select('contact', [], $condition, $params);
-
-               if (!DBA::isResult($contacts_stmt)) {
-                       notice(DI::l10n()->t('No contacts.'));
-                       return $o;
-               }
-
-               $contacts = [];
-
-               while ($contact = DBA::fetch($contacts_stmt)) {
-                       if ($contact['self']) {
-                               continue;
-                       }
-
-                       $contact_details = Contact::getByURLForUser($contact['url'], $a->profile['uid']) ?: $contact;
-
-                       $contacts[] = [
-                               'id'           => $contact['id'],
-                               'img_hover'    => DI::l10n()->t('Visit %s\'s profile [%s]', $contact_details['name'], $contact['url']),
-                               'photo_menu'   => Contact::photoMenu($contact),
-                               'thumb'        => ProxyUtils::proxifyUrl($contact_details['thumb'], false, ProxyUtils::SIZE_THUMB),
-                               'name'         => substr($contact_details['name'], 0, 20),
-                               'username'     => $contact_details['name'],
-                               'details'      => $contact_details['location'],
-                               'tags'         => $contact_details['keywords'],
-                               'about'        => $contact_details['about'],
-                               'account_type' => Contact::getAccountType($contact_details),
-                               'url'          => Contact::magicLink($contact['url']),
-                               'sparkle'      => '',
-                               'itemurl'      => $contact_details['addr'] ? : $contact['url'],
-                               'network'      => ContactSelector::networkToName($contact['network'], $contact['url'], $contact['protocol']),
-                       ];
-               }
-
-               DBA::close($contacts_stmt);
+               $contacts = array_map(
+                       [Module\Contact::class, 'getContactTemplateVars'],
+                       Model\Contact::selectToArray([], $condition, $params)
+               );
 
+               $desc = '';
                switch ($type) {
-                       case 'followers':    $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total); break;
-                       case 'following':    $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total); break;
-                       case 'mutuals':      $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total); break;
-
-                       case 'all': default: $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total); break;
+                       case 'followers':
+                               $title = DI::l10n()->tt('Follower (%s)', 'Followers (%s)', $total);
+                               break;
+                       case 'following':
+                               $title = DI::l10n()->tt('Following (%s)', 'Following (%s)', $total);
+                               break;
+                       case 'mutuals':
+                               $title = DI::l10n()->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
+                               $desc = DI::l10n()->t(
+                                       'These contacts both follow and are followed by <strong>%s</strong>.',
+                                       htmlentities($profile['name'], ENT_COMPAT, 'UTF-8')
+                               );
+                               break;
+                       case 'all':
+                       default:
+                               $title = DI::l10n()->tt('Contact (%s)', 'Contacts (%s)', $total);
+                               break;
                }
 
                $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
                $o .= Renderer::replaceMacros($tpl, [
                        '$title'    => $title,
-                       '$nickname' => $nickname,
-                       '$type'     => $type,
+                       '$desc'     => $desc,
+                       '$tabs'     => $tabs,
 
-                       '$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'),
+                       '$noresult_label'  => DI::l10n()->t('No contacts.'),
 
                        '$contacts' => $contacts,
                        '$paginate' => $pager->renderFull($total),