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\BaseModule;
25 use Friendica\Core\Protocol;
26 use Friendica\Core\Renderer;
27 use Friendica\Core\Worker;
29 use Friendica\Model\Contact as ContactModel;
30 use Friendica\Network\HTTPException\ForbiddenException;
31 use Friendica\Network\HTTPException\NotFoundException;
32 use Friendica\Util\DateTimeFormat;
33 use Friendica\Util\Strings;
34 use Friendica\Worker\Delivery;
37 * Suggest friends to a known contact
39 class FriendSuggest extends BaseModule
41 public static function init(array $parameters = [])
44 throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
48 public static function post(array $parameters = [])
50 $cid = intval($parameters['contact']);
52 // We do query the "uid" as well to ensure that it is our contact
53 if (!DI::dba()->exists('contact', ['id' => $cid, 'uid' => local_user()])) {
54 throw new NotFoundException(DI::l10n()->t('Contact not found.'));
57 $suggest_contact_id = intval($_POST['suggest']);
58 if (empty($suggest_contact_id)) {
62 // We do query the "uid" as well to ensure that it is our contact
63 $contact = DI::dba()->selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]);
64 if (empty($contact)) {
65 notice(DI::l10n()->t('Suggested contact not found.'));
69 $note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
71 $suggest = DI::fsuggest()->save(DI::fsuggestFactory()->createNew(
81 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
83 info(DI::l10n()->t('Friend suggestion sent.'));
86 public static function content(array $parameters = [])
88 $cid = intval($parameters['contact']);
90 $contact = DI::dba()->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]);
91 if (empty($contact)) {
92 notice(DI::l10n()->t('Contact not found.'));
93 DI::baseUrl()->redirect();
96 $suggestableContacts = ContactModel::selectToArray(['id', 'name'], [
111 $formattedContacts = [];
113 foreach ($suggestableContacts as $suggestableContact) {
114 $formattedContacts[$suggestableContact['id']] = $suggestableContact['name'];
117 $tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
118 return Renderer::replaceMacros($tpl, [
119 '$contact_id' => $cid,
120 '$fsuggest_title' => DI::l10n()->t('Suggest Friends'),
121 '$fsuggest_select' => [
123 DI::l10n()->t('Suggest a friend for %s', $contact['name']),
128 '$submit' => DI::l10n()->t('Submit'),