Skip to content

Commit

Permalink
Merge pull request #3115 from nextcloud/refactor/events
Browse files Browse the repository at this point in the history
Refactor/events
  • Loading branch information
dartcafe authored Oct 17, 2023
2 parents 7dd6e86 + 5129953 commit a0dcae6
Show file tree
Hide file tree
Showing 37 changed files with 399 additions and 49 deletions.
6 changes: 5 additions & 1 deletion appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@
['name' => 'poll#add', 'url' => '/poll/add', 'verb' => 'POST'],
['name' => 'poll#update', 'url' => '/poll/{pollId}', 'verb' => 'PUT'],
['name' => 'poll#delete', 'url' => '/poll/{pollId}', 'verb' => 'DELETE'],


['name' => 'poll#close', 'url' => '/poll/{pollId}/close', 'verb' => 'PUT'],
['name' => 'poll#reopen', 'url' => '/poll/{pollId}/reopen', 'verb' => 'PUT'],
['name' => 'poll#toggleArchive', 'url' => '/poll/{pollId}/toggleArchive', 'verb' => 'PUT'],
['name' => 'poll#clone', 'url' => '/poll/{pollId}/clone', 'verb' => 'POST'],
['name' => 'poll#getParticipantsEmailAddresses', 'url' => '/poll/{pollId}/addresses', 'verb' => 'GET'],
Expand Down Expand Up @@ -136,6 +138,8 @@
['name' => 'poll_api#clone', 'url' => '/api/v1.0/poll/{pollId}/clone', 'verb' => 'POST'],
['name' => 'poll_api#get_participants_email_addresses', 'url' => '/api/v1.0/poll/{pollId}/addresses', 'verb' => 'GET'],
['name' => 'poll_api#enum', 'url' => '/api/v1.0/enum/poll', 'verb' => 'GET'],
['name' => 'poll_api#close', 'url' => '/api/v1.0/poll/{pollId}/close', 'verb' => 'PUT'],
['name' => 'poll_api#reopen', 'url' => '/api/v1.0/poll/{pollId}/reopen', 'verb' => 'PUT'],

['name' => 'option_api#list', 'url' => '/api/v1.0/poll/{pollId}/options', 'verb' => 'GET'],
['name' => 'option_api#add', 'url' => '/api/v1.0/poll/{pollId}/option', 'verb' => 'POST'],
Expand Down
52 changes: 51 additions & 1 deletion lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,34 @@

