mirror of
https://github.com/RaspbianFrance/raspisms.git
synced 2025-04-20 16:37:48 +02:00
finally, we will just do limit checking all over again during sending phase
This commit is contained in:
parent
55fe91619b
commit
4f0c585f78
3 changed files with 58 additions and 65 deletions
|
@ -119,9 +119,9 @@ interface AdapterInterface
|
||||||
* @param array $medias : Array of medias to link to the MMS, [['http_url' => HTTP public url of the media et 'local_uri' => local uri to media file]]
|
* @param array $medias : Array of medias to link to the MMS, [['http_url' => HTTP public url of the media et 'local_uri' => local uri to media file]]
|
||||||
*
|
*
|
||||||
* @return array : [
|
* @return array : [
|
||||||
* bool 'error' => false if no error, true else
|
* bool 'error' => false if no error, true else,
|
||||||
* ?string 'error_message' => null if no error, else error message
|
* ?string 'error_message' => null if no error, else error message,
|
||||||
* array 'uid' => Uid of the sms created on success
|
* array 'uid' => Uid of the sms created on success,
|
||||||
* ]
|
* ]
|
||||||
*/
|
*/
|
||||||
public function send(string $destination, string $text, bool $flash = false, bool $mms = false, array $medias = []): array;
|
public function send(string $destination, string $text, bool $flash = false, bool $mms = false, array $medias = []): array;
|
||||||
|
|
|
@ -651,31 +651,13 @@ use Monolog\Logger;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// This should only happen if the user try to send a message without any phone in his account, then we simply ignore.
|
||||||
$id_phone = $phone_to_use['id'] ?? $random_phone['id'] ?? null;
|
if (!$random_phone && !$phone_to_use)
|
||||||
|
|
||||||
$forcefail = false;
|
|
||||||
if (!$id_phone) // This should only happen if the user try to send a message without any phone
|
|
||||||
{
|
{
|
||||||
$forcefail = true;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($users_phones[$id_user][$id_phone] ?? false)
|
$id_phone = $phone_to_use['id'] ?? $random_phone['id'];
|
||||||
{
|
|
||||||
if ($users_phones[$id_user][$id_phone]['remaining_volume'] <= 0)
|
|
||||||
{
|
|
||||||
$forcefail = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Consume one sms from remaining volume of phone, dont forget to do the same for the entry in mms phones
|
|
||||||
$users_phones[$id_user][$id_phone]['remaining_volume'] --;
|
|
||||||
if ($users_mms_phones[$id_user][$id_phone] ?? false)
|
|
||||||
{
|
|
||||||
$users_mms_phones[$id_user][$id_phone] --;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
$sms_per_scheduled[$id_scheduled][] = [
|
$sms_per_scheduled[$id_scheduled][] = [
|
||||||
'id_user' => $id_user,
|
'id_user' => $id_user,
|
||||||
'id_scheduled' => $id_scheduled,
|
'id_scheduled' => $id_scheduled,
|
||||||
|
@ -685,10 +667,14 @@ use Monolog\Logger;
|
||||||
'mms' => $scheduled['mms'],
|
'mms' => $scheduled['mms'],
|
||||||
'medias' => $scheduled['medias'],
|
'medias' => $scheduled['medias'],
|
||||||
'text' => $text,
|
'text' => $text,
|
||||||
'forcefail' => $forcefail,
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
// Consume one sms from remaining volume of phone, dont forget to do the same for the entry in mms phones
|
||||||
|
$users_phones[$id_user][$id_phone]['remaining_volume'] --;
|
||||||
|
if ($users_mms_phones[$id_user][$id_phone] ?? false)
|
||||||
|
{
|
||||||
|
$users_mms_phones[$id_user][$id_phone] --;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -253,17 +253,6 @@ namespace controllers\internals;
|
||||||
'error_message' => null,
|
'error_message' => null,
|
||||||
];
|
];
|
||||||
|
|
||||||
//If we reached our max quota, do not send the message
|
|
||||||
$internal_quota = new Quota($this->bdd);
|
|
||||||
$nb_credits = $internal_quota::compute_credits_for_message($text); //Calculate how much credit the message require
|
|
||||||
if (!$internal_quota->has_enough_credit($id_user, $nb_credits))
|
|
||||||
{
|
|
||||||
$return['error'] = false;
|
|
||||||
$return['error_message'] = 'Not enough credit to send message.';
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
|
||||||
|
|
||||||
$at = (new \DateTime())->format('Y-m-d H:i:s');
|
$at = (new \DateTime())->format('Y-m-d H:i:s');
|
||||||
$media_uris = [];
|
$media_uris = [];
|
||||||
foreach ($medias as $media)
|
foreach ($medias as $media)
|
||||||
|
@ -286,38 +275,56 @@ namespace controllers\internals;
|
||||||
$text .= "\n" . join(' - ', $media_urls);
|
$text .= "\n" . join(' - ', $media_urls);
|
||||||
}
|
}
|
||||||
|
|
||||||
$response = $adapter->send($destination, $text, $flash, $mms, $media_uris);
|
//If we reached our max quota, do not send the message
|
||||||
|
$internal_quota = new Quota($this->bdd);
|
||||||
if ($response['error'])
|
$nb_credits = $internal_quota::compute_credits_for_message($text); //Calculate how much credit the message require
|
||||||
|
if (!$internal_quota->has_enough_credit($id_user, $nb_credits))
|
||||||
{
|
{
|
||||||
$return['error'] = true;
|
$return['error'] = true;
|
||||||
$return['error_message'] = $response['error_message'];
|
$return['error_message'] = 'Not enough credit to send message.';
|
||||||
$status = \models\Sended::STATUS_FAILED;
|
|
||||||
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $originating_scheduled, $status);
|
|
||||||
|
|
||||||
$sended = [
|
|
||||||
'id' => $sended_id,
|
|
||||||
'at' => $at,
|
|
||||||
'status' => $status,
|
|
||||||
'text' => $text,
|
|
||||||
'destination' => $destination,
|
|
||||||
'origin' => $id_phone,
|
|
||||||
'mms' => $mms,
|
|
||||||
'medias' => $medias,
|
|
||||||
'originating_scheduled' => $originating_scheduled,
|
|
||||||
];
|
|
||||||
|
|
||||||
$internal_webhook = new Webhook($this->bdd);
|
|
||||||
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND_SMS, $sended);
|
|
||||||
|
|
||||||
return $return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$internal_quota->consume_credit($id_user, $nb_credits);
|
//If we reached limit for this phone, do not send the message
|
||||||
|
$internal_phone = new Phone($this->bdd);
|
||||||
|
$internal_sended = new Sended($this->bdd);
|
||||||
|
$limits = $internal_phone->get_limits($id_phone);
|
||||||
|
|
||||||
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $response['uid'] ?? uniqid(), $adapter->meta_classname(), $flash, $mms, $medias, $originating_scheduled, $status);
|
$remaining_volume = PHP_INT_MAX;
|
||||||
|
foreach ($limits as $limit)
|
||||||
|
{
|
||||||
|
$startpoint = new \DateTime($limit['startpoint']);
|
||||||
|
$consumed = $internal_sended->count_since_for_phone_and_user($id_user, $id_phone, $startpoint);
|
||||||
|
$remaining_volume = min(($limit['volume'] - $consumed), $remaining_volume);
|
||||||
|
}
|
||||||
|
|
||||||
$sended = [
|
if ($remaining_volume < 1)
|
||||||
|
{
|
||||||
|
$return['error'] = true;
|
||||||
|
$return['error_message'] = 'Phone send limit have been reached.';
|
||||||
|
}
|
||||||
|
|
||||||
|
$uid = uniqid();
|
||||||
|
if (!$return['error'])
|
||||||
|
{
|
||||||
|
$response = $adapter->send($destination, $text, $flash, $mms, $media_uris);
|
||||||
|
$uid = $response['uid'] ?? $uid;
|
||||||
|
|
||||||
|
if ($response['error'])
|
||||||
|
{
|
||||||
|
$return['error'] = true;
|
||||||
|
$return['error_message'] = $response['error_message'];
|
||||||
|
}
|
||||||
|
else // If send with success, consume credit
|
||||||
|
{
|
||||||
|
$internal_quota->consume_credit($id_user, $nb_credits);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If we fail to send or not, we will always save message as sended, only the status will change.
|
||||||
|
$status = $return['error'] ? \models\Sended::STATUS_FAILED : \models\Sended::STATUS_UNKNOWN;
|
||||||
|
$sended_id = $this->create($id_user, $id_phone, $at, $text, $destination, $uid, $adapter->meta_classname(), $flash, $mms, $medias, $originating_scheduled, $status);
|
||||||
|
|
||||||
|
$webhook_body = [
|
||||||
'id' => $sended_id,
|
'id' => $sended_id,
|
||||||
'at' => $at,
|
'at' => $at,
|
||||||
'status' => $status,
|
'status' => $status,
|
||||||
|
@ -330,7 +337,7 @@ namespace controllers\internals;
|
||||||
];
|
];
|
||||||
|
|
||||||
$internal_webhook = new Webhook($this->bdd);
|
$internal_webhook = new Webhook($this->bdd);
|
||||||
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND_SMS, $sended);
|
$internal_webhook->trigger($id_user, \models\Webhook::TYPE_SEND_SMS, $webhook_body);
|
||||||
|
|
||||||
return $return;
|
return $return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue