Skip to content

Commit

Permalink
Mark Emails Not Found As Processed
Browse files Browse the repository at this point in the history
  • Loading branch information
galeaspablo committed Jan 30, 2017
1 parent 80a4f14 commit d93b0c4
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 12 deletions.
44 changes: 44 additions & 0 deletions DataObject/BatchEmailResponse.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

namespace FL\GmailBundle\DataObject;

class BatchEmailResponse
{
/**
* @var \Google_Service_Gmail_Message[]
*/
private $foundApiMessages;

/**
* @var int[]
*/
private $allGmailIdsRequested;

/**
* @param \Google_Service_Gmail_Message[] $foundApiMessages
* @param int[] $allGmailIdsRequested
*/
public function __construct(
array $foundApiMessages,
array $allGmailIdsRequested
) {
$this->foundApiMessages = $foundApiMessages;
$this->allGmailIdsRequested = $allGmailIdsRequested;
}

/**
* @return \Google_Service_Gmail_Message[]
*/
public function getFoundApiMessages(): array
{
return $this->foundApiMessages;
}

/**
* @return \int[]
*/
public function getAllGmailIdsRequested(): array
{
return $this->allGmailIdsRequested;
}
}
33 changes: 30 additions & 3 deletions Services/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FL\GmailBundle\Services;

use FL\GmailBundle\DataObject\BatchEmailResponse;
use Psr\Http\Message\RequestInterface;

/**
Expand Down Expand Up @@ -87,9 +88,12 @@ public function get(string $userId, string $emailId, array $options = [])
* @param array $emailIds
* @param array $options
*
* @return \Google_Service_Gmail_Message[]|mixed[]|null
* @return BatchEmailResponse
*
* @throws \Google_Service_Gmail_Message
* @throws \Google_Service_Exception
*/
public function getBatch(string $userId, array $emailIds, array $options = [])
public function getBatch(string $userId, array $emailIds, array $options = []): BatchEmailResponse
{
$gmailBatchService = $this->googleServices->getGoogleBatchServiceGmailForUserId($userId);
$gmailBatchClient = $gmailBatchService->getClient();
Expand All @@ -105,7 +109,30 @@ public function getBatch(string $userId, array $emailIds, array $options = [])
$batchResponses = array_merge($batchResponses, $batchRequest->execute());
}

return $batchResponses;
$foundApiMessages = [];
foreach ($batchResponses as $response) {
if ($response instanceof \Google_Service_Gmail_Message) {
$foundApiMessages[] = $response;
continue;
}
if (
$response instanceof \Google_Service_Exception &&
$response->getCode() === 404
) {
continue;
}
if ($response instanceof \Google_Service_Exception) {
throw $response;
}
throw new \RuntimeException(sprintf(
'Expected response to be of class %s or %s, but instead got %s',
\Google_Service_Gmail_Message::class,
\Google_Service_Exception::class,
get_class($response)
));
}

return new BatchEmailResponse($foundApiMessages, $emailIds);
}

/**
Expand Down
15 changes: 6 additions & 9 deletions Services/SyncMessages.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,13 @@ public function syncFromGmailIds(GmailIdsInterface $gmailIds)
}
}

$apiMessages = $this->email->getBatch($userId, $gmailIdsNotInCache) ?? [];
$processedGmailIds = [];
foreach ($apiMessages as $apiMessage) {
if ($apiMessage instanceof \Google_Service_Gmail_Message) {
$this->processApiMessage($userId, $this->oAuth->resolveDomain(), $apiMessage);
$processedGmailIds[] = $apiMessage->getId();
}
// Todo Messages That Weren't Found should be marked as processed
// e.g. Messages that were deleted in gmail, but not in our application
$batchResponse = $this->email->getBatch($userId, $gmailIdsNotInCache) ?? [];
foreach ($batchResponse->getFoundApiMessages() as $apiMessage) {
$this->processApiMessage($userId, $this->oAuth->resolveDomain(), $apiMessage);
}
// Mark all emails as processed, including those that weren't found.
// E.g. gmailIds that have now been deleted in gmail.
$processedGmailIds = $batchResponse->getAllGmailIdsRequested();

$syncEvent = new GmailSyncMessagesEvent($this->gmailMessageCache[$userId], $this->gmailLabelCache[$userId], $processedGmailIds, $userId);
$this->dispatcher->dispatch(GmailSyncMessagesEvent::EVENT_NAME, $syncEvent);
Expand Down

0 comments on commit d93b0c4

Please sign in to comment.