mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-21 00:46:27 +02:00
Extract messages processing from phone daemons and webhook daemons to place in internal controllers
This commit is contained in:
parent
63b68cbeb7
commit
2482a997e7
8 changed files with 293 additions and 203 deletions
|
@ -11,79 +11,125 @@
|
|||
|
||||
namespace controllers\internals;
|
||||
|
||||
class Webhook extends StandardController
|
||||
class Webhook extends StandardController
|
||||
{
|
||||
protected $bdd;
|
||||
protected $model;
|
||||
|
||||
/**
|
||||
* Create a new webhook.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $url : Webhook url
|
||||
* @param string $type : Webhook type
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
|
||||
*/
|
||||
public function create(int $id_user, string $url, string $type)
|
||||
{
|
||||
protected $bdd;
|
||||
protected $model;
|
||||
$webhook = [
|
||||
'id_user' => $id_user,
|
||||
'url' => $url,
|
||||
'type' => $type,
|
||||
];
|
||||
|
||||
/**
|
||||
* Create a new webhook.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $url : Webhook url
|
||||
* @param string $type : Webhook type
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
|
||||
*/
|
||||
public function create(int $id_user, string $url, string $type)
|
||||
$result = $this->get_model()->insert($webhook);
|
||||
if (!$result)
|
||||
{
|
||||
$webhook = [
|
||||
'id_user' => $id_user,
|
||||
'url' => $url,
|
||||
'type' => $type,
|
||||
return false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a webhook.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id : Webhook id
|
||||
* @param string $url : Webhook url
|
||||
* @param string $type : Webhook type
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id, string $url, string $type)
|
||||
{
|
||||
$datas = [
|
||||
'url' => $url,
|
||||
'type' => $type,
|
||||
];
|
||||
|
||||
return $this->get_model()->update_for_user($id_user, $id, $datas);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all webhooks for a user and for a type of webhook.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $type : Webhook type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_type_and_user(int $id_user, string $type)
|
||||
{
|
||||
return $this->get_model()->gets_for_type_and_user($id_user, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Webhook($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Trigger a webhook and transmit the signal to webhook daemon if needed.
|
||||
* @param int $user_id : User to trigger the webhook for
|
||||
* @param string $type : Type of webhook to trigger
|
||||
* @param array $sms : The sms [
|
||||
* int 'id' => SMS id,
|
||||
* string 'at' => SMS date,
|
||||
* string 'text' => sms body,
|
||||
* string 'origin' => sms origin (number or phone id)
|
||||
* string 'destination' => sms destination (number or phone id)
|
||||
* ]
|
||||
* @return bool : False if no trigger, true else
|
||||
*/
|
||||
private function trigger (int $user_id, string $type, array $sms)
|
||||
{
|
||||
$internal_setting = new Setting($this->bdd);
|
||||
$settings = $internal_setting->gets_for_user($user_id);
|
||||
|
||||
if (!$settings['webhook'] ?? false)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
$webhooks = $this->gets_for_type_and_user($id_user, $type);
|
||||
foreach ($webhooks as $webhook)
|
||||
{
|
||||
$message = [
|
||||
'url' => $webhook['url'],
|
||||
'datas' => [
|
||||
'webhook_type' => $webhook['type'],
|
||||
'id' => $sms['id'],
|
||||
'at' => $sms['at'],
|
||||
'text' => $sms['text'],
|
||||
'origin' => $sms['origin'],
|
||||
'destination' => $sms['destination'],
|
||||
],
|
||||
];
|
||||
|
||||
$result = $this->get_model()->insert($webhook);
|
||||
if (!$result)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update a webhook.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param int $id : Webhook id
|
||||
* @param string $url : Webhook url
|
||||
* @param string $type : Webhook type
|
||||
*
|
||||
* @return mixed bool|int : False if cannot create webhook, id of the new webhook else
|
||||
*/
|
||||
public function update_for_user(int $id_user, int $id, string $url, string $type)
|
||||
{
|
||||
$datas = [
|
||||
'url' => $url,
|
||||
'type' => $type,
|
||||
];
|
||||
|
||||
return $this->get_model()->update_for_user($id_user, $id, $datas);
|
||||
}
|
||||
|
||||
/**
|
||||
* Find all webhooks for a user and for a type of webhook.
|
||||
*
|
||||
* @param int $id_user : User id
|
||||
* @param string $type : Webhook type
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function gets_for_type_and_user(int $id_user, string $type)
|
||||
{
|
||||
return $this->get_model()->gets_for_type_and_user($id_user, $type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the model for the Controller.
|
||||
*
|
||||
* @return \descartes\Model
|
||||
*/
|
||||
protected function get_model(): \descartes\Model
|
||||
{
|
||||
$this->model = $this->model ?? new \models\Webhook($this->bdd);
|
||||
|
||||
return $this->model;
|
||||
$error_code = null;
|
||||
$queue = msg_get_queue(QUEUE_ID_WEBHOOK);
|
||||
$success = msg_send($queue, QUEUE_TYPE_WEBHOOK, $message, true, true, $error_code);
|
||||
return (bool) $success;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue