diff --git a/src/Biopen/GeoDirectoryBundle/EventListener/WebhookListener.php b/src/Biopen/GeoDirectoryBundle/EventListener/WebhookListener.php new file mode 100644 index 0000000000000000000000000000000000000000..46fc919d59135d755252daa22e16dc70c5e72b3f --- /dev/null +++ b/src/Biopen/GeoDirectoryBundle/EventListener/WebhookListener.php @@ -0,0 +1,70 @@ +<?php + +namespace Biopen\GeoDirectoryBundle\EventListener; + +use Biopen\GeoDirectoryBundle\Document\Element; +use Biopen\GeoDirectoryBundle\Document\ElementStatus; +use Biopen\GeoDirectoryBundle\Services\WebhookService; +use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; + +class WebhookListener +{ + protected $webhookService; + + public function __construct(WebhookService $webhookService) + { + $this->webhookService = $webhookService; + } + + public function postPersist(LifecycleEventArgs $args) + { + $document = $args->getDocument(); + + if ($document instanceof Element) + { + // Only call webhooks if the element added is immediately visible + if( $document->isVisible() ) { + $this->webhookService->queue('add', $document); + } + } + } + + public function postUpdate(LifecycleEventArgs $args) + { + $document = $args->getDocument(); + + if ($document instanceof Element) + { + $uow = $args->getDocumentManager()->getUnitOfWork(); + $changeSet = $uow->getDocumentChangeSet($document); + + // We check if the status is in the changeSet AND if there was a previous status + // (On addition, postPersist and postUpdate are both called so this avoids a duplicate) + if( isset($changeSet['status']) && isset($changeSet['status'][0]) ) { + if( $changeSet['status'][0] < ElementStatus::PendingModification && $changeSet['status'][1] >= ElementStatus::PendingModification ) { + // Call webhooks if the element was hidden and is now visible + $this->webhookService->queue('add', $document); + } else if( $changeSet['status'][0] >= ElementStatus::PendingModification && $changeSet['status'][1] < ElementStatus::PendingModification ) { + // Call webhooks if the element was visible and is now hidden + $this->webhookService->queue('delete', $document); + } + } else { + // We don't queue edit actions yet because postUpdate is called thousands of times on user addition (?) + // $this->webhookService->queue('edit', $document); + } + } + } + + public function postRemove(LifecycleEventArgs $args) + { + $document = $args->getDocument(); + + if ($document instanceof Element) + { + // Only call webhooks if the element removed was visible + if( $document->isVisible() ) { + $this->webhookService->queue('delete', $document); + } + } + } +} \ No newline at end of file diff --git a/src/Biopen/GeoDirectoryBundle/Resources/config/services.yml b/src/Biopen/GeoDirectoryBundle/Resources/config/services.yml index 72ee566a02ecad234ecbed556e987cf9e3c07533..ba9c2ce50bcfbed44f2bfa01bf5aafe4509fc594 100755 --- a/src/Biopen/GeoDirectoryBundle/Resources/config/services.yml +++ b/src/Biopen/GeoDirectoryBundle/Resources/config/services.yml @@ -11,11 +11,11 @@ services: biopen.element_action_service: class: Biopen\GeoDirectoryBundle\Services\ElementActionService - arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service", "@biopen.element_pending_service", "@biopen.webhook_service" ] + arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service", "@biopen.element_pending_service" ] biopen.element_pending_service: class: Biopen\GeoDirectoryBundle\Services\ElementPendingService - arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service", "@biopen.webhook_service" ] + arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service" ] biopen.element_form_service: class: Biopen\GeoDirectoryBundle\Services\ElementFormService @@ -41,6 +41,14 @@ services: - { name: doctrine_mongodb.odm.event_listener, event: postUpdate } - { name: doctrine_mongodb.odm.event_listener, event: postRemove } + biopen.webhook_listener: + class: Biopen\GeoDirectoryBundle\EventListener\WebhookListener + arguments: [ '@biopen.webhook_service' ] + tags: + - { name: doctrine_mongodb.odm.event_listener, event: postPersist, lazy: true } # lazy to avoid circular dependencies + - { name: doctrine_mongodb.odm.event_listener, event: postUpdate, lazy: true } # lazy to avoid circular dependencies + - { name: doctrine_mongodb.odm.event_listener, event: postRemove, lazy: true } # lazy to avoid circular dependencies + biopen.db_integrity: class: Biopen\GeoDirectoryBundle\EventListener\DatabaseIntegrityWatcher tags: diff --git a/src/Biopen/GeoDirectoryBundle/Services/ElementActionService.php b/src/Biopen/GeoDirectoryBundle/Services/ElementActionService.php index 7810cd1cead52ca73e672e294b15f3b8efeb2bda..180c42c92ef921d4492780e2b31b02fa74cf812a 100755 --- a/src/Biopen/GeoDirectoryBundle/Services/ElementActionService.php +++ b/src/Biopen/GeoDirectoryBundle/Services/ElementActionService.php @@ -42,13 +42,12 @@ class ElementActionService /** * Constructor */ - public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService, ElementPendingService $elementPendingService, WebhookService $webhookService) + public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService, ElementPendingService $elementPendingService) { $this->em = $documentManager; $this->securityContext = $securityContext; $this->mailService = $mailService; $this->elementPendingService = $elementPendingService; - $this->webhookService = $webhookService; } public function import($element, $sendMail = false, $message = null, $status = null) @@ -58,8 +57,6 @@ class ElementActionService $element->setStatus($status); if ($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message); $element->updateTimestamp(); - - // $this->webhookService->queue('add', $element); } public function add($element, $sendMail = true, $message = null) @@ -68,8 +65,6 @@ class ElementActionService $element->setStatus(ElementStatus::AddedByAdmin); if($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message); $element->updateTimestamp(); - - $this->webhookService->queue('add', $element); } public function edit($element, $sendMail = true, $message = null, $modifiedByOwner = false, $directModerationWithHash = false) @@ -87,8 +82,6 @@ class ElementActionService $element->setStatus($status); if (!$modifiedByOwner) $this->resolveReports($element, $message); $element->updateTimestamp(); - - $this->webhookService->queue('edit', $element); } public function createPending($element, $editMode, $userEmail) @@ -118,8 +111,6 @@ class ElementActionService $element->setStatus($newStatus); $this->resolveReports($element, $message); $element->updateTimestamp(); - - $this->webhookService->queue('delete', $element); } public function restore($element, $sendMail = true, $message = null) @@ -129,8 +120,6 @@ class ElementActionService $this->resolveReports($element, $message); if($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message); $element->updateTimestamp(); - - $this->webhookService->queue('add', $element); } public function resolveReports($element, $message = '', $addContribution = false) diff --git a/src/Biopen/GeoDirectoryBundle/Services/ElementPendingService.php b/src/Biopen/GeoDirectoryBundle/Services/ElementPendingService.php index c8ab833cb4d02ecefa26004633e8285b81b3c9b5..d4f80dc1f87ab4dac8e6ad22bca708e8beb1bbba 100755 --- a/src/Biopen/GeoDirectoryBundle/Services/ElementPendingService.php +++ b/src/Biopen/GeoDirectoryBundle/Services/ElementPendingService.php @@ -33,12 +33,11 @@ class ElementPendingService /** * Constructor */ - public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService, WebhookService $webhookService) + public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService) { $this->em = $documentManager; $this->securityContext = $securityContext; $this->mailService = $mailService; - $this->webhookService = $webhookService; } // When element in added or modified by non admin, we go throw this function @@ -108,8 +107,6 @@ class ElementPendingService private function acceptNewElement($element, $message) { $this->mailService->sendAutomatedMail('add', $element, $message); - - $this->webhookService->queue('add', $element); } public function refuseNewElement($element) @@ -133,8 +130,6 @@ class ElementPendingService } $this->mailService->sendAutomatedMail('edit', $element, $message); - - $this->webhookService->queue('edit', $element); } private function refuseModifiedElement($element)