From 81d8fe8e9b1b18f0bbbcc44822a770b350a9139c Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 16 Mar 2024 21:33:13 +0100 Subject: [PATCH 01/12] join votes for participation check Signed-off-by: dartcafe --- lib/Db/Poll.php | 7 +++++++ lib/Db/PollMapper.php | 29 ++++++++++++++++++++++++++++- lib/Db/VoteMapper.php | 13 ------------- lib/Model/Acl.php | 6 +----- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/Db/Poll.php b/lib/Db/Poll.php index 6580d93fd..053eabbd6 100644 --- a/lib/Db/Poll.php +++ b/lib/Db/Poll.php @@ -136,6 +136,7 @@ class Poll extends EntityWithUser implements JsonSerializable { protected bool $hasOrphanedVotes = false; protected int $maxDate = 0; protected int $minDate = 0; + protected int $currentUserVotes = 0; public function __construct() { $this->addType('created', 'int'); @@ -153,6 +154,7 @@ public function __construct() { $this->addType('lastInteraction', 'int'); $this->addType('maxDate', 'int'); $this->addType('minDate', 'int'); + $this->addType('currentUserVotes', 'int'); $this->urlGenerator = Container::queryClass(IURLGenerator::class); $this->userMapper = Container::queryClass(UserMapper::class); $this->voteMapper = Container::queryClass(VoteMapper::class); @@ -190,6 +192,7 @@ public function jsonSerialize(): array { 'summary' => [ 'orphanedVotes' => count($this->voteMapper->findOrphanedByPollandUser($this->id, $this->userMapper->getCurrentUserCached()->getId())), 'yesByCurrentUser' => count($this->voteMapper->getYesVotesByParticipant($this->getPollId(), $this->userMapper->getCurrentUserCached()->getId())), + 'countVotes' => $this->getCurrentUserCountVotes(), ], ]; } @@ -275,6 +278,10 @@ public function getProposalsExpired(): bool { ); } + public function getCurrentUserCountVotes(): int { + return $this->currentUserVotes; + } + public function getDescription(): string { return $this->description ?? ''; } diff --git a/lib/Db/PollMapper.php b/lib/Db/PollMapper.php index 93718d24c..de08bca37 100644 --- a/lib/Db/PollMapper.php +++ b/lib/Db/PollMapper.php @@ -40,7 +40,10 @@ class PollMapper extends QBMapper { /** * @psalm-suppress PossiblyUnusedMethod */ - public function __construct(IDBConnection $db) { + public function __construct( + IDBConnection $db, + private UserMapper $userMapper, + ) { parent::__construct($db, Poll::TABLE, Poll::class); } @@ -167,6 +170,7 @@ public function deleteByUserId(string $userId): void { * Build the enhanced query with joined tables */ protected function buildQuery(): IQueryBuilder { + $currentUserId = $this->userMapper->getCurrentUser()->getId(); $qb = $this->db->getQueryBuilder(); $qb->select(self::TABLE . '.*') @@ -174,6 +178,7 @@ protected function buildQuery(): IQueryBuilder { // ->groupBy(self::TABLE . '.id') ->from($this->getTableName(), self::TABLE); $this->joinOptionsForMaxDate($qb, self::TABLE); + $this->joinCurrentUserVotes($qb, self::TABLE, $currentUserId); $qb->groupBy(self::TABLE . '.id'); return $qb; } @@ -201,4 +206,26 @@ protected function joinOptionsForMaxDate(IQueryBuilder &$qb, string $fromAlias): ); } + /** + * Joins options to evaluate min and max option date for date polls + * if text poll or no options are set, + * the min value is the current time, + * the max value is null + */ + protected function joinCurrentUserVotes(IQueryBuilder &$qb, string $fromAlias, $currentUserId): void { + $joinAlias = 'user_vote'; + // force value into a MIN function to avoid grouping errors + $qb->selectAlias($qb->func()->count($joinAlias . '.vote_answer'), 'current_user_votes'); + + $qb->leftJoin( + $fromAlias, + Vote::TABLE, + $joinAlias, + $qb->expr()->andX( + $qb->expr()->eq($joinAlias . '.poll_id', $fromAlias . '.id'), + $qb->expr()->eq($joinAlias . '.user_id', $qb->createNamedParameter($currentUserId, IQueryBuilder::PARAM_STR)), + ) + ); + } + } diff --git a/lib/Db/VoteMapper.php b/lib/Db/VoteMapper.php index 549816d4d..e950982be 100644 --- a/lib/Db/VoteMapper.php +++ b/lib/Db/VoteMapper.php @@ -125,19 +125,6 @@ public function findParticipantsByPoll(int $pollId): array { return $this->findEntities($qb); } - - /** - * @throws \OCP\AppFramework\Db\DoesNotExistException if not found - * @return Vote[] - * @psalm-return array - */ - public function findParticipantsVotes(int $pollId, string $userId): array { - $qb = $this->buildQuery(); - $qb->andWhere($qb->expr()->eq(self::TABLE . '.poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) - ->andWhere($qb->expr()->eq(self::TABLE . '.user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))); - return $this->findEntities($qb); - } - public function deleteByPollAndUserId(int $pollId, string $userId): void { $qb = $this->db->getQueryBuilder(); $qb->delete($this->getTableName()) diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index cc8784f75..691560e59 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -33,7 +33,6 @@ use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\UserMapper; -use OCA\Polls\Db\VoteMapper; use OCA\Polls\Exceptions\ForbiddenException; use OCA\Polls\Exceptions\InsufficientAttributesException; use OCA\Polls\Exceptions\InvalidPollIdException; @@ -79,7 +78,6 @@ public function __construct( private ISession $session, private ShareMapper $shareMapper, private UserMapper $userMapper, - private VoteMapper $voteMapper, private Poll $poll, private Share $share, ) { @@ -323,9 +321,7 @@ private function getIsOpenPoll(): bool { * @return bool Returns true, if the current user is already a particitipant of the current poll. */ private function getIsParticipant(): bool { - return count( - $this->voteMapper->findParticipantsVotes($this->getPollId(), $this->getUserId()) - ) > 0; + return $this->getPoll()->getCurrentUserCountVotes() > 0; } /** From 8c6c0ee5f2b09988f49aaecbc932b0413fb08033 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 16 Mar 2024 21:53:50 +0100 Subject: [PATCH 02/12] adjust tests Signed-off-by: dartcafe --- tests/Unit/Db/CommentMapperTest.php | 22 +++++++++++++++++++++- tests/Unit/Db/LogMapperTest.php | 20 +++++++++++++++++++- tests/Unit/Db/OptionMapperTest.php | 3 +-- tests/Unit/Db/PollMapperTest.php | 19 ++++++++++++++++++- tests/Unit/Db/SubscriptionMapperTest.php | 19 ++++++++++++++++++- tests/Unit/Db/VoteMapperTest.php | 11 +---------- 6 files changed, 78 insertions(+), 16 deletions(-) diff --git a/tests/Unit/Db/CommentMapperTest.php b/tests/Unit/Db/CommentMapperTest.php index cca3fade1..0e8fa461e 100644 --- a/tests/Unit/Db/CommentMapperTest.php +++ b/tests/Unit/Db/CommentMapperTest.php @@ -29,14 +29,27 @@ use OCA\Polls\Db\Comment; use OCA\Polls\Db\CommentMapper; +use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class CommentMapperTest extends UnitTestCase { private IDBConnection $con; + private ISession $session; + private IUserManager $userManager; + private IUserSession $userSession; + private LoggerInterface $logger; private CommentMapper $commentMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; + /** @var Poll[] $polls */ private array $polls = []; + /** @var Comment[] $comments */ private array $comments = []; /** @@ -45,8 +58,15 @@ class CommentMapperTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); $this->con = Server::get(IDBConnection::class); + $this->logger = Server::get(LoggerInterface::class); + $this->session = Server::get(ISession::class); + $this->userManager = Server::get(IUserManager::class); + $this->userSession = Server::get(IUserSession::class); + $this->session->set('ncPollsUserId', 'TestUser'); + + $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); $this->commentMapper = new CommentMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/LogMapperTest.php b/tests/Unit/Db/LogMapperTest.php index 4223fddb7..e46e16662 100644 --- a/tests/Unit/Db/LogMapperTest.php +++ b/tests/Unit/Db/LogMapperTest.php @@ -27,14 +27,24 @@ use OCA\Polls\Db\LogMapper; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; use OCA\Polls\Tests\Unit\UnitTestCase; use OCP\IDBConnection; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class LogMapperTest extends UnitTestCase { private IDBConnection $con; + private ISession $session; + private IUserManager $userManager; + private IUserSession $userSession; + private LoggerInterface $logger; private LogMapper $logMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Log[] $logs*/ private array $logs = []; /** @var Poll[] $polls*/ @@ -46,8 +56,16 @@ class LogMapperTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); $this->con = Server::get(IDBConnection::class); + $this->logger = Server::get(LoggerInterface::class); + $this->session = Server::get(ISession::class); + $this->userManager = Server::get(IUserManager::class); + $this->userSession = Server::get(IUserSession::class); + $this->session->set('ncPollsUserId', 'TestUser'); + + $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); + $this->logMapper = new LogMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/OptionMapperTest.php b/tests/Unit/Db/OptionMapperTest.php index 1738d9092..50ca1728c 100644 --- a/tests/Unit/Db/OptionMapperTest.php +++ b/tests/Unit/Db/OptionMapperTest.php @@ -36,7 +36,6 @@ use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; use OCA\Polls\Db\UserMapper; -use OCP\IGroupManager; use OCP\ISession; use OCP\IUserManager; use OCP\IUserSession; @@ -76,7 +75,7 @@ protected function setUp(): void { $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); $this->voteMapper = new VoteMapper($this->con, $this->logger); $this->optionMapper = new OptionMapper($this->con, $this->userMapper); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/PollMapperTest.php b/tests/Unit/Db/PollMapperTest.php index 868ba26ae..9a7b707b3 100644 --- a/tests/Unit/Db/PollMapperTest.php +++ b/tests/Unit/Db/PollMapperTest.php @@ -29,12 +29,23 @@ use OCP\IDBConnection; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; use OCA\Polls\Tests\Unit\UnitTestCase; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class PollMapperTest extends UnitTestCase { private IDBConnection $con; + private ISession $session; + private IUserManager $userManager; + private IUserSession $userSession; + private LoggerInterface $logger; + private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Poll[] $polls*/ private array $polls = []; @@ -44,7 +55,13 @@ class PollMapperTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); $this->con = Server::get(IDBConnection::class); - $this->pollMapper = new PollMapper($this->con); + $this->logger = Server::get(LoggerInterface::class); + $this->session = Server::get(ISession::class); + $this->userManager = Server::get(IUserManager::class); + $this->userSession = Server::get(IUserSession::class); + $this->session->set('ncPollsUserId', 'TestUser'); + $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll'), diff --git a/tests/Unit/Db/SubscriptionMapperTest.php b/tests/Unit/Db/SubscriptionMapperTest.php index 95916c330..1840a6131 100644 --- a/tests/Unit/Db/SubscriptionMapperTest.php +++ b/tests/Unit/Db/SubscriptionMapperTest.php @@ -29,12 +29,22 @@ use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Subscription; use OCA\Polls\Db\SubscriptionMapper; +use OCA\Polls\Db\UserMapper; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class SubscriptionMapperTest extends UnitTestCase { private IDBConnection $con; + private ISession $session; + private IUserManager $userManager; + private IUserSession $userSession; + private LoggerInterface $logger; private SubscriptionMapper $subscriptionMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Poll[] $polls */ private array $polls = []; /** @var Poll[] $polls */ @@ -47,8 +57,15 @@ class SubscriptionMapperTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); $this->con = Server::get(IDBConnection::class); + $this->logger = Server::get(LoggerInterface::class); + $this->session = Server::get(ISession::class); + $this->userManager = Server::get(IUserManager::class); + $this->userSession = Server::get(IUserSession::class); + $this->session->set('ncPollsUserId', 'TestUser'); + $this->subscriptionMapper = new SubscriptionMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/VoteMapperTest.php b/tests/Unit/Db/VoteMapperTest.php index 433f176bd..407ca0419 100644 --- a/tests/Unit/Db/VoteMapperTest.php +++ b/tests/Unit/Db/VoteMapperTest.php @@ -72,7 +72,7 @@ protected function setUp(): void { $this->session->set('ncPollsUserId', 'TestUser'); $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->voteMapper = new VoteMapper($this->con, $this->logger); $this->optionMapper = new OptionMapper($this->con, $this->userMapper); @@ -134,15 +134,6 @@ public function testParticipantsByPoll() { } } - /** - * testParticipantsByPoll - */ - public function testFindParticipantsVotes() { - foreach ($this->votes as $vote) { - $this->assertTrue(count($this->voteMapper->findParticipantsVotes($vote->getPollId(), $vote->getUserId())) > 0); - } - } - /** * testUpdate */ From 2cac4cb54c485386a3f3eda734e22528a14bb03c Mon Sep 17 00:00:00 2001 From: "dependabot[bot]" <49699333+dependabot[bot]@users.noreply.github.com> Date: Sat, 16 Mar 2024 22:58:23 +0000 Subject: [PATCH 03/12] Build(deps): Bump follow-redirects from 1.15.4 to 1.15.6 Bumps [follow-redirects](https://github.com/follow-redirects/follow-redirects) from 1.15.4 to 1.15.6. - [Release notes](https://github.com/follow-redirects/follow-redirects/releases) - [Commits](https://github.com/follow-redirects/follow-redirects/compare/v1.15.4...v1.15.6) --- updated-dependencies: - dependency-name: follow-redirects dependency-type: indirect ... Signed-off-by: dependabot[bot] --- package-lock.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3cdd558ff..2a0d27772 100644 --- a/package-lock.json +++ b/package-lock.json @@ -9101,9 +9101,9 @@ } }, "node_modules/follow-redirects": { - "version": "1.15.4", - "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.4.tgz", - "integrity": "sha512-Cr4D/5wlrb0z9dgERpUL3LrmPKVDsETIJhaCMeDfuFYcqa5bldGV6wBsAN6X/vxlXQtFBMrXdXxdL8CbDTGniw==", + "version": "1.15.6", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.6.tgz", + "integrity": "sha512-wWN62YITEaOpSK584EZXJafH1AGpO8RVgElfkuXbTOrPX4fIfOyEpW/CsiNd8JdYrAoOvafRTOEnvsO++qCqFA==", "funding": [ { "type": "individual", From 386d76e6b2a7bed2140e0a1ba833c563f00e20bf Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 17 Mar 2024 09:23:22 +0100 Subject: [PATCH 04/12] register UserMapper Signed-off-by: dartcafe --- lib/AppInfo/Application.php | 17 +++++++++++++++++ lib/Model/UserBase.php | 3 ++- tests/Unit/Db/OptionMapperTest.php | 2 +- tests/Unit/Db/VoteMapperTest.php | 2 +- 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 5e298d24a..594e5e677 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -28,6 +28,7 @@ use OCA\Polls\AppConstants; use OCA\Polls\Dashboard\PollWidget; +use OCA\Polls\Db\UserMapper; use OCA\Polls\Event\CommentAddEvent; use OCA\Polls\Event\CommentDeleteEvent; use OCA\Polls\Event\CommentEvent; @@ -71,7 +72,13 @@ use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Group\Events\GroupDeletedEvent; +use OCP\IDBConnection; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\User\Events\UserDeletedEvent; +use Psr\Container\ContainerInterface; +use Psr\Log\LoggerInterface; /** * @psalm-api @@ -91,6 +98,16 @@ public function boot(IBootContext $context): void { public function register(IRegistrationContext $context): void { include_once __DIR__ . '/../../vendor/autoload.php'; + $context->registerService(UserMapper::class, function(ContainerInterface $c): UserMapper { + return new UserMapper( + $c->get(IDBConnection::class), + $c->get(ISession::class), + $c->get(IUserSession::class), + $c->get(IUserManager::class), + $c->get(LoggerInterface::class), + ); + }); + $context->registerMiddleWare(RequestAttributesMiddleware::class); $context->registerNotifierService(Notifier::class); diff --git a/lib/Model/UserBase.php b/lib/Model/UserBase.php index d4692e353..f01be4fe2 100644 --- a/lib/Model/UserBase.php +++ b/lib/Model/UserBase.php @@ -44,6 +44,7 @@ use OCP\IGroupManager; use OCP\IL10N; use OCP\IUserSession; +use OCP\Server; use OCP\Share\IShare; class UserBase implements JsonSerializable { @@ -91,7 +92,7 @@ public function __construct( $this->l10n = Container::getL10N(); $this->groupManager = Container::queryClass(IGroupManager::class); $this->timeZone = Container::queryClass(IDateTimeZone::class); - $this->userMapper = Container::queryClass(UserMapper::class); + $this->userMapper = Server::get(UserMapper::class); $this->userSession = Container::queryClass(IUserSession::class); $this->appSettings = Container::queryClass(AppSettings::class); } diff --git a/tests/Unit/Db/OptionMapperTest.php b/tests/Unit/Db/OptionMapperTest.php index 1738d9092..9ff453fe1 100644 --- a/tests/Unit/Db/OptionMapperTest.php +++ b/tests/Unit/Db/OptionMapperTest.php @@ -73,7 +73,7 @@ protected function setUp(): void { $this->session->set('ncPollsUserId', 'TestUser'); - $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); + $this->userMapper = Server::get(UserMapper::class); $this->voteMapper = new VoteMapper($this->con, $this->logger); $this->optionMapper = new OptionMapper($this->con, $this->userMapper); $this->pollMapper = new PollMapper($this->con); diff --git a/tests/Unit/Db/VoteMapperTest.php b/tests/Unit/Db/VoteMapperTest.php index 433f176bd..382d18862 100644 --- a/tests/Unit/Db/VoteMapperTest.php +++ b/tests/Unit/Db/VoteMapperTest.php @@ -71,7 +71,7 @@ protected function setUp(): void { $this->userSession = Server::get(IUserSession::class); $this->session->set('ncPollsUserId', 'TestUser'); - $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); + $this->userMapper = Server::get(UserMapper::class); $this->pollMapper = new PollMapper($this->con); $this->voteMapper = new VoteMapper($this->con, $this->logger); $this->optionMapper = new OptionMapper($this->con, $this->userMapper); From 245cdf95c06e8d39cec59a057806ee0392d6d036 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 17 Mar 2024 09:27:55 +0100 Subject: [PATCH 05/12] register AppSettings Signed-off-by: dartcafe --- lib/AppInfo/Application.php | 11 +++++++++++ lib/Controller/PollController.php | 3 ++- lib/Cron/JanitorCron.php | 3 ++- lib/Db/Share.php | 5 +++-- lib/Model/Acl.php | 1 - lib/Model/Settings/AppSettings.php | 15 +++++++-------- lib/Model/UserBase.php | 8 ++++---- lib/Service/SettingsService.php | 6 ++---- lib/Service/WatchService.php | 5 ++--- 9 files changed, 33 insertions(+), 24 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 594e5e677..61165d4c9 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -65,6 +65,7 @@ use OCA\Polls\Listener\UserDeletedListener; use OCA\Polls\Listener\VoteListener; use OCA\Polls\Middleware\RequestAttributesMiddleware; +use OCA\Polls\Model\Settings\AppSettings; use OCA\Polls\Notification\Notifier; use OCA\Polls\Provider\SearchProvider; use OCP\AppFramework\App; @@ -72,7 +73,9 @@ use OCP\AppFramework\Bootstrap\IBootstrap; use OCP\AppFramework\Bootstrap\IRegistrationContext; use OCP\Group\Events\GroupDeletedEvent; +use OCP\IConfig; use OCP\IDBConnection; +use OCP\IGroupManager; use OCP\ISession; use OCP\IUserManager; use OCP\IUserSession; @@ -108,6 +111,14 @@ public function register(IRegistrationContext $context): void { ); }); + $context->registerService(AppSettings::class, function(ContainerInterface $c): AppSettings { + return new AppSettings( + $c->get(IConfig::class), + $c->get(IGroupManager::class), + $c->get(IUserSession::class), + ); + }); + $context->registerMiddleWare(RequestAttributesMiddleware::class); $context->registerNotifierService(Notifier::class); diff --git a/lib/Controller/PollController.php b/lib/Controller/PollController.php index df6a90168..eb73a379b 100644 --- a/lib/Controller/PollController.php +++ b/lib/Controller/PollController.php @@ -34,6 +34,7 @@ use OCP\AppFramework\Http\Attribute\NoAdminRequired; use OCP\AppFramework\Http\JSONResponse; use OCP\IRequest; +use OCP\Server; /** * @psalm-api @@ -56,7 +57,7 @@ public function __construct( #[NoAdminRequired] public function list(): JSONResponse { return $this->response(function () { - $appSettings = new AppSettings; + $appSettings = Server::get(AppSettings::class); return [ 'list' => $this->pollService->list(), 'pollCreationAllowed' => $appSettings->getPollCreationAllowed(), diff --git a/lib/Cron/JanitorCron.php b/lib/Cron/JanitorCron.php index 5c94403cf..b922a9569 100644 --- a/lib/Cron/JanitorCron.php +++ b/lib/Cron/JanitorCron.php @@ -34,6 +34,7 @@ use OCA\Polls\Model\Settings\AppSettings; use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\TimedJob; +use OCP\Server; /** * @psalm-api @@ -52,7 +53,7 @@ public function __construct( ) { parent::__construct($time); parent::setInterval(86400); // run once a day - $this->appSettings = new AppSettings; + $this->appSettings = Server::get(AppSettings::class); } /** diff --git a/lib/Db/Share.php b/lib/Db/Share.php index 48afaf2f1..ce664ae10 100644 --- a/lib/Db/Share.php +++ b/lib/Db/Share.php @@ -30,6 +30,7 @@ use OCA\Polls\Helper\Container; use OCA\Polls\Model\Settings\AppSettings; use OCP\IURLGenerator; +use OCP\Server; /** * @psalm-suppress UnusedProperty @@ -146,8 +147,8 @@ public function __construct() { $this->addType('locked', 'int'); $this->addType('reminderSent', 'int'); $this->addType('deleted', 'int'); - $this->urlGenerator = Container::queryClass(IURLGenerator::class); - $this->appSettings = new AppSettings; + $this->urlGenerator = Server::get(IURLGenerator::class); + $this->appSettings = Server::get(AppSettings::class); } /** diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index cc8784f75..ef5c724c5 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -84,7 +84,6 @@ public function __construct( private Share $share, ) { $this->pollId = null; - $this->appSettings = new AppSettings; } /** diff --git a/lib/Model/Settings/AppSettings.php b/lib/Model/Settings/AppSettings.php index d0451c763..236d09f53 100644 --- a/lib/Model/Settings/AppSettings.php +++ b/lib/Model/Settings/AppSettings.php @@ -64,16 +64,15 @@ class AppSettings implements JsonSerializable { public const SETTING_UPDATE_TYPE_PERIODIC_POLLING = 'periodicPolling'; public const SETTING_UPDATE_TYPE_DEFAULT = self::SETTING_UPDATE_TYPE_NO_POLLING; - private IConfig $config; - private IGroupManager $groupManager; - private IUserSession $session; private string $userId = ''; + + public function __construct( + private IConfig $config, + private IGroupManager $groupManager, + private IUserSession $session, - public function __construct() { - $this->config = Container::queryClass(IConfig::class); - $this->session = Container::queryClass(IUserSession::class); - $this->userId = Container::queryClass(IUserSession::class)->getUser()?->getUId() ?? ''; - $this->groupManager = Container::queryClass(IGroupManager::class); + ) { + $this->userId = $this->session->getUser()?->getUId() ?? ''; } // Getters diff --git a/lib/Model/UserBase.php b/lib/Model/UserBase.php index f01be4fe2..7eb1a8cbf 100644 --- a/lib/Model/UserBase.php +++ b/lib/Model/UserBase.php @@ -90,11 +90,11 @@ public function __construct( ) { $this->icon = 'icon-share'; $this->l10n = Container::getL10N(); - $this->groupManager = Container::queryClass(IGroupManager::class); - $this->timeZone = Container::queryClass(IDateTimeZone::class); + $this->groupManager = Server::get(IGroupManager::class); + $this->timeZone = Server::get(IDateTimeZone::class); $this->userMapper = Server::get(UserMapper::class); - $this->userSession = Container::queryClass(IUserSession::class); - $this->appSettings = Container::queryClass(AppSettings::class); + $this->userSession = Server::get(IUserSession::class); + $this->appSettings = Server::get(AppSettings::class); } public function getId(): string { diff --git a/lib/Service/SettingsService.php b/lib/Service/SettingsService.php index f27cc2558..2df736761 100644 --- a/lib/Service/SettingsService.php +++ b/lib/Service/SettingsService.php @@ -28,13 +28,11 @@ use OCA\Polls\Model\Settings\AppSettings; class SettingsService { - private AppSettings $appSettings; - + /** * @psalm-suppress PossiblyUnusedMethod */ - public function __construct() { - $this->appSettings = new AppSettings; + public function __construct(private AppSettings $appSettings) { } /** diff --git a/lib/Service/WatchService.php b/lib/Service/WatchService.php index a60af2cdb..2dc4712c0 100644 --- a/lib/Service/WatchService.php +++ b/lib/Service/WatchService.php @@ -36,9 +36,8 @@ use OCP\ISession; class WatchService { - private AppSettings $appSettings; private Watch $watch; - + /** * @psalm-suppress PossiblyUnusedMethod */ @@ -46,8 +45,8 @@ public function __construct( private ISession $session, private WatchMapper $watchMapper, private Acl $acl, + private AppSettings $appSettings, ) { - $this->appSettings = new AppSettings; $this->watch = new Watch; } From 46a2acd9d7b1c5137195271a7d735c628fe3bf03 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 17 Mar 2024 10:18:32 +0100 Subject: [PATCH 06/12] register mappers Signed-off-by: dartcafe --- lib/AppInfo/Application.php | 48 +++++++++++++++++++++++- lib/Db/Share.php | 1 - lib/Model/Settings/AppSettings.php | 1 - lib/Service/WatchService.php | 3 +- tests/Unit/Db/CommentMapperTest.php | 7 +--- tests/Unit/Db/LogMapperTest.php | 7 +--- tests/Unit/Db/OptionMapperTest.php | 23 ++---------- tests/Unit/Db/PollMapperTest.php | 5 +-- tests/Unit/Db/SubscriptionMapperTest.php | 9 ++--- tests/Unit/Db/VoteMapperTest.php | 21 ++--------- 10 files changed, 61 insertions(+), 64 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 61165d4c9..8b31bd10e 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -28,7 +28,13 @@ use OCA\Polls\AppConstants; use OCA\Polls\Dashboard\PollWidget; +use OCA\Polls\Db\CommentMapper; +use OCA\Polls\Db\LogMapper; +use OCA\Polls\Db\OptionMapper; +use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\SubscriptionMapper; use OCA\Polls\Db\UserMapper; +use OCA\Polls\Db\VoteMapper; use OCA\Polls\Event\CommentAddEvent; use OCA\Polls\Event\CommentDeleteEvent; use OCA\Polls\Event\CommentEvent; @@ -101,7 +107,7 @@ public function boot(IBootContext $context): void { public function register(IRegistrationContext $context): void { include_once __DIR__ . '/../../vendor/autoload.php'; - $context->registerService(UserMapper::class, function(ContainerInterface $c): UserMapper { + $context->registerService(UserMapper::class, function (ContainerInterface $c): UserMapper { return new UserMapper( $c->get(IDBConnection::class), $c->get(ISession::class), @@ -111,7 +117,7 @@ public function register(IRegistrationContext $context): void { ); }); - $context->registerService(AppSettings::class, function(ContainerInterface $c): AppSettings { + $context->registerService(AppSettings::class, function (ContainerInterface $c): AppSettings { return new AppSettings( $c->get(IConfig::class), $c->get(IGroupManager::class), @@ -119,6 +125,44 @@ public function register(IRegistrationContext $context): void { ); }); + $context->registerService(PollMapper::class, function (ContainerInterface $c): PollMapper { + return new PollMapper( + $c->get(IDBConnection::class), + ); + }); + + $context->registerService(CommentMapper::class, function (ContainerInterface $c): CommentMapper { + return new CommentMapper( + $c->get(IDBConnection::class), + ); + }); + + $context->registerService(VoteMapper::class, function (ContainerInterface $c): VoteMapper { + return new VoteMapper( + $c->get(IDBConnection::class), + $c->get(LoggerInterface::class), + ); + }); + + $context->registerService(OptionMapper::class, function (ContainerInterface $c): OptionMapper { + return new OptionMapper( + $c->get(IDBConnection::class), + $c->get(UserMapper::class), + ); + }); + + $context->registerService(SubscriptionMapper::class, function (ContainerInterface $c): SubscriptionMapper { + return new SubscriptionMapper( + $c->get(IDBConnection::class), + ); + }); + + $context->registerService(LogMapper::class, function (ContainerInterface $c): LogMapper { + return new LogMapper( + $c->get(IDBConnection::class), + ); + }); + $context->registerMiddleWare(RequestAttributesMiddleware::class); $context->registerNotifierService(Notifier::class); diff --git a/lib/Db/Share.php b/lib/Db/Share.php index ce664ae10..eb5f3bc57 100644 --- a/lib/Db/Share.php +++ b/lib/Db/Share.php @@ -27,7 +27,6 @@ use JsonSerializable; use OCA\Polls\AppConstants; -use OCA\Polls\Helper\Container; use OCA\Polls\Model\Settings\AppSettings; use OCP\IURLGenerator; use OCP\Server; diff --git a/lib/Model/Settings/AppSettings.php b/lib/Model/Settings/AppSettings.php index 236d09f53..6fce8d364 100644 --- a/lib/Model/Settings/AppSettings.php +++ b/lib/Model/Settings/AppSettings.php @@ -27,7 +27,6 @@ use JsonSerializable; use OCA\Polls\AppConstants; -use OCA\Polls\Helper\Container; use OCA\Polls\Model\Group\Group; use OCP\IConfig; use OCP\IGroupManager; diff --git a/lib/Service/WatchService.php b/lib/Service/WatchService.php index 2dc4712c0..c4518efdd 100644 --- a/lib/Service/WatchService.php +++ b/lib/Service/WatchService.php @@ -36,7 +36,6 @@ use OCP\ISession; class WatchService { - private Watch $watch; /** * @psalm-suppress PossiblyUnusedMethod @@ -46,8 +45,8 @@ public function __construct( private WatchMapper $watchMapper, private Acl $acl, private AppSettings $appSettings, + private Watch $watch, ) { - $this->watch = new Watch; } /** diff --git a/tests/Unit/Db/CommentMapperTest.php b/tests/Unit/Db/CommentMapperTest.php index cca3fade1..973da1b20 100644 --- a/tests/Unit/Db/CommentMapperTest.php +++ b/tests/Unit/Db/CommentMapperTest.php @@ -24,7 +24,6 @@ namespace OCA\Polls\Tests\Unit\Db; use League\FactoryMuffin\Faker\Facade as Faker; -use OCP\IDBConnection; use OCA\Polls\Tests\Unit\UnitTestCase; use OCA\Polls\Db\Comment; @@ -33,7 +32,6 @@ use OCP\Server; class CommentMapperTest extends UnitTestCase { - private IDBConnection $con; private CommentMapper $commentMapper; private PollMapper $pollMapper; private array $polls = []; @@ -44,9 +42,8 @@ class CommentMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->commentMapper = new CommentMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->commentMapper = Server::get(CommentMapper::class); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/LogMapperTest.php b/tests/Unit/Db/LogMapperTest.php index 4223fddb7..bd7b9f55a 100644 --- a/tests/Unit/Db/LogMapperTest.php +++ b/tests/Unit/Db/LogMapperTest.php @@ -28,11 +28,9 @@ use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; use OCA\Polls\Tests\Unit\UnitTestCase; -use OCP\IDBConnection; use OCP\Server; class LogMapperTest extends UnitTestCase { - private IDBConnection $con; private LogMapper $logMapper; private PollMapper $pollMapper; /** @var Log[] $logs*/ @@ -45,9 +43,8 @@ class LogMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->logMapper = new LogMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->logMapper = Server::get(LogMapper::class); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/OptionMapperTest.php b/tests/Unit/Db/OptionMapperTest.php index 9ff453fe1..bb0478667 100644 --- a/tests/Unit/Db/OptionMapperTest.php +++ b/tests/Unit/Db/OptionMapperTest.php @@ -26,7 +26,6 @@ namespace OCA\Polls\Db; -use OCP\IDBConnection; use OCA\Polls\Tests\Unit\UnitTestCase; use OCA\Polls\Db\Poll; @@ -35,24 +34,14 @@ use OCA\Polls\Db\OptionMapper; use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; -use OCA\Polls\Db\UserMapper; -use OCP\IGroupManager; use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use OCP\Server; -use Psr\Log\LoggerInterface; class OptionMapperTest extends UnitTestCase { - private IDBConnection $con; private ISession $session; - private IUserManager $userManager; - private IUserSession $userSession; - private LoggerInterface $logger; private OptionMapper $optionMapper; private PollMapper $pollMapper; private VoteMapper $voteMapper; - private UserMapper $userMapper; /** @var Poll[] $polls */ private array $polls = []; /** @var Option[] $options */ @@ -65,18 +54,12 @@ class OptionMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->logger = Server::get(LoggerInterface::class); $this->session = Server::get(ISession::class); - $this->userManager = Server::get(IUserManager::class); - $this->userSession = Server::get(IUserSession::class); $this->session->set('ncPollsUserId', 'TestUser'); - - $this->userMapper = Server::get(UserMapper::class); - $this->voteMapper = new VoteMapper($this->con, $this->logger); - $this->optionMapper = new OptionMapper($this->con, $this->userMapper); - $this->pollMapper = new PollMapper($this->con); + $this->voteMapper = Server::get(VoteMapper::class); + $this->optionMapper = Server::get(OptionMapper::class); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/PollMapperTest.php b/tests/Unit/Db/PollMapperTest.php index 868ba26ae..d1e5b4dee 100644 --- a/tests/Unit/Db/PollMapperTest.php +++ b/tests/Unit/Db/PollMapperTest.php @@ -26,14 +26,12 @@ namespace OCA\Polls\Tests\Unit\Db; use League\FactoryMuffin\Faker\Facade as Faker; -use OCP\IDBConnection; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; use OCA\Polls\Tests\Unit\UnitTestCase; use OCP\Server; class PollMapperTest extends UnitTestCase { - private IDBConnection $con; private PollMapper $pollMapper; /** @var Poll[] $polls*/ private array $polls = []; @@ -43,8 +41,7 @@ class PollMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll'), diff --git a/tests/Unit/Db/SubscriptionMapperTest.php b/tests/Unit/Db/SubscriptionMapperTest.php index 95916c330..8d89cc596 100644 --- a/tests/Unit/Db/SubscriptionMapperTest.php +++ b/tests/Unit/Db/SubscriptionMapperTest.php @@ -23,7 +23,6 @@ namespace OCA\Polls\Tests\Unit\Db; -use OCP\IDBConnection; use OCA\Polls\Tests\Unit\UnitTestCase; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; @@ -32,12 +31,11 @@ use OCP\Server; class SubscriptionMapperTest extends UnitTestCase { - private IDBConnection $con; private SubscriptionMapper $subscriptionMapper; private PollMapper $pollMapper; /** @var Poll[] $polls */ private array $polls = []; - /** @var Poll[] $polls */ + /** @var Subscription[] $subscriptions */ private array $subscriptions = []; private array $users = []; @@ -46,9 +44,8 @@ class SubscriptionMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->subscriptionMapper = new SubscriptionMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->subscriptionMapper = Server::get(SubscriptionMapper::class); + $this->pollMapper = Server::get(PollMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/VoteMapperTest.php b/tests/Unit/Db/VoteMapperTest.php index 382d18862..c8d01f56a 100644 --- a/tests/Unit/Db/VoteMapperTest.php +++ b/tests/Unit/Db/VoteMapperTest.php @@ -26,7 +26,6 @@ namespace OCA\Polls\Tests\Unit\Db; -use OCP\IDBConnection; use OCA\Polls\Tests\Unit\UnitTestCase; use OCA\Polls\Db\Poll; @@ -35,22 +34,13 @@ use OCA\Polls\Db\OptionMapper; use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; -use OCA\Polls\Db\UserMapper; use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use OCP\Server; -use Psr\Log\LoggerInterface; class VoteMapperTest extends UnitTestCase { - private IDBConnection $con; private ISession $session; - private IUserManager $userManager; - private IUserSession $userSession; - private LoggerInterface $logger; private OptionMapper $optionMapper; private PollMapper $pollMapper; - private UserMapper $userMapper; private VoteMapper $voteMapper; /** @var Poll[] $polls */ private array $polls = []; @@ -64,17 +54,12 @@ class VoteMapperTest extends UnitTestCase { */ protected function setUp(): void { parent::setUp(); - $this->con = Server::get(IDBConnection::class); - $this->logger = Server::get(LoggerInterface::class); $this->session = Server::get(ISession::class); - $this->userManager = Server::get(IUserManager::class); - $this->userSession = Server::get(IUserSession::class); $this->session->set('ncPollsUserId', 'TestUser'); - $this->userMapper = Server::get(UserMapper::class); - $this->pollMapper = new PollMapper($this->con); - $this->voteMapper = new VoteMapper($this->con, $this->logger); - $this->optionMapper = new OptionMapper($this->con, $this->userMapper); + $this->pollMapper = Server::get(PollMapper::class); + $this->voteMapper = Server::get(VoteMapper::class); + $this->optionMapper = Server::get(OptionMapper::class); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') From 9076969d0b3d00de7a34b81c77cef4038ba6bb0d Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 17 Mar 2024 18:13:56 +0100 Subject: [PATCH 07/12] Query classes via Server::get() insted of ContainerInterface::get() Signed-off-by: dartcafe --- lib/AppInfo/Application.php | 88 ++++++++++++++++++++----------------- lib/Helper/Container.php | 19 +++----- 2 files changed, 53 insertions(+), 54 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 8b31bd10e..7d0b776eb 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -106,7 +106,54 @@ public function boot(IBootContext $context): void { public function register(IRegistrationContext $context): void { include_once __DIR__ . '/../../vendor/autoload.php'; + $this->registerServices($context); + $context->registerMiddleWare(RequestAttributesMiddleware::class); + $context->registerNotifierService(Notifier::class); + + $context->registerEventListener(CommentEvent::class, CommentListener::class); + $context->registerEventListener(CommentAddEvent::class, CommentListener::class); + $context->registerEventListener(CommentDeleteEvent::class, CommentListener::class); + + $context->registerEventListener(OptionEvent::class, OptionListener::class); + $context->registerEventListener(OptionConfirmedEvent::class, OptionListener::class); + $context->registerEventListener(OptionCreatedEvent::class, OptionListener::class); + $context->registerEventListener(OptionDeletedEvent::class, OptionListener::class); + + $context->registerEventListener(PollEvent::class, PollListener::class); + $context->registerEventListener(PollExpiredEvent::class, PollListener::class); + $context->registerEventListener(PollOptionReorderedEvent::class, PollListener::class); + $context->registerEventListener(PollOwnerChangeEvent::class, PollListener::class); + $context->registerEventListener(PollRestoredEvent::class, PollListener::class); + $context->registerEventListener(PollTakeoverEvent::class, PollListener::class); + $context->registerEventListener(PollUpdatedEvent::class, PollListener::class); + $context->registerEventListener(PollReopenEvent::class, PollListener::class); + $context->registerEventListener(PollCloseEvent::class, PollListener::class); + + $context->registerEventListener(ShareEvent::class, ShareListener::class); + $context->registerEventListener(ShareChangedDisplayNameEvent::class, ShareListener::class); + $context->registerEventListener(ShareChangedLabelEvent::class, ShareListener::class); + $context->registerEventListener(ShareChangedEmailEvent::class, ShareListener::class); + $context->registerEventListener(ShareChangedRegistrationConstraintEvent::class, ShareListener::class); + $context->registerEventListener(ShareCreateEvent::class, ShareListener::class); + $context->registerEventListener(ShareDeletedEvent::class, ShareListener::class); + $context->registerEventListener(ShareLockedEvent::class, ShareListener::class); + $context->registerEventListener(ShareRegistrationEvent::class, ShareListener::class); + $context->registerEventListener(ShareTypeChangedEvent::class, ShareListener::class); + + $context->registerEventListener(VoteEvent::class, VoteListener::class); + $context->registerEventListener(VoteSetEvent::class, VoteListener::class); + $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); + $context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class); + + $context->registerSearchProvider(SearchProvider::class); + $context->registerDashboardWidget(PollWidget::class); + } + + /** + * Register some Services + */ + private function registerServices(IRegistrationContext $context) { $context->registerService(UserMapper::class, function (ContainerInterface $c): UserMapper { return new UserMapper( $c->get(IDBConnection::class), @@ -162,46 +209,5 @@ public function register(IRegistrationContext $context): void { $c->get(IDBConnection::class), ); }); - - $context->registerMiddleWare(RequestAttributesMiddleware::class); - $context->registerNotifierService(Notifier::class); - - $context->registerEventListener(CommentEvent::class, CommentListener::class); - $context->registerEventListener(CommentAddEvent::class, CommentListener::class); - $context->registerEventListener(CommentDeleteEvent::class, CommentListener::class); - - $context->registerEventListener(OptionEvent::class, OptionListener::class); - $context->registerEventListener(OptionConfirmedEvent::class, OptionListener::class); - $context->registerEventListener(OptionCreatedEvent::class, OptionListener::class); - $context->registerEventListener(OptionDeletedEvent::class, OptionListener::class); - - $context->registerEventListener(PollEvent::class, PollListener::class); - $context->registerEventListener(PollExpiredEvent::class, PollListener::class); - $context->registerEventListener(PollOptionReorderedEvent::class, PollListener::class); - $context->registerEventListener(PollOwnerChangeEvent::class, PollListener::class); - $context->registerEventListener(PollRestoredEvent::class, PollListener::class); - $context->registerEventListener(PollTakeoverEvent::class, PollListener::class); - $context->registerEventListener(PollUpdatedEvent::class, PollListener::class); - $context->registerEventListener(PollReopenEvent::class, PollListener::class); - $context->registerEventListener(PollCloseEvent::class, PollListener::class); - - $context->registerEventListener(ShareEvent::class, ShareListener::class); - $context->registerEventListener(ShareChangedDisplayNameEvent::class, ShareListener::class); - $context->registerEventListener(ShareChangedLabelEvent::class, ShareListener::class); - $context->registerEventListener(ShareChangedEmailEvent::class, ShareListener::class); - $context->registerEventListener(ShareChangedRegistrationConstraintEvent::class, ShareListener::class); - $context->registerEventListener(ShareCreateEvent::class, ShareListener::class); - $context->registerEventListener(ShareDeletedEvent::class, ShareListener::class); - $context->registerEventListener(ShareLockedEvent::class, ShareListener::class); - $context->registerEventListener(ShareRegistrationEvent::class, ShareListener::class); - $context->registerEventListener(ShareTypeChangedEvent::class, ShareListener::class); - - $context->registerEventListener(VoteEvent::class, VoteListener::class); - $context->registerEventListener(VoteSetEvent::class, VoteListener::class); - $context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class); - $context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class); - - $context->registerSearchProvider(SearchProvider::class); - $context->registerDashboardWidget(PollWidget::class); } } diff --git a/lib/Helper/Container.php b/lib/Helper/Container.php index 0a1d37a29..0faa8b2da 100644 --- a/lib/Helper/Container.php +++ b/lib/Helper/Container.php @@ -31,34 +31,27 @@ use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCP\App\IAppManager; -use OCP\AppFramework\App; use OCP\IL10N; use OCP\L10N\IFactory; -use Psr\Container\ContainerInterface; +use OCP\Server; abstract class Container { - public static function getContainer(): ContainerInterface { - $app = new App(AppConstants::APP_ID); - return $app->getContainer(); - } - public static function queryClass(string $class): mixed { - return self::getContainer()->get($class); + return Server::get($class); } public static function queryPoll(int $pollId): Poll { - return self::queryClass(PollMapper::class)->find($pollId); + return Server::get(PollMapper::class)->find($pollId); } public static function findShare(int $pollId, string $userId): Share { - return self::queryClass(ShareMapper::class) - ->findByPollAndUser($pollId, $userId); + return Server::get(ShareMapper::class)->findByPollAndUser($pollId, $userId); } public static function getL10N(?string $lang = null): IL10N { - return self::queryClass(IFactory::class)->get(AppConstants::APP_ID, $lang); + return Server::get(IFactory::class)->get(AppConstants::APP_ID, $lang); } public static function isAppEnabled(string $app): bool { - return self::queryClass(IAppManager::class)->isEnabledForUser($app); + return Server::get(IAppManager::class)->isEnabledForUser($app); } } From 9f83f55cfd6b347138a006669d1876324de854cf Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 16 Mar 2024 21:33:13 +0100 Subject: [PATCH 08/12] join votes for participation check Signed-off-by: dartcafe --- lib/Db/Poll.php | 7 +++++++ lib/Db/PollMapper.php | 29 ++++++++++++++++++++++++++++- lib/Db/VoteMapper.php | 13 ------------- lib/Model/Acl.php | 6 +----- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/Db/Poll.php b/lib/Db/Poll.php index 6580d93fd..053eabbd6 100644 --- a/lib/Db/Poll.php +++ b/lib/Db/Poll.php @@ -136,6 +136,7 @@ class Poll extends EntityWithUser implements JsonSerializable { protected bool $hasOrphanedVotes = false; protected int $maxDate = 0; protected int $minDate = 0; + protected int $currentUserVotes = 0; public function __construct() { $this->addType('created', 'int'); @@ -153,6 +154,7 @@ public function __construct() { $this->addType('lastInteraction', 'int'); $this->addType('maxDate', 'int'); $this->addType('minDate', 'int'); + $this->addType('currentUserVotes', 'int'); $this->urlGenerator = Container::queryClass(IURLGenerator::class); $this->userMapper = Container::queryClass(UserMapper::class); $this->voteMapper = Container::queryClass(VoteMapper::class); @@ -190,6 +192,7 @@ public function jsonSerialize(): array { 'summary' => [ 'orphanedVotes' => count($this->voteMapper->findOrphanedByPollandUser($this->id, $this->userMapper->getCurrentUserCached()->getId())), 'yesByCurrentUser' => count($this->voteMapper->getYesVotesByParticipant($this->getPollId(), $this->userMapper->getCurrentUserCached()->getId())), + 'countVotes' => $this->getCurrentUserCountVotes(), ], ]; } @@ -275,6 +278,10 @@ public function getProposalsExpired(): bool { ); } + public function getCurrentUserCountVotes(): int { + return $this->currentUserVotes; + } + public function getDescription(): string { return $this->description ?? ''; } diff --git a/lib/Db/PollMapper.php b/lib/Db/PollMapper.php index 93718d24c..de08bca37 100644 --- a/lib/Db/PollMapper.php +++ b/lib/Db/PollMapper.php @@ -40,7 +40,10 @@ class PollMapper extends QBMapper { /** * @psalm-suppress PossiblyUnusedMethod */ - public function __construct(IDBConnection $db) { + public function __construct( + IDBConnection $db, + private UserMapper $userMapper, + ) { parent::__construct($db, Poll::TABLE, Poll::class); } @@ -167,6 +170,7 @@ public function deleteByUserId(string $userId): void { * Build the enhanced query with joined tables */ protected function buildQuery(): IQueryBuilder { + $currentUserId = $this->userMapper->getCurrentUser()->getId(); $qb = $this->db->getQueryBuilder(); $qb->select(self::TABLE . '.*') @@ -174,6 +178,7 @@ protected function buildQuery(): IQueryBuilder { // ->groupBy(self::TABLE . '.id') ->from($this->getTableName(), self::TABLE); $this->joinOptionsForMaxDate($qb, self::TABLE); + $this->joinCurrentUserVotes($qb, self::TABLE, $currentUserId); $qb->groupBy(self::TABLE . '.id'); return $qb; } @@ -201,4 +206,26 @@ protected function joinOptionsForMaxDate(IQueryBuilder &$qb, string $fromAlias): ); } + /** + * Joins options to evaluate min and max option date for date polls + * if text poll or no options are set, + * the min value is the current time, + * the max value is null + */ + protected function joinCurrentUserVotes(IQueryBuilder &$qb, string $fromAlias, $currentUserId): void { + $joinAlias = 'user_vote'; + // force value into a MIN function to avoid grouping errors + $qb->selectAlias($qb->func()->count($joinAlias . '.vote_answer'), 'current_user_votes'); + + $qb->leftJoin( + $fromAlias, + Vote::TABLE, + $joinAlias, + $qb->expr()->andX( + $qb->expr()->eq($joinAlias . '.poll_id', $fromAlias . '.id'), + $qb->expr()->eq($joinAlias . '.user_id', $qb->createNamedParameter($currentUserId, IQueryBuilder::PARAM_STR)), + ) + ); + } + } diff --git a/lib/Db/VoteMapper.php b/lib/Db/VoteMapper.php index 549816d4d..e950982be 100644 --- a/lib/Db/VoteMapper.php +++ b/lib/Db/VoteMapper.php @@ -125,19 +125,6 @@ public function findParticipantsByPoll(int $pollId): array { return $this->findEntities($qb); } - - /** - * @throws \OCP\AppFramework\Db\DoesNotExistException if not found - * @return Vote[] - * @psalm-return array - */ - public function findParticipantsVotes(int $pollId, string $userId): array { - $qb = $this->buildQuery(); - $qb->andWhere($qb->expr()->eq(self::TABLE . '.poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) - ->andWhere($qb->expr()->eq(self::TABLE . '.user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))); - return $this->findEntities($qb); - } - public function deleteByPollAndUserId(int $pollId, string $userId): void { $qb = $this->db->getQueryBuilder(); $qb->delete($this->getTableName()) diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index cc8784f75..691560e59 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -33,7 +33,6 @@ use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\UserMapper; -use OCA\Polls\Db\VoteMapper; use OCA\Polls\Exceptions\ForbiddenException; use OCA\Polls\Exceptions\InsufficientAttributesException; use OCA\Polls\Exceptions\InvalidPollIdException; @@ -79,7 +78,6 @@ public function __construct( private ISession $session, private ShareMapper $shareMapper, private UserMapper $userMapper, - private VoteMapper $voteMapper, private Poll $poll, private Share $share, ) { @@ -323,9 +321,7 @@ private function getIsOpenPoll(): bool { * @return bool Returns true, if the current user is already a particitipant of the current poll. */ private function getIsParticipant(): bool { - return count( - $this->voteMapper->findParticipantsVotes($this->getPollId(), $this->getUserId()) - ) > 0; + return $this->getPoll()->getCurrentUserCountVotes() > 0; } /** From 662fba99429db1205a2bf8f5b9b8ad6a2a45d14a Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 16 Mar 2024 21:53:50 +0100 Subject: [PATCH 09/12] adjust tests Signed-off-by: dartcafe --- tests/Unit/Db/CommentMapperTest.php | 22 +++++++++++++++++++++- tests/Unit/Db/LogMapperTest.php | 20 +++++++++++++++++++- tests/Unit/Db/OptionMapperTest.php | 3 +-- tests/Unit/Db/PollMapperTest.php | 19 ++++++++++++++++++- tests/Unit/Db/SubscriptionMapperTest.php | 19 ++++++++++++++++++- tests/Unit/Db/VoteMapperTest.php | 11 +---------- 6 files changed, 78 insertions(+), 16 deletions(-) diff --git a/tests/Unit/Db/CommentMapperTest.php b/tests/Unit/Db/CommentMapperTest.php index cca3fade1..0e8fa461e 100644 --- a/tests/Unit/Db/CommentMapperTest.php +++ b/tests/Unit/Db/CommentMapperTest.php @@ -29,14 +29,27 @@ use OCA\Polls\Db\Comment; use OCA\Polls\Db\CommentMapper; +use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class CommentMapperTest extends UnitTestCase { private IDBConnection $con; + private ISession $session; + private IUserManager $userManager; + private IUserSession $userSession; + private LoggerInterface $logger; private CommentMapper $commentMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; + /** @var Poll[] $polls */ private array $polls = []; + /** @var Comment[] $comments */ private array $comments = []; /** @@ -45,8 +58,15 @@ class CommentMapperTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); $this->con = Server::get(IDBConnection::class); + $this->logger = Server::get(LoggerInterface::class); + $this->session = Server::get(ISession::class); + $this->userManager = Server::get(IUserManager::class); + $this->userSession = Server::get(IUserSession::class); + $this->session->set('ncPollsUserId', 'TestUser'); + + $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); $this->commentMapper = new CommentMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/LogMapperTest.php b/tests/Unit/Db/LogMapperTest.php index 4223fddb7..e46e16662 100644 --- a/tests/Unit/Db/LogMapperTest.php +++ b/tests/Unit/Db/LogMapperTest.php @@ -27,14 +27,24 @@ use OCA\Polls\Db\LogMapper; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; use OCA\Polls\Tests\Unit\UnitTestCase; use OCP\IDBConnection; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class LogMapperTest extends UnitTestCase { private IDBConnection $con; + private ISession $session; + private IUserManager $userManager; + private IUserSession $userSession; + private LoggerInterface $logger; private LogMapper $logMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Log[] $logs*/ private array $logs = []; /** @var Poll[] $polls*/ @@ -46,8 +56,16 @@ class LogMapperTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); $this->con = Server::get(IDBConnection::class); + $this->logger = Server::get(LoggerInterface::class); + $this->session = Server::get(ISession::class); + $this->userManager = Server::get(IUserManager::class); + $this->userSession = Server::get(IUserSession::class); + $this->session->set('ncPollsUserId', 'TestUser'); + + $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); + $this->logMapper = new LogMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/OptionMapperTest.php b/tests/Unit/Db/OptionMapperTest.php index 1738d9092..50ca1728c 100644 --- a/tests/Unit/Db/OptionMapperTest.php +++ b/tests/Unit/Db/OptionMapperTest.php @@ -36,7 +36,6 @@ use OCA\Polls\Db\Vote; use OCA\Polls\Db\VoteMapper; use OCA\Polls\Db\UserMapper; -use OCP\IGroupManager; use OCP\ISession; use OCP\IUserManager; use OCP\IUserSession; @@ -76,7 +75,7 @@ protected function setUp(): void { $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); $this->voteMapper = new VoteMapper($this->con, $this->logger); $this->optionMapper = new OptionMapper($this->con, $this->userMapper); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/PollMapperTest.php b/tests/Unit/Db/PollMapperTest.php index 868ba26ae..9a7b707b3 100644 --- a/tests/Unit/Db/PollMapperTest.php +++ b/tests/Unit/Db/PollMapperTest.php @@ -29,12 +29,23 @@ use OCP\IDBConnection; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; use OCA\Polls\Tests\Unit\UnitTestCase; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class PollMapperTest extends UnitTestCase { private IDBConnection $con; + private ISession $session; + private IUserManager $userManager; + private IUserSession $userSession; + private LoggerInterface $logger; + private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Poll[] $polls*/ private array $polls = []; @@ -44,7 +55,13 @@ class PollMapperTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); $this->con = Server::get(IDBConnection::class); - $this->pollMapper = new PollMapper($this->con); + $this->logger = Server::get(LoggerInterface::class); + $this->session = Server::get(ISession::class); + $this->userManager = Server::get(IUserManager::class); + $this->userSession = Server::get(IUserSession::class); + $this->session->set('ncPollsUserId', 'TestUser'); + $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll'), diff --git a/tests/Unit/Db/SubscriptionMapperTest.php b/tests/Unit/Db/SubscriptionMapperTest.php index 95916c330..1840a6131 100644 --- a/tests/Unit/Db/SubscriptionMapperTest.php +++ b/tests/Unit/Db/SubscriptionMapperTest.php @@ -29,12 +29,22 @@ use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Subscription; use OCA\Polls\Db\SubscriptionMapper; +use OCA\Polls\Db\UserMapper; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class SubscriptionMapperTest extends UnitTestCase { private IDBConnection $con; + private ISession $session; + private IUserManager $userManager; + private IUserSession $userSession; + private LoggerInterface $logger; private SubscriptionMapper $subscriptionMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Poll[] $polls */ private array $polls = []; /** @var Poll[] $polls */ @@ -47,8 +57,15 @@ class SubscriptionMapperTest extends UnitTestCase { protected function setUp(): void { parent::setUp(); $this->con = Server::get(IDBConnection::class); + $this->logger = Server::get(LoggerInterface::class); + $this->session = Server::get(ISession::class); + $this->userManager = Server::get(IUserManager::class); + $this->userSession = Server::get(IUserSession::class); + $this->session->set('ncPollsUserId', 'TestUser'); + $this->subscriptionMapper = new SubscriptionMapper($this->con); - $this->pollMapper = new PollMapper($this->con); + $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->polls = [ $this->fm->instance('OCA\Polls\Db\Poll') diff --git a/tests/Unit/Db/VoteMapperTest.php b/tests/Unit/Db/VoteMapperTest.php index 433f176bd..407ca0419 100644 --- a/tests/Unit/Db/VoteMapperTest.php +++ b/tests/Unit/Db/VoteMapperTest.php @@ -72,7 +72,7 @@ protected function setUp(): void { $this->session->set('ncPollsUserId', 'TestUser'); $this->userMapper = new UserMapper($this->con, $this->session, $this->userSession, $this->userManager, $this->logger); - $this->pollMapper = new PollMapper($this->con); + $this->pollMapper = new PollMapper($this->con, $this->userMapper); $this->voteMapper = new VoteMapper($this->con, $this->logger); $this->optionMapper = new OptionMapper($this->con, $this->userMapper); @@ -134,15 +134,6 @@ public function testParticipantsByPoll() { } } - /** - * testParticipantsByPoll - */ - public function testFindParticipantsVotes() { - foreach ($this->votes as $vote) { - $this->assertTrue(count($this->voteMapper->findParticipantsVotes($vote->getPollId(), $vote->getUserId())) > 0); - } - } - /** * testUpdate */ From 2cd89d29bd7f3d3f5d47826ec44da369f5f0de92 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 16 Mar 2024 21:33:13 +0100 Subject: [PATCH 10/12] join votes for participation check Signed-off-by: dartcafe --- lib/Db/Poll.php | 7 +++++++ lib/Db/PollMapper.php | 29 ++++++++++++++++++++++++++++- lib/Db/VoteMapper.php | 13 ------------- lib/Model/Acl.php | 6 +----- 4 files changed, 36 insertions(+), 19 deletions(-) diff --git a/lib/Db/Poll.php b/lib/Db/Poll.php index 6580d93fd..053eabbd6 100644 --- a/lib/Db/Poll.php +++ b/lib/Db/Poll.php @@ -136,6 +136,7 @@ class Poll extends EntityWithUser implements JsonSerializable { protected bool $hasOrphanedVotes = false; protected int $maxDate = 0; protected int $minDate = 0; + protected int $currentUserVotes = 0; public function __construct() { $this->addType('created', 'int'); @@ -153,6 +154,7 @@ public function __construct() { $this->addType('lastInteraction', 'int'); $this->addType('maxDate', 'int'); $this->addType('minDate', 'int'); + $this->addType('currentUserVotes', 'int'); $this->urlGenerator = Container::queryClass(IURLGenerator::class); $this->userMapper = Container::queryClass(UserMapper::class); $this->voteMapper = Container::queryClass(VoteMapper::class); @@ -190,6 +192,7 @@ public function jsonSerialize(): array { 'summary' => [ 'orphanedVotes' => count($this->voteMapper->findOrphanedByPollandUser($this->id, $this->userMapper->getCurrentUserCached()->getId())), 'yesByCurrentUser' => count($this->voteMapper->getYesVotesByParticipant($this->getPollId(), $this->userMapper->getCurrentUserCached()->getId())), + 'countVotes' => $this->getCurrentUserCountVotes(), ], ]; } @@ -275,6 +278,10 @@ public function getProposalsExpired(): bool { ); } + public function getCurrentUserCountVotes(): int { + return $this->currentUserVotes; + } + public function getDescription(): string { return $this->description ?? ''; } diff --git a/lib/Db/PollMapper.php b/lib/Db/PollMapper.php index 93718d24c..de08bca37 100644 --- a/lib/Db/PollMapper.php +++ b/lib/Db/PollMapper.php @@ -40,7 +40,10 @@ class PollMapper extends QBMapper { /** * @psalm-suppress PossiblyUnusedMethod */ - public function __construct(IDBConnection $db) { + public function __construct( + IDBConnection $db, + private UserMapper $userMapper, + ) { parent::__construct($db, Poll::TABLE, Poll::class); } @@ -167,6 +170,7 @@ public function deleteByUserId(string $userId): void { * Build the enhanced query with joined tables */ protected function buildQuery(): IQueryBuilder { + $currentUserId = $this->userMapper->getCurrentUser()->getId(); $qb = $this->db->getQueryBuilder(); $qb->select(self::TABLE . '.*') @@ -174,6 +178,7 @@ protected function buildQuery(): IQueryBuilder { // ->groupBy(self::TABLE . '.id') ->from($this->getTableName(), self::TABLE); $this->joinOptionsForMaxDate($qb, self::TABLE); + $this->joinCurrentUserVotes($qb, self::TABLE, $currentUserId); $qb->groupBy(self::TABLE . '.id'); return $qb; } @@ -201,4 +206,26 @@ protected function joinOptionsForMaxDate(IQueryBuilder &$qb, string $fromAlias): ); } + /** + * Joins options to evaluate min and max option date for date polls + * if text poll or no options are set, + * the min value is the current time, + * the max value is null + */ + protected function joinCurrentUserVotes(IQueryBuilder &$qb, string $fromAlias, $currentUserId): void { + $joinAlias = 'user_vote'; + // force value into a MIN function to avoid grouping errors + $qb->selectAlias($qb->func()->count($joinAlias . '.vote_answer'), 'current_user_votes'); + + $qb->leftJoin( + $fromAlias, + Vote::TABLE, + $joinAlias, + $qb->expr()->andX( + $qb->expr()->eq($joinAlias . '.poll_id', $fromAlias . '.id'), + $qb->expr()->eq($joinAlias . '.user_id', $qb->createNamedParameter($currentUserId, IQueryBuilder::PARAM_STR)), + ) + ); + } + } diff --git a/lib/Db/VoteMapper.php b/lib/Db/VoteMapper.php index 549816d4d..e950982be 100644 --- a/lib/Db/VoteMapper.php +++ b/lib/Db/VoteMapper.php @@ -125,19 +125,6 @@ public function findParticipantsByPoll(int $pollId): array { return $this->findEntities($qb); } - - /** - * @throws \OCP\AppFramework\Db\DoesNotExistException if not found - * @return Vote[] - * @psalm-return array - */ - public function findParticipantsVotes(int $pollId, string $userId): array { - $qb = $this->buildQuery(); - $qb->andWhere($qb->expr()->eq(self::TABLE . '.poll_id', $qb->createNamedParameter($pollId, IQueryBuilder::PARAM_INT))) - ->andWhere($qb->expr()->eq(self::TABLE . '.user_id', $qb->createNamedParameter($userId, IQueryBuilder::PARAM_STR))); - return $this->findEntities($qb); - } - public function deleteByPollAndUserId(int $pollId, string $userId): void { $qb = $this->db->getQueryBuilder(); $qb->delete($this->getTableName()) diff --git a/lib/Model/Acl.php b/lib/Model/Acl.php index ef5c724c5..414125edc 100644 --- a/lib/Model/Acl.php +++ b/lib/Model/Acl.php @@ -33,7 +33,6 @@ use OCA\Polls\Db\Share; use OCA\Polls\Db\ShareMapper; use OCA\Polls\Db\UserMapper; -use OCA\Polls\Db\VoteMapper; use OCA\Polls\Exceptions\ForbiddenException; use OCA\Polls\Exceptions\InsufficientAttributesException; use OCA\Polls\Exceptions\InvalidPollIdException; @@ -79,7 +78,6 @@ public function __construct( private ISession $session, private ShareMapper $shareMapper, private UserMapper $userMapper, - private VoteMapper $voteMapper, private Poll $poll, private Share $share, ) { @@ -322,9 +320,7 @@ private function getIsOpenPoll(): bool { * @return bool Returns true, if the current user is already a particitipant of the current poll. */ private function getIsParticipant(): bool { - return count( - $this->voteMapper->findParticipantsVotes($this->getPollId(), $this->getUserId()) - ) > 0; + return $this->getPoll()->getCurrentUserCountVotes() > 0; } /** From 19665044bc667c50d6f0163995eb26e31be15e4b Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sat, 16 Mar 2024 21:53:50 +0100 Subject: [PATCH 11/12] adjust tests Signed-off-by: dartcafe --- tests/Unit/Db/CommentMapperTest.php | 9 +++++++++ tests/Unit/Db/LogMapperTest.php | 3 +++ tests/Unit/Db/PollMapperTest.php | 6 ++++++ tests/Unit/Db/SubscriptionMapperTest.php | 6 ++++++ tests/Unit/Db/VoteMapperTest.php | 9 --------- 5 files changed, 24 insertions(+), 9 deletions(-) diff --git a/tests/Unit/Db/CommentMapperTest.php b/tests/Unit/Db/CommentMapperTest.php index 973da1b20..89f15b91d 100644 --- a/tests/Unit/Db/CommentMapperTest.php +++ b/tests/Unit/Db/CommentMapperTest.php @@ -28,13 +28,22 @@ use OCA\Polls\Db\Comment; use OCA\Polls\Db\CommentMapper; +use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class CommentMapperTest extends UnitTestCase { private CommentMapper $commentMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; + /** @var Poll[] $polls */ private array $polls = []; + /** @var Comment[] $comments */ private array $comments = []; /** diff --git a/tests/Unit/Db/LogMapperTest.php b/tests/Unit/Db/LogMapperTest.php index bd7b9f55a..edd515f1b 100644 --- a/tests/Unit/Db/LogMapperTest.php +++ b/tests/Unit/Db/LogMapperTest.php @@ -27,12 +27,15 @@ use OCA\Polls\Db\LogMapper; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; use OCA\Polls\Tests\Unit\UnitTestCase; use OCP\Server; +use Psr\Log\LoggerInterface; class LogMapperTest extends UnitTestCase { private LogMapper $logMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Log[] $logs*/ private array $logs = []; /** @var Poll[] $polls*/ diff --git a/tests/Unit/Db/PollMapperTest.php b/tests/Unit/Db/PollMapperTest.php index d1e5b4dee..d5f33f88f 100644 --- a/tests/Unit/Db/PollMapperTest.php +++ b/tests/Unit/Db/PollMapperTest.php @@ -28,11 +28,17 @@ use League\FactoryMuffin\Faker\Facade as Faker; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; +use OCA\Polls\Db\UserMapper; use OCA\Polls\Tests\Unit\UnitTestCase; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class PollMapperTest extends UnitTestCase { private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Poll[] $polls*/ private array $polls = []; diff --git a/tests/Unit/Db/SubscriptionMapperTest.php b/tests/Unit/Db/SubscriptionMapperTest.php index 8d89cc596..26ee71681 100644 --- a/tests/Unit/Db/SubscriptionMapperTest.php +++ b/tests/Unit/Db/SubscriptionMapperTest.php @@ -28,11 +28,17 @@ use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Subscription; use OCA\Polls\Db\SubscriptionMapper; +use OCA\Polls\Db\UserMapper; +use OCP\ISession; +use OCP\IUserManager; +use OCP\IUserSession; use OCP\Server; +use Psr\Log\LoggerInterface; class SubscriptionMapperTest extends UnitTestCase { private SubscriptionMapper $subscriptionMapper; private PollMapper $pollMapper; + private UserMapper $userMapper; /** @var Poll[] $polls */ private array $polls = []; /** @var Subscription[] $subscriptions */ diff --git a/tests/Unit/Db/VoteMapperTest.php b/tests/Unit/Db/VoteMapperTest.php index c8d01f56a..f8a50c363 100644 --- a/tests/Unit/Db/VoteMapperTest.php +++ b/tests/Unit/Db/VoteMapperTest.php @@ -119,15 +119,6 @@ public function testParticipantsByPoll() { } } - /** - * testParticipantsByPoll - */ - public function testFindParticipantsVotes() { - foreach ($this->votes as $vote) { - $this->assertTrue(count($this->voteMapper->findParticipantsVotes($vote->getPollId(), $vote->getUserId())) > 0); - } - } - /** * testUpdate */ From d7ff4e903112c7cdcae711906948765b66915740 Mon Sep 17 00:00:00 2001 From: dartcafe Date: Sun, 17 Mar 2024 18:52:08 +0100 Subject: [PATCH 12/12] some merging aftermath Signed-off-by: dartcafe --- lib/AppInfo/Application.php | 1 + tests/Unit/Db/CommentMapperTest.php | 12 ------------ tests/Unit/Db/LogMapperTest.php | 6 ------ tests/Unit/Db/PollMapperTest.php | 8 -------- tests/Unit/Db/SubscriptionMapperTest.php | 11 ----------- 5 files changed, 1 insertion(+), 37 deletions(-) diff --git a/lib/AppInfo/Application.php b/lib/AppInfo/Application.php index 7d0b776eb..f8f94e7f5 100644 --- a/lib/AppInfo/Application.php +++ b/lib/AppInfo/Application.php @@ -175,6 +175,7 @@ private function registerServices(IRegistrationContext $context) { $context->registerService(PollMapper::class, function (ContainerInterface $c): PollMapper { return new PollMapper( $c->get(IDBConnection::class), + $c->get(UserMapper::class) ); }); diff --git a/tests/Unit/Db/CommentMapperTest.php b/tests/Unit/Db/CommentMapperTest.php index 38034d278..9fcc3af8e 100644 --- a/tests/Unit/Db/CommentMapperTest.php +++ b/tests/Unit/Db/CommentMapperTest.php @@ -29,24 +29,12 @@ use OCA\Polls\Db\Comment; use OCA\Polls\Db\CommentMapper; use OCA\Polls\Db\Poll; -use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; -use OCA\Polls\Db\UserMapper; -use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; -use OCA\Polls\Db\UserMapper; -use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use OCP\Server; -use Psr\Log\LoggerInterface; -use Psr\Log\LoggerInterface; class CommentMapperTest extends UnitTestCase { private CommentMapper $commentMapper; private PollMapper $pollMapper; - private UserMapper $userMapper; /** @var Poll[] $polls */ private array $polls = []; /** @var Comment[] $comments */ diff --git a/tests/Unit/Db/LogMapperTest.php b/tests/Unit/Db/LogMapperTest.php index f69077c69..bd7b9f55a 100644 --- a/tests/Unit/Db/LogMapperTest.php +++ b/tests/Unit/Db/LogMapperTest.php @@ -27,18 +27,12 @@ use OCA\Polls\Db\LogMapper; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; -use OCA\Polls\Db\UserMapper; -use OCA\Polls\Db\UserMapper; use OCA\Polls\Tests\Unit\UnitTestCase; use OCP\Server; -use Psr\Log\LoggerInterface; -use Psr\Log\LoggerInterface; class LogMapperTest extends UnitTestCase { private LogMapper $logMapper; private PollMapper $pollMapper; - private UserMapper $userMapper; - private UserMapper $userMapper; /** @var Log[] $logs*/ private array $logs = []; /** @var Poll[] $polls*/ diff --git a/tests/Unit/Db/PollMapperTest.php b/tests/Unit/Db/PollMapperTest.php index 26458827e..d1e5b4dee 100644 --- a/tests/Unit/Db/PollMapperTest.php +++ b/tests/Unit/Db/PollMapperTest.php @@ -28,19 +28,11 @@ use League\FactoryMuffin\Faker\Facade as Faker; use OCA\Polls\Db\Poll; use OCA\Polls\Db\PollMapper; -use OCA\Polls\Db\UserMapper; -use OCA\Polls\Db\UserMapper; use OCA\Polls\Tests\Unit\UnitTestCase; -use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use OCP\Server; -use Psr\Log\LoggerInterface; class PollMapperTest extends UnitTestCase { private PollMapper $pollMapper; - private UserMapper $userMapper; - private UserMapper $userMapper; /** @var Poll[] $polls*/ private array $polls = []; diff --git a/tests/Unit/Db/SubscriptionMapperTest.php b/tests/Unit/Db/SubscriptionMapperTest.php index 18f46d915..8d89cc596 100644 --- a/tests/Unit/Db/SubscriptionMapperTest.php +++ b/tests/Unit/Db/SubscriptionMapperTest.php @@ -28,22 +28,11 @@ use OCA\Polls\Db\PollMapper; use OCA\Polls\Db\Subscription; use OCA\Polls\Db\SubscriptionMapper; -use OCA\Polls\Db\UserMapper; -use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; -use OCA\Polls\Db\UserMapper; -use OCP\ISession; -use OCP\IUserManager; -use OCP\IUserSession; use OCP\Server; -use Psr\Log\LoggerInterface; class SubscriptionMapperTest extends UnitTestCase { private SubscriptionMapper $subscriptionMapper; private PollMapper $pollMapper; - private UserMapper $userMapper; - private UserMapper $userMapper; /** @var Poll[] $polls */ private array $polls = []; /** @var Subscription[] $subscriptions */