]> git.mxchange.org Git - friendica.git/blob - mod/match.php
Use the top-level author theme if they're a local user in mod/display
[friendica.git] / mod / match.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 use Friendica\App;
23 use Friendica\Content\Widget;
24 use Friendica\Core\Renderer;
25 use Friendica\Core\Search;
26 use Friendica\Database\DBA;
27 use Friendica\DI;
28 use Friendica\Model\Contact;
29 use Friendica\Model\Profile;
30 use Friendica\Module\Contact as ModuleContact;
31
32 /**
33  * Controller for /match.
34  *
35  * It takes keywords from your profile and queries the directory server for
36  * matching keywords from other profiles.
37  *
38  * @param App $a App
39  *
40  * @return string
41  * @throws ImagickException
42  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
43  * @throws Exception
44  */
45 function match_content(App $a)
46 {
47         if (!local_user()) {
48                 return '';
49         }
50
51         DI::page()['aside'] .= Widget::findPeople();
52         DI::page()['aside'] .= Widget::follow();
53
54         $_SESSION['return_path'] = DI::args()->getCommand();
55
56         $profile = Profile::getByUID(local_user());
57
58         if (!DBA::isResult($profile)) {
59                 return '';
60         }
61         if (!$profile['pub_keywords'] && (!$profile['prv_keywords'])) {
62                 notice(DI::l10n()->t('No keywords to match. Please add keywords to your profile.'));
63                 return '';
64         }
65
66         $params = [];
67         $tags = trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']);
68
69         if (DI::mode()->isMobile()) {
70                 $limit = DI::pConfig()->get(local_user(), 'system', 'itemspage_mobile_network',
71                         DI::config()->get('system', 'itemspage_network_mobile'));
72         } else {
73                 $limit = DI::pConfig()->get(local_user(), 'system', 'itemspage_network',
74                         DI::config()->get('system', 'itemspage_network'));
75         }
76
77         $params['s'] = $tags;
78         $params['n'] = 100;
79
80         $entries = [];
81         foreach ([Search::getGlobalDirectory(), DI::baseUrl()] as $server) {
82                 if (empty($server)) {
83                         continue;
84                 }
85
86                 $msearch = json_decode(DI::httpClient()->post($server . '/msearch', $params)->getBody());
87                 if (!empty($msearch)) {
88                         $entries = match_get_contacts($msearch, $entries, $limit);
89                 }
90         }
91
92         if (empty($entries)) {
93                 info(DI::l10n()->t('No matches'));
94         }
95
96         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
97         $o = Renderer::replaceMacros($tpl, [
98                 '$title'    => DI::l10n()->t('Profile Match'),
99                 '$contacts' => array_slice($entries, 0, $limit),
100         ]);
101
102         return $o;
103 }
104
105 function match_get_contacts($msearch, $entries, $limit)
106 {
107         if (empty($msearch->results)) {
108                 return $entries;
109         }
110
111         foreach ($msearch->results as $profile) {
112                 if (!$profile) {
113                         continue;
114                 }
115
116                 // Already known contact
117                 $contact = Contact::getByURL($profile->url, null, ['rel'], local_user());
118                 if (!empty($contact) && in_array($contact['rel'], [Contact::FRIEND, Contact::SHARING])) {
119                         continue;
120                 }
121
122                 $contact = Contact::getByURLForUser($profile->url, local_user());
123                 if (!empty($contact)) {
124                         $entries[$contact['id']] = ModuleContact::getContactTemplateVars($contact);
125                 }
126
127                 if (count($entries) == $limit) {
128                         break;
129                 }
130         }
131         return $entries;
132 }