]> git.mxchange.org Git - friendica.git/blob - src/Module/Contact/MatchInterests.php
Removed redundant maximagesize = INF statements
[friendica.git] / src / Module / Contact / MatchInterests.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\Contact;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Content\Widget;
27 use Friendica\Core\Config\Capability\IManageConfigValues;
28 use Friendica\Core\L10n;
29 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
30 use Friendica\Core\Renderer;
31 use Friendica\Core\Search;
32 use Friendica\Core\Session\Capability\IHandleUserSessions;
33 use Friendica\Database\Database;
34 use Friendica\Model\Contact;
35 use Friendica\Model\Profile;
36 use Friendica\Module\Contact as ModuleContact;
37 use Friendica\Module\Response;
38 use Friendica\Navigation\SystemMessages;
39 use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests;
40 use Friendica\Network\HTTPException\InternalServerErrorException;
41 use Friendica\Util\Profiler;
42 use Psr\Log\LoggerInterface;
43
44 /**
45  * It takes keywords from your profile and queries the directory server for
46  * matching keywords from other profiles.
47  */
48 class MatchInterests extends BaseModule
49 {
50         const FETCH_PER_PAGE = 100;
51
52         /** @var IHandleUserSessions */
53         protected $session;
54         /** @var Database */
55         protected $database;
56         /** @var SystemMessages */
57         protected $systemMessages;
58         /** @var App\Page */
59         protected $page;
60         /** @var App\Mode */
61         protected $mode;
62         /** @var IManageConfigValues */
63         protected $config;
64         /** @var IManagePersonalConfigValues */
65         protected $pConfig;
66         /** @var ICanSendHttpRequests */
67         protected $httpClient;
68
69         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IHandleUserSessions $session, Database $database, SystemMessages $systemMessages, App\Page $page, App\Mode $mode, IManageConfigValues $config, IManagePersonalConfigValues $pConfig, ICanSendHttpRequests $httpClient, array $server, array $parameters = [])
70         {
71                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
72
73                 $this->session        = $session;
74                 $this->database       = $database;
75                 $this->systemMessages = $systemMessages;
76                 $this->page           = $page;
77                 $this->mode           = $mode;
78                 $this->config         = $config;
79                 $this->pConfig        = $pConfig;
80                 $this->httpClient     = $httpClient;
81         }
82
83         protected function content(array $request = []): string
84         {
85                 if (!$this->session->getLocalUserId()) {
86                         $this->systemMessages->addNotice($this->t('Permission denied.'));
87                         $this->baseUrl->redirect('login&return_path=match');
88                 }
89
90                 $profile = Profile::getByUID($this->session->getLocalUserId());
91
92                 if (empty($profile)) {
93                         $this->logger->warning('Couldn\'t find Profile for user id in session.', ['uid' => $this->session->getLocalUserId()]);
94                         throw new InternalServerErrorException($this->t('Invalid request.'));
95                 }
96
97                 $this->page['aside'] .= Widget::findPeople();
98                 $this->page['aside'] .= Widget::follow();
99
100                 if (empty($profile['pub_keywords']) && empty($profile['prv_keywords'])) {
101                         $this->systemMessages->addNotice($this->t('No keywords to match. Please add keywords to your profile.'));
102                         return '';
103                 }
104
105                 if ($this->mode->isMobile()) {
106                         $limit = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_mobile_network')
107                                          ?? $this->config->get('system', 'itemspage_network_mobile');
108                 } else {
109                         $limit = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_network')
110                                          ?? $this->config->get('system', 'itemspage_network');
111                 }
112
113                 $searchParameters = [
114                         's' => trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']),
115                         'n' => self::FETCH_PER_PAGE,
116                 ];
117
118                 $entries = [];
119
120                 foreach ([Search::getGlobalDirectory(), $this->baseUrl] as $server) {
121                         if (empty($server)) {
122                                 continue;
123                         }
124
125                         $result = $this->httpClient->post($server . '/search/user/tags', $searchParameters);
126                         if (!$result->isSuccess()) {
127                                 // try legacy endpoint
128                                 $result = $this->httpClient->post($server . '/msearch', $searchParameters);
129                                 if (!$result->isSuccess()) {
130                                         $this->logger->notice('Search-Endpoint not available for server.', ['server' => $server]);
131                                         continue;
132                                 }
133                         }
134
135                         $entries = $this->parseContacts(json_decode($result->getBody()), $entries, $limit);
136                 }
137
138                 if (empty($entries)) {
139                         $this->systemMessages->addNotice($this->t('No matches'));
140                 }
141
142                 $tpl = Renderer::getMarkupTemplate('contact/list.tpl');
143                 return Renderer::replaceMacros($tpl, [
144                         '$title'    => $this->t('Profile Match'),
145                         '$contacts' => array_slice($entries, 0, $limit),
146                 ]);
147         }
148
149         /**
150          * parses the JSON result and adds the new entries until the limit is reached
151          *
152          * @param       $jsonResult
153          * @param array $entries
154          * @param int   $limit
155          *
156          * @return array the new entries array
157          */
158         protected function parseContacts($jsonResult, array $entries, int $limit): array
159         {
160                 if (empty($jsonResult->results)) {
161                         return $entries;
162                 }
163
164                 foreach ($jsonResult->results as $profile) {
165                         if (!$profile) {
166                                 continue;
167                         }
168
169                         // Already known contact
170                         $contact = Contact::getByURL($profile->url, null, ['rel'], $this->session->getLocalUserId());
171                         if (!empty($contact) && in_array($contact['rel'], [Contact::FRIEND, Contact::SHARING])) {
172                                 continue;
173                         }
174
175                         $contact = Contact::getByURLForUser($profile->url, $this->session->getLocalUserId());
176                         if (!empty($contact)) {
177                                 $entries[$contact['id']] = ModuleContact::getContactTemplateVars($contact);
178                         }
179
180                         if (count($entries) == $limit) {
181                                 break;
182                         }
183                 }
184                 return $entries;
185         }
186 }