Moved notification settings to DB

This commit is contained in:
Henry Whitaker
2020-07-07 20:51:57 +01:00
parent f543e1345c
commit 4ef0303dff
25 changed files with 837 additions and 112 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Helpers;
use App\Events\TestNotificationEvent;
use App\Setting;
use Carbon\Carbon;
@@ -38,10 +39,11 @@ class SettingsHelper {
{
$setting = SettingsHelper::get($name);
if($value == false) {
$value = "0";
}
if($setting !== false) {
if($value == false) {
$value = "0";
}
$setting->value = $value;
$setting->save();
} else {
@@ -74,4 +76,75 @@ class SettingsHelper {
}
return $base;
}
/**
* Check whether a setting is defined in ENV vars or through DB
*
* @param string $key
* @return boolean
*/
public static function settingIsEditable(string $key)
{
// Try exact key
$val = exec('echo $' . $key);
if($val == "") {
return true;
}
// Try key all caps
$val = exec('echo $' . strtoupper($key));
if($val == "") {
return true;
}
return false;
}
/**
* Get the application config
*
* @return array
*/
public static function getConfig()
{
return [
'base' => SettingsHelper::getBase(),
'graphs' => [
'download_upload_graph_enabled' => SettingsHelper::get('download_upload_graph_enabled'),
'download_upload_graph_width' => SettingsHelper::get('download_upload_graph_width'),
'ping_graph_enabled' => SettingsHelper::get('ping_graph_enabled'),
'ping_graph_width' => SettingsHelper::get('ping_graph_width'),
'failure_graph_enabled' => SettingsHelper::get('failure_graph_enabled'),
'failure_graph_width' => SettingsHelper::get('failure_graph_width'),
],
'editable' => [
'slack_webhook' => SettingsHelper::settingIsEditable('slack_webhook'),
'telegram_bot_token' => SettingsHelper::settingIsEditable('telegram_bot_token'),
'telegram_chat_id' => SettingsHelper::settingIsEditable('telegram_chat_id'),
]
];
}
/**
* Send test notification to agents
*
* @param boolean|string $agent
* @return void
*/
public static function testNotification($agent = true)
{
$agents = [ 'slack', 'telegram' ];
if($agent === true) {
event(new TestNotificationEvent($agents));
return true;
}
if(in_array($agent, $agents)) {
event(new TestNotificationEvent([ $agent ]));
return true;
}
}
}