]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Contacts.php
Merge pull request #13238 from annando/issue-13221
[friendica.git] / src / Module / Profile / Contacts.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2023, 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]
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                 // 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(
120                                         [],
121                                         ['uri-id' => $contact['uri-id'], 'uid' => [0, $this->userSession->getLocalUserId()]],
122                                         ['order' => ['uid' => 'DESC']]
123                                 );
124                                 return $contact ? Module\Contact::getContactTemplateVars($contact) : null;
125                         },
126                         Model\Contact::selectToArray(['uri-id'], $condition, $params)
127                 );
128
129                 // Remove nonexistent contacts
130                 $contacts = array_filter($contacts);
131
132                 $desc = '';
133                 switch ($type) {
134                         case 'followers':
135                                 $title = $this->tt('Follower (%s)', 'Followers (%s)', $total);
136                                 break;
137                         case 'following':
138                                 $title = $this->tt('Following (%s)', 'Following (%s)', $total);
139                                 break;
140                         case 'mutuals':
141                                 $title = $this->tt('Mutual friend (%s)', 'Mutual friends (%s)', $total);
142                                 $desc  = $this->t(
143                                         'These contacts both follow and are followed by <strong>%s</strong>.',
144                                         htmlentities($profile['name'], ENT_COMPAT, 'UTF-8')
145                                 );
146                                 break;
147                         case 'all':
148                         default:
149                                 $title = $this->tt('Contact (%s)', 'Contacts (%s)', $total);
150                                 break;
151                 }
152
153                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
154                 $o   .= Renderer::replaceMacros($tpl, [
155                         '$title' => $title,
156                         '$desc'  => $desc,
157                         '$tabs'  => $tabs,
158
159                         '$noresult_label' => $this->t('No contacts.'),
160
161                         '$contacts' => $contacts,
162                         '$paginate' => $pager->renderFull($total),
163                 ]);
164
165                 return $o;
166         }
167 }