]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Contacts.php
Remove DI dependency in Module\Profile\Contacts
[friendica.git] / src / Module / Profile / Contacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
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.
11  *
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.
16  *
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/>.
19  *
20  */
21
22 namespace Friendica\Module\Profile;
23
24 use Friendica\App;
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;
33 use Friendica\Model;
34 use Friendica\Module;
35 use Friendica\Module\Response;
36 use Friendica\Network\HTTPException;
37 use Friendica\Util\Profiler;
38 use Psr\Log\LoggerInterface;
39
40 class Contacts extends Module\BaseProfile
41 {
42         /** @var IManageConfigValues */
43         private $config;
44         /** @var IHandleUserSessions */
45         private $userSession;
46         /** @var App */
47         private $app;
48         /** @var Database */
49         private $database;
50
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 = [])
52         {
53                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
54
55                 $this->config      = $config;
56                 $this->userSession = $userSession;
57                 $this->app         = $app;
58                 $this->database    = $database;
59         }
60
61         protected function content(array $request = []): string
62         {
63                 if ($this->config->get('system', 'block_public') && !$this->userSession->isAuthenticated()) {
64                         throw new HTTPException\NotFoundException($this->t('User not found.'));
65                 }
66
67                 $nickname = $this->parameters['nickname'];
68                 $type     = $this->parameters['type'] ?? 'all';
69
70                 $profile = Model\Profile::load($this->app, $nickname);
71                 if (empty($profile)) {
72                         throw new HTTPException\NotFoundException($this->t('User not found.'));
73                 }
74
75                 $is_owner = $profile['uid'] == $this->userSession->getLocalUserId();
76
77                 if ($profile['hide-friends'] && !$is_owner) {
78                         throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
79                 }
80
81                 Nav::setSelected('home');
82
83                 $o = self::getTabsHTML('contacts', $is_owner, $profile['nickname'], $profile['hide-friends']);
84
85                 $tabs = self::getContactFilterTabs('profile/' . $nickname, $type, $this->userSession->isAuthenticated() && $profile['uid'] != $this->userSession->getLocalUserId());
86
87                 $condition = [
88                         'uid'     => $profile['uid'],
89                         'blocked' => false,
90                         'pending' => false,
91                         'hidden'  => false,
92                         'archive' => false,
93                         'failed'  => false,
94                         'self'    => false,
95                         'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED]
96                 ];
97
98                 switch ($type) {
99                         case 'followers':
100                                 $condition['rel'] = [Model\Contact::FOLLOWER, Model\Contact::FRIEND];
101                                 break;
102                         case 'following':
103                                 $condition['rel'] = [Model\Contact::SHARING, Model\Contact::FRIEND];
104                                 break;
105                         case 'mutuals':
106                                 $condition['rel'] = Model\Contact::FRIEND;
107                                 break;
108                 }
109
110                 $total = $this->database->count('contact', $condition);
111
112                 $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
113
114                 $params = ['order' => ['name' => false], 'limit' => [$pager->getStart(), $pager->getItemsPerPage()]];
115
116                 $contacts = array_map(
117                         [Module\Contact::class, 'getContactTemplateVars'],
118                         Model\Contact::selectToArray([], $condition, $params)
119                 );
120
121                 $desc = '';
122                 switch ($type) {
123                         case 'followers':
124                                 $title = $this->tt('Follower (%s)', 'Followers (%s)', $total);
125                                 break;
126                         case 'following':
127                                 $title = $this->tt('Following (%s)', 'Following (%s)', $total);
128                                 break;
129                         case 'mutuals':
130                                 $title = $this->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
131                                 $desc  = $this->t(
132                                         'These contacts both follow and are followed by <strong>%s</strong>.',
133                                         htmlentities($profile['name'], ENT_COMPAT, 'UTF-8')
134                                 );
135                                 break;
136                         case 'all':
137                         default:
138                                 $title = $this->tt('Contact (%s)', 'Contacts (%s)', $total);
139                                 break;
140                 }
141
142                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
143                 $o   .= Renderer::replaceMacros($tpl, [
144                         '$title' => $title,
145                         '$desc'  => $desc,
146                         '$tabs'  => $tabs,
147
148                         '$noresult_label' => $this->t('No contacts.'),
149
150                         '$contacts' => $contacts,
151                         '$paginate' => $pager->renderFull($total),
152                 ]);
153
154                 return $o;
155         }
156 }