3 * @copyright Copyright (C) 2010-2023, the Friendica project
5 * @license GNU AGPL version 3 or any later version
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Affero General Public License for more details.
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program. If not, see <https://www.gnu.org/licenses/>.
22 namespace Friendica\Module\Profile;
25 use Friendica\Content\Nav;
26 use Friendica\Content\Pager;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\L10n;
29 use Friendica\Core\Protocol;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Session\Capability\IHandleUserSessions;
32 use Friendica\Database\Database;
35 use Friendica\Module\Response;
36 use Friendica\Network\HTTPException;
37 use Friendica\Util\Profiler;
38 use Psr\Log\LoggerInterface;
40 class Contacts extends Module\BaseProfile
42 /** @var IManageConfigValues */
44 /** @var IHandleUserSessions */
51 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 = [])
53 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
55 $this->config = $config;
56 $this->userSession = $userSession;
58 $this->database = $database;
61 protected function content(array $request = []): string
63 if ($this->config->get('system', 'block_public') && !$this->userSession->isAuthenticated()) {
64 throw new HTTPException\NotFoundException($this->t('User not found.'));
67 $nickname = $this->parameters['nickname'];
68 $type = $this->parameters['type'] ?? 'all';
70 $profile = Model\Profile::load($this->app, $nickname);
71 if (empty($profile)) {
72 throw new HTTPException\NotFoundException($this->t('User not found.'));
75 $is_owner = $profile['uid'] == $this->userSession->getLocalUserId();
77 if ($profile['hide-friends'] && !$is_owner) {
78 throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
81 Nav::setSelected('home');
83 $o = self::getTabsHTML('contacts', $is_owner, $profile['nickname'], $profile['hide-friends']);
85 $tabs = self::getContactFilterTabs('profile/' . $nickname, $type, $this->userSession->isAuthenticated() && $profile['uid'] != $this->userSession->getLocalUserId());
88 'uid' => $profile['uid'],
95 'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS]
100 $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND];
103 $condition['rel'] = [Model\Contact::SHARING, Model\Contact::FRIEND];
106 $condition['rel'] = Model\Contact::FRIEND;
110 $total = $this->database->count('contact', $condition);
112 $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
114 $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
116 // Contact list is obtained from the visited profile user, but the contact display is visitor dependent
117 $contacts = array_map(
118 function ($contact) {
119 $contact = Model\Contact::selectFirst(
121 ['uri-id' => $contact['uri-id'], 'uid' => [0, $this->userSession->getLocalUserId()]],
122 ['order' => ['uid' => 'DESC']]
124 return $contact ? Module\Contact::getContactTemplateVars($contact) : null;
126 Model\Contact::selectToArray(['uri-id'], $condition, $params)
129 // Remove nonexistent contacts
130 $contacts = array_filter($contacts);
135 $title = $this->tt('Follower (%s)', 'Followers (%s)', $total);
138 $title = $this->tt('Following (%s)', 'Following (%s)', $total);
141 $title = $this->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
143 'These contacts both follow and are followed by <strong>%s</strong>.',
144 htmlentities($profile['name'], ENT_COMPAT, 'UTF-8')
149 $title = $this->tt('Contact (%s)', 'Contacts (%s)', $total);
153 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
154 $o .= Renderer::replaceMacros($tpl, [
159 '$noresult_label' => $this->t('No contacts.'),
161 '$contacts' => $contacts,
162 '$paginate' => $pager->renderFull($total),