diff --git a/src/BaseRepository.php b/src/BaseRepository.php deleted file mode 100644 index 1294be88ca..0000000000 --- a/src/BaseRepository.php +++ /dev/null @@ -1,251 +0,0 @@ -. - * - */ - -namespace Friendica; - -use Friendica\Database\Database; -use Friendica\Database\DBA; -use Friendica\Network\HTTPException; -use Psr\Log\LoggerInterface; - -/** - * Repositories are Factories linked to one or more database tables. - * - * @see BaseModel - * @see BaseCollection - */ -abstract class BaseRepository extends BaseFactory -{ - const LIMIT = 30; - - /** @var Database */ - protected $dba; - - /** @var string */ - protected static $table_name; - - /** @var BaseModel */ - protected static $model_class; - - /** @var BaseCollection */ - protected static $collection_class; - - public function __construct(Database $dba, LoggerInterface $logger) - { - parent::__construct($logger); - - $this->dba = $dba; - $this->logger = $logger; - } - - /** - * Fetches a single model record. The condition array is expected to contain a unique index (primary or otherwise). - * - * Chainable. - * - * @param array $condition - * @return BaseModel - * @throws HTTPException\NotFoundException - */ - public function selectFirst(array $condition) - { - $data = $this->dba->selectFirst(static::$table_name, [], $condition); - - if (!$data) { - throw new HTTPException\NotFoundException(static::class . ' record not found.'); - } - - return $this->create($data); - } - - /** - * Populates a Collection according to the condition. - * - * Chainable. - * - * @param array $condition - * @param array $params - * @return BaseCollection - * @throws \Exception - */ - public function select(array $condition = [], array $params = []) - { - $models = $this->selectModels($condition, $params); - - return new static::$collection_class($models); - } - - /** - * Populates the collection according to the condition. Retrieves a limited subset of models depending on the boundaries - * and the limit. The total count of rows matching the condition is stored in the collection. - * - * max_id and min_id are susceptible to the query order: - * - min_id alone only reliably works with ASC order - * - max_id alone only reliably works with DESC order - * If the wrong order is detected in either case, we inverse the query order and we reverse the model array after the query - * - * Chainable. - * - * @param array $condition - * @param array $params - * @param int? $min_id Retrieve models with an id no fewer than this, as close to it as possible - * @param int? $max_id Retrieve models with an id no greater than this, as close to it as possible - * @param int $limit - * @return BaseCollection - * @throws \Exception - */ - public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT) - { - $totalCount = DBA::count(static::$table_name, $condition); - - $boundCondition = $condition; - - $reverseModels = false; - - if (isset($min_id)) { - $boundCondition = DBA::mergeConditions($boundCondition, ['`id` > ?', $min_id]); - if (!isset($max_id) && isset($params['order']['id']) && ($params['order']['id'] === true || $params['order']['id'] === 'DESC')) { - $reverseModels = true; - $params['order']['id'] = 'ASC'; - } - } - - if (isset($max_id)) { - $boundCondition = DBA::mergeConditions($boundCondition, ['`id` < ?', $max_id]); - if (!isset($min_id) && (!isset($params['order']['id']) || $params['order']['id'] === false || $params['order']['id'] === 'ASC')) { - $reverseModels = true; - $params['order']['id'] = 'DESC'; - } - } - - $params['limit'] = $limit; - - $models = $this->selectModels($boundCondition, $params); - - if ($reverseModels) { - $models = array_reverse($models); - } - - return new static::$collection_class($models, $totalCount); - } - - /** - * This method updates the database row from the model. - * - * @param BaseModel $model - * @return bool - * @throws \Exception - */ - public function update(BaseModel $model) - { - if ($this->dba->update(static::$table_name, $model->toArray(), ['id' => $model->id], $model->getOriginalData())) { - $model->resetOriginalData(); - return true; - } - - return false; - } - - /** - * This method creates a new database row and returns a model if it was successful. - * - * @param array $fields - * @return BaseModel|bool - * @throws \Exception - */ - public function insert(array $fields) - { - $return = $this->dba->insert(static::$table_name, $fields); - - if (!$return) { - throw new HTTPException\InternalServerErrorException('Unable to insert new row in table "' . static::$table_name . '"'); - } - - $fields['id'] = $this->dba->lastInsertId(); - $return = $this->create($fields); - - return $return; - } - - /** - * Deletes the model record from the database. - * - * @param BaseModel $model - * @return bool - * @throws \Exception - */ - public function delete(BaseModel &$model) - { - if ($success = $this->dba->delete(static::$table_name, ['id' => $model->id])) { - $model = null; - } - - return $success; - } - - /** - * Base instantiation method, can be overriden to add specific dependencies - * - * @param array $data - * @return BaseModel - */ - protected function create(array $data) - { - return new static::$model_class($this->dba, $this->logger, $data); - } - - /** - * @param array $condition Query condition - * @param array $params Additional query parameters - * @return BaseModel[] - * @throws \Exception - */ - protected function selectModels(array $condition, array $params = []) - { - $result = $this->dba->select(static::$table_name, [], $condition, $params); - - /** @var BaseModel $prototype */ - $prototype = null; - - $models = []; - - while ($record = $this->dba->fetch($result)) { - if ($prototype === null) { - $prototype = $this->create($record); - $models[] = $prototype; - } else { - $models[] = static::$model_class::createFromPrototype($prototype, $record); - } - } - - $this->dba->close($result); - - return $models; - } - - /** - * @param BaseCollection $collection - */ - public function saveCollection(BaseCollection $collection) - { - $collection->map([$this, 'update']); - } -} diff --git a/src/Collection/FSuggests.php b/src/Collection/FSuggests.php deleted file mode 100644 index 78c964636f..0000000000 --- a/src/Collection/FSuggests.php +++ /dev/null @@ -1,29 +0,0 @@ -. - * - */ - -namespace Friendica\Collection; - -use Friendica\BaseCollection; - -class FSuggests extends BaseCollection -{ - -} diff --git a/src/Collection/Notifications.php b/src/Collection/Notifications.php deleted file mode 100644 index adf0f48581..0000000000 --- a/src/Collection/Notifications.php +++ /dev/null @@ -1,36 +0,0 @@ -. - * - */ - -namespace Friendica\Collection; - -use Friendica\BaseCollection; -use Friendica\Model; - -class Notifications extends BaseCollection -{ - /** - * @return Model\Notification - */ - public function current() - { - return parent::current(); - } -} diff --git a/src/Contact/FriendSuggest/Depository/FriendSuggest.php b/src/Contact/FriendSuggest/Depository/FriendSuggest.php new file mode 100644 index 0000000000..9f3b39acca --- /dev/null +++ b/src/Contact/FriendSuggest/Depository/FriendSuggest.php @@ -0,0 +1,72 @@ + $fsuggest->uid, + 'cid' => $fsuggest->cid, + 'name' => $fsuggest->name, + 'url' => $fsuggest->url, + 'request' => $fsuggest->request, + 'photo' => $fsuggest->photo, + 'note' => $fsuggest->note, + ]; + } + + /** + * @param array $condition + * @param array $params + * + * @return Entity\FriendSuggest + * + * @throws NotFoundException The underlying exception if there's no FriendSuggest with the given conditions + */ + private function selectOne(array $condition, array $params = []): Entity\FriendSuggest + { + return parent::_selectOne($condition, $params); + } + + public function selectOneById(int $id): Entity\FriendSuggest + { + return $this->selectOne(['id' => $id]); + } + + public function save(Entity\FriendSuggest $fsuggest): Entity\FriendSuggest + { + try { + $fields = $this->convertToTableRow($fsuggest); + + if ($fsuggest->id) { + $this->db->update(self::$table_name, $fields, ['id' => $fsuggest->id]); + return $this->factory->createFromTableRow($fields); + } else { + $this->db->insert(self::$table_name, $fields); + return $this->selectOneById($this->db->lastInsertId()); + } + } catch (\Exception $exception) { + throw new FriendSuggestPersistenceException(sprintf('Cannot insert/update the FriendSuggestion %d for user %d', $fsuggest->id, $fsuggest->uid), $exception); + } + } +} diff --git a/src/Contact/FriendSuggest/Entity/FriendSuggest.php b/src/Contact/FriendSuggest/Entity/FriendSuggest.php new file mode 100644 index 0000000000..4fc8a2df96 --- /dev/null +++ b/src/Contact/FriendSuggest/Entity/FriendSuggest.php @@ -0,0 +1,83 @@ +. + * + */ + +namespace Friendica\Contact\FriendSuggest\Entity; + +use Friendica\BaseEntity; + +/** + * Model for interacting with a friend suggestion + * + * @property-read int $uid + * @property-read int $cid + * @property-read string $name + * @property-read string $url + * @property-read string $request + * @property-read string $photo + * @property-read string $note + * @property-read \DateTime created + * @property-read int|null $id + */ +class FriendSuggest extends BaseEntity +{ + /** @var int */ + protected $uid; + /** @var int */ + protected $cid; + /** @var string */ + protected $name; + /** @var string */ + protected $url; + /** @var string */ + protected $request; + /** @var string */ + protected $photo; + /** @var string */ + protected $note; + /** @var \DateTime */ + protected $created; + /** @var int|null */ + protected $id; + + /** + * @param int $uid + * @param int $cid + * @param string $name + * @param string $url + * @param string $request + * @param string $photo + * @param string $note + * @param \DateTime $created + * @param int|null $id + */ + public function __construct(int $uid, int $cid, string $name, string $url, string $request, string $photo, string $note, \DateTime $created, ?int $id = null) + { + $this->uid = $uid; + $this->cid = $cid; + $this->name = $name; + $this->url = $url; + $this->request = $request; + $this->photo = $photo; + $this->note = $note; + $this->created = $created; + $this->id = $id; + } +} diff --git a/src/Contact/FriendSuggest/Exception/FriendSuggestPersistenceException.php b/src/Contact/FriendSuggest/Exception/FriendSuggestPersistenceException.php new file mode 100644 index 0000000000..4b901272df --- /dev/null +++ b/src/Contact/FriendSuggest/Exception/FriendSuggestPersistenceException.php @@ -0,0 +1,11 @@ +createFromTableRow([ + 'uid' => $uid, + 'cid' => $cid, + 'name' => $name, + 'url' => $url, + 'request' => $request, + 'photo' => $photo, + 'note' => $note, + ]); + } +} diff --git a/src/DI.php b/src/DI.php index 28df28a325..eea219bd22 100644 --- a/src/DI.php +++ b/src/DI.php @@ -427,11 +427,19 @@ abstract class DI // /** - * @return Repository\FSuggest; + * @return Contact\FriendSuggest\Depository\FriendSuggest; */ public static function fsuggest() { - return self::$dice->create(Repository\FSuggest::class); + return self::$dice->create(Contact\FriendSuggest\Depository\FriendSuggest::class); + } + + /** + * @return Contact\FriendSuggest\Factory\FriendSuggest; + */ + public static function fsuggestFactory() + { + return self::$dice->create(Contact\FriendSuggest\Factory\FriendSuggest::class); } /** diff --git a/src/Model/FSuggest.php b/src/Model/FSuggest.php deleted file mode 100644 index d8dbd3911f..0000000000 --- a/src/Model/FSuggest.php +++ /dev/null @@ -1,41 +0,0 @@ -. - * - */ - -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 -{ - -} diff --git a/src/Module/FriendSuggest.php b/src/Module/FriendSuggest.php index a53d47c481..e8e0fc2f09 100644 --- a/src/Module/FriendSuggest.php +++ b/src/Module/FriendSuggest.php @@ -68,16 +68,15 @@ class FriendSuggest extends BaseModule $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() - ]); + $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); diff --git a/src/Protocol/ActivityPub/Transmitter.php b/src/Protocol/ActivityPub/Transmitter.php index ab7b63866d..5e6f772472 100644 --- a/src/Protocol/ActivityPub/Transmitter.php +++ b/src/Protocol/ActivityPub/Transmitter.php @@ -1782,7 +1782,7 @@ class Transmitter { $owner = User::getOwnerDataById($uid); - $suggestion = DI::fsuggest()->getById($suggestion_id); + $suggestion = DI::fsuggest()->selectOneById($suggestion_id); $data = ['@context' => ActivityPub::CONTEXT, 'id' => DI::baseUrl() . '/activity/' . System::createGUID(), diff --git a/src/Repository/FSuggest.php b/src/Repository/FSuggest.php deleted file mode 100644 index 1bbc6c739f..0000000000 --- a/src/Repository/FSuggest.php +++ /dev/null @@ -1,93 +0,0 @@ -. - * - */ - -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 $min_id - * @param int|null $max_id - * @param int $limit - * @return Collection\FSuggests - * @throws \Exception - */ - public function selectByBoundaries(array $condition = [], array $params = [], int $min_id = null, int $max_id = null, int $limit = self::LIMIT) - { - return parent::selectByBoundaries($condition, $params, $min_id, $max_id, $limit); - } -} diff --git a/src/Worker/Notifier.php b/src/Worker/Notifier.php index d69419eb18..16baea281d 100644 --- a/src/Worker/Notifier.php +++ b/src/Worker/Notifier.php @@ -90,7 +90,7 @@ class Notifier 'APDelivery', $cmd, $target_id, $inbox, $uid, $receivers, $post_uriid); } } elseif ($cmd == Delivery::SUGGESTION) { - $suggest = DI::fsuggest()->getById($target_id); + $suggest = DI::fsuggest()->selectOneById($target_id); $uid = $suggest->uid; $recipients[] = $suggest->cid; } elseif ($cmd == Delivery::REMOVAL) {