Skip to content

Commit

Permalink
Cleanup open invitations after specified expiration time.
Browse files Browse the repository at this point in the history
  • Loading branch information
redblom committed May 14, 2024
1 parent a3e5046 commit 3c5a7f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/Controller/OcmController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '',
Expand Down
22 changes: 22 additions & 0 deletions lib/Federation/InvitationMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions lib/Job/CleanupInvitations.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
Expand Down
14 changes: 14 additions & 0 deletions lib/Service/InvitationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
}

0 comments on commit 3c5a7f4

Please sign in to comment.