friendica/src/Module/FriendSuggest.php

132 lines
3.6 KiB
PHP
Raw Normal View History

<?php
2020-02-09 14:45:36 +00:00
/**
2021-03-29 06:40:20 +00:00
* @copyright Copyright (C) 2010-2021, the Friendica project
2020-02-09 14:45:36 +00:00
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
*/
namespace Friendica\Module;
use Friendica\BaseModule;
use Friendica\Core\Protocol;
use Friendica\Core\Renderer;
use Friendica\Core\Worker;
use Friendica\DI;
use Friendica\Model\Contact as ContactModel;
use Friendica\Network\HTTPException\ForbiddenException;
use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
use Friendica\Worker\Delivery;
/**
* Suggest friends to a known contact
*/
class FriendSuggest extends BaseModule
{
2021-11-14 22:13:47 +00:00
public function init()
{
2020-01-31 22:51:11 +00:00
if (!local_user()) {
throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
}
}
2021-11-14 22:13:47 +00:00
public function post()
{
$cid = intval(static::$parameters['contact']);
// We do query the "uid" as well to ensure that it is our contact
if (!DI::dba()->exists('contact', ['id' => $cid, 'uid' => local_user()])) {
throw new NotFoundException(DI::l10n()->t('Contact not found.'));
}
$suggest_contact_id = intval($_POST['suggest']);
if (empty($suggest_contact_id)) {
return;
}
// We do query the "uid" as well to ensure that it is our contact
$contact = DI::dba()->selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]);
if (empty($contact)) {
notice(DI::l10n()->t('Suggested contact not found.'));
return;
}
$note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
2021-10-21 20:57:13 +00:00
$suggest = DI::fsuggest()->save(DI::fsuggestFactory()->createNew(
local_user(),
$cid,
$contact['name'],
$contact['url'],
$contact['request'],
$contact['avatar'],
$note
));
Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
info(DI::l10n()->t('Friend suggestion sent.'));
}
2021-11-14 22:13:47 +00:00
public function content(): string
{
$cid = intval(static::$parameters['contact']);
$contact = DI::dba()->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]);
if (empty($contact)) {
notice(DI::l10n()->t('Contact not found.'));
DI::baseUrl()->redirect();
}
2021-10-22 17:10:55 +00:00
$suggestableContacts = ContactModel::selectToArray(['id', 'name'], [
2020-01-31 23:27:46 +00:00
'`uid` = ?
AND `id` != ?
AND `network` = ?
AND NOT `self`
AND NOT `blocked`
AND NOT `pending`
AND NOT `archive`
AND NOT `deleted`
AND `notify` != ""',
local_user(),
$cid,
Protocol::DFRN,
]);
$formattedContacts = [];
2021-10-22 17:10:55 +00:00
foreach ($suggestableContacts as $suggestableContact) {
$formattedContacts[$suggestableContact['id']] = $suggestableContact['name'];
}
$tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
return Renderer::replaceMacros($tpl, [
2020-01-31 22:51:11 +00:00
'$contact_id' => $cid,
'$fsuggest_title' => DI::l10n()->t('Suggest Friends'),
'$fsuggest_select' => [
'suggest',
DI::l10n()->t('Suggest a friend for %s', $contact['name']),
'',
'',
$formattedContacts,
],
2020-01-31 22:51:11 +00:00
'$submit' => DI::l10n()->t('Submit'),
]);
}
}