From: Philipp Date: Wed, 9 Nov 2022 21:36:02 +0000 (+0100) Subject: Rename Match to MatchInterests because PHP8-lint rejects "match" X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=815d8975e61c881c17a32e62cce261942028af53;p=friendica.git Rename Match to MatchInterests because PHP8-lint rejects "match" --- diff --git a/src/Module/Contact/Match.php b/src/Module/Contact/Match.php deleted file mode 100644 index 0e187b069b..0000000000 --- a/src/Module/Contact/Match.php +++ /dev/null @@ -1,188 +0,0 @@ -. - * - */ - -namespace Friendica\Module\Contact; - -use Friendica\App; -use Friendica\BaseModule; -use Friendica\Content\Widget; -use Friendica\Core\Config\Capability\IManageConfigValues; -use Friendica\Core\L10n; -use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; -use Friendica\Core\Renderer; -use Friendica\Core\Search; -use Friendica\Core\Session\Capability\IHandleUserSessions; -use Friendica\Database\Database; -use Friendica\Model\Contact; -use Friendica\Model\Profile; -use Friendica\Module\Contact as ModuleContact; -use Friendica\Module\Response; -use Friendica\Navigation\SystemMessages; -use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests; -use Friendica\Network\HTTPException\InternalServerErrorException; -use Friendica\Util\Profiler; -use Psr\Log\LoggerInterface; - -/** - * Controller for /match. - * - * It takes keywords from your profile and queries the directory server for - * matching keywords from other profiles. - */ -class Match extends BaseModule -{ - const FETCH_PER_PAGE = 100; - - /** @var IHandleUserSessions */ - protected $session; - /** @var Database */ - protected $database; - /** @var SystemMessages */ - protected $systemMessages; - /** @var App\Page */ - protected $page; - /** @var App\Mode */ - protected $mode; - /** @var IManageConfigValues */ - protected $config; - /** @var IManagePersonalConfigValues */ - protected $pConfig; - /** @var ICanSendHttpRequests */ - protected $httpClient; - - 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 = []) - { - parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); - - $this->session = $session; - $this->database = $database; - $this->systemMessages = $systemMessages; - $this->page = $page; - $this->mode = $mode; - $this->config = $config; - $this->pConfig = $pConfig; - $this->httpClient = $httpClient; - } - - protected function content(array $request = []): string - { - if (!$this->session->getLocalUserId()) { - $this->systemMessages->addNotice($this->t('Permission denied.')); - $this->baseUrl->redirect('login&return_path=match'); - } - - $profile = Profile::getByUID($this->session->getLocalUserId()); - - if (empty($profile)) { - $this->logger->warning('Couldn\'t find Profile for user id in session.', ['uid' => $this->session->getLocalUserId()]); - throw new InternalServerErrorException($this->t('Invalid request.')); - } - - $this->page['aside'] .= Widget::findPeople(); - $this->page['aside'] .= Widget::follow(); - - if (empty($profile['pub_keywords']) && empty($profile['prv_keywords'])) { - $this->systemMessages->addNotice($this->t('No keywords to match. Please add keywords to your profile.')); - return ''; - } - - if ($this->mode->isMobile()) { - $limit = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_mobile_network') - ?? $this->config->get('system', 'itemspage_network_mobile'); - } else { - $limit = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_network') - ?? $this->config->get('system', 'itemspage_network'); - } - - $searchParameters = [ - 's' => trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']), - 'n' => self::FETCH_PER_PAGE, - ]; - - $entries = []; - - foreach ([Search::getGlobalDirectory(), $this->baseUrl] as $server) { - if (empty($server)) { - continue; - } - - $result = $this->httpClient->post($server . '/search/user/tags', $searchParameters); - if (!$result->isSuccess()) { - // try legacy endpoint - $result = $this->httpClient->post($server . '/contact/search/tags', $searchParameters); - if (!$result->isSuccess()) { - $this->logger->notice('Search-Endpoint not available for server.', ['server' => $server]); - continue; - } - } - - $entries = $this->parseContacts(json_decode($result->getBody()), $entries, $limit); - } - - if (empty($entries)) { - $this->systemMessages->addNotice($this->t('No matches')); - } - - $tpl = Renderer::getMarkupTemplate('contact/list.tpl'); - return Renderer::replaceMacros($tpl, [ - '$title' => $this->t('Profile Match'), - '$contacts' => array_slice($entries, 0, $limit), - ]); - } - - /** - * parses the JSON result and adds the new entries until the limit is reached - * - * @param $jsonResult - * @param array $entries - * @param int $limit - * - * @return array the new entries array - */ - protected function parseContacts($jsonResult, array $entries, int $limit): array - { - if (empty($jsonResult->results)) { - return $entries; - } - - foreach ($jsonResult->results as $profile) { - if (!$profile) { - continue; - } - - // Already known contact - $contact = Contact::getByURL($profile->url, null, ['rel'], $this->session->getLocalUserId()); - if (!empty($contact) && in_array($contact['rel'], [Contact::FRIEND, Contact::SHARING])) { - continue; - } - - $contact = Contact::getByURLForUser($profile->url, $this->session->getLocalUserId()); - if (!empty($contact)) { - $entries[$contact['id']] = ModuleContact::getContactTemplateVars($contact); - } - - if (count($entries) == $limit) { - break; - } - } - return $entries; - } -} diff --git a/src/Module/Contact/MatchInterests.php b/src/Module/Contact/MatchInterests.php new file mode 100644 index 0000000000..c9601a0fdf --- /dev/null +++ b/src/Module/Contact/MatchInterests.php @@ -0,0 +1,186 @@ +. + * + */ + +namespace Friendica\Module\Contact; + +use Friendica\App; +use Friendica\BaseModule; +use Friendica\Content\Widget; +use Friendica\Core\Config\Capability\IManageConfigValues; +use Friendica\Core\L10n; +use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues; +use Friendica\Core\Renderer; +use Friendica\Core\Search; +use Friendica\Core\Session\Capability\IHandleUserSessions; +use Friendica\Database\Database; +use Friendica\Model\Contact; +use Friendica\Model\Profile; +use Friendica\Module\Contact as ModuleContact; +use Friendica\Module\Response; +use Friendica\Navigation\SystemMessages; +use Friendica\Network\HTTPClient\Capability\ICanSendHttpRequests; +use Friendica\Network\HTTPException\InternalServerErrorException; +use Friendica\Util\Profiler; +use Psr\Log\LoggerInterface; + +/** + * It takes keywords from your profile and queries the directory server for + * matching keywords from other profiles. + */ +class MatchInterests extends BaseModule +{ + const FETCH_PER_PAGE = 100; + + /** @var IHandleUserSessions */ + protected $session; + /** @var Database */ + protected $database; + /** @var SystemMessages */ + protected $systemMessages; + /** @var App\Page */ + protected $page; + /** @var App\Mode */ + protected $mode; + /** @var IManageConfigValues */ + protected $config; + /** @var IManagePersonalConfigValues */ + protected $pConfig; + /** @var ICanSendHttpRequests */ + protected $httpClient; + + 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 = []) + { + parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters); + + $this->session = $session; + $this->database = $database; + $this->systemMessages = $systemMessages; + $this->page = $page; + $this->mode = $mode; + $this->config = $config; + $this->pConfig = $pConfig; + $this->httpClient = $httpClient; + } + + protected function content(array $request = []): string + { + if (!$this->session->getLocalUserId()) { + $this->systemMessages->addNotice($this->t('Permission denied.')); + $this->baseUrl->redirect('login&return_path=match'); + } + + $profile = Profile::getByUID($this->session->getLocalUserId()); + + if (empty($profile)) { + $this->logger->warning('Couldn\'t find Profile for user id in session.', ['uid' => $this->session->getLocalUserId()]); + throw new InternalServerErrorException($this->t('Invalid request.')); + } + + $this->page['aside'] .= Widget::findPeople(); + $this->page['aside'] .= Widget::follow(); + + if (empty($profile['pub_keywords']) && empty($profile['prv_keywords'])) { + $this->systemMessages->addNotice($this->t('No keywords to match. Please add keywords to your profile.')); + return ''; + } + + if ($this->mode->isMobile()) { + $limit = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_mobile_network') + ?? $this->config->get('system', 'itemspage_network_mobile'); + } else { + $limit = $this->pConfig->get($this->session->getLocalUserId(), 'system', 'itemspage_network') + ?? $this->config->get('system', 'itemspage_network'); + } + + $searchParameters = [ + 's' => trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']), + 'n' => self::FETCH_PER_PAGE, + ]; + + $entries = []; + + foreach ([Search::getGlobalDirectory(), $this->baseUrl] as $server) { + if (empty($server)) { + continue; + } + + $result = $this->httpClient->post($server . '/search/user/tags', $searchParameters); + if (!$result->isSuccess()) { + // try legacy endpoint + $result = $this->httpClient->post($server . '/contact/search/tags', $searchParameters); + if (!$result->isSuccess()) { + $this->logger->notice('Search-Endpoint not available for server.', ['server' => $server]); + continue; + } + } + + $entries = $this->parseContacts(json_decode($result->getBody()), $entries, $limit); + } + + if (empty($entries)) { + $this->systemMessages->addNotice($this->t('No matches')); + } + + $tpl = Renderer::getMarkupTemplate('contact/list.tpl'); + return Renderer::replaceMacros($tpl, [ + '$title' => $this->t('Profile Match'), + '$contacts' => array_slice($entries, 0, $limit), + ]); + } + + /** + * parses the JSON result and adds the new entries until the limit is reached + * + * @param $jsonResult + * @param array $entries + * @param int $limit + * + * @return array the new entries array + */ + protected function parseContacts($jsonResult, array $entries, int $limit): array + { + if (empty($jsonResult->results)) { + return $entries; + } + + foreach ($jsonResult->results as $profile) { + if (!$profile) { + continue; + } + + // Already known contact + $contact = Contact::getByURL($profile->url, null, ['rel'], $this->session->getLocalUserId()); + if (!empty($contact) && in_array($contact['rel'], [Contact::FRIEND, Contact::SHARING])) { + continue; + } + + $contact = Contact::getByURLForUser($profile->url, $this->session->getLocalUserId()); + if (!empty($contact)) { + $entries[$contact['id']] = ModuleContact::getContactTemplateVars($contact); + } + + if (count($entries) == $limit) { + break; + } + } + return $entries; + } +} diff --git a/static/routes.config.php b/static/routes.config.php index ee0b8d6767..0958ee877b 100644 --- a/static/routes.config.php +++ b/static/routes.config.php @@ -367,28 +367,28 @@ return [ '/compose[/{type}]' => [Module\Item\Compose::class, [R::GET, R::POST]], '/contact' => [ - '[/]' => [Module\Contact::class, [R::GET]], - '/{id:\d+}[/]' => [Module\Contact\Profile::class, [R::GET, R::POST]], + '[/]' => [Module\Contact::class, [R::GET]], + '/{id:\d+}[/]' => [Module\Contact\Profile::class, [R::GET, R::POST]], '/{id:\d+}/{action:block|ignore|update|updateprofile}' - => [Module\Contact\Profile::class, [R::GET]], - '/{id:\d+}/advanced' => [Module\Contact\Advanced::class, [R::GET, R::POST]], - '/{id:\d+}/conversations' => [Module\Contact\Conversations::class, [R::GET]], - '/{id:\d+}/contacts[/{type}]' => [Module\Contact\Contacts::class, [R::GET]], - '/{id:\d+}/media' => [Module\Contact\Media::class, [R::GET]], - '/{id:\d+}/posts' => [Module\Contact\Posts::class, [R::GET]], - '/{id:\d+}/revoke' => [Module\Contact\Revoke::class, [R::GET, R::POST]], - '/archived' => [Module\Contact::class, [R::GET]], - '/batch' => [Module\Contact::class, [R::GET, R::POST]], - '/blocked' => [Module\Contact::class, [R::GET]], - '/follow' => [Module\Contact\Follow::class, [R::GET, R::POST]], - '/hidden' => [Module\Contact::class, [R::GET]], - '/hovercard' => [Module\Contact\Hovercard::class, [R::GET]], - '/ignored' => [Module\Contact::class, [R::GET]], - '/match' => [Module\Contact\Match::class, [R::GET]], - '/pending' => [Module\Contact::class, [R::GET]], - '/redir/{id:\d+}' => [Module\Contact\Redir::class, [R::GET]], - '/suggestions' => [Module\Contact\Suggestions::class, [R::GET]], - '/unfollow' => [Module\Contact\Unfollow::class, [R::GET, R::POST]], + => [Module\Contact\Profile::class, [R::GET]], + '/{id:\d+}/advanced' => [Module\Contact\Advanced::class, [R::GET, R::POST]], + '/{id:\d+}/conversations' => [Module\Contact\Conversations::class, [R::GET]], + '/{id:\d+}/contacts[/{type}]' => [Module\Contact\Contacts::class, [R::GET]], + '/{id:\d+}/media' => [Module\Contact\Media::class, [R::GET]], + '/{id:\d+}/posts' => [Module\Contact\Posts::class, [R::GET]], + '/{id:\d+}/revoke' => [Module\Contact\Revoke::class, [R::GET, R::POST]], + '/archived' => [Module\Contact::class, [R::GET]], + '/batch' => [Module\Contact::class, [R::GET, R::POST]], + '/blocked' => [Module\Contact::class, [R::GET]], + '/follow' => [Module\Contact\Follow::class, [R::GET, R::POST]], + '/hidden' => [Module\Contact::class, [R::GET]], + '/hovercard' => [Module\Contact\Hovercard::class, [R::GET]], + '/ignored' => [Module\Contact::class, [R::GET]], + '/match' => [Module\Contact\MatchInterests::class, [R::GET]], + '/pending' => [Module\Contact::class, [R::GET]], + '/redir/{id:\d+}' => [Module\Contact\Redir::class, [R::GET]], + '/suggestions' => [Module\Contact\Suggestions::class, [R::GET]], + '/unfollow' => [Module\Contact\Unfollow::class, [R::GET, R::POST]], ], '/credits' => [Module\Credits::class, [R::GET]],