]> git.mxchange.org Git - friendica.git/blob - src/Module/Profile/Common.php
spelling: parameter
[friendica.git] / src / Module / Profile / Common.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\Module;
33 use Friendica\Model\Contact;
34 use Friendica\Model\Profile;
35 use Friendica\Module\BaseProfile;
36 use Friendica\Module\Response;
37 use Friendica\Network\HTTPException;
38 use Friendica\Util\Profiler;
39 use Psr\Log\LoggerInterface;
40
41 class Common extends BaseProfile
42 {
43         /** @var IManageConfigValues */
44         private $config;
45         /** @var IHandleUserSessions */
46         private $userSession;
47         /** @var App */
48         private $app;
49
50         public function __construct(App $app, IHandleUserSessions $userSession, IManageConfigValues $config, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
51         {
52                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
53
54                 $this->config = $config;
55                 $this->userSession = $userSession;
56                 $this->app = $app;
57         }
58
59         protected function content(array $request = []): string
60         {
61                 if ($this->config->get('system', 'block_public') && !$this->userSession->isAuthenticated()) {
62                         throw new HTTPException\NotFoundException($this->t('User not found.'));
63                 }
64
65                 Nav::setSelected('home');
66
67                 $nickname = $this->parameters['nickname'];
68
69                 $profile = Profile::load($this->app, $nickname);
70                 if (empty($profile)) {
71                         throw new HTTPException\NotFoundException($this->t('User not found.'));
72                 }
73
74                 if (!empty($profile['hide-friends'])) {
75                         throw new HTTPException\ForbiddenException($this->t('Permission denied.'));
76                 }
77
78                 $displayCommonTab = $this->userSession->isAuthenticated() && $profile['uid'] != $this->userSession->getLocalUserId();
79
80                 if (!$displayCommonTab) {
81                         $this->baseUrl->redirect('profile/' . $nickname . '/contacts');
82                 };
83
84                 $o = self::getTabsHTML('contacts', false, $profile['nickname'], $profile['hide-friends']);
85
86                 $tabs = self::getContactFilterTabs('profile/' . $nickname, 'common', $displayCommonTab);
87
88                 $sourceId = Contact::getIdForURL($this->userSession->getMyUrl());
89                 $targetId = Contact::getPublicIdByUserId($profile['uid']);
90
91                 $condition = [
92                         'blocked' => false,
93                         'deleted' => false,
94                         'network' => [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::DIASPORA, Protocol::OSTATUS, Protocol::FEED],
95                 ];
96
97                 $total = Contact\Relation::countCommon($sourceId, $targetId, $condition);
98
99                 $pager = new Pager($this->l10n, $this->args->getQueryString(), 30);
100
101                 $commonFollows = Contact\Relation::listCommon($sourceId, $targetId, $condition, $pager->getItemsPerPage(), $pager->getStart());
102
103                 // Contact list is obtained from the visited profile user, but the contact display is visitor dependent
104                 $contacts = array_map(
105                         function ($contact) {
106                                 $contact = Contact::selectFirst(
107                                         [],
108                                         ['uri-id' => $contact['uri-id'], 'uid' => [0, $this->userSession->getLocalUserId()]],
109                                         ['order' => ['uid' => 'DESC']]
110                                 );
111                                 return Module\Contact::getContactTemplateVars($contact);
112                         },
113                         $commonFollows
114                 );
115
116                 $title = $this->tt('Common contact (%s)', 'Common contacts (%s)', $total);
117                 $desc = $this->t(
118                         'Both <strong>%s</strong> and yourself have publicly interacted with these contacts (follow, comment or likes on public posts).',
119                         htmlentities($profile['name'], ENT_COMPAT, 'UTF-8')
120                 );
121
122                 $tpl = Renderer::getMarkupTemplate('profile/contacts.tpl');
123                 $o .= Renderer::replaceMacros($tpl, [
124                         '$title'    => $title,
125                         '$desc'     => $desc,
126                         '$tabs'     => $tabs,
127
128                         '$noresult_label'  => $this->t('No common contacts.'),
129
130                         '$contacts' => $contacts,
131                         '$paginate' => $pager->renderFull($total),
132                 ]);
133
134                 return $o;
135         }
136 }