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