3 * @copyright Copyright (C) 2010-2021, the Friendica project
5 * @license GNU AGPL version 3 or any later version
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.
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.
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/>.
22 namespace Friendica\Module;
24 use Friendica\App\BaseURL;
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\Strings;
35 use Friendica\Worker\Delivery;
38 * Suggest friends to a known contact
40 class FriendSuggest extends BaseModule
46 /** @var \Friendica\Contact\FriendSuggest\Repository\FriendSuggest */
47 protected $friendSuggestRepo;
48 /** @var \Friendica\Contact\FriendSuggest\Factory\FriendSuggest */
49 protected $friendSuggestFac;
51 public function __construct(BaseURL $baseUrl, Database $dba, \Friendica\Contact\FriendSuggest\Repository\FriendSuggest $friendSuggestRepo, \Friendica\Contact\FriendSuggest\Factory\FriendSuggest $friendSuggestFac, L10n $l10n, array $parameters = [])
53 parent::__construct($l10n, $parameters);
56 throw new ForbiddenException($this->t('Permission denied.'));
59 $this->baseUrl = $baseUrl;
61 $this->friendSuggestRepo = $friendSuggestRepo;
62 $this->friendSuggestFac = $friendSuggestFac;
65 public function post()
67 $cid = intval($this->parameters['contact']);
69 // We do query the "uid" as well to ensure that it is our contact
70 if (!$this->dba->exists('contact', ['id' => $cid, 'uid' => local_user()])) {
71 throw new NotFoundException($this->t('Contact not found.'));
74 $suggest_contact_id = intval($_POST['suggest']);
75 if (empty($suggest_contact_id)) {
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' => local_user()]);
81 if (empty($contact)) {
82 notice($this->t('Suggested contact not found.'));
86 $note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
88 $suggest = $this->friendSuggestRepo->save($this->friendSuggestFac->createNew(
98 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
100 info($this->t('Friend suggestion sent.'));
103 public function content(): string
105 $cid = intval($this->parameters['contact']);
107 $contact = $this->dba->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]);
108 if (empty($contact)) {
109 notice($this->t('Contact not found.'));
110 $this->baseUrl->redirect();
113 $suggestableContacts = ContactModel::selectToArray(['id', 'name'], [
128 $formattedContacts = [];
130 foreach ($suggestableContacts as $suggestableContact) {
131 $formattedContacts[$suggestableContact['id']] = $suggestableContact['name'];
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' => [
140 $this->t('Suggest a friend for %s', $contact['name']),
145 '$submit' => $this->t('Submit'),