Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better delete and restore #3215

Merged
merged 9 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions appinfo/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
['name' => 'public#set_vote', 'url' => '/s/{token}/vote', 'verb' => 'PUT'],
['name' => 'public#add_option', 'url' => '/s/{token}/option', 'verb' => 'POST'],
['name' => 'public#delete_option', 'url' => '/s/{token}/option/{optionId}', 'verb' => 'DELETE'],
['name' => 'public#restore_option', 'url' => '/s/{token}/option/{optionId}/restore', 'verb' => 'PUT'],

['name' => 'public#add_comment', 'url' => '/s/{token}/comment', 'verb' => 'POST'],
['name' => 'public#restore_comment', 'url' => '/s/{token}/comment/{commentId}/restore', 'verb' => 'PUT', 'postfix' => 'auth'],
['name' => 'public#delete_comment', 'url' => '/s/{token}/comment/{commentId}', 'verb' => 'DELETE', 'postfix' => 'public'],
['name' => 'public#subscribe', 'url' => '/s/{token}/subscribe', 'verb' => 'PUT'],
['name' => 'public#unsubscribe', 'url' => '/s/{token}/unsubscribe', 'verb' => 'PUT'],
Expand Down Expand Up @@ -78,6 +81,7 @@
['name' => 'option#addBulk', 'url' => '/option/bulk', 'verb' => 'POST'],
['name' => 'option#update', 'url' => '/option/{optionId}', 'verb' => 'PUT'],
['name' => 'option#delete', 'url' => '/option/{optionId}', 'verb' => 'DELETE'],
['name' => 'option#restore', 'url' => '/option/{optionId}/restore', 'verb' => 'PUT'],

['name' => 'option#shift', 'url' => '/poll/{pollId}/shift', 'verb' => 'POST'],
['name' => 'option#reorder', 'url' => '/poll/{pollId}/options/reorder', 'verb' => 'POST'],
Expand All @@ -94,6 +98,7 @@
['name' => 'share#add', 'url' => '/poll/{pollId}/share', 'verb' => 'POST'],
['name' => 'share#send_all_invitations', 'url' => '/poll/{pollId}/inviteAll', 'verb' => 'PUT'],
['name' => 'share#delete', 'url' => '/share/{token}', 'verb' => 'DELETE'],
['name' => 'share#restore', 'url' => '/share/{token}/restore', 'verb' => 'PUT'],
['name' => 'share#personal', 'url' => '/share/personal', 'verb' => 'POST'],
['name' => 'share#sendInvitation', 'url' => '/share/{token}/invite', 'verb' => 'POST'],
['name' => 'share#resolveGroup', 'url' => '/share/{token}/resolve', 'verb' => 'GET'],
Expand All @@ -114,6 +119,7 @@

['name' => 'comment#list', 'url' => '/poll/{pollId}/comments', 'verb' => 'GET'],
['name' => 'comment#add', 'url' => '/poll/{pollId}/comment', 'verb' => 'POST'],
['name' => 'comment#restore', 'url' => '/comment/{commentId}/restore', 'verb' => 'PUT', 'postfix' => 'auth'],
['name' => 'comment#delete', 'url' => '/comment/{commentId}', 'verb' => 'DELETE', 'postfix' => 'auth'],

['name' => 'system#user_search', 'url' => '/search/users/{query}', 'verb' => 'GET'],
Expand Down Expand Up @@ -146,6 +152,7 @@
['name' => 'option_api#add', 'url' => '/api/v1.0/poll/{pollId}/option', 'verb' => 'POST'],
['name' => 'option_api#update', 'url' => '/api/v1.0/option/{optionId}', 'verb' => 'PUT'],
['name' => 'option_api#delete', 'url' => '/api/v1.0/option/{optionId}', 'verb' => 'DELETE'],
['name' => 'option_api#restore', 'url' => '/api/v1.0/option/{optionId}/restore', 'verb' => 'PUT'],
['name' => 'option_api#setOrder', 'url' => '/api/v1.0/option/{optionId}/setorder/{order}', 'verb' => 'PUT'],
['name' => 'option_api#confirm', 'url' => '/api/v1.0/option/{optionId}/confirm', 'verb' => 'PUT'],

