From 3c5a7f4a2abb7dfcf4592d488c91190d9b39537b Mon Sep 17 00:00:00 2001 From: Antoon Prins Date: Tue, 14 May 2024 17:12:37 +0200 Subject: [PATCH] Cleanup open invitations after specified expiration time. --- lib/Controller/OcmController.php | 2 +- lib/Federation/InvitationMapper.php | 22 ++++++++++++++++++++++ lib/Job/CleanupInvitations.php | 2 ++ lib/Service/InvitationService.php | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 1 deletion(-) diff --git a/lib/Controller/OcmController.php b/lib/Controller/OcmController.php index 60cefc8..44dcd05 100644 --- a/lib/Controller/OcmController.php +++ b/lib/Controller/OcmController.php @@ -51,7 +51,7 @@ public function __construct($appName, IRequest $request, InvitationService $invi * @param string $name the recipient name * @return DataResponse */ - // FIXME: make sure we follow the OCM protocol regarding response codes + // FIXME: verify we follow the OCM protocol regarding response codes public function inviteAccepted( string $recipientProvider = '', string $token = '', diff --git a/lib/Federation/InvitationMapper.php b/lib/Federation/InvitationMapper.php index 88c17a7..10c25e0 100644 --- a/lib/Federation/InvitationMapper.php +++ b/lib/Federation/InvitationMapper.php @@ -181,6 +181,28 @@ public function deleteForStatus(array $statuses): void } } + /** + * Deletes all expired open invitations. + * + * @param int $periodSeconds the number of seconds after which open invitations will be considered expired. + */ + public function deleteExpiredOpenInvitation(int $expirationPeriod) + { + try { + $time = \time() - $expirationPeriod; + $qb = $this->db->getQueryBuilder(); + $qb->delete(Schema::TABLE_INVITATIONS) + ->where($qb->expr()->in(Schema::INVITATION_STATUS, $qb->createParameter(Schema::INVITATION_STATUS))) + ->andWhere($qb->expr()->lt(Schema::INVITATION_TIMESTAMP, $qb->createParameter('time'))); + $qb->setParameter(Schema::INVITATION_STATUS, [Invitation::STATUS_OPEN], IQueryBuilder::PARAM_STR_ARRAY); + $qb->setParameter('time', $time, IQueryBuilder::PARAM_INT); + $qb->execute(); + } catch (Exception $e) { + $this->logger->error($e->getMessage() . ' Trace: ' . $e->getTraceAsString(), ['app' => InvitationApp::APP_NAME]); + throw new Exception('An error occurred trying to delete open invitations.'); + } + } + /** * Builds and returns a new VInvitation object from specified associative array. * diff --git a/lib/Job/CleanupInvitations.php b/lib/Job/CleanupInvitations.php index 1137e04..aa6ce7a 100644 --- a/lib/Job/CleanupInvitations.php +++ b/lib/Job/CleanupInvitations.php @@ -28,6 +28,8 @@ protected function run($argument) { try { $this->invitationService->deleteForStatus([Invitation::STATUS_DECLINED, Invitation::STATUS_INVALID, Invitation::STATUS_REVOKED]); + // 2592000 seconds is 30 days + $this->invitationService->deleteExpiredOpenInvitation(2592000); } catch (ServiceException $e) { $this->logger->error($e->getMessage(), ['app' => InvitationApp::APP_NAME]); } diff --git a/lib/Service/InvitationService.php b/lib/Service/InvitationService.php index dba5ee9..55fb93a 100644 --- a/lib/Service/InvitationService.php +++ b/lib/Service/InvitationService.php @@ -154,4 +154,18 @@ public function deleteForStatus(array $statuses): void throw new ServiceException($e->getMessage()); } } + + /** + * Deletes all expired open invitations. + * + * @param int $expirationPeriod the number of seconds after which open invitations will be considered expired. + */ + public function deleteExpiredOpenInvitation(int $expirationPeriod) + { + try { + $this->mapper->deleteExpiredOpenInvitation($expirationPeriod); + } catch (Exception $e) { + throw new ServiceException($e->getMessage()); + } + } }