Merge pull request #8210 from nupplaphil/task/mod_fsuggest

Move mod/fsuggest to src/Module/SuggestFriend
pull/8213/head
Hypolite Petovan 2020-01-31 18:43:32 -05:00 committed by GitHub
commit 96b9619608
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 244 additions and 218 deletions

View File

@ -1,96 +0,0 @@
<?php
/**
* @file mod/fsuggest.php
*/
use Friendica\App;
use Friendica\Core\ACL;
use Friendica\Core\Worker;
use Friendica\Database\DBA;
use Friendica\DI;
use Friendica\Util\DateTimeFormat;
use Friendica\Util\Strings;
use Friendica\Worker\Delivery;
function fsuggest_post(App $a)
{
if (!local_user()) {
return;
}
if ($a->argc != 2) {
return;
}
$contact_id = intval($a->argv[1]);
if (empty($contact_id)) {
return;
}
// We do query the "uid" as well to ensure that it is our contact
if (!DBA::exists('contact', ['id' => $contact_id, 'uid' => local_user()])) {
notice(DI::l10n()->t('Contact not found.') . EOL);
return;
}
$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 = DBA::selectFirst('contact', ['name', 'url', 'request', 'avatar'], ['id' => $suggest_contact_id, 'uid' => local_user()]);
if (!DBA::isResult($contact)) {
notice(DI::l10n()->t('Suggested contact not found.') . EOL);
return;
}
$note = Strings::escapeHtml(trim($_POST['note'] ?? ''));
$fields = ['uid' => local_user(),'cid' => $contact_id, 'name' => $contact['name'],
'url' => $contact['url'], 'request' => $contact['request'],
'photo' => $contact['avatar'], 'note' => $note, 'created' => DateTimeFormat::utcNow()];
DBA::insert('fsuggest', $fields);
Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, DBA::lastInsertId());
info(DI::l10n()->t('Friend suggestion sent.') . EOL);
}
function fsuggest_content(App $a)
{
if (! local_user()) {
notice(DI::l10n()->t('Permission denied.') . EOL);
return;
}
if ($a->argc != 2) {
return;
}
$contact_id = intval($a->argv[1]);
$contact = DBA::selectFirst('contact', [], ['id' => $contact_id, 'uid' => local_user()]);
if (! DBA::isResult($contact)) {
notice(DI::l10n()->t('Contact not found.') . EOL);
return;
}
$o = '<h3>' . DI::l10n()->t('Suggest Friends') . '</h3>';
$o .= '<div id="fsuggest-desc" >' . DI::l10n()->t('Suggest a friend for %s', $contact['name']) . '</div>';
$o .= '<form id="fsuggest-form" action="fsuggest/' . $contact_id . '" method="post" >';
$o .= ACL::getSuggestContactSelectHTML(
'suggest',
'suggest-select',
['size' => 4, 'exclude' => $contact_id, 'networks' => 'DFRN_ONLY', 'single' => true]
);
$o .= '<div id="fsuggest-submit-wrapper"><input id="fsuggest-submit" type="submit" name="submit" value="' . DI::l10n()->t('Submit') . '" /></div>';
$o .= '</form>';
return $o;
}

View File

@ -0,0 +1,10 @@
<?php
namespace Friendica\Collection;
use Friendica\BaseCollection;
class FSuggests extends BaseCollection
{
}

View File

@ -19,119 +19,6 @@ use Friendica\Model\Group;
*/
class ACL
{
/**
* Returns a select input tag with all the contact of the local user
*
* @param string $selname Name attribute of the select input tag
* @param string $selclass Class attribute of the select input tag
* @param array $options Available options:
* - size: length of the select box
* - mutual_friends: Only used for the hook
* - single: Only used for the hook
* - exclude: Only used for the hook
* @param array $preselected Contact ID that should be already selected
* @return string
* @throws \Exception
*/
public static function getSuggestContactSelectHTML($selname, $selclass, array $options = [], array $preselected = [])
{
$a = DI::app();
$networks = null;
$size = ($options['size'] ?? 0) ?: 4;
$mutual = !empty($options['mutual_friends']);
$single = !empty($options['single']) && empty($options['multiple']);
$exclude = $options['exclude'] ?? false;
switch (($options['networks'] ?? '') ?: Protocol::PHANTOM) {
case 'DFRN_ONLY':
$networks = [Protocol::DFRN];
break;
case 'PRIVATE':
$networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA];
break;
case 'TWO_WAY':
if (!empty($a->user['prvnets'])) {
$networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA];
} else {
$networks = [Protocol::ACTIVITYPUB, Protocol::DFRN, Protocol::MAIL, Protocol::DIASPORA, Protocol::OSTATUS];
}
break;
default: /// @TODO Maybe log this call?
break;
}
$x = ['options' => $options, 'size' => $size, 'single' => $single, 'mutual' => $mutual, 'exclude' => $exclude, 'networks' => $networks];
Hook::callAll('contact_select_options', $x);
$o = '';
$sql_extra = '';
if (!empty($x['mutual'])) {
$sql_extra .= sprintf(" AND `rel` = %d ", intval(Contact::FRIEND));
}
if (!empty($x['exclude'])) {
$sql_extra .= sprintf(" AND `id` != %d ", intval($x['exclude']));
}
if (!empty($x['networks'])) {
/// @TODO rewrite to foreach()
array_walk($x['networks'], function (&$value) {
$value = "'" . DBA::escape($value) . "'";
});
$str_nets = implode(',', $x['networks']);
$sql_extra .= " AND `network` IN ( $str_nets ) ";
}
$tabindex = (!empty($options['tabindex']) ? 'tabindex="' . $options["tabindex"] . '"' : '');
if (!empty($x['single'])) {
$o .= "<select name=\"$selname\" id=\"$selclass\" class=\"$selclass\" size=\"" . $x['size'] . "\" $tabindex >\r\n";
} else {
$o .= "<select name=\"{$selname}[]\" id=\"$selclass\" class=\"$selclass\" multiple=\"multiple\" size=\"" . $x['size'] . "$\" $tabindex >\r\n";
}
$stmt = DBA::p("SELECT `id`, `name`, `url`, `network` FROM `contact`
WHERE `uid` = ? AND NOT `self` AND NOT `blocked` AND NOT `pending` AND NOT `archive` AND NOT `deleted` AND `notify` != ''
$sql_extra
ORDER BY `name` ASC ", intval(local_user())
);
$contacts = DBA::toArray($stmt);
$arr = ['contact' => $contacts, 'entry' => $o];
// e.g. 'network_pre_contact_deny', 'profile_pre_contact_allow'
Hook::callAll(DI::module()->getName() . '_pre_' . $selname, $arr);
if (DBA::isResult($contacts)) {
foreach ($contacts as $contact) {
if (in_array($contact['id'], $preselected)) {
$selected = ' selected="selected" ';
} else {
$selected = '';
}
$trimmed = mb_substr($contact['name'], 0, 20);
$o .= "<option value=\"{$contact['id']}\" $selected title=\"{$contact['name']}|{$contact['url']}\" >$trimmed</option>\r\n";
}
}
$o .= '</select>' . PHP_EOL;
Hook::callAll(DI::module()->getName() . '_post_' . $selname, $o);
return $o;
}
/**
* Returns a select input tag with all the contact of the local user
*

View File

@ -300,6 +300,14 @@ abstract class DI
// "Repository" namespace
//
/**
* @return Repository\FSuggest;
*/
public static function fsuggest()
{
return self::$dice->create(Repository\FSuggest::class);
}
/**
* @return Repository\Introduction
*/

22
src/Model/FSuggest.php Normal file
View File

@ -0,0 +1,22 @@
<?php
namespace Friendica\Model;
use Friendica\BaseModel;
/**
* Model for interacting with a friend suggestion
*
* @property int uid
* @property int cid
* @property string name
* @property string url
* @property string request
* @property string photo
* @property string note
* @property string created
*/
class FSuggest extends BaseModel
{
}

View File

@ -0,0 +1,113 @@
<?php
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
{
public static function init(array $parameters = [])
{
if (!local_user()) {
throw new ForbiddenException(DI::l10n()->t('Permission denied.'));
}
}
public static function post(array $parameters = [])
{
$cid = intval($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'] ?? ''));
$suggest = DI::fsuggest()->insert([
'uid' => local_user(),
'cid' => $cid,
'name' => $contact['name'],
'url' => $contact['url'],
'request' => $contact['request'],
'photo' => $contact['avatar'],
'note' => $note,
'created' => DateTimeFormat::utcNow()
]);
Worker::add(PRIORITY_HIGH, 'Notifier', Delivery::SUGGESTION, $suggest->id);
info(DI::l10n()->t('Friend suggestion sent.'));
}
public static function content(array $parameters = [])
{
$cid = intval($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();
}
$contacts = ContactModel::selectToArray(['id', 'name'], [
'`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 = [];
foreach ($contacts as $contact) {
$formattedContacts[$contact['id']] = $contact['name'];
}
$tpl = Renderer::getMarkupTemplate('fsuggest.tpl');
return Renderer::replaceMacros($tpl, [
'$contact_id' => $cid,
'$fsuggest_title' => DI::l10n()->t('Suggest Friends'),
'$fsuggest_select' => [
'suggest',
DI::l10n()->t('Suggest a friend for %s', $contact['name']),
'',
'',
$formattedContacts,
],
'$submit' => DI::l10n()->t('Submit'),
]);
}
}

View File

@ -1510,14 +1510,14 @@ class Transmitter
{
$owner = User::getOwnerDataById($uid);
$suggestion = DBA::selectFirst('fsuggest', ['url', 'note', 'created'], ['id' => $suggestion_id]);
$suggestion = DI::fsuggest()->getById($suggestion_id);
$data = ['@context' => ActivityPub::CONTEXT,
'id' => DI::baseUrl() . '/activity/' . System::createGUID(),
'type' => 'Announce',
'actor' => $owner['url'],
'object' => $suggestion['url'],
'content' => $suggestion['note'],
'object' => $suggestion->url,
'content' => $suggestion->note,
'instrument' => self::getService(),
'to' => [ActivityPub::PUBLIC_COLLECTION],
'cc' => []];

View File

@ -0,0 +1,74 @@
<?php
namespace Friendica\Repository;
use Friendica\BaseRepository;
use Friendica\Collection;
use Friendica\Model;
class FSuggest extends BaseRepository
{
protected static $table_name = 'fsuggest';
protected static $model_class = Model\FSuggest::class;
protected static $collection_class = Collection\FSuggests::class;
/**
* @param array $data
* @return Model\FSuggest
*/
protected function create(array $data)
{
return new Model\FSuggest($this->dba, $this->logger, $data);
}
/**
* Returns the Friend Suggest based on it's ID
*
* @param int $id The id of the fsuggest
*
* @return Model\FSuggest
*
* @throws \Friendica\Network\HTTPException\NotFoundException
*/
public function getById(int $id)
{
return $this->selectFirst(['id' => $id]);
}
/**
* @param array $condition
* @return Model\FSuggest
* @throws \Friendica\Network\HTTPException\NotFoundException
*/
public function selectFirst(array $condition)
{
return parent::selectFirst($condition);
}
/**
* @param array $condition
* @param array $params
* @return Collection\FSuggests
* @throws \Exception
*/
public function select(array $condition = [], array $params = [])
{
return parent::select($condition, $params);
}
/**
* @param array $condition
* @param array $params
* @param int|null $max_id
* @param int|null $since_id
* @param int $limit
* @return Collection\FSuggests
* @throws \Exception
*/
public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT)
{
return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit);
}
}

View File

@ -70,12 +70,9 @@ class Notifier
'APDelivery', $cmd, $target_id, $inbox, $uid);
}
} elseif ($cmd == Delivery::SUGGESTION) {
$suggest = DBA::selectFirst('fsuggest', ['uid', 'cid'], ['id' => $target_id]);
if (!DBA::isResult($suggest)) {
return;
}
$uid = $suggest['uid'];
$recipients[] = $suggest['cid'];
$suggest = DI::fsuggest()->getById($target_id);
$uid = $suggest->uid;
$recipients[] = $suggest->cid;
} elseif ($cmd == Delivery::REMOVAL) {
return self::notifySelfRemoval($target_id, $a->queue['priority'], $a->queue['created']);
} elseif ($cmd == Delivery::RELOCATION) {

View File

@ -132,6 +132,8 @@ return [
'/following/{owner}' => [Module\Following::class, [R::GET]],
'/friendica[/json]' => [Module\Friendica::class, [R::GET]],
'/fsuggest/{contact:\d+}' => [Module\FriendSuggest::class, [R::GET, R::POST]],
'/group' => [
'[/]' => [Module\Group::class, [R::GET, R::POST]],
'/{group:\d+}' => [Module\Group::class, [R::GET, R::POST]],

View File

@ -0,0 +1,9 @@
<div class="generic-page-wrapper">
<h2>{{$fsuggest_title}}</h2>
<form id="fsuggest-form" action="fsuggest/{{$contact_id}}" method="post">
{{include file="field_select.tpl" field=$fsuggest_select}}
<div id="fsuggest-submit-wrapper">
<input id="fsuggest-submit" type="submit" name="submit" value="{{$submit}}" />
</div>
</form>
</div>