*/ class LiveUpdateListener implements IEventListener { private LoggerInterface $logger; private SessionService $sessionService; private IRequest $request; private StackMapper $stackMapper; private $queue; public function __construct( ContainerInterface $container, IRequest $request, LoggerInterface $logger, SessionService $sessionService, StackMapper $stackMapper, ) { try { $this->queue = $container->get(IQueue::class); } catch (\Exception $e) { // most likely notify_push is not installed. return; } $this->logger = $logger; $this->sessionService = $sessionService; $this->request = $request; $this->stackMapper = $stackMapper; } public function handle(Event $event): void { if (!$this->queue) { // notify_push is not active return; } try { // the web frontend is adding the Session-ID as a header // TODO: verify the token! this currently allows to spoof a token from someone // else, preventing this person from getting updates $causingSessionToken = $this->request->getHeader('x-nc-deck-session'); if ( $event instanceof SessionCreatedEvent || $event instanceof SessionClosedEvent || $event instanceof BoardUpdatedEvent || $event instanceof AAclEvent ) { $this->sessionService->notifyAllSessions($this->queue, $event->getBoardId(), NotifyPushEvents::DeckBoardUpdate, [ 'id' => $event->getBoardId() ], $causingSessionToken); } elseif ($event instanceof ACardEvent) { $boardId = $this->stackMapper->findBoardId($event->getCard()->getStackId()); $this->sessionService->notifyAllSessions($this->queue, $boardId, NotifyPushEvents::DeckCardUpdate, [ 'boardId' => $boardId, 'cardId' => $event->getCard()->getId() ], $causingSessionToken); // if card got moved to a diferent board, we should notify // also sessions active on the previous board if ($event instanceof CardUpdatedEvent && $event->getCardBefore()) { $previousBoardId = $this->stackMapper->findBoardId($event->getCardBefore()->getStackId()); if ($boardId !== $previousBoardId) { $this->sessionService->notifyAllSessions($this->queue, $previousBoardId, NotifyPushEvents::DeckCardUpdate, [ 'boardId' => $boardId, 'cardId' => $event->getCard()->getId() ], $causingSessionToken); } } } } catch (\Exception $e) { $this->logger->error('Error when handling live update event', ['exception' => $e]); } } }