From 26d6afd27fe29cbd0ecba0f1683a9eef03631363 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 19 Jan 2020 23:17:52 +0100 Subject: [PATCH 1/5] Move "Notify::getTab()" to notification module since it's presentation layer logic --- mod/notifications.php | 55 ++++++++++++++++++++++++++++++++++++++++++- src/Model/Notify.php | 52 ---------------------------------------- 2 files changed, 54 insertions(+), 53 deletions(-) diff --git a/mod/notifications.php b/mod/notifications.php index 43d3f7871a..33ff910236 100644 --- a/mod/notifications.php +++ b/mod/notifications.php @@ -13,8 +13,36 @@ use Friendica\Core\Renderer; use Friendica\Core\System; use Friendica\Database\DBA; use Friendica\DI; +use Friendica\Model\Notify; use Friendica\Module\Security\Login; +/** @var array Array of URL parameters */ +const URL_TYPES = [ + Notify::NETWORK => 'network', + Notify::SYSTEM => 'system', + Notify::HOME => 'home', + Notify::PERSONAL => 'personal', + Notify::INTRO => 'intros', +]; + +/** @var array Array of the allowed notifies and their printable name */ +const PRINT_TYPES = [ + Notify::NETWORK => 'Network', + Notify::SYSTEM => 'System', + Notify::HOME => 'Home', + Notify::PERSONAL => 'Personal', + Notify::INTRO => 'Introductions', +]; + +/** @var array The array of access keys for notify pages */ +const ACCESS_KEYS = [ + Notify::NETWORK => 'w', + Notify::SYSTEM => 'y', + Notify::HOME => 'h', + Notify::PERSONAL => 'r', + Notify::INTRO => 'i', +]; + function notifications_post(App $a) { if (!local_user()) { @@ -61,7 +89,7 @@ function notifications_content(App $a) $o = ''; // Get the nav tabs for the notification pages - $tabs = $nm->getTabs(); + $tabs = getTabs($this->args->get(1, '')); $notif_content = []; $notif_nocontent = ''; @@ -320,3 +348,28 @@ function notifications_content(App $a) return $o; } + +/** + * List of pages for the Notifications TabBar + * + * @param string $selected The selected notification tab + * + * @return array with with notifications TabBar data + * @throws Exception + */ +function getTabs(string $selected = '') +{ + $tabs = []; + + foreach (URL_TYPES as $type => $url) { + $tabs[] = [ + 'label' => $this->l10n->t(PRINT_TYPES[$type]), + 'url' => 'notifications/' . $url, + 'sel' => (($selected == $url) ? 'active' : ''), + 'id' => $type . '-tab', + 'accesskey' => ACCESS_KEYS[$type], + ]; + } + + return $tabs; +} diff --git a/src/Model/Notify.php b/src/Model/Notify.php index a27635a07e..7e8a71c165 100644 --- a/src/Model/Notify.php +++ b/src/Model/Notify.php @@ -36,33 +36,6 @@ final class Notify const HOME = 'home'; const INTRO = 'intro'; - /** @var array Array of URL parameters */ - const URL_TYPES = [ - self::NETWORK => 'network', - self::SYSTEM => 'system', - self::HOME => 'home', - self::PERSONAL => 'personal', - self::INTRO => 'intros', - ]; - - /** @var array Array of the allowed notifies and their printable name */ - const PRINT_TYPES = [ - self::NETWORK => 'Network', - self::SYSTEM => 'System', - self::HOME => 'Home', - self::PERSONAL => 'Personal', - self::INTRO => 'Introductions', - ]; - - /** @var array The array of access keys for notify pages */ - const ACCESS_KEYS = [ - self::NETWORK => 'w', - self::SYSTEM => 'y', - self::HOME => 'h', - self::PERSONAL => 'r', - self::INTRO => 'i', - ]; - /** @var Database */ private $dba; /** @var L10n */ @@ -196,31 +169,6 @@ final class Notify return $this->dba->update('notify', ['seen' => $seen], ['uid' => local_user()]); } - /** - * List of pages for the Notifications TabBar - * - * @return array with with notifications TabBar data - * @throws Exception - */ - public function getTabs() - { - $selected = $this->args->get(1, ''); - - $tabs = []; - - foreach (self::URL_TYPES as $type => $url) { - $tabs[] = [ - 'label' => $this->l10n->t(self::PRINT_TYPES[$type]), - 'url' => 'notifications/' . $url, - 'sel' => (($selected == $url) ? 'active' : ''), - 'id' => $type . '-tab', - 'accesskey' => self::ACCESS_KEYS[$type], - ]; - } - - return $tabs; - } - /** * Format the notification query in an usable array * From b46b72ad3b345ff271e435199fea795264e3b799 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Sun, 19 Jan 2020 23:22:30 +0100 Subject: [PATCH 2/5] Introduce "order" and "limit" argument instead of "param" array for BaseRepository and make Repositories more Dependency Injectable --- src/BaseRepository.php | 33 ++++++++++++++++------- src/Collection/Introductions.php | 4 --- src/Repository/Introduction.php | 16 +++++++----- src/Repository/PermissionSet.php | 45 +++++++++++++++++++------------- 4 files changed, 59 insertions(+), 39 deletions(-) diff --git a/src/BaseRepository.php b/src/BaseRepository.php index c0bcab18f9..a66914e88b 100644 --- a/src/BaseRepository.php +++ b/src/BaseRepository.php @@ -63,13 +63,15 @@ abstract class BaseRepository extends BaseFactory * Chainable. * * @param array $condition - * @param array $params + * @param array $order An optional array with order information + * @param int|array $limit Optional limit information + * * @return BaseCollection * @throws \Exception */ - public function select(array $condition = [], array $params = []) + public function select(array $condition = [], array $order = [], $limit = null) { - $models = $this->selectModels($condition, $params); + $models = $this->selectModels($condition, $order, $limit); return new static::$collection_class($models); } @@ -81,14 +83,15 @@ abstract class BaseRepository extends BaseFactory * Chainable. * * @param array $condition - * @param array $params + * @param array $order * @param int? $max_id * @param int? $since_id * @param int $limit + * * @return BaseCollection * @throws \Exception */ - public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) + public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) { $condition = DBA::collapseCondition($condition); @@ -104,9 +107,7 @@ abstract class BaseRepository extends BaseFactory $boundCondition[] = $since_id; } - $params['limit'] = $limit; - - $models = $this->selectModels($boundCondition, $params); + $models = $this->selectModels($boundCondition, $order, $limit); $totalCount = DBA::count(static::$table_name, $condition); @@ -175,12 +176,24 @@ abstract class BaseRepository extends BaseFactory /** * @param array $condition Query condition - * @param array $params Additional query parameters + * @param array $order An optional array with order information + * @param int|array $limit Optional limit information + * * @return BaseModel[] * @throws \Exception */ - protected function selectModels(array $condition, array $params = []) + protected function selectModels(array $condition, array $order = [], $limit = null) { + $params = []; + + if (!empty($order)) { + $params['order'] = $order; + } + + if (!empty($limit)) { + $params['limit'] = $limit; + } + $result = $this->dba->select(static::$table_name, [], $condition, $params); /** @var BaseModel $prototype */ diff --git a/src/Collection/Introductions.php b/src/Collection/Introductions.php index 0d4e319cb7..b0d31d4f1d 100644 --- a/src/Collection/Introductions.php +++ b/src/Collection/Introductions.php @@ -3,11 +3,7 @@ namespace Friendica\Collection; use Friendica\BaseCollection; -use Friendica\Model\Introduction; -/** - * @property Introduction[] $models - */ class Introductions extends BaseCollection { diff --git a/src/Repository/Introduction.php b/src/Repository/Introduction.php index 65c2e1ebc7..4f9806312c 100644 --- a/src/Repository/Introduction.php +++ b/src/Repository/Introduction.php @@ -35,26 +35,28 @@ class Introduction extends BaseRepository /** * @param array $condition - * @param array $params + * @param array $order An optional array with order information + * @param int|array $limit Optional limit information + * * @return Collection\Introductions * @throws \Exception */ - public function select(array $condition = [], array $params = []) + public function select(array $condition = [], array $order = [], $limit = null) { - return parent::select($condition, $params); + return parent::select($condition, $order, $limit); } /** * @param array $condition - * @param array $params + * @param array $order * @param int|null $max_id * @param int|null $since_id - * @param int $limit + * @param int|array $limit * @return Collection\Introductions * @throws \Exception */ - public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) + public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) { - return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit); + return parent::selectByBoundaries($condition, $order, $max_id, $since_id, $limit); } } diff --git a/src/Repository/PermissionSet.php b/src/Repository/PermissionSet.php index 906dd716e6..27c77d6052 100644 --- a/src/Repository/PermissionSet.php +++ b/src/Repository/PermissionSet.php @@ -2,15 +2,14 @@ namespace Friendica\Repository; -use Friendica\BaseModel; use Friendica\BaseRepository; use Friendica\Collection; -use Friendica\Core\L10n; -use Friendica\Database\DBA; -use Friendica\DI; +use Friendica\Database\Database; use Friendica\Model; use Friendica\Model\Group; use Friendica\Network\HTTPException; +use Friendica\Util\ACLFormatter; +use Psr\Log\LoggerInterface; class PermissionSet extends BaseRepository { @@ -20,6 +19,16 @@ class PermissionSet extends BaseRepository protected static $collection_class = Collection\PermissionSets::class; + /** @var ACLFormatter */ + private $aclFormatter; + + public function __construct(Database $dba, LoggerInterface $logger, ACLFormatter $aclFormatter) + { + parent::__construct($dba, $logger); + + $this->aclFormatter = $aclFormatter; + } + /** * @param array $data * @return Model\PermissionSet @@ -52,27 +61,29 @@ class PermissionSet extends BaseRepository /** * @param array $condition - * @param array $params + * @param array $order An optional array with order information + * @param int|array $limit Optional limit information + * * @return Collection\PermissionSets * @throws \Exception */ - public function select(array $condition = [], array $params = []) + public function select(array $condition = [], array $order = [], $limit = null) { - return parent::select($condition, $params); + return parent::select($condition, $order, $limit); } /** * @param array $condition - * @param array $params + * @param array $order * @param int|null $max_id * @param int|null $since_id - * @param int $limit + * @param int|array $limit * @return Collection\PermissionSets * @throws \Exception */ - public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) + public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) { - return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit); + return parent::selectByBoundaries($condition, $order, $max_id, $since_id, $limit); } /** @@ -93,12 +104,10 @@ class PermissionSet extends BaseRepository string $deny_cid = null, string $deny_gid = null ) { - $ACLFormatter = DI::aclFormatter(); - - $allow_cid = $ACLFormatter->sanitize($allow_cid); - $allow_gid = $ACLFormatter->sanitize($allow_gid); - $deny_cid = $ACLFormatter->sanitize($deny_cid); - $deny_gid = $ACLFormatter->sanitize($deny_gid); + $allow_cid = $this->aclFormatter->sanitize($allow_cid); + $allow_gid = $this->aclFormatter->sanitize($allow_gid); + $deny_cid = $this->aclFormatter->sanitize($deny_cid); + $deny_gid = $this->aclFormatter->sanitize($deny_gid); // Public permission if (!$allow_cid && !$allow_gid && !$deny_cid && !$deny_gid) { @@ -134,7 +143,7 @@ class PermissionSet extends BaseRepository public function selectByContactId($contact_id, $uid) { $groups = []; - if (DBA::exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) { + if ($this->dba->exists('contact', ['id' => $contact_id, 'uid' => $uid, 'blocked' => false])) { $groups = Group::getIdsByContactId($contact_id); } From 7984965183c4dd6879d59fe848ff5687851bbb65 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Wed, 22 Jan 2020 00:13:33 +0100 Subject: [PATCH 3/5] Add DI:: call --- mod/notifications.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/mod/notifications.php b/mod/notifications.php index 33ff910236..deee5f1b7a 100644 --- a/mod/notifications.php +++ b/mod/notifications.php @@ -89,7 +89,7 @@ function notifications_content(App $a) $o = ''; // Get the nav tabs for the notification pages - $tabs = getTabs($this->args->get(1, '')); + $tabs = getTabs(DI::args()->get(1, '')); $notif_content = []; $notif_nocontent = ''; @@ -363,7 +363,7 @@ function getTabs(string $selected = '') foreach (URL_TYPES as $type => $url) { $tabs[] = [ - 'label' => $this->l10n->t(PRINT_TYPES[$type]), + 'label' => DI::l10n()->t(PRINT_TYPES[$type]), 'url' => 'notifications/' . $url, 'sel' => (($selected == $url) ? 'active' : ''), 'id' => $type . '-tab', From a77c78522cdd68cb2fee6feb3f3e58ce1ce5d62f Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Wed, 22 Jan 2020 00:22:01 +0100 Subject: [PATCH 4/5] Add properties to PermissionSet Model --- src/Model/PermissionSet.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Model/PermissionSet.php b/src/Model/PermissionSet.php index 9bf2642c8b..09ffc22bb2 100644 --- a/src/Model/PermissionSet.php +++ b/src/Model/PermissionSet.php @@ -10,6 +10,12 @@ use Friendica\DI; /** * functions for interacting with the permission set of an object (item, photo, event, ...) + * + * @property int uid + * @property string allow_cid + * @property string allow_gid + * @property string deny_cid + * @property string deny_gid */ class PermissionSet extends BaseModel { From 82f37ccdaf73e2f5f7e5b15d4e35cc64c1299b79 Mon Sep 17 00:00:00 2001 From: nupplaPhil Date: Wed, 22 Jan 2020 20:28:56 +0100 Subject: [PATCH 5/5] Revert Repo::select()/selectFirst() changes --- src/BaseRepository.php | 33 ++++++++++---------------------- src/Repository/Introduction.php | 16 +++++++--------- src/Repository/PermissionSet.php | 16 +++++++--------- 3 files changed, 24 insertions(+), 41 deletions(-) diff --git a/src/BaseRepository.php b/src/BaseRepository.php index a66914e88b..c0bcab18f9 100644 --- a/src/BaseRepository.php +++ b/src/BaseRepository.php @@ -63,15 +63,13 @@ abstract class BaseRepository extends BaseFactory * Chainable. * * @param array $condition - * @param array $order An optional array with order information - * @param int|array $limit Optional limit information - * + * @param array $params * @return BaseCollection * @throws \Exception */ - public function select(array $condition = [], array $order = [], $limit = null) + public function select(array $condition = [], array $params = []) { - $models = $this->selectModels($condition, $order, $limit); + $models = $this->selectModels($condition, $params); return new static::$collection_class($models); } @@ -83,15 +81,14 @@ abstract class BaseRepository extends BaseFactory * Chainable. * * @param array $condition - * @param array $order + * @param array $params * @param int? $max_id * @param int? $since_id * @param int $limit - * * @return BaseCollection * @throws \Exception */ - public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) + public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) { $condition = DBA::collapseCondition($condition); @@ -107,7 +104,9 @@ abstract class BaseRepository extends BaseFactory $boundCondition[] = $since_id; } - $models = $this->selectModels($boundCondition, $order, $limit); + $params['limit'] = $limit; + + $models = $this->selectModels($boundCondition, $params); $totalCount = DBA::count(static::$table_name, $condition); @@ -176,24 +175,12 @@ abstract class BaseRepository extends BaseFactory /** * @param array $condition Query condition - * @param array $order An optional array with order information - * @param int|array $limit Optional limit information - * + * @param array $params Additional query parameters * @return BaseModel[] * @throws \Exception */ - protected function selectModels(array $condition, array $order = [], $limit = null) + protected function selectModels(array $condition, array $params = []) { - $params = []; - - if (!empty($order)) { - $params['order'] = $order; - } - - if (!empty($limit)) { - $params['limit'] = $limit; - } - $result = $this->dba->select(static::$table_name, [], $condition, $params); /** @var BaseModel $prototype */ diff --git a/src/Repository/Introduction.php b/src/Repository/Introduction.php index 4f9806312c..65c2e1ebc7 100644 --- a/src/Repository/Introduction.php +++ b/src/Repository/Introduction.php @@ -35,28 +35,26 @@ class Introduction extends BaseRepository /** * @param array $condition - * @param array $order An optional array with order information - * @param int|array $limit Optional limit information - * + * @param array $params * @return Collection\Introductions * @throws \Exception */ - public function select(array $condition = [], array $order = [], $limit = null) + public function select(array $condition = [], array $params = []) { - return parent::select($condition, $order, $limit); + return parent::select($condition, $params); } /** * @param array $condition - * @param array $order + * @param array $params * @param int|null $max_id * @param int|null $since_id - * @param int|array $limit + * @param int $limit * @return Collection\Introductions * @throws \Exception */ - public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) + public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) { - return parent::selectByBoundaries($condition, $order, $max_id, $since_id, $limit); + return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit); } } diff --git a/src/Repository/PermissionSet.php b/src/Repository/PermissionSet.php index 27c77d6052..7cbee4b836 100644 --- a/src/Repository/PermissionSet.php +++ b/src/Repository/PermissionSet.php @@ -61,29 +61,27 @@ class PermissionSet extends BaseRepository /** * @param array $condition - * @param array $order An optional array with order information - * @param int|array $limit Optional limit information - * + * @param array $params * @return Collection\PermissionSets * @throws \Exception */ - public function select(array $condition = [], array $order = [], $limit = null) + public function select(array $condition = [], array $params = []) { - return parent::select($condition, $order, $limit); + return parent::select($condition, $params); } /** * @param array $condition - * @param array $order + * @param array $params * @param int|null $max_id * @param int|null $since_id - * @param int|array $limit + * @param int $limit * @return Collection\PermissionSets * @throws \Exception */ - public function selectByBoundaries(array $condition = [], array $order = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) + public function selectByBoundaries(array $condition = [], array $params = [], int $max_id = null, int $since_id = null, int $limit = self::LIMIT) { - return parent::selectByBoundaries($condition, $order, $max_id, $since_id, $limit); + return parent::selectByBoundaries($condition, $params, $max_id, $since_id, $limit); } /**