Expand All @@ -156,6 +163,7 @@
['name' => 'share_api#get', 'url' => '/api/v1.0/share/{token}', 'verb' => 'GET'],
['name' => 'share_api#add', 'url' => '/api/v1.0/poll/{pollId}/share/{type}', 'verb' => 'POST'],
['name' => 'share_api#delete', 'url' => '/api/v1.0/share/{token}', 'verb' => 'DELETE'],
['name' => 'share_api#restore', 'url' => '/api/v1.0/share/{token}/restore', 'verb' => 'PUT'],
['name' => 'share_api#sendInvitation', 'url' => '/api/v1.0/share/send/{token}', 'verb' => 'PUT'],
['name' => 'share_api#lock', 'url' => '/api/v1.0/share/lock/{token}', 'verb' => 'PUT'],
['name' => 'share_api#unlock', 'url' => '/api/v1.0/share/unlock/{token}', 'verb' => 'PUT'],
Expand All @@ -167,6 +175,7 @@
['name' => 'comment_api#list', 'url' => '/api/v1.0/poll/{pollId}/comments', 'verb' => 'GET'],
['name' => 'comment_api#add', 'url' => '/api/v1.0/poll/{pollId}/comment', 'verb' => 'POST'],
['name' => 'comment_api#delete', 'url' => '/api/v1.0/comment/{commentId}', 'verb' => 'DELETE'],
['name' => 'comment_api#restore', 'url' => '/api/v1.0/comment/{commentId}/restore', 'verb' => 'PUT'],

]
];
3 changes: 1 addition & 2 deletions lib/AppInfo/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@
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;
Expand Down Expand Up @@ -119,12 +118,12 @@ public function register(IRegistrationContext $context): void {
$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);
$context->registerDashboardWidget(PollWidget::class);
}
Expand Down
15 changes: 0 additions & 15 deletions lib/Controller/BaseApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use OCA\Polls\Exceptions\Exception;
use OCA\Polls\Exceptions\NoUpdatesException;
use OCP\AppFramework\ApiController;
use OCP\AppFramework\Db\DoesNotExistException;
use OCP\AppFramework\Http;
use OCP\AppFramework\Http\Attribute\NoAdminRequired;
use OCP\AppFramework\Http\JSONResponse;
Expand Down Expand Up @@ -81,18 +80,4 @@ protected function responseCreate(Closure $callback): JSONResponse {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}

/**
* responseDeleteTolerant
*/
#[NoAdminRequired]
protected function responseDeleteTolerant(Closure $callback): JSONResponse {
try {
return new JSONResponse($callback(), Http::STATUS_OK);
} catch (DoesNotExistException $e) {
return new JSONResponse(['message' => 'Not found, assume already deleted'], Http::STATUS_OK);
} catch (Exception $e) {
return new JSONResponse(['message' => $e->getMessage()], $e->getStatus());
}
}
}
19 changes: 15 additions & 4 deletions lib/Controller/CommentApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,24 @@ public function add(int $pollId, string $comment): JSONResponse {
#[NoAdminRequired]
#[NoCSRFRequired]
public function delete(int $commentId): JSONResponse {
return $this->responseDeleteTolerant(fn () => [
'comment' => $this->deleteComment($commentId)
$comment = $this->commentService->get($commentId);

return $this->response(fn () => [
'comment' => $this->commentService->delete($comment, $this->acl->setPollId($comment->getPollId()))
]);
}

private function deleteComment(int $commentId): Comment {
/**
* Restore comment
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function restore(int $commentId): JSONResponse {
$comment = $this->commentService->get($commentId);
return $this->commentService->delete($comment, $this->acl->setPollId($comment->getPollId()));

return $this->response(fn () => [
'comment' => $this->commentService->delete($comment, $this->acl->setPollId($comment->getPollId()), true)
]);
}
}
14 changes: 13 additions & 1 deletion lib/Controller/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,20 @@ public function add(int $pollId, string $message): JSONResponse {
public function delete(int $commentId): JSONResponse {
$comment = $this->commentService->get($commentId);

return $this->responseDeleteTolerant(fn () => [
return $this->response(fn () => [
'comment' => $this->commentService->delete($comment, $this->acl->setPollId($comment->getPollId()))
]);
}

/**
* Restore deleted Comment
*/
#[NoAdminRequired]
public function restore(int $commentId): JSONResponse {
$comment = $this->commentService->get($commentId);

return $this->response(fn () => [
'comment' => $this->commentService->delete($comment, $this->acl->setPollId($comment->getPollId()), true)
]);
}
}
12 changes: 11 additions & 1 deletion lib/Controller/OptionApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,17 @@ public function update(int $optionId, int $timestamp = 0, string $pollOptionText
#[NoAdminRequired]
#[NoCSRFRequired]
public function delete(int $optionId): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['option' => $this->optionService->delete($optionId)]);
return $this->response(fn () => ['option' => $this->optionService->delete($optionId)]);
}

/**
* Restore option
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function restore(int $optionId): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->delete($optionId, restore: true)]);
}

/**
Expand Down
10 changes: 9 additions & 1 deletion lib/Controller/OptionController.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,15 @@ public function update(int $optionId, int $timestamp, string $text, int $duratio
*/
#[NoAdminRequired]
public function delete(int $optionId): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['option' => $this->optionService->delete($optionId)]);
return $this->response(fn () => ['option' => $this->optionService->delete($optionId)]);
}

