Skip to content

Commit

Permalink
Merge pull request #3021 from nextcloud/ref/events
Browse files Browse the repository at this point in the history
refactor events
  • Loading branch information
dartcafe authored Aug 13, 2023
2 parents 6a53a83 + 299e8ba commit 7fb5102
Show file tree
Hide file tree
Showing 68 changed files with 241 additions and 262 deletions.
7 changes: 4 additions & 3 deletions lib/Activity/PollChanges.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
*/
namespace OCA\Polls\Activity;

use OCA\Polls\AppConstants;
use OCP\Activity\IFilter;
use OCP\IL10N;
use OCP\IURLGenerator;
Expand All @@ -34,23 +35,23 @@ public function __construct(
}

public function getIdentifier() : string {
return 'polls';
return AppConstants::APP_ID;
}

public function getName() : string {
return $this->l10n->t('Poll changes');
}

public function getIcon() : string {
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath('polls', 'polls.svg'));
return $this->urlGenerator->getAbsoluteURL($this->urlGenerator->imagePath(AppConstants::APP_ID, 'polls.svg'));
}

public function getPriority() : int {
return 70;
}

public function allowedApps() : array {
return ['polls'];
return [AppConstants::APP_ID];
}

public function filterTypes(array $types) : array {
Expand Down
30 changes: 30 additions & 0 deletions lib/AppConstants.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

/**
* @copyright Copyright (c) 2023 René Gieling <[email protected]>
*
* @author René Gieling <[email protected]>
*
* @license GNU AGPL version 3 or any later version
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/

namespace OCA\Polls;

abstract class AppConstants {
/** @var string */
public const APP_ID = 'polls';
}
63 changes: 13 additions & 50 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,31 +25,13 @@

namespace OCA\Polls\AppInfo;

use OCA\Polls\AppConstants;
use OCA\Polls\Dashboard\PollWidget;
use OCA\Polls\Event\CommentAddEvent;
use OCA\Polls\Event\CommentDeleteEvent;
use OCA\Polls\Event\OptionConfirmedEvent;
use OCA\Polls\Event\OptionCreatedEvent;
use OCA\Polls\Event\OptionDeletedEvent;
use OCA\Polls\Event\OptionUnconfirmedEvent;
use OCA\Polls\Event\OptionUpdatedEvent;
use OCA\Polls\Event\PollArchivedEvent;
use OCA\Polls\Event\PollCreatedEvent;
use OCA\Polls\Event\PollDeletedEvent;
use OCA\Polls\Event\PollExpiredEvent;
use OCA\Polls\Event\PollOptionReorderedEvent;
use OCA\Polls\Event\PollOwnerChangeEvent;
use OCA\Polls\Event\PollRestoredEvent;
use OCA\Polls\Event\PollTakeoverEvent;
use OCA\Polls\Event\PollUpdatedEvent;
use OCA\Polls\Event\ShareChangedDisplayNameEvent;
use OCA\Polls\Event\ShareChangedEmailEvent;
use OCA\Polls\Event\ShareChangedRegistrationConstraintEvent;
use OCA\Polls\Event\ShareCreateEvent;
use OCA\Polls\Event\ShareDeletedEvent;
use OCA\Polls\Event\ShareRegistrationEvent;
use OCA\Polls\Event\ShareTypeChangedEvent;
use OCA\Polls\Event\VoteSetEvent;
use OCA\Polls\Event\CommentEvent;
use OCA\Polls\Event\OptionEvent;
use OCA\Polls\Event\PollEvent;
use OCA\Polls\Event\ShareEvent;
use OCA\Polls\Event\VoteEvent;
use OCA\Polls\Listener\CommentListener;
use OCA\Polls\Listener\GroupDeletedListener;
use OCA\Polls\Listener\OptionListener;
Expand All @@ -69,10 +51,10 @@

