wip: continue sync to remote

This commit is contained in:
2022-05-26 00:10:25 +02:00
parent 782436ee2e
commit 59a64e9a62
6 changed files with 184 additions and 57 deletions

View File

@@ -27,6 +27,7 @@ use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
{
@@ -40,6 +41,8 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
private RemoteEventConverter $remoteEventConverter;
private TranslatorInterface $translator;
private OnBehalfOfUserTokenStorage $tokenStorage;
private UrlGeneratorInterface $urlGenerator;
@@ -54,6 +57,7 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
OnBehalfOfUserTokenStorage $tokenStorage,
OnBehalfOfUserHttpClient $userHttpClient,
RemoteEventConverter $remoteEventConverter,
TranslatorInterface $translator,
UrlGeneratorInterface $urlGenerator
) {
$this->calendarRangeRepository = $calendarRangeRepository;
@@ -62,6 +66,7 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
$this->logger = $logger;
$this->remoteEventConverter = $remoteEventConverter;
$this->tokenStorage = $tokenStorage;
$this->translator = $translator;
$this->urlGenerator = $urlGenerator;
$this->userHttpClient = $userHttpClient;
}
@@ -162,9 +167,16 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
} else {
if (null !== $previousMainUser) {
// cancel event in previousMainUserCalendar
// CREATE event in currentMainUser
$this->cancelOnRemote(
$calendar->getRemoteId(),
$this->translator->trans('remote_ms_graph.cancel_event_because_main_user_is_%label%', ['%label%' => $calendar->getMainUser()]),
$previousMainUser,
'calendar_'.$calendar->getRemoteId()
);
$this->createCalendarOnRemote($calendar);
} else {
$this->patchCalendarOnRemote($calendar, $newInvites);
}
// PATCH event in currentMainUer
}
if ($calendar->hasCalendarRange() && $calendar->getCalendarRange()->hasRemoteId()) {
@@ -172,10 +184,19 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
$calendar->getCalendarRange()->getRemoteId(),
$calendar->getCalendarRange()->getUser()
);
$calendar->getCalendarRange()
->addRemoteAttributes([
'lastModifiedDateTime' => null,
'changeKey' => null,
'previousId' => $calendar->getCalendarRange()->getRemoteId()
])
->setRemoteId('')
;
}
if (null !== $previousCalendarRange) {
// create previousCalendarRange
$this->createRemoteCalendarRange($previousCalendarRange);
}
}
@@ -188,6 +209,34 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
}
}
private function patchCalendarOnRemote(Calendar $calendar, array $newInvites): void
{
$eventDatas = [];
$eventDatas[] = $this->remoteEventConverter->calendarToEvent($calendar);
if (0 < count($newInvites)) {
$eventDatas[] = $this->remoteEventConverter->calendarToEventAttendeesOnly($calendar);
}
foreach ($eventDatas as $eventData) {
[
'id' => $id,
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey
] = $this->patchOnRemote(
$calendar->getRemoteId(),
$eventData,
$calendar->getMainUser(),
'calendar_'.$calendar->getId()
);
$calendar->addRemoteAttributes([
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey,
]);
}
}
private function createCalendarOnRemote(Calendar $calendar): void
{
$eventData = $this->remoteEventConverter->calendarToEvent($calendar);
@@ -227,7 +276,6 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
return [];
}
dump($eventData);
try {
$event = $this->machineHttpClient->request(
@@ -257,9 +305,8 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
private function createRemoteCalendarRange(CalendarRange $calendarRange): void
{
$userId = $this->mapCalendarToUser->getUserId($calendarRange->getUser());
$calendarId = $this->mapCalendarToUser->getCalendarId($calendarRange->getUser());
if (null === $userId || null === $calendarId) {
if (null === $userId) {
$this->logger->warning('user does not have userId nor calendarId', [
'user_id' => $calendarRange->getUser()->getId(),
'calendar_range_id' => $calendarRange->getId(),
@@ -270,27 +317,20 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
$eventData = $this->remoteEventConverter->calendarRangeToEvent($calendarRange);
try {
$event = $this->machineHttpClient->request(
'POST',
'users/' . $userId . '/calendar/events',
[
'json' => $eventData,
]
)->toArray();
} catch (ClientExceptionInterface $e) {
$this->logger->warning('could not save calendar range to remote', [
'exception' => $e->getTraceAsString(),
'calendarRangeId' => $calendarRange->getId(),
]);
[
'id' => $id,
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey
] = $this->createOnRemote(
$eventData,
$calendarRange->getUser(),
'calendar_range_' . $calendarRange->getId()
);
throw $e;
}
$calendarRange->setRemoteId($event['id'])
$calendarRange->setRemoteId($id)
->addRemoteAttributes([
'lastModifiedDateTime' => $this->remoteEventConverter->getLastModifiedDate($event)->getTimestamp(),
'changeKey' => $event['changeKey'],
'lastModifiedDateTime' => $lastModified,
'changeKey' => $changeKey
]);
}
@@ -364,6 +404,35 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
);
}
private function cancelOnRemote(string $remoteId, string $comment, User $user, string $identifier): void
{
$userId = $this->mapCalendarToUser->getUserId($user);
if (null === $userId) {
return;
}
try {
$this->machineHttpClient->request(
'POST',
"users/{$userId}/calendar/events/{$remoteId}/cancel",
[
'json' => ['Comment' => $comment]
]
);
} catch (ClientExceptionInterface $e) {
$this->logger->warning('could not update calendar range to remote', [
'exception' => $e->getTraceAsString(),
'content' => $e->getResponse()->getContent(),
'calendarRangeId' => $identifier,
]);
throw $e;
}
}
/**
* @param string $identifier an identifier for logging in case of something does not work
*