/**
* Restore option
*/
#[NoAdminRequired]
public function restore(int $optionId): JSONResponse {
return $this->response(fn () => ['option' => $this->optionService->delete($optionId, restore: true)]);
}

/**
Expand Down
26 changes: 24 additions & 2 deletions lib/Controller/PublicController.php
Original file line number Diff line number Diff line change
Expand Up @@ -171,11 +171,21 @@ public function addOption(string $token, int $timestamp = 0, string $text = '',
*/
#[PublicPage]
public function deleteOption(string $token, int $optionId): JSONResponse {
return $this->responseDeleteTolerant(fn () => [
return $this->response(fn () => [
'option' => $this->optionService->delete($optionId, $this->acl->setToken($token, Acl::PERMISSION_POLL_VIEW))
], $token);
}

/**
* Restore option
*/
#[PublicPage]
public function restoreOption(string $token, int $optionId): JSONResponse {
return $this->response(fn () => [
'option' => $this->optionService->delete($optionId, $this->acl->setToken($token, Acl::PERMISSION_POLL_VIEW), true)
], $token);
}

/**
* Set Vote
*/
Expand Down Expand Up @@ -212,11 +222,23 @@ public function addComment(string $token, string $message): JSONResponse {
#[PublicPage]
public function deleteComment(int $commentId, string $token): JSONResponse {
$comment = $this->commentService->get($commentId);
return $this->responseDeleteTolerant(fn () => [
return $this->response(fn () => [
'comment' => $this->commentService->delete($comment, $this->acl->setToken($token, Acl::PERMISSION_COMMENT_ADD))
], $token);
}

/**
* Restore deleted Comment
*/
#[PublicPage]
public function restoreComment(int $commentId, string $token): JSONResponse {
$comment = $this->commentService->get($commentId);

return $this->response(fn () => [
'comment' => $this->commentService->delete($comment, $this->acl->setToken($token, Acl::PERMISSION_COMMENT_ADD), true)
]);
}

/**
* Get subscription status
*/
Expand Down
16 changes: 13 additions & 3 deletions lib/Controller/ShareApiController.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,17 @@ public function add(int $pollId, string $type, string $userId = ''): JSONRespons
#[NoAdminRequired]
#[NoCSRFRequired]
public function delete(string $token): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['share' => $this->shareService->delete(token: $token)]);
return $this->response(fn () => ['share' => $this->shareService->delete(token: $token)]);
}

/**
* Delete share
*/
#[CORS]
#[NoAdminRequired]
#[NoCSRFRequired]
public function restore(string $token): JSONResponse {
return $this->response(fn () => ['share' => $this->shareService->delete(token: $token, restore: true)]);
}

/**
Expand All @@ -90,7 +100,7 @@ public function delete(string $token): JSONResponse {
#[NoAdminRequired]
#[NoCSRFRequired]
public function lock(string $token): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['share' => $this->shareService->lock(token: $token)]);
return $this->response(fn () => ['share' => $this->shareService->lock(token: $token)]);
}

/**
Expand All @@ -100,7 +110,7 @@ public function lock(string $token): JSONResponse {
#[NoAdminRequired]
#[NoCSRFRequired]
public function unlock(string $token): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['share' => $this->shareService->unlock(token: $token)]);
return $this->response(fn () => ['share' => $this->shareService->lock(token: $token, unlock: true)]);
}

/**
Expand Down
16 changes: 12 additions & 4 deletions lib/Controller/ShareController.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,31 @@ public function setEmailAddress(string $token, string $emailAddress = ''): JSONR
*/
#[NoAdminRequired]
public function delete(string $token): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['share' => $this->shareService->delete(token: $token)]);
return $this->response(fn () => ['share' => $this->shareService->delete(token: $token)]);
}

/**
* Delete share
*/
#[NoAdminRequired]
public function restore(string $token): JSONResponse {
return $this->response(fn () => ['share' => $this->shareService->delete(token: $token, restore: true)]);
}

/**
* Delete or restore share
*/
#[NoAdminRequired]
public function lock(string $token): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['share' => $this->shareService->lock(token: $token)]);
return $this->response(fn () => ['share' => $this->shareService->lock(token: $token)]);
}

