3 * @copyright Copyright (C) 2010-2022, 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\Contact;
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;
45 * It takes keywords from your profile and queries the directory server for
46 * matching keywords from other profiles.
48 class MatchInterests extends BaseModule
50 const FETCH_PER_PAGE = 100;
52 /** @var IHandleUserSessions */
56 /** @var SystemMessages */
57 protected $systemMessages;
62 /** @var IManageConfigValues */
64 /** @var IManagePersonalConfigValues */
66 /** @var ICanSendHttpRequests */
67 protected $httpClient;
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 = [])
71 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
73 $this->session = $session;
74 $this->database = $database;
75 $this->systemMessages = $systemMessages;
78 $this->config = $config;
79 $this->pConfig = $pConfig;
80 $this->httpClient = $httpClient;
83 protected function content(array $request = []): string
85 if (!$this->session->getLocalUserId()) {
86 $this->systemMessages->addNotice($this->t('Permission denied.'));
87 $this->baseUrl->redirect('login&return_path=match');
90 $profile = Profile::getByUID($this->session->getLocalUserId());
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.'));
97 $this->page['aside'] .= Widget::findPeople();
98 $this->page['aside'] .= Widget::follow();
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.'));
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');
109 $limit = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_network')
110 ?? $this->config->get('system', 'itemspage_network');
113 $searchParameters = [
114 's' => trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']),
115 'n' => self::FETCH_PER_PAGE,
120 foreach ([Search::getGlobalDirectory(), $this->baseUrl] as $server) {
121 if (empty($server)) {
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]);
135 $entries = $this->parseContacts(json_decode($result->getBody()), $entries, $limit);
138 if (empty($entries)) {
139 $this->systemMessages->addNotice($this->t('No matches'));
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),
150 * parses the JSON result and adds the new entries until the limit is reached
153 * @param array $entries
156 * @return array the new entries array
158 protected function parseContacts($jsonResult, array $entries, int $limit): array
160 if (empty($jsonResult->results)) {
164 foreach ($jsonResult->results as $profile) {
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])) {
175 $contact = Contact::getByURLForUser($profile->url, $this->session->getLocalUserId());
176 if (!empty($contact)) {
177 $entries[$contact['id']] = ModuleContact::getContactTemplateVars($contact);
180 if (count($entries) == $limit) {