class Application extends App implements IBootstrap {
/** @var string */
public const APP_ID = 'polls';
public const APP_ID = AppConstants::APP_ID;

public function __construct(array $urlParams = []) {
parent::__construct(self::APP_ID, $urlParams);
parent::__construct(AppConstants::APP_ID, $urlParams);
}

public function boot(IBootContext $context): void {
Expand All @@ -85,30 +67,11 @@ public function register(IRegistrationContext $context): void {
$context->registerMiddleWare(RequestAttributesMiddleware::class);
$context->registerNotifierService(Notifier::class);

$context->registerEventListener(CommentAddEvent::class, CommentListener::class);
$context->registerEventListener(CommentDeleteEvent::class, CommentListener::class);
$context->registerEventListener(OptionConfirmedEvent::class, OptionListener::class);
$context->registerEventListener(OptionCreatedEvent::class, OptionListener::class);
$context->registerEventListener(OptionDeletedEvent::class, OptionListener::class);
$context->registerEventListener(OptionUnconfirmedEvent::class, OptionListener::class);
$context->registerEventListener(PollOptionReorderedEvent::class, OptionListener::class);
$context->registerEventListener(OptionUpdatedEvent::class, OptionListener::class);
$context->registerEventListener(PollArchivedEvent::class, PollListener::class);
$context->registerEventListener(PollCreatedEvent::class, PollListener::class);
$context->registerEventListener(PollDeletedEvent::class, PollListener::class);
$context->registerEventListener(PollExpiredEvent::class, PollListener::class);
$context->registerEventListener(PollRestoredEvent::class, PollListener::class);
$context->registerEventListener(PollOwnerChangeEvent::class, PollListener::class);
$context->registerEventListener(PollTakeoverEvent::class, PollListener::class);
$context->registerEventListener(PollUpdatedEvent::class, PollListener::class);
$context->registerEventListener(ShareChangedEmailEvent::class, ShareListener::class);
$context->registerEventListener(ShareChangedDisplayNameEvent::class, ShareListener::class);
$context->registerEventListener(ShareChangedRegistrationConstraintEvent::class, ShareListener::class);
$context->registerEventListener(ShareCreateEvent::class, ShareListener::class);
$context->registerEventListener(ShareDeletedEvent::class, ShareListener::class);
$context->registerEventListener(ShareRegistrationEvent::class, ShareListener::class);
$context->registerEventListener(ShareTypeChangedEvent::class, ShareListener::class);
$context->registerEventListener(VoteSetEvent::class, VoteListener::class);
$context->registerEventListener(CommentEvent::class, CommentListener::class);
$context->registerEventListener(OptionEvent::class, OptionListener::class);
$context->registerEventListener(PollEvent::class, PollListener::class);
$context->registerEventListener(ShareEvent::class, ShareListener::class);
$context->registerEventListener(VoteEvent::class, VoteListener::class);
$context->registerEventListener(UserDeletedEvent::class, UserDeletedListener::class);
$context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class);
$context->registerSearchProvider(SearchProvider::class);
Expand Down
3 changes: 2 additions & 1 deletion lib/Command/Poll/TransferOwnership.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Polls\Command\Poll;

use OCA\Polls\AppConstants;
use OCA\Polls\Service\PollService;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -42,7 +43,7 @@ public function __construct(

protected function configure(): void {
$this
->setName('polls:poll:transfer-ownership')
->setName(AppConstants::APP_ID . ':poll:transfer-ownership')
->setDescription('Transfer the ownership of one user\'s polls to another user.')
->addArgument(
'source-user',
Expand Down
3 changes: 2 additions & 1 deletion lib/Command/Share/Add.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\Polls\Command\Share;

use OC\Core\Command\Base;
use OCA\Polls\AppConstants;
use OCA\Polls\Db\Poll;
use OCA\Polls\Exceptions\ShareAlreadyExistsException;
use OCA\Polls\Model\Group\Group;
Expand All @@ -41,7 +42,7 @@ class Add extends Base {

protected function configure(): void {
$this
->setName('polls:share:add')
->setName(AppConstants::APP_ID . ':share:add')
->setDescription('Invites users to a poll')
->addArgument(
'id',
Expand Down
3 changes: 2 additions & 1 deletion lib/Command/Share/Remove.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
namespace OCA\Polls\Command\Share;

use OC\Core\Command\Base;
use OCA\Polls\AppConstants;
use OCA\Polls\Db\Poll;
use OCA\Polls\Db\Share;
use OCA\Polls\Model\Group\Group;
Expand All @@ -42,7 +43,7 @@ class Remove extends Base {

protected function configure(): void {
$this
->setName('polls:share:remove')
->setName(AppConstants::APP_ID . ':share:remove')
->setDescription('Remove user invitations from a poll')
->addArgument(
'id',
Expand Down
3 changes: 2 additions & 1 deletion lib/Controller/AdminController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Polls\Controller;

use OCA\Polls\AppConstants;
use OCA\Polls\Service\PollService;
use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\TemplateResponse;
Expand All @@ -45,7 +46,7 @@ public function __construct(
* @NoCSRFRequired
*/
public function index(): TemplateResponse {
return new TemplateResponse('polls', 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
return new TemplateResponse(AppConstants::APP_ID, 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Controller/CommentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Polls\Controller;

use \OCA\Polls\Db\Comment;
use OCA\Polls\Model\Acl;
use OCA\Polls\Service\CommentService;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -74,7 +75,7 @@ public function delete(int $commentId): JSONResponse {
]);
}

private function deleteComment($commentId) {
private function deleteComment(int $commentId): Comment {
$comment = $this->commentService->get($commentId);
return $this->commentService->delete($comment, $this->acl->setPollId($comment->getPollId()));
}
Expand Down
5 changes: 3 additions & 2 deletions lib/Controller/PageController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

namespace OCA\Polls\Controller;

use OCA\Polls\AppConstants;
use OCA\Polls\Service\NotificationService;
use OCP\AppFramework\Controller;

Expand All @@ -46,7 +47,7 @@ public function __construct(
* @NoCSRFRequired
*/
public function index(): TemplateResponse {
return new TemplateResponse('polls', 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
return new TemplateResponse(AppConstants::APP_ID, 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
}

/**
Expand All @@ -55,6 +56,6 @@ public function index(): TemplateResponse {
*/
public function vote(int $id): TemplateResponse {
$this->notificationService->removeNotification($id);
return new TemplateResponse('polls', 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
return new TemplateResponse(AppConstants::APP_ID, 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
}
}
3 changes: 2 additions & 1 deletion lib/Controller/PollApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Polls\Controller;

use OCA\Polls\AppConstants;
use OCA\Polls\Exceptions\Exception;
use OCA\Polls\Model\Acl;
use OCA\Polls\Service\PollService;
Expand All @@ -49,7 +50,7 @@ public function __construct(
*/
public function list(): JSONResponse {
try {
return new JSONResponse(['polls' => $this->pollService->list()], Http::STATUS_OK);
return new JSONResponse([AppConstants::APP_ID => $this->pollService->list()], Http::STATUS_OK);
} catch (DoesNotExistException $e) {
return new JSONResponse([], Http::STATUS_NOT_FOUND);
} catch (Exception $e) {
Expand Down
5 changes: 3 additions & 2 deletions lib/Controller/PublicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Polls\Controller;

use OCA\Polls\AppConstants;
use OCA\Polls\Model\Acl;
use OCA\Polls\Service\CommentService;
use OCA\Polls\Service\MailService;
Expand Down Expand Up @@ -70,9 +71,9 @@ public function __construct(
*/
public function votePage(string $token) {
if ($this->userSession->isLoggedIn()) {
return new TemplateResponse('polls', 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
return new TemplateResponse(AppConstants::APP_ID, 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
} else {
$template = new PublicTemplateResponse('polls', 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
$template = new PublicTemplateResponse(AppConstants::APP_ID, 'polls.tmpl', ['urlGenerator' => $this->urlGenerator]);
$template->setFooterVisible(false);
return $template;
}
Expand Down
7 changes: 4 additions & 3 deletions lib/Dashboard/PollWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

namespace OCA\Polls\Dashboard;

use OCA\Polls\AppConstants;
use OCP\Dashboard\IWidget;
use OCP\IL10N;
use OCP\IURLGenerator;
Expand All @@ -35,7 +36,7 @@ public function __construct(
}

public function getId(): string {
return 'polls';
return AppConstants::APP_ID;
}

public function getTitle(): string {
Expand All @@ -51,10 +52,10 @@ public function getIconClass(): string {
}

public function getUrl(): ?string {
return $this->urlGenerator->linkToRouteAbsolute('polls.page.index');
return $this->urlGenerator->linkToRouteAbsolute(AppConstants::APP_ID . '.page.index');
}

public function load(): void {
\OCP\Util::addScript('polls', 'polls-dashboard');
\OCP\Util::addScript(AppConstants::APP_ID, 'polls-dashboard');
}
}
12 changes: 6 additions & 6 deletions lib/Db/OptionMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,17 @@ public function findDateBoundaries(int $pollId): array {

$results = $qb->executeQuery()->fetchAll(\PDO::FETCH_COLUMN);

try {
return [
'min' => min($results),
'max' => max($results),
];
} catch (\Throwable $th) {
if (empty($results)) {
return [
'min' => 0,
'max' => 0,
];
}

return [
'min' => min($results),
'max' => max($results),
];
}

/**
Expand Down
3 changes: 2 additions & 1 deletion lib/Db/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
namespace OCA\Polls\Db;

use JsonSerializable;
use OCA\Polls\AppConstants;
use OCA\Polls\Exceptions\NoDeadLineException;
use OCA\Polls\Helper\Container;
use OCP\IURLGenerator;
Expand Down Expand Up @@ -216,7 +217,7 @@ public function getUri(): string {

public function getVoteUrl() : string {
return $this->urlGenerator->linkToRouteAbsolute(
'polls.page.vote',
AppConstants::APP_ID . '.page.vote',
['id' => $this->getId()]
);
}
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/Preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function __construct() {
$this->setPreferences(json_encode(self::DEFAULT));
}

public function getPreferences_decoded() {
public function getPreferences_decoded(): mixed {
return json_decode($this->getPreferences());
}

Expand Down Expand Up @@ -105,7 +105,7 @@ public function getCheckCalendarsAfter(): int {
*/
public function jsonSerialize(): array {
return [
'preferences' => json_decode($this->preferences) ?? '',
'preferences' => json_decode($this->preferences ?? ''),
];
}
}
Loading

0 comments on commit 7fb5102

Please sign in to comment.