]> git.mxchange.org Git - friendica.git/blob - mod/match.php
Fetch photo fields, ensuring that they are filled
[friendica.git] / mod / match.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2020, Friendica
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
31 /**
32  * Controller for /match.
33  *
34  * It takes keywords from your profile and queries the directory server for
35  * matching keywords from other profiles.
36  *
37  * @param App $a App
38  *
39  * @return string
40  * @throws ImagickException
41  * @throws \Friendica\Network\HTTPException\InternalServerErrorException
42  * @throws Exception
43  */
44 function match_content(App $a)
45 {
46         if (!local_user()) {
47                 return '';
48         }
49
50         DI::page()['aside'] .= Widget::findPeople();
51         DI::page()['aside'] .= Widget::follow();
52
53         $_SESSION['return_path'] = DI::args()->getCommand();
54
55         $profile = Profile::getByUID(local_user());
56
57         if (!DBA::isResult($profile)) {
58                 return '';
59         }
60         if (!$profile['pub_keywords'] && (!$profile['prv_keywords'])) {
61                 notice(DI::l10n()->t('No keywords to match. Please add keywords to your profile.'));
62                 return '';
63         }
64
65         $params = [];
66         $tags = trim($profile['pub_keywords'] . ' ' . $profile['prv_keywords']);
67
68         $params['s'] = $tags;
69         $params['n'] = 100;
70
71         if (strlen(DI::config()->get('system', 'directory'))) {
72                 $host = Search::getGlobalDirectory();
73         } else {
74                 $host = DI::baseUrl();
75         }
76
77         $msearch_json = DI::httpRequest()->post($host . '/msearch', $params)->getBody();
78
79         $msearch = json_decode($msearch_json);
80
81         $start = $_GET['start'] ?? 0;
82         $entries = [];
83         $paginate = '';
84
85         if (!empty($msearch->results)) {
86                 for ($i = $start;count($entries) < 10 && $i < $msearch->total; $i++) {
87                         $profile = $msearch->results[$i];
88
89                         // Already known contact
90                         if (!$profile || Contact::getIdForURL($profile->url, local_user(), false)) {
91                                 continue;
92                         }
93
94                         // Workaround for wrong directory photo URL
95                         $profile->photo = str_replace('http:///photo/', Search::getGlobalDirectory() . '/photo/', $profile->photo);
96
97                         $connlnk = DI::baseUrl() . '/follow/?url=' . $profile->url;
98                         $photo_menu = [
99                                 'profile' => [DI::l10n()->t("View Profile"), Contact::magicLink($profile->url)],
100                                 'follow' => [DI::l10n()->t("Connect/Follow"), $connlnk]
101                         ];
102
103                         $contact_details = Contact::getByURL($profile->url, false);
104
105                         $entry = [
106                                 'url'          => Contact::magicLink($profile->url),
107                                 'itemurl'      => $contact_details['addr'] ?? $profile->url,
108                                 'name'         => $profile->name,
109                                 'details'      => $contact_details['location'] ?? '',
110                                 'tags'         => $contact_details['keywords'] ?? '',
111                                 'about'        => $contact_details['about'] ?? '',
112                                 'account_type' => Contact::getAccountType($contact_details),
113                                 'thumb'        => Contact::getThumb($contact_details, $profile->photo),
114                                 'conntxt'      => DI::l10n()->t('Connect'),
115                                 'connlnk'      => $connlnk,
116                                 'img_hover'    => $profile->tags,
117                                 'photo_menu'   => $photo_menu,
118                                 'id'           => $i,
119                         ];
120                         $entries[] = $entry;
121                 }
122
123                 $data = [
124                         'class' => 'pager',
125                         'first' => [
126                                 'url'   => 'match',
127                                 'text'  => DI::l10n()->t('first'),
128                                 'class' => 'previous' . ($start == 0 ? 'disabled' : '')
129                         ],
130                         'next'  => [
131                                 'url'   => 'match?start=' . $i,
132                                 'text'  => DI::l10n()->t('next'),
133                                 'class' =>  'next' . ($i >= $msearch->total ? ' disabled' : '')
134                         ]
135                 ];
136
137                 $tpl = Renderer::getMarkupTemplate('paginate.tpl');
138                 $paginate = Renderer::replaceMacros($tpl, ['pager' => $data]);
139         }
140
141         if (empty($entries)) {
142                 info(DI::l10n()->t('No matches'));
143         }
144
145         $tpl = Renderer::getMarkupTemplate('viewcontact_template.tpl');
146         $o = Renderer::replaceMacros($tpl, [
147                 '$title'    => DI::l10n()->t('Profile Match'),
148                 '$contacts' => $entries,
149                 '$paginate' => $paginate
150         ]);
151
152         return $o;
153 }