mirror of
https://github.com/alchemy-fr/Phraseanet.git
synced 2025-10-12 20:43:25 +00:00
Add webhook task
This commit is contained in:
140
lib/classes/task/period/apiwebhooks.php
Normal file
140
lib/classes/task/period/apiwebhooks.php
Normal file
@@ -0,0 +1,140 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of Phraseanet
|
||||
*
|
||||
* (c) 2005-2014 Alchemy
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
use Guzzle\Http\Client as GuzzleClient;
|
||||
|
||||
class task_period_apiwebhooks extends task_appboxAbstract
|
||||
{
|
||||
public static function getName()
|
||||
{
|
||||
return _('Api Webhook');
|
||||
}
|
||||
|
||||
public static function help()
|
||||
{
|
||||
return _('Notify Phraseanet Oauth2 client applications using webhooks.');
|
||||
}
|
||||
|
||||
protected function retrieveContent(appbox $appbox)
|
||||
{
|
||||
$stmt = $appbox->get_connection()->prepare('SELECT id, `type`, `data` FROM api_webhooks');
|
||||
$stmt->execute();
|
||||
$rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
|
||||
return $rs;
|
||||
}
|
||||
|
||||
protected function processOneContent(appbox $appbox, array $row)
|
||||
{
|
||||
$data = null;
|
||||
switch ($row['type']) {
|
||||
case \API_Webhook::NEW_FEED_ENTRY:
|
||||
$data = $this->processNewFeedEntry($row);
|
||||
}
|
||||
|
||||
if (null === $data) {
|
||||
return;
|
||||
}
|
||||
$urls = $this->getApplicationHookUrls($appbox);
|
||||
$this->sendData($urls, $data);
|
||||
}
|
||||
|
||||
protected function postProcessOneContent(appbox $appbox, array $row)
|
||||
{
|
||||
$w = new API_Webhook($appbox, $row['id']);
|
||||
$w->delete();
|
||||
}
|
||||
|
||||
protected function getApplicationHookUrls(appbox $appbox)
|
||||
{
|
||||
$stmt = $appbox->get_connection()->prepare('SELECT webhook_url FROM api_applications WHERE webhook_url IS NOT NULL');
|
||||
$stmt->execute();
|
||||
$rows = $stmt->fetchAll(\PDO::FETCH_ASSOC);
|
||||
$stmt->closeCursor();
|
||||
|
||||
return array_map(function($row) {
|
||||
return $row['webhook_url'];
|
||||
}, $rows);
|
||||
}
|
||||
|
||||
protected function sendData(array $urls, array $data)
|
||||
{
|
||||
if (count($urls) === 0) {
|
||||
return;
|
||||
}
|
||||
$client = new GuzzleClient();
|
||||
$body = json_encode($data);
|
||||
$requests = [];
|
||||
foreach ($urls as $url) {
|
||||
$requests[] = $client->createRequest('POST', $url, [
|
||||
'Content-Type' => 'application/json'
|
||||
], $body);
|
||||
}
|
||||
$client->send($requests);
|
||||
}
|
||||
|
||||
protected function processNewFeedEntry(array $row)
|
||||
{
|
||||
$data = json_decode($row['data']);
|
||||
if (!isset($data->{"feed_id"}) || !isset($data->{"entry_id"})) {
|
||||
return;
|
||||
}
|
||||
$feed = new Feed_Adapter($this->dependencyContainer, $data->{"feed_id"});
|
||||
$entry = new \Feed_Entry_Adapter($this->dependencyContainer, $feed, $data->{"entry_id"});
|
||||
|
||||
$Query = new \User_Query($this->dependencyContainer);
|
||||
|
||||
$Query->include_phantoms(true)
|
||||
->include_invite(false)
|
||||
->include_templates(false)
|
||||
->email_not_null(true);
|
||||
|
||||
if ($entry->get_feed()->get_collection()) {
|
||||
$Query->on_base_ids(array($entry->get_feed()->get_collection()->get_base_id()));
|
||||
}
|
||||
|
||||
$start = 0;
|
||||
$perLoop = 100;
|
||||
$users = array();
|
||||
|
||||
do {
|
||||
$results = $Query->limit($start, $perLoop)->execute()->get_results();
|
||||
foreach ($results as $user) {
|
||||
$users[] = array(
|
||||
'mail' => $user->get_email(),
|
||||
'firstname' => $user->get_firstname(),
|
||||
'lastname' => $user->get_lastname(),
|
||||
);
|
||||
}
|
||||
$start += $perLoop;
|
||||
} while (count($results) > 0);
|
||||
|
||||
return array(
|
||||
'name' => $row['type'],
|
||||
'data' => array(
|
||||
'feed' => array(
|
||||
'title' => $feed->get_title(),
|
||||
'description' => $feed->get_subtitle(),
|
||||
),
|
||||
'entry' => array(
|
||||
'author' => array(
|
||||
'name' => $entry->get_author_name(),
|
||||
'email' => $entry->get_author_email()
|
||||
),
|
||||
'title' => $entry->get_title(),
|
||||
'description' => $entry->get_subtitle(),
|
||||
),
|
||||
'users' => $users
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user