|
47 | 47 |
|
48 | 48 | class CalendarListener implements IEventListener {
|
49 | 49 |
|
50 |
| - public function __construct( |
| 50 | + public function __construct( |
51 | 51 | private LoggerInterface $logger,
|
52 | 52 | private SubscriptionService $subscriptionService,
|
53 | 53 | private TransportManager $transportManager,
|
54 | 54 | private ErrorHandlingHelper $errorHandlingHelper,
|
55 | 55 | private $userId,
|
56 | 56 | ) {}
|
57 | 57 |
|
58 |
| - public function handle(Event $event): void { |
59 |
| - if (($event instanceOf CalendarObjectCreatedEvent) || ($event instanceOf CalendarObjectDeletedEvent) || |
60 |
| - ($event instanceOf CalendarObjectUpdatedEvent) || ($event instanceOf CalendarObjectMovedToTrashEvent) || |
| 58 | + public function handle(Event $event): void { |
| 59 | + if (($event instanceOf CalendarObjectCreatedEvent) || ($event instanceOf CalendarObjectDeletedEvent) || |
| 60 | + ($event instanceOf CalendarObjectUpdatedEvent) || ($event instanceOf CalendarObjectMovedToTrashEvent) || |
61 | 61 | ($event instanceOf CalendarObjectRestoredEvent)) {
|
62 |
| - $this->notifyAllForCalendar($event->getCalendarData()); |
63 |
| - } |
| 62 | + $this->notifyAllSubscriptionsToResource("calendar", $event->getCalendarData()['id'], $event->getCalendarData()['{http://sabredav.org/ns}sync-token']); |
| 63 | + } |
64 | 64 |
|
65 | 65 | if($event instanceOf CalendarObjectMovedEvent) {
|
66 |
| - $this->notifyAllForCalendar($event->getSourceCalendarData()); |
67 |
| - $this->notifyAllForCalendar($event->getTargetCalendarData()); |
| 66 | + $this->notifyAllSubscriptionsToResource("calendar", $event->getSourceCalendarData()['id'], $event->getSourceCalendarData()['{http://sabredav.org/ns}sync-token']); |
| 67 | + $this->notifyAllSubscriptionsToResource("calendar", $event->getTargetCalendarData()['id'], $event->getTargetCalendarData()['{http://sabredav.org/ns}sync-token']); |
68 | 68 | }
|
69 |
| - } |
70 |
| - private function notifyAllForCalendar(array $calendarData): void { |
71 |
| - $collectionName = $calendarData['uri']; |
72 |
| - $syncToken = $calendarData['{http://sabredav.org/ns}sync-token']; |
73 |
| - |
74 |
| - $subscriptions = $this->subscriptionService->findAll($collectionName); |
| 69 | + } |
75 | 70 |
|
76 |
| - $this->errorHandlingHelper->convertErrorsToExceptions(function () use ($subscriptions, $collectionName, $syncToken) { |
| 71 | + private function notifyAllSubscriptionsToResource(string $resourceType, int $resourceId, string $syncToken): void { |
| 72 | + $subscriptions = $this->subscriptionService->findAll($resourceType, $resourceId); |
| 73 | + |
| 74 | + $this->errorHandlingHelper->convertErrorsToExceptions(function () use ($subscriptions, $resourceType, $resourceId, $syncToken) { |
77 | 75 | foreach($subscriptions as $subscription) {
|
| 76 | + // TODO: The subscription was able to be registered and has not expired yet, that means the user had access to the resource as of recently, |
| 77 | + // but we need to check if that is actually still the case here |
| 78 | + |
78 | 79 | $transport = $this->transportManager->getTransport($subscription->getTransport());
|
79 | 80 |
|
80 | 81 | try {
|
81 |
| - $transport->notify($subscription->getId(), $subscription->getUserId(), $collectionName, $syncToken); |
| 82 | + $transport->notify($subscription->getId(), $subscription->getUserId(), $resourceType, $resourceId, $syncToken); |
82 | 83 | $this->subscriptionService->update($subscription->getUserId(), $subscription->getId(), failCounter: 0);
|
83 | 84 | } catch (\Throwable $e) {
|
84 | 85 | $this->logger->error("transport " . $subscription->getTransport() . " failed to deliver notification to subscription " . $subscription->getId() . ". error message: " . $e->getMessage());
|
85 | 86 | $this->subscriptionService->update($subscription->getUserId(), $subscription->getId(), failCounter: $subscription->getFailCounter() + 1);
|
86 | 87 | }
|
87 | 88 | }
|
88 |
| - }); |
| 89 | + }); |
89 | 90 | }
|
90 | 91 | }
|
0 commit comments