use OCA\Polls\AppConstants;
use OCA\Polls\Dashboard\PollWidget;
use OCA\Polls\Event\CommentAddEvent;
use OCA\Polls\Event\CommentDeleteEvent;
use OCA\Polls\Event\CommentEvent;
use OCA\Polls\Event\OptionConfirmedEvent;
use OCA\Polls\Event\OptionCreatedEvent;
use OCA\Polls\Event\OptionDeletedEvent;
use OCA\Polls\Event\OptionEvent;
use OCA\Polls\Event\PollCloseEvent;
use OCA\Polls\Event\PollEvent;
use OCA\Polls\Event\PollExpiredEvent;
use OCA\Polls\Event\PollOptionReorderedEvent;
use OCA\Polls\Event\PollOwnerChangeEvent;
use OCA\Polls\Event\PollReopenEvent;
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\ShareEvent;
use OCA\Polls\Event\ShareLockedEvent;
use OCA\Polls\Event\ShareRegistrationEvent;
use OCA\Polls\Event\ShareTypeChangedEvent;
use OCA\Polls\Event\ShareUnlockedEvent;
use OCA\Polls\Event\VoteEvent;
use OCA\Polls\Event\VoteSetEvent;
use OCA\Polls\Listener\CommentListener;
use OCA\Polls\Listener\GroupDeletedListener;
use OCA\Polls\Listener\OptionListener;
Expand Down Expand Up @@ -66,12 +89,39 @@ public function register(IRegistrationContext $context): void {

$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(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(ShareUnlockedEvent::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);
Expand Down
32 changes: 32 additions & 0 deletions lib/Controller/PollApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,38 @@ public function toggleArchive(int $pollId): JSONResponse {
}
}

/**
* Close poll
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
public function close(int $pollId): JSONResponse {
try {
return new JSONResponse(['poll' => $this->pollService->close($pollId)], Http::STATUS_OK);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}

/**
* Reopen poll
* @NoAdminRequired
* @CORS
* @NoCSRFRequired
*/
public function reopen(int $pollId): JSONResponse {
try {
return new JSONResponse(['poll' => $this->pollService->reopen($pollId)], Http::STATUS_OK);
} catch (DoesNotExistException $e) {
return new JSONResponse(['error' => 'Poll not found'], Http::STATUS_NOT_FOUND);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}

/**
* Delete poll
* @NoAdminRequired
Expand Down
26 changes: 26 additions & 0 deletions lib/Controller/PollController.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,37 @@ public function delete(int $pollId): JSONResponse {
return $this->responseDeleteTolerant(fn () => $this->pollService->delete($pollId));
}

/**
* Close poll
* @NoAdminRequired
*/
public function close(int $pollId): JSONResponse {
return $this->response(fn () => [
'poll' => $this->pollService->close($pollId),
'acl' => $this->acl->setPollId($pollId),
]);
}

/**
* Reopen poll
* @NoAdminRequired
*/
public function reopen(int $pollId): JSONResponse {
return $this->response(fn () => [
'poll' => $this->pollService->reopen($pollId),
'acl' => $this->acl->setPollId($pollId),
]);
}

/**
* Clone poll
* @NoAdminRequired
*/
public function clone(int $pollId): JSONResponse {
return $this->response(fn () => $this->clonePoll($pollId));
}

private function clonePoll(int $pollId): JSONResponse {
$poll = $this->pollService->clone($pollId);
$this->optionService->clone($pollId, $poll->getId());
return $this->get($pollId);
Expand Down
11 changes: 9 additions & 2 deletions lib/Db/Poll.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,16 @@ public function deserializeArray(array $array): self {
}

public function getExpired(): bool {
$compareTime = time();
$expiry = $this->getExpire();
// \OC::$server->getLogger()->error('time:' . $compareTime);
// \OC::$server->getLogger()->error('expire:' . $expiry);
// \OC::$server->getLogger()->error('diff:' . $expiry - $compareTime);

return (
$this->getExpire() > 0
&& $this->getExpire() < time()
$expiry > 0
&& $expiry < $compareTime
// && $this->getExpire() < ($compareTime + 1)
);
}

Expand Down
35 changes: 35 additions & 0 deletions lib/Event/PollCloseEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* @copyright Copyright (c) 2021 René Gieling <github@dartcafe.de>
*
* @author René Gieling <github@dartcafe.de>
*
* @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\Event;

use OCA\Polls\Db\Poll;

class PollCloseEvent extends PollEvent {
public function __construct(
protected Poll $poll,
) {
parent::__construct($poll);
$this->eventId = self::CLOSE;
}
}
4 changes: 3 additions & 1 deletion lib/Event/PollEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,13 @@ abstract class PollEvent extends BaseEvent {
public const DELETE = 'poll_delete';
public const RESTORE = 'poll_restore';
public const EXPIRE = 'poll_expire';
public const CLOSE = 'poll_closed';
public const REOPEN = 'poll_reopened';
public const OWNER_CHANGE = 'poll_change_owner';
public const OPTION_REORDER = 'poll_option_reorder';

public function __construct(
protected Poll $poll,
protected Poll $poll
) {
parent::__construct($poll);
$this->activityObjectType = 'poll';
Expand Down
2 changes: 1 addition & 1 deletion lib/Event/PollExpiredEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class PollExpiredEvent extends PollEvent {
public function __construct(
Poll $poll
protected Poll $poll
) {
parent::__construct($poll);
$this->eventId = self::EXPIRE;
Expand Down
2 changes: 1 addition & 1 deletion lib/Event/PollOptionReorderedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class PollOptionReorderedEvent extends PollEvent {
public function __construct(
Poll $poll
protected Poll $poll
) {
parent::__construct($poll);
$this->log = false;
Expand Down
2 changes: 1 addition & 1 deletion lib/Event/PollOwnerChangeEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class PollOwnerChangeEvent extends PollEvent {
public function __construct(
Poll $poll
protected Poll $poll
) {
parent::__construct($poll);
$this->eventId = self::OWNER_CHANGE;
Expand Down
35 changes: 35 additions & 0 deletions lib/Event/PollReopenEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
/*
* @copyright Copyright (c) 2021 René Gieling <github@dartcafe.de>
*
* @author René Gieling <github@dartcafe.de>
*
* @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\Event;

use OCA\Polls\Db\Poll;

class PollReopenEvent extends PollEvent {
public function __construct(
protected Poll $poll,
) {
parent::__construct($poll);
$this->eventId = self::REOPEN;
}
}
2 changes: 1 addition & 1 deletion lib/Event/PollRestoredEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class PollRestoredEvent extends PollEvent {
public function __construct(
Poll $poll
protected Poll $poll
) {
parent::__construct($poll);
$this->eventId = self::RESTORE;
Expand Down
2 changes: 1 addition & 1 deletion lib/Event/PollTakeoverEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

class PollTakeoverEvent extends PollOwnerChangeEvent {
public function __construct(
Poll $poll
protected Poll $poll
) {
parent::__construct($poll);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Event/PollUpdatedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

class PollUpdatedEvent extends PollEvent {
public function __construct(
Poll $poll
protected Poll $poll
) {
parent::__construct($poll);
$this->eventId = self::UPDATE;
Expand Down
4 changes: 3 additions & 1 deletion lib/Event/ShareChangedDisplayNameEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use OCA\Polls\Db\Share;

class ShareChangedDisplayNameEvent extends ShareEvent {
public function __construct(Share $share) {
public function __construct(
protected Share $share
) {
parent::__construct($share);
$this->eventId = self::CHANGE_DISPLAY_NAME;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Event/ShareChangedEmailEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use OCA\Polls\Db\Share;

class ShareChangedEmailEvent extends ShareEvent {
public function __construct(Share $share) {
public function __construct(
protected Share $share
) {
parent::__construct($share);
$this->eventId = self::CHANGE_EMAIL;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Event/ShareChangedRegistrationConstraintEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use OCA\Polls\Db\Share;

class ShareChangedRegistrationConstraintEvent extends ShareEvent {
public function __construct(Share $share) {
public function __construct(
protected Share $share
) {
parent::__construct($share);
$this->eventId = self::CHANGE_REG_CONSTR;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Event/ShareCreateEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use OCA\Polls\Db\Share;

class ShareCreateEvent extends ShareEvent {
public function __construct(Share $share) {
public function __construct(
protected Share $share
) {
parent::__construct($share);
$this->eventId = self::ADD;
}
Expand Down
4 changes: 3 additions & 1 deletion lib/Event/ShareDeletedEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@
use OCA\Polls\Db\Share;

class ShareDeletedEvent extends ShareEvent {
public function __construct(Share $share) {
public function __construct(
protected Share $share
) {
parent::__construct($share);
$this->eventId = self::DELETE;
}
Expand Down
Loading

0 comments on commit a0dcae6

Please sign in to comment.