-
Notifications
You must be signed in to change notification settings - Fork 506
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
58 changed files
with
3,037 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?php defined('SYSPATH') OR die('No direct access allowed.'); | ||
|
||
/** | ||
* Ushahidi API webhooks Controller | ||
* | ||
* @author Ushahidi Team <team@ushahidi.com> | ||
* @package Ushahidi\Application\Controllers | ||
* @copyright 2013 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
class Controller_Api_Webhooks extends Ushahidi_Rest { | ||
|
||
protected function _scope() | ||
{ | ||
return 'webhooks'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php defined('SYSPATH') or die('No direct script access'); | ||
|
||
/** | ||
* Ushahidi Webhook Console Command | ||
* | ||
* @author Ushahidi Team <team@ushahidi.com> | ||
* @package Ushahidi\Console | ||
* @copyright 2014 Ushahidi | ||
* @license https://www.gnu.org/licenses/agpl-3.0.html GNU Affero General Public License Version 3 (AGPL3) | ||
*/ | ||
|
||
use Ushahidi\Console\Command; | ||
|
||
use Ushahidi\Core\Tool\Signer; | ||
use Ushahidi\Core\Entity\PostRepository; | ||
use Ushahidi\Core\Entity\WebhookJobRepository; | ||
use Ushahidi\Core\Entity\WebhookRepository; | ||
|
||
use Symfony\Component\Console\Input\InputArgument; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
use GuzzleHttp\Client; | ||
use GuzzleHttp\Psr7\Request; | ||
|
||
class Ushahidi_Console_Webhook extends Command | ||
{ | ||
private $db; | ||
private $postRepository; | ||
private $webhookRepository; | ||
private $webhookJobRepository; | ||
private $signer; | ||
private $client; | ||
|
||
public function setDatabase(Database $db) | ||
{ | ||
$this->db = $db; | ||
} | ||
|
||
public function setWebhookRepo(WebhookRepository $repo) | ||
{ | ||
$this->webhookRepository = $repo; | ||
} | ||
|
||
public function setPostRepo(PostRepository $repo) | ||
{ | ||
$this->postRepository = $repo; | ||
} | ||
|
||
public function setSigner(Signer $signer) | ||
{ | ||
$this->signer = $signer; | ||
} | ||
|
||
public function setWebhookJobRepo(WebhookJobRepository $repo) | ||
{ | ||
$this->webhookJobRepository = $repo; | ||
} | ||
|
||
protected function configure() | ||
{ | ||
$this | ||
->setName('webhook') | ||
->setDescription('Manage webhook requests') | ||
->addArgument('action', InputArgument::OPTIONAL, 'list, send', 'list') | ||
->addOption('limit', ['l'], InputOption::VALUE_OPTIONAL, 'number of webhook requests to be sent') | ||
; | ||
} | ||
|
||
protected function executeList(InputInterface $input, OutputInterface $output) | ||
{ | ||
return [ | ||
[ | ||
'Available actions' => 'send' | ||
] | ||
]; | ||
} | ||
|
||
protected function executeSend(InputInterface $input, OutputInterface $output) | ||
{ | ||
|
||
$this->client = new GuzzleHttp\Client(); | ||
|
||
$limit = $input->getOption('limit'); | ||
|
||
$count = 0; | ||
|
||
// Get Queued webhook requests | ||
$webhook_requests = $this->webhookJobRepository->getJobs($limit); | ||
|
||
// Start transaction | ||
$this->db->begin(); | ||
|
||
foreach ($webhook_requests as $webhook_request) { | ||
$this->generateRequest($webhook_request); | ||
|
||
$count++; | ||
} | ||
|
||
// Finally commit changes | ||
$this->db->commit(); | ||
|
||
return [ | ||
[ | ||
'Message' => sprintf('%d webhook requests sent', $count) | ||
] | ||
]; | ||
} | ||
|
||
private function generateRequest($webhook_request) | ||
{ | ||
// Delete queued webhook request | ||
//$this->webhookJobRepository->delete($webhook_request); | ||
|
||
// Get post data | ||
$post = $this->postRepository->get($webhook_request->post_id); | ||
$json = json_encode($post->asArray()); | ||
|
||
// Get webhook data | ||
$webhook = $this->webhookRepository->getByEventType($webhook_request->event_type); | ||
|
||
$this->signer = new Signer($webhook->shared_secret); | ||
|
||
$signature = $this->signer->sign($webhook->url, $json); | ||
|
||
// This is an asynchronous request, we don't expect a result | ||
// this can be extended to allow for handling of the returned promise | ||
$promise = $this->client->requestAsync('POST', $webhook->url, [ | ||
'headers' => [ | ||
'X-Platform-Signature' => $signature, | ||
'Accept' => 'application/json' | ||
], | ||
'json' => $post->asArray() | ||
]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.