Remove contact immediately on 410 response code

This commit is contained in:
Matthew Exon 2024-07-13 17:49:01 +02:00
parent 946337645d
commit 2b617fc5c2
4 changed files with 44 additions and 0 deletions

View file

@ -84,6 +84,13 @@ interface ICanHandleHttpResponses
*/ */
public function isSuccess(): bool; public function isSuccess(): bool;
/**
* Returns if the URL is permanently gone (return code 410)
*
* @return bool
*/
public function isGone(): bool;
/** /**
* @return string * @return string
*/ */

View file

@ -56,6 +56,11 @@ class CurlResult implements ICanHandleHttpResponses
*/ */
private $isSuccess; private $isSuccess;
/**
* @var boolean true (if HTTP 410 result) or false
*/
private $isGone;
/** /**
* @var string the URL which was called * @var string the URL which was called
*/ */
@ -148,6 +153,7 @@ class CurlResult implements ICanHandleHttpResponses
$this->parseBodyHeader($result); $this->parseBodyHeader($result);
$this->checkSuccess(); $this->checkSuccess();
$this->checkGone();
$this->checkRedirect(); $this->checkRedirect();
$this->checkInfo(); $this->checkInfo();
} }
@ -194,6 +200,11 @@ class CurlResult implements ICanHandleHttpResponses
} }
} }
private function checkGone()
{
$this->isGone = $this->returnCode == 410;
}
private function checkRedirect() private function checkRedirect()
{ {
if (!array_key_exists('url', $this->info)) { if (!array_key_exists('url', $this->info)) {
@ -322,6 +333,12 @@ class CurlResult implements ICanHandleHttpResponses
return $this->isSuccess; return $this->isSuccess;
} }
/** {@inheritDoc} */
public function isGone(): bool
{
return $this->isSuccess;
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getUrl(): string public function getUrl(): string
{ {

View file

@ -38,6 +38,8 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
private $isTimeout; private $isTimeout;
/** @var boolean */ /** @var boolean */
private $isSuccess; private $isSuccess;
/** @var boolean */
private $isGone;
/** /**
* @var int the error number or 0 (zero) if no error * @var int the error number or 0 (zero) if no error
*/ */
@ -63,6 +65,7 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
$this->errorNumber = $errorNumber; $this->errorNumber = $errorNumber;
$this->checkSuccess(); $this->checkSuccess();
$this->checkGone();
$this->checkRedirect($response); $this->checkRedirect($response);
} }
@ -86,6 +89,11 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
} }
} }
private function checkGone()
{
$this->isGone = $this->getStatusCode() == 410;
}
private function checkRedirect(ResponseInterface $response) private function checkRedirect(ResponseInterface $response)
{ {
$headersRedirect = $response->getHeader(RedirectMiddleware::HISTORY_HEADER) ?? []; $headersRedirect = $response->getHeader(RedirectMiddleware::HISTORY_HEADER) ?? [];
@ -135,6 +143,12 @@ class GuzzleResponse extends Response implements ICanHandleHttpResponses, Respon
return $this->isSuccess; return $this->isSuccess;
} }
/** {@inheritDoc} */
public function isGone(): bool
{
return $this->isGone;
}
/** {@inheritDoc} */ /** {@inheritDoc} */
public function getUrl(): string public function getUrl(): string
{ {

View file

@ -174,6 +174,12 @@ class OnePoll
return false; return false;
} }
if ($curlResult->isGone()) {
Logger::notice('URL is permanently gone', ['id' => $contact['id'], 'url' => $contact['poll']]);
Contact::remove($contact['id']);
return false;
}
if ($curlResult->redirectIsPermanent()) { if ($curlResult->redirectIsPermanent()) {
Logger::notice('Poll address permanently changed', [ Logger::notice('Poll address permanently changed', [
'id' => $contact['id'], 'id' => $contact['id'],