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