2020-01-31 23:50:46 +01:00
|
|
|
<?php
|
2020-02-09 15:45:36 +01:00
|
|
|
/**
|
2021-03-29 08:40:20 +02:00
|
|
|
* @copyright Copyright (C) 2010-2021, the Friendica project
|
2020-02-09 15:45:36 +01: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/>.
|
|
|
|
*
|
|
|
|
*/
|
2020-01-31 23:50:46 +01:00
|
|
|
|
|
|
|
namespace Friendica\Module;
|
|
|
|
|
2021-11-20 15:38:03 +01:00
|
|
|
use Friendica\App;
|
2020-01-31 23:50:46 +01:00
|
|
|
use Friendica\BaseModule;
|
2021-11-19 20:18:48 +01:00
|
|
|
use Friendica\Core\L10n;
|
2020-01-31 23:50:46 +01:00
|
|
|
use Friendica\Core\Protocol;
|
|
|
|
use Friendica\Core\Renderer;
|
|
|
|
use Friendica\Core\Worker;
|
2021-11-19 20:18:48 +01:00
|
|
|
use Friendica\Database\Database;
|
2020-01-31 23:50:46 +01:00
|
|
|
use Friendica\Model\Contact as ContactModel;
|
|
|
|
use Friendica\Network\HTTPException\ForbiddenException;
|
|
|
|
use Friendica\Network\HTTPException\NotFoundException;
|
2021-11-20 15:38:03 +01:00
|
|
|
use Friendica\Util\Profiler;
|
2020-01-31 23:50:46 +01:00
|
|
|
use Friendica\Util\Strings;
|
|
|
|
use Friendica\Worker\Delivery;
|
2021-11-20 15:38:03 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2020-01-31 23:50:46 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Suggest friends to a known contact
|
|
|
|
*/
|
|
|
|
class FriendSuggest extends BaseModule
|
|
|
|
{
|
2021-11-19 20:18:48 +01:00
|
|
|
/** @var Database */
|
|
|
|
protected $dba;
|
|
|
|
/** @var \Friendica\Contact\FriendSuggest\Repository\FriendSuggest */
|
|
|
|
protected $friendSuggestRepo;
|
|
|
|
/** @var \Friendica\Contact\FriendSuggest\Factory\FriendSuggest */
|
|
|
|
protected $friendSuggestFac;
|
|
|
|
|
2021-11-20 15:38:03 +01:00
|
|
|
public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler,Database $dba, \Friendica\Contact\FriendSuggest\Repository\FriendSuggest $friendSuggestRepo, \Friendica\Contact\FriendSuggest\Factory\FriendSuggest $friendSuggestFac, array $server, array $parameters = [])
|
2020-01-31 23:50:46 +01:00
|
|
|
{
|
2021-11-20 15:38:03 +01:00
|
|
|
parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $server, $parameters);
|
2021-11-19 20:18:48 +01:00
|
|
|
|
2020-01-31 23:51:11 +01:00
|
|
|
if (!local_user()) {
|
2021-11-19 20:18:48 +01:00
|
|
|
throw new ForbiddenException($this->t('Permission denied.'));
|
2020-01-31 23:50:46 +01:00
|
|
|
}
|
2021-11-19 20:18:48 +01:00
|
|
|
|
|
|
|
$this->dba = $dba;
|
|
|
|
$this->friendSuggestRepo = $friendSuggestRepo;
|
|
|
|
$this->friendSuggestFac = $friendSuggestFac;
|
2020-01-31 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
2021-11-20 15:38:03 +01:00
|
|
|
protected function post(array $request = [], array $post = [])
|
2020-01-31 23:50:46 +01:00
|
|
|
{
|
2021-11-14 23:19:25 +01:00
|
|
|
$cid = intval($this->parameters['contact']);
|
2020-01-31 23:50:46 +01:00
|
|
|
|
|
|
|
// We do query the "uid" as well to ensure that it is our contact
|
2021-11-19 20:18:48 +01:00
|
|
|
if (!$this->dba->exists('contact', ['id' => $cid, 'uid' => local_user()])) {
|
|
|
|
throw new NotFoundException($this->t('Contact not found.'));
|
2020-01-31 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$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
|
2021-11-19 20:18:48 +01:00
|
|
|
$contact = $this->dba->selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]);
|
2020-01-31 23:50:46 +01:00
|
|
|
if (empty($contact)) {
|
2021-11-19 20:18:48 +01:00
|
|
|
notice($this->t('Suggested contact not found.'));
|
2020-01-31 23:50:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
|
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
$suggest = $this->friendSuggestRepo->save($this->friendSuggestFac->createNew(
|
2021-10-21 22:57:13 +02:00
|
|
|
local_user(),
|
|
|
|
$cid,
|
|
|
|
$contact['name'],
|
|
|
|
$contact['url'],
|
|
|
|
$contact['request'],
|
|
|
|
$contact['avatar'],
|
|
|
|
$note
|
|
|
|
));
|
2020-01-31 23:50:46 +01:00
|
|
|
|
|
|
|
Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
|
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
info($this->t('Friend suggestion sent.'));
|
2020-01-31 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
2021-11-20 15:38:03 +01:00
|
|
|
protected function content(array $request = []): string
|
2020-01-31 23:50:46 +01:00
|
|
|
{
|
2021-11-14 23:19:25 +01:00
|
|
|
$cid = intval($this->parameters['contact']);
|
2020-01-31 23:50:46 +01:00
|
|
|
|
2021-11-19 20:18:48 +01:00
|
|
|
$contact = $this->dba->selectFirst('contact', [], ['id' => $cid, 'uid' => local_user()]);
|
2020-01-31 23:50:46 +01:00
|
|
|
if (empty($contact)) {
|
2021-11-19 20:18:48 +01:00
|
|
|
notice($this->t('Contact not found.'));
|
|
|
|
$this->baseUrl->redirect();
|
2020-01-31 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
2021-10-22 19:10:55 +02:00
|
|
|
$suggestableContacts = ContactModel::selectToArray(['id', 'name'], [
|
2020-02-01 00:27:46 +01:00
|
|
|
'`uid` = ?
|
|
|
|
AND `id` != ?
|
|
|
|
AND `network` = ?
|
|
|
|
AND NOT `self`
|
|
|
|
AND NOT `blocked`
|
|
|
|
AND NOT `pending`
|
|
|
|
AND NOT `archive`
|
|
|
|
AND NOT `deleted`
|
|
|
|
AND `notify` != ""',
|
2020-01-31 23:50:46 +01:00
|
|
|
local_user(),
|
|
|
|
$cid,
|
|
|
|
Protocol::DFRN,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$formattedContacts = [];
|
|
|
|
|
2021-10-22 19:10:55 +02:00
|
|
|
foreach ($suggestableContacts as $suggestableContact) {
|
|
|
|
$formattedContacts[$suggestableContact['id']] = $suggestableContact['name'];
|
2020-01-31 23:50:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
|
|
|
|
return Renderer::replaceMacros($tpl, [
|
2020-01-31 23:51:11 +01:00
|
|
|
'$contact_id' => $cid,
|
2021-11-19 20:18:48 +01:00
|
|
|
'$fsuggest_title' => $this->t('Suggest Friends'),
|
2020-01-31 23:50:46 +01:00
|
|
|
'$fsuggest_select' => [
|
|
|
|
'suggest',
|
2021-11-19 20:18:48 +01:00
|
|
|
$this->t('Suggest a friend for %s', $contact['name']),
|
2020-01-31 23:50:46 +01:00
|
|
|
'',
|
|
|
|
'',
|
|
|
|
$formattedContacts,
|
|
|
|
],
|
2021-11-19 20:18:48 +01:00
|
|
|
'$submit' => $this->t('Submit'),
|
2020-01-31 23:50:46 +01:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|