Add settings to enable/disable phone priority and phone limits

This commit is contained in:
osaajani 2023-02-20 15:48:47 +01:00
parent 4ea624b0d9
commit b825bd6d6e
7 changed files with 461 additions and 324 deletions

View file

@ -669,32 +669,46 @@ use Monolog\Logger;
$phones_subset = $mms_only ?: $phones_subset;
}
// Keep only phones with remaining volume and available status
// Keep only available phones
$remaining_volume_phones = array_filter($phones_subset, function ($phone) {
return $phone['remaining_volume'] > 0 && $phone['status'] == \models\Phone::STATUS_AVAILABLE;
return $phone['status'] == \models\Phone::STATUS_AVAILABLE;
});
$phones_subset = $remaining_volume_phones ?: $phones_subset;
$max_priority_phones = [];
$max_priority = PHP_INT_MIN;
foreach ($phones_subset as $phone)
// Keep only phones with remaining volume
if ((int) ($users_settings[$id_user]['phone_limit'] ?? false))
{
if ($phone['priority'] < $max_priority)
{
continue;
}
elseif ($phone['priority'] == $max_priority)
{
$max_priority_phones[] = $phone;
}
elseif ($phone['priority'] > $max_priority)
{
$max_priority_phones = [$phone];
$max_priority = $phone['priority'];
}
$remaining_volume_phones = array_filter($phones_subset, function ($phone) {
return $phone['remaining_volume'] > 0;
});
$phones_subset = $remaining_volume_phones ?: $phones_subset;
}
$phones_subset = $max_priority_phones;
if ((int) ($users_settings[$id_user]['phone_priority'] ?? false))
{
$max_priority_phones = [];
$max_priority = PHP_INT_MIN;
foreach ($phones_subset as $phone)
{
if ($phone['priority'] < $max_priority)
{
continue;
}
elseif ($phone['priority'] == $max_priority)
{
$max_priority_phones[] = $phone;
}
elseif ($phone['priority'] > $max_priority)
{
$max_priority_phones = [$phone];
$max_priority = $phone['priority'];
}
}
$phones_subset = $max_priority_phones;
}
if ($phones_subset)
{
$random_phone = $phones_subset[array_rand($phones_subset)];

View file

@ -255,6 +255,9 @@ use Exception;
'error_message' => null,
];
$internal_setting = new Setting();
$user_settings = $internal_setting->gets_for_user($id_user);
$at = (new \DateTime())->format('Y-m-d H:i:s');
$media_uris = [];
foreach ($medias as $media)
@ -295,20 +298,23 @@ use Exception;
throw new Exception('Invalid phone status : ' . $phone['status']);
}
//If we reached limit for this phone, do not send the message
$limits = $internal_phone->get_limits($id_phone);
$remaining_volume = PHP_INT_MAX;
foreach ($limits as $limit)
//If we reached limit for this phone and phone limits are enabled, do not send the message
if ((int) ($users_settings['phone_limit'] ?? false))
{
$startpoint = new \DateTime($limit['startpoint']);
$consumed = $this->count_since_for_phone_and_user($id_user, $id_phone, $startpoint);
$remaining_volume = min(($limit['volume'] - $consumed), $remaining_volume);
}
$limits = $internal_phone->get_limits($id_phone);
if ($remaining_volume < 1)
{
throw new Exception('Phone send limit have been reached.');
$remaining_volume = PHP_INT_MAX;
foreach ($limits as $limit)
{
$startpoint = new \DateTime($limit['startpoint']);
$consumed = $this->count_since_for_phone_and_user($id_user, $id_phone, $startpoint);
$remaining_volume = min(($limit['volume'] - $consumed), $remaining_volume);
}
if ($remaining_volume < 1)
{
throw new Exception('Phone send limit have been reached.');
}
}
$response = $adapter->send($destination, $text, $flash, $mms, $media_uris);