/**
* Delete share
* Lock or unlock share
*/
#[NoAdminRequired]
public function unlock(string $token): JSONResponse {
return $this->responseDeleteTolerant(fn () => ['share' => $this->shareService->unlock(token: $token)]);
return $this->response(fn () => ['share' => $this->shareService->lock(token: $token, unlock: true)]);
}

/**
Expand Down
27 changes: 22 additions & 5 deletions lib/Cron/JanitorCron.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@

namespace OCA\Polls\Cron;

use OCA\Polls\Db\CommentMapper;
use OCA\Polls\Db\LogMapper;
use OCA\Polls\Db\OptionMapper;
use OCA\Polls\Db\PollMapper;
use OCA\Polls\Db\ShareMapper;
use OCA\Polls\Db\WatchMapper;
use OCA\Polls\Model\Settings\AppSettings;
use OCP\AppFramework\Utility\ITimeFactory;
Expand All @@ -39,7 +42,10 @@ public function __construct(
protected ITimeFactory $time,
private LogMapper $logMapper,
private PollMapper $pollMapper,
private WatchMapper $watchMapper
private WatchMapper $watchMapper,
private CommentMapper $commentMapper,
private OptionMapper $optionMapper,
private ShareMapper $shareMapper,
) {
parent::__construct($time);
parent::setInterval(86400); // run once a day
Expand All @@ -54,14 +60,25 @@ public function __construct(
* @return void
*/
protected function run($argument) {
$this->logMapper->deleteProcessedEntries(); // delete processed log entries
$this->logMapper->deleteOldEntries(time() - (86400 * 7)); // delete entries older than 7 days
$this->watchMapper->deleteOldEntries(time() - 86400); // delete entries older than 1 day
// delete processed log entries
$this->logMapper->deleteProcessedEntries();

// delete entries older than 7 days
$this->logMapper->deleteOldEntries(time() - (86400 * 7));

// delete entries older than 1 day
$this->watchMapper->deleteOldEntries(time() - 86400);

// purge entries virtually deleted more than 12 hour ago
$this->commentMapper->purgeDeletedComments(time() - 4320);
$this->optionMapper->purgeDeletedOptions(time() - 4320);
$this->shareMapper->purgeDeletedShares(time() - 4320);

// archive polls after defined days after closing date
if ($this->appSettings->getBooleanSetting(AppSettings::SETTING_AUTO_ARCHIVE) && $this->appSettings->getIntegerSetting(AppSettings::SETTING_AUTO_ARCHIVE_OFFSET) > 0) {
$this->pollMapper->archiveExpiredPolls(
time() - ($this->appSettings->getAutoarchiveOffset() * 86400)
); // archive polls after defined days after closing date
);
}
}
}
Loading