Commit 842b888c authored by Sebastien Rosset's avatar Sebastien Rosset

Call WebhookService when elemented are added/edited/removed

parent 2648faf4
......@@ -14,7 +14,7 @@ abstract class WebhookFormat
abstract class WebhookAction
{
const Add = 'add';
const Update = 'update';
const Edit = 'edit';
const Delete = 'delete';
}
......
......@@ -15,7 +15,7 @@ services:
biopen.element_pending_service:
class: Biopen\GeoDirectoryBundle\Services\ElementPendingService
arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service" ]
arguments: [ "@doctrine.odm.mongoDB.document_manager", "@security.context", "@biopen.mail_service", "@biopen.webhook_service" ]
biopen.element_form_service:
class: Biopen\GeoDirectoryBundle\Services\ElementFormService
......
......@@ -58,6 +58,8 @@ class ElementActionService
$element->setStatus($status);
if($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message);
$element->updateTimestamp();
$this->webhookService->queue('add', $element, $this->securityContext->getToken()->getUser());
}
public function add($element, $sendMail = true, $message = null)
......@@ -66,6 +68,8 @@ class ElementActionService
$element->setStatus(ElementStatus::AddedByAdmin);
if($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message);
$element->updateTimestamp();
$this->webhookService->queue('add', $element, $this->securityContext->getToken()->getUser());
}
public function edit($element, $sendMail = true, $message = null, $modifiedByOwner = false, $directModerationWithHash = false)
......@@ -83,6 +87,8 @@ class ElementActionService
$element->setStatus($status);
if (!$modifiedByOwner) $this->resolveReports($element, $message);
$element->updateTimestamp();
$this->webhookService->queue('edit', $element, $this->securityContext->getToken()->getUser());
}
public function createPending($element, $editMode, $userEmail)
......@@ -111,6 +117,8 @@ class ElementActionService
$this->resolveReports($element, $message);
if($sendMail) $this->mailService->sendAutomatedMail('delete', $element, $message);
$element->updateTimestamp();
$this->webhookService->queue('delete', $element, $this->securityContext->getToken()->getUser());
}
public function restore($element, $sendMail = true, $message = null)
......@@ -120,6 +128,8 @@ class ElementActionService
$this->resolveReports($element, $message);
if($sendMail) $this->mailService->sendAutomatedMail('add', $element, $message);
$element->updateTimestamp();
$this->webhookService->queue('add', $element, $this->securityContext->getToken()->getUser());
}
public function resolveReports($element, $message = '', $addContribution = false)
......
......@@ -33,11 +33,12 @@ class ElementPendingService
/**
* Constructor
*/
public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService)
public function __construct(DocumentManager $documentManager, SecurityContext $securityContext, MailService $mailService, WebhookService $webhookService)
{
$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
......@@ -106,7 +107,9 @@ class ElementPendingService
private function acceptNewElement($element, $message)
{
$this->mailService->sendAutomatedMail('add', $element, $message);
$this->mailService->sendAutomatedMail('add', $element, $message);
$this->webhookService->queue('add', $element, $this->securityContext->getToken()->getUser());
}
public function refuseNewElement($element)
......@@ -129,7 +132,9 @@ class ElementPendingService
$element->setModifiedElement(null);
}
$this->mailService->sendAutomatedMail('edit', $element, $message);
$this->mailService->sendAutomatedMail('edit', $element, $message);
$this->webhookService->queue('edit', $element, $this->securityContext->getToken()->getUser());
}
private function refuseModifiedElement($element)
......
......@@ -14,6 +14,7 @@ use Doctrine\ODM\MongoDB\DocumentManager;
use GuzzleHttp\Client;
use GuzzleHttp\Pool;
use GuzzleHttp\Psr7\Response;
use http\Exception\InvalidArgumentException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Router;
......@@ -103,11 +104,13 @@ class WebhookService
{
switch($data['action']) {
case WebhookAction::Add:
return "**AJOUT** Acteur \"{$data['data']['name']}\" ajoutée par {$data['user']}\n{$data['link']}";
case WebhookAction::Update:
return "**AJOUT** Acteur \"{$data['data']['name']}\" ajouté par {$data['user']}\n{$data['link']}";
case WebhookAction::Edit:
return "**MODIFICATION** Acteur \"{$data['data']['name']}\ mise à jour par {$data['user']}\n{$data['link']}";
case WebhookAction::Delete:
return "**SUPPRESSION** Acteur \"{$data['data']['name']}\" supprimée par {$data['user']}";
return "**SUPPRESSION** Acteur \"{$data['data']['name']}\" supprimé par {$data['user']}";
default:
throw new InvalidArgumentException(sprintf('The webhook action "%s" is invalid.', $data['action']));
}
}
......@@ -139,6 +142,9 @@ class WebhookService
case WebhookFormat::Slack:
return ["text" => $data['text']];
default:
throw new InvalidArgumentException(sprintf('The webhook format "%s" is invalid.', $format));
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment