]> git.mxchange.org Git - friendica.git/blob - src/Module/FriendSuggest.php
Merge pull request #12025 from annando/no-boot-src-module
[friendica.git] / src / Module / FriendSuggest.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 namespace Friendica\Module;
23
24 use Friendica\App;
25 use Friendica\BaseModule;
26 use Friendica\Core\L10n;
27 use Friendica\Core\Protocol;
28 use Friendica\Core\Renderer;
29 use Friendica\Core\Session;
30 use Friendica\Core\Worker;
31 use Friendica\Database\Database;
32 use Friendica\DI;
33 use Friendica\Model\Contact as ContactModel;
34 use Friendica\Network\HTTPException\ForbiddenException;
35 use Friendica\Network\HTTPException\NotFoundException;
36 use Friendica\Util\Profiler;
37 use Friendica\Util\Strings;
38 use Friendica\Worker\Delivery;
39 use Psr\Log\LoggerInterface;
40
41 /**
42  * Suggest friends to a known contact
43  */
44 class FriendSuggest extends BaseModule
45 {
46         /** @var Database */
47         protected $dba;
48         /** @var \Friendica\Contact\FriendSuggest\Repository\FriendSuggest */
49         protected $friendSuggestRepo;
50         /** @var \Friendica\Contact\FriendSuggest\Factory\FriendSuggest */
51         protected $friendSuggestFac;
52
53         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, Database $dba, \Friendica\Contact\FriendSuggest\Repository\FriendSuggest $friendSuggestRepo, \Friendica\Contact\FriendSuggest\Factory\FriendSuggest $friendSuggestFac, array $server, array $parameters = [])
54         {
55                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
56
57                 if (!Session::getLocalUser()) {
58                         throw new ForbiddenException($this->t('Permission denied.'));
59                 }
60
61                 $this->dba               = $dba;
62                 $this->friendSuggestRepo = $friendSuggestRepo;
63                 $this->friendSuggestFac  = $friendSuggestFac;
64         }
65
66         protected function post(array $request = [])
67         {
68                 $cid = intval($this->parameters['contact']);
69
70                 // We do query the "uid" as well to ensure that it is our contact
71                 if (!$this->dba->exists('contact', ['id' => $cid, 'uid' => Session::getLocalUser()])) {
72                         throw new NotFoundException($this->t('Contact not found.'));
73                 }
74
75                 $suggest_contact_id = intval($_POST['suggest']);
76                 if (empty($suggest_contact_id)) {
77                         return;
78                 }
79
80                 // We do query the "uid" as well to ensure that it is our contact
81                 $contact = $this->dba->selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => Session::getLocalUser()]);
82                 if (empty($contact)) {
83                         DI::sysmsg()->addNotice($this->t('Suggested contact not found.'));
84                         return;
85                 }
86
87                 $note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
88
89                 $suggest = $this->friendSuggestRepo->save($this->friendSuggestFac->createNew(
90                         Session::getLocalUser(),
91                         $cid,
92                         $contact['name'],
93                         $contact['url'],
94                         $contact['request'],
95                         $contact['avatar'],
96                         $note
97                 ));
98
99                 Worker::add(Worker::PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
100
101                 DI::sysmsg()->addInfo($this->t('Friend suggestion sent.'));
102         }
103
104         protected function content(array $request = []): string
105         {
106                 $cid = intval($this->parameters['contact']);
107
108                 $contact = $this->dba->selectFirst('contact', [], ['id' => $cid, 'uid' => Session::getLocalUser()]);
109                 if (empty($contact)) {
110                         DI::sysmsg()->addNotice($this->t('Contact not found.'));
111                         $this->baseUrl->redirect();
112                 }
113
114                 $suggestableContacts = ContactModel::selectToArray(['id', 'name'], [
115                         '`uid` = ? 
116                         AND `id` != ? 
117                         AND `network` = ? 
118                         AND NOT `self` 
119                         AND NOT `blocked` 
120                         AND NOT `pending` 
121                         AND NOT `archive` 
122                         AND NOT `deleted` 
123                         AND `notify` != ""',
124                         Session::getLocalUser(),
125                         $cid,
126                         Protocol::DFRN,
127                 ]);
128
129                 $formattedContacts = [];
130
131                 foreach ($suggestableContacts as $suggestableContact) {
132                         $formattedContacts[$suggestableContact['id']] = $suggestableContact['name'];
133                 }
134
135                 $tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
136                 return Renderer::replaceMacros($tpl, [
137                         '$contact_id'      => $cid,
138                         '$fsuggest_title'  => $this->t('Suggest Friends'),
139                         '$fsuggest_select' => [
140                                 'suggest',
141                                 $this->t('Suggest a friend for %s', $contact['name']),
142                                 '',
143                                 '',
144                                 $formattedContacts,
145                         ],
146                         '$submit'          => $this->t('Submit'),
147                 ]);
148         }
149 }