3 * @copyright Copyright (C) 2020, Friendica
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()->insert([
72 'uid' => local_user(),
74 'name' => $contact['name'],
75 'url' => $contact['url'],
76 'request' => $contact['request'],
77 'photo' => $contact['avatar'],
79 'created' => DateTimeFormat::utcNow()
82 Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
84 info(DI::l10n()->t('Friend suggestion sent.'));
87 public static function content(array $parameters = [])
89 $cid = intval($parameters['contact']);
91 $contact = DI::dba()->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]);
92 if (empty($contact)) {
93 notice(DI::l10n()->t('Contact not found.'));
94 DI::baseUrl()->redirect();
97 $contacts = ContactModel::selectToArray(['id', 'name'], [
112 $formattedContacts = [];
114 foreach ($contacts as $contact) {
115 $formattedContacts[$contact['id']] = $contact['name'];
118 $tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
119 return Renderer::replaceMacros($tpl, [
120 '$contact_id' => $cid,
121 '$fsuggest_title' => DI::l10n()->t('Suggest Friends'),
122 '$fsuggest_select' => [
124 DI::l10n()->t('Suggest a friend for %s', $contact['name']),
129 '$submit' => DI::l10n()->t('Submit'),