Use ProfileField::selectPublicFieldsByUserId

This commit is contained in:
Philipp Holzer 2021-10-08 19:45:20 +02:00
parent 1c0f92c382
commit a9981c792e
Signed by: nupplaPhil
GPG key ID: 24A7501396EB5432
9 changed files with 170 additions and 40 deletions

View file

@ -460,6 +460,11 @@ abstract class DI
return self::$dice->create(Repository\ProfileField::class); return self::$dice->create(Repository\ProfileField::class);
} }
public static function profileFieldNew(): Profile\ProfileField\Depository\ProfileField
{
return self::$dice->create(Profile\ProfileField\Depository\ProfileField::class);
}
public static function notification(): Navigation\Notifications\Depository\Notification public static function notification(): Navigation\Notifications\Depository\Notification
{ {
return self::$dice->create(Navigation\Notifications\Depository\Notification::class); return self::$dice->create(Navigation\Notifications\Depository\Notification::class);

View file

@ -23,7 +23,7 @@ namespace Friendica\Factory\Api\Mastodon;
use Friendica\BaseFactory; use Friendica\BaseFactory;
use Friendica\Collection\Api\Mastodon\Fields; use Friendica\Collection\Api\Mastodon\Fields;
use Friendica\Collection\ProfileFields; use Friendica\Profile\ProfileField\Collection\ProfileFields;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\Profile\ProfileField\Entity\ProfileField; use Friendica\Profile\ProfileField\Entity\ProfileField;
use Friendica\Network\HTTPException; use Friendica\Network\HTTPException;

View file

@ -21,7 +21,7 @@
namespace Friendica\Module\Api\Friendica\Profile; namespace Friendica\Module\Api\Friendica\Profile;
use Friendica\Collection\ProfileFields; use Friendica\Profile\ProfileField\Collection\ProfileFields;
use Friendica\Content\Text\BBCode; use Friendica\Content\Text\BBCode;
use Friendica\DI; use Friendica\DI;
use Friendica\Model\Contact; use Friendica\Model\Contact;
@ -45,7 +45,7 @@ class Show extends BaseApi
$profile = Profile::getByUID($uid); $profile = Profile::getByUID($uid);
$profileFields = DI::profileField()->select(['uid' => $uid, 'psid' => PermissionSet::PUBLIC]); $profileFields = DI::profileFieldNew()->selectPublicFieldsByUserId($uid);
$profile = self::formatProfile($profile, $profileFields); $profile = self::formatProfile($profile, $profileFields);

View file

@ -19,7 +19,7 @@
* *
*/ */
namespace Friendica\Collection; namespace Friendica\Profile\ProfileField\Collection;
use Friendica\BaseCollection; use Friendica\BaseCollection;

View file

@ -0,0 +1,56 @@
<?php
namespace Friendica\Profile\ProfileField\Depository;
use Friendica\BaseDepository;
use Friendica\Database\Database;
use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Profile\ProfileField\Factory;
use Friendica\Profile\ProfileField\Entity;
use Friendica\Profile\ProfileField\Collection;
use Friendica\Security\PermissionSet\Depository\PermissionSet;
use Psr\Log\LoggerInterface;
class ProfileField extends BaseDepository
{
/** @var Factory\ProfileField */
protected $factory;
protected static $table_name = 'profile_field';
public function __construct(Database $database, LoggerInterface $logger, Factory\ProfileField $factory)
{
parent::__construct($database, $logger, $factory);
}
/**
* @param array $condition
* @param array $params
* @return Entity\ProfileField
* @throws NotFoundException
*/
private function selectOne(array $condition, array $params = []): Entity\ProfileField
{
return parent::_selectOne($condition, $params);
}
private function select(array $condition, array $params = []): Collection\ProfileFields
{
return new Collection\ProfileFields(parent::_select($condition, $params)->getArrayCopy());
}
/**
* Returns all public available ProfileFields for a specific user
*
* @param int $uid the user id
*
* @return Collection\ProfileFields
*/
public function selectPublicFieldsByUserId(int $uid): Collection\ProfileFields
{
return $this->select([
'uid' => $uid,
'psid' => PermissionSet::PUBLIC,
]);
}
}

View file

@ -21,12 +21,10 @@
namespace Friendica\Profile\ProfileField\Entity; namespace Friendica\Profile\ProfileField\Entity;
use Friendica\BaseModel; use Friendica\BaseEntity;
use Friendica\Database\Database; use Friendica\Profile\ProfileField\Exception\UnexpectedPermissionSetException;
use Friendica\Network\HTTPException\NotFoundException;
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository; use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
use Friendica\Security\PermissionSet\Entity\PermissionSet; use Friendica\Security\PermissionSet\Entity\PermissionSet;
use Psr\Log\LoggerInterface;
/** /**
* Custom profile field model class. * Custom profile field model class.
@ -34,40 +32,61 @@ use Psr\Log\LoggerInterface;
* Custom profile fields are user-created arbitrary profile fields that can be assigned a permission set to restrict its * Custom profile fields are user-created arbitrary profile fields that can be assigned a permission set to restrict its
* display to specific Friendica contacts as it requires magic authentication to work. * display to specific Friendica contacts as it requires magic authentication to work.
* *
* @property int uid * @property-read int|null $id
* @property int order * @property-read int $uid
* @property int psid * @property-read int $order
* @property string label * @property-read int $permissionSetId
* @property string value * @property-read string $label
* @property string created * @property-read string $value
* @property string edited * @property-read \DateTime $created
* @property PermissionSet permissionSet * @property-read \DateTime $edited
* @property PermissionSet $permissionSet
*/ */
class ProfileField extends BaseModel class ProfileField extends BaseEntity
{ {
/** @var int|null */
protected $id;
/** @var PermissionSet */ /** @var PermissionSet */
private $permissionSet; protected $permissionSet;
/** @var PermissionSetDepository */ /** @var PermissionSetDepository */
private $permissionSetDepository; protected $permissionSetDepository;
/** @var int */
protected $uid;
/** @var int */
protected $order;
/** @var int */
protected $psid;
/** @var string */
protected $label;
/** @var string */
protected $value;
/** @var \DateTime */
protected $created;
/** @var \DateTime */
protected $edited;
public function __construct(Database $dba, LoggerInterface $logger, PermissionSetDepository $permissionSetDepository, array $data = []) public function __construct(PermissionSetDepository $permissionSetDepository, int $uid, int $order, int $permissionSetId, string $label, string $value, \DateTime $created, \DateTime $edited, int $id = null)
{ {
parent::__construct($dba, $logger, $data);
$this->permissionSetDepository = $permissionSetDepository; $this->permissionSetDepository = $permissionSetDepository;
$this->uid = $uid;
$this->order = $order;
$this->psid = $permissionSetId;
$this->label = $label;
$this->value = $value;
$this->created = $created;
$this->edited = $edited;
$this->id = $id;
} }
public function __get($name) public function __get($name)
{ {
$this->checkValid();
switch ($name) { switch ($name) {
case 'permissionSet': case 'permissionSet':
if (empty($this->permissionSet)) { if (empty($this->permissionSet)) {
$permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $this->uid); $permissionSet = $this->permissionSetDepository->selectOneById($this->psid, $this->uid);
if ($permissionSet->uid !== $this->uid) { if ($permissionSet->uid !== $this->uid) {
throw new NotFoundException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $this->uid)); throw new UnexpectedPermissionSetException(sprintf('PermissionSet %d (user-id: %d) for ProfileField %d (user-id: %d) is invalid.', $permissionSet->id, $permissionSet->uid, $this->id, $this->uid));
} }
$this->permissionSet = $permissionSet; $this->permissionSet = $permissionSet;

View file

@ -0,0 +1,7 @@
<?php
namespace Friendica\Profile\ProfileField\Exception;
class UnexpectedPermissionSetException extends \Exception
{
}

View file

@ -0,0 +1,39 @@
<?php
namespace Friendica\Profile\ProfileField\Factory;
use Friendica\BaseFactory;
use Friendica\Security\PermissionSet\Depository\PermissionSet as PermissionSetDepository;
use Friendica\Profile\ProfileField\Entity;
use Friendica\Capabilities\ICanCreateFromTableRow;
use Psr\Log\LoggerInterface;
class ProfileField extends BaseFactory implements ICanCreateFromTableRow
{
/** @var PermissionSetDepository */
private $permissionSetDepository;
public function __construct(LoggerInterface $logger, PermissionSetDepository $permissionSetDepository)
{
parent::__construct($logger);
$this->permissionSetDepository = $permissionSetDepository;
}
/**
* @inheritDoc
*/
public function createFromTableRow(array $row): Entity\ProfileField
{
return new Entity\ProfileField(
$this->permissionSetDepository,
$row['uid'],
$row['order'],
$row['psid'],
$row['label'],
$row['value'],
new \DateTime($row['created'], new \DateTimeZone('UTC')),
new \DateTime($row['edited'] ?? 'now', new \DateTimeZone('UTC'))
);
}
}

View file

@ -23,7 +23,6 @@ namespace Friendica\Repository;
use Friendica\BaseModel; use Friendica\BaseModel;
use Friendica\BaseRepository; use Friendica\BaseRepository;
use Friendica\Collection;
use Friendica\Core\L10n; use Friendica\Core\L10n;
use Friendica\Database\Database; use Friendica\Database\Database;
use Friendica\Database\DBA; use Friendica\Database\DBA;
@ -38,7 +37,7 @@ class ProfileField extends BaseRepository
protected static $model_class = \Friendica\Profile\ProfileField\Entity\ProfileField::class; protected static $model_class = \Friendica\Profile\ProfileField\Entity\ProfileField::class;
protected static $collection_class = Collection\ProfileFields::class; protected static $collection_class = \Friendica\Profile\ProfileField\Collection\ProfileFields::class;
/** @var PermissionSet */ /** @var PermissionSet */
private $permissionSet; private $permissionSet;
@ -80,7 +79,8 @@ class ProfileField extends BaseRepository
/** /**
* @param array $condition * @param array $condition
* @param array $params * @param array $params
* @return Collection\ProfileFields *
* @return \Friendica\Profile\ProfileField\Collection\ProfileFields
* @throws \Exception * @throws \Exception
*/ */
public function select(array $condition = [], array $params = []) public function select(array $condition = [], array $params = [])
@ -94,7 +94,8 @@ class ProfileField extends BaseRepository
* @param int|null $min_id * @param int|null $min_id
* @param int|null $max_id * @param int|null $max_id
* @param int $limit * @param int $limit
* @return Collection\ProfileFields *
* @return \Friendica\Profile\ProfileField\Collection\ProfileFields
* @throws \Exception * @throws \Exception
*/ */
public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT) public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT)
@ -104,7 +105,8 @@ class ProfileField extends BaseRepository
/** /**
* @param int $uid Field owner user Id * @param int $uid Field owner user Id
* @return Collection\ProfileFields *
* @return \Friendica\Profile\ProfileField\Collection\ProfileFields
* @throws \Exception * @throws \Exception
*/ */
public function selectByUserId(int $uid) public function selectByUserId(int $uid)
@ -120,7 +122,8 @@ class ProfileField extends BaseRepository
* *
* @param int $cid Private contact id, must be owned by $uid * @param int $cid Private contact id, must be owned by $uid
* @param int $uid Field owner user id * @param int $uid Field owner user id
* @return Collection\ProfileFields *
* @return \Friendica\Profile\ProfileField\Collection\ProfileFields
* @throws \Exception * @throws \Exception
*/ */
public function selectByContactId(int $cid, int $uid) public function selectByContactId(int $cid, int $uid)
@ -166,14 +169,15 @@ class ProfileField extends BaseRepository
} }
/** /**
* @param int $uid User Id * @param int $uid User Id
* @param Collection\ProfileFields $profileFields Collection of existing profile fields * @param \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields Collection of existing profile fields
* @param array $profileFieldInputs Array of profile field form inputs indexed by profile field id * @param array $profileFieldInputs Array of profile field form inputs indexed by profile field id
* @param array $profileFieldOrder List of profile field id in order * @param array $profileFieldOrder List of profile field id in order
* @return Collection\ProfileFields *
* @return \Friendica\Profile\ProfileField\Collection\ProfileFields
* @throws \Exception * @throws \Exception
*/ */
public function updateCollectionFromForm(int $uid, Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder) public function updateCollectionFromForm(int $uid, \Friendica\Profile\ProfileField\Collection\ProfileFields $profileFields, array $profileFieldInputs, array $profileFieldOrder)
{ {
// Returns an associative array of id => order values // Returns an associative array of id => order values
$profileFieldOrder = array_flip($profileFieldOrder); $profileFieldOrder = array_flip($profileFieldOrder);
@ -232,8 +236,8 @@ class ProfileField extends BaseRepository
$profileFieldInputs[$profileField->id]['group_deny'] ?? '' $profileFieldInputs[$profileField->id]['group_deny'] ?? ''
))->id; ))->id;
$profileField->psid = $psid; $profileField->permissionSetId = $psid;
$profileField->label = $profileFieldInputs[$profileField->id]['label']; $profileField->label = $profileFieldInputs[$profileField->id]['label'];
$profileField->value = $profileFieldInputs[$profileField->id]['value']; $profileField->value = $profileFieldInputs[$profileField->id]['value'];
$profileField->order = $profileFieldOrder[$profileField->id]; $profileField->order = $profileFieldOrder[$profileField->id];