mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-08-25 17:13:49 +00:00
count events on remote calendars
This commit is contained in:
@@ -29,10 +29,13 @@ use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use function array_key_exists;
|
||||
use function count;
|
||||
|
||||
class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
{
|
||||
private array $cacheScheduleTimeForUser = [];
|
||||
|
||||
private CalendarRangeRepository $calendarRangeRepository;
|
||||
|
||||
private LoggerInterface $logger;
|
||||
@@ -73,6 +76,43 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
$this->userHttpClient = $userHttpClient;
|
||||
}
|
||||
|
||||
public function countEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): int
|
||||
{
|
||||
$userId = $this->mapCalendarToUser->getUserId($user);
|
||||
|
||||
if (null === $userId) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
try {
|
||||
$data = $this->userHttpClient->request(
|
||||
'GET',
|
||||
'users/' . $userId . '/calendarView',
|
||||
[
|
||||
'query' => [
|
||||
'startDateTime' => $startDate->format(DateTimeImmutable::ATOM),
|
||||
'endDateTime' => $endDate->format(DateTimeImmutable::ATOM),
|
||||
'$count' => 'true',
|
||||
'$top' => 0,
|
||||
],
|
||||
]
|
||||
)->toArray();
|
||||
} catch (ClientExceptionInterface $e) {
|
||||
if (403 === $e->getResponse()->getStatusCode()) {
|
||||
return count($this->getScheduleTimesForUser($user, $startDate, $endDate));
|
||||
}
|
||||
|
||||
$this->logger->error('Could not get list of event on MSGraph', [
|
||||
'error_code' => $e->getResponse()->getStatusCode(),
|
||||
'error' => $e->getResponse()->getInfo(),
|
||||
]);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
return $data['@odata.count'];
|
||||
}
|
||||
|
||||
public function getMakeReadyResponse(string $returnPath): Response
|
||||
{
|
||||
return new RedirectResponse($this->urlGenerator
|
||||
@@ -84,7 +124,15 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
return $this->tokenStorage->hasToken();
|
||||
}
|
||||
|
||||
public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate): array
|
||||
/**
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface
|
||||
* @throws \Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface
|
||||
*
|
||||
* @return array|\Chill\CalendarBundle\RemoteCalendar\Model\RemoteEvent[]
|
||||
*/
|
||||
public function listEventsForUser(User $user, DateTimeImmutable $startDate, DateTimeImmutable $endDate, ?int $offset = 0, ?int $limit = 50): array
|
||||
{
|
||||
$userId = $this->mapCalendarToUser->getUserId($user);
|
||||
|
||||
@@ -101,6 +149,8 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
'startDateTime' => $startDate->format(DateTimeImmutable::ATOM),
|
||||
'endDateTime' => $endDate->format(DateTimeImmutable::ATOM),
|
||||
'$select' => 'id,subject,start,end,isAllDay',
|
||||
'$top' => $limit,
|
||||
'$skip' => $offset,
|
||||
],
|
||||
]
|
||||
)->toArray();
|
||||
@@ -127,7 +177,7 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
return $this->getScheduleTimesForUser($user, $startDate, $endDate);
|
||||
}
|
||||
|
||||
$this->logger->debug('Could not get list of event on MSGraph', [
|
||||
$this->logger->error('Could not get list of event on MSGraph', [
|
||||
'error_code' => $e->getResponse()->getStatusCode(),
|
||||
'error' => $e->getResponse()->getInfo(),
|
||||
]);
|
||||
@@ -475,6 +525,10 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
{
|
||||
$userId = $this->mapCalendarToUser->getUserId($user);
|
||||
|
||||
if (array_key_exists($userId, $this->cacheScheduleTimeForUser)) {
|
||||
return $this->cacheScheduleTimeForUser[$userId];
|
||||
}
|
||||
|
||||
if (null === $userId) {
|
||||
return [];
|
||||
}
|
||||
@@ -508,12 +562,14 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(
|
||||
$this->cacheScheduleTimeForUser[$userId] = array_map(
|
||||
function ($item) {
|
||||
return $this->remoteEventConverter->convertAvailabilityToRemoteEvent($item);
|
||||
},
|
||||
$response['value'][0]['scheduleItems']
|
||||
);
|
||||
|
||||
return $this->cacheScheduleTimeForUser[$userId];
|
||||
}
|
||||
|
||||
private function patchCalendarOnRemote(Calendar $calendar, array $newInvites): void
|
||||
|
Reference in New Issue
Block a user