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/>.
23 use Friendica\Content\Widget;
24 use Friendica\Core\Renderer;
25 use Friendica\Core\Search;
26 use Friendica\Core\Session;
27 use Friendica\Database\DBA;
29 use Friendica\Model\Contact;
30 use Friendica\Model\Profile;
31 use Friendica\Module\Contact as ModuleContact;
34 * Controller for /match.
36 * It takes keywords from your profile and queries the directory server for
37 * matching keywords from other profiles.
42 * @throws ImagickException
43 * @throws \Friendica\Network\HTTPException\InternalServerErrorException
46 function match_content(App $a)
48 if (!DI::userSession()->getLocalUserId()) {
52 DI::page()['aside'] .= Widget::findPeople();
53 DI::page()['aside'] .= Widget::follow();
55 $_SESSION['return_path'] = DI::args()->getCommand();
57 $profile = Profile::getByUID(DI::userSession()->getLocalUserId());
59 if (!DBA::isResult($profile)) {
62 if (!$profile['pub_keywords'] && (!$profile['prv_keywords'])) {
63 DI::sysmsg()->addNotice(DI::l10n()->t('No keywords to match. Please add keywords to your profile.'));
68 $tags = trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']);
70 if (DI::mode()->isMobile()) {
71 $limit = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_mobile_network',
72 DI::config()->get('system', 'itemspage_network_mobile'));
74 $limit = DI::pConfig()->get(DI::userSession()->getLocalUserId(), 'system', 'itemspage_network',
75 DI::config()->get('system', 'itemspage_network'));
82 foreach ([Search::getGlobalDirectory(), DI::baseUrl()] as $server) {
87 $msearch = json_decode(DI::httpClient()->post($server . '/msearch', $params)->getBody());
88 if (!empty($msearch)) {
89 $entries = match_get_contacts($msearch, $entries, $limit);
93 if (empty($entries)) {
94 DI::sysmsg()->addInfo(DI::l10n()->t('No matches'));
97 $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
98 $o = Renderer::replaceMacros($tpl, [
99 '$title' => DI::l10n()->t('Profile Match'),
100 '$contacts' => array_slice($entries, 0, $limit),
106 function match_get_contacts($msearch, $entries, $limit)
108 if (empty($msearch->results)) {
112 foreach ($msearch->results as $profile) {
117 // Already known contact
118 $contact = Contact::getByURL($profile->url, null, ['rel'], DI::userSession()->getLocalUserId());
119 if (!empty($contact) && in_array($contact['rel'], [Contact::FRIEND, Contact::SHARING])) {
123 $contact = Contact::getByURLForUser($profile->url, DI::userSession()->getLocalUserId());
124 if (!empty($contact)) {
125 $entries[$contact['id']] = ModuleContact::getContactTemplateVars($contact);
128 if (count($entries) == $limit) {