adds generalized webhooks, updates settings page, translations, validation
This commit is contained in:
@@ -12,6 +12,7 @@ class SlackSettingsForm extends Component
|
||||
public $webhook_channel;
|
||||
public $webhook_botname;
|
||||
public $isDisabled ='disabled' ;
|
||||
public $webhook_name;
|
||||
public $webhook_link;
|
||||
public $webhook_placeholder;
|
||||
public $webhook_icon;
|
||||
@@ -20,31 +21,38 @@ class SlackSettingsForm extends Component
|
||||
public Setting $setting;
|
||||
|
||||
protected $rules = [
|
||||
'webhook_endpoint' => 'url|required_with:slack_channel|starts_with:https://hooks.slack.com/|nullable',
|
||||
'webhook_channel' => 'required_with:slack_endpoint|starts_with:#|nullable',
|
||||
'webhook_endpoint' => 'url|required_with:webhook_channel|starts_with:https://hooks.slack.com/services,https://discord.com/api/webhooks|nullable',
|
||||
'webhook_channel' => 'required_with:webhook_endpoint|starts_with:#|nullable',
|
||||
'webhook_botname' => 'string|nullable',
|
||||
];
|
||||
static $webhook_text= [
|
||||
"Slack" => array(
|
||||
"name" => "Slack",
|
||||
"icon" => 'fab fa-slack',
|
||||
"placeholder" => "https://hooks.slack.com/services/XXXXXXXXXXXXXXXXXXXXX",
|
||||
"link" => 'https://api.slack.com/messaging/webhooks'
|
||||
"link" => 'https://api.slack.com/messaging/webhooks',
|
||||
),
|
||||
"Discord" => array(
|
||||
"name" => "Discord",
|
||||
"icon" => 'fab fa-discord',
|
||||
"placeholder" => "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXX",
|
||||
"link" => 'https://support.discord.com/hc/en-us/articles/360045093012-Server-Integrations-Page'
|
||||
"link" => 'https://support.discord.com/hc/en-us/articles/360045093012-Server-Integrations-Page',
|
||||
),
|
||||
"RocketChat"=> array(
|
||||
"icon" => 'fab fa-rocketchat',
|
||||
"placeholder" => "https://discord.com/api/webhooks/XXXXXXXXXXXXXXXXXXXXX",
|
||||
"link" => '',
|
||||
"General"=> array(
|
||||
"name" => "General",
|
||||
"icon" => "fab fa-hashtag",
|
||||
"placeholder" => "",
|
||||
"link" => "",
|
||||
),
|
||||
];
|
||||
|
||||
public function mount(){
|
||||
|
||||
$this->setting = Setting::getSettings();
|
||||
$this->webhook_selected = $this->setting->webhook_selected;
|
||||
$this->webhook_placeholder = self::$webhook_text[$this->setting->webhook_selected]["placeholder"];
|
||||
$this->webhook_name = self::$webhook_text[$this->setting->webhook_selected]["name"];
|
||||
$this->webhook_icon = self::$webhook_text[$this->setting->webhook_selected]["icon"];
|
||||
$this->webhook_endpoint = $this->setting->webhook_endpoint;
|
||||
$this->webhook_channel = $this->setting->webhook_channel;
|
||||
$this->webhook_botname = $this->setting->webhook_botname;
|
||||
@@ -53,11 +61,12 @@ class SlackSettingsForm extends Component
|
||||
|
||||
}
|
||||
public function updated($field){
|
||||
// $this->webhook_selected = $this->webhook_options;
|
||||
$this->validateOnly($field ,$this->rules);
|
||||
if($this->webhook_selected != 'General') {
|
||||
$this->validateOnly($field, $this->rules);
|
||||
}
|
||||
}
|
||||
public function updatedWebhookSelected(){
|
||||
|
||||
$this->webhook_name = self::$webhook_text[$this->webhook_selected]['name'];
|
||||
$this->webhook_icon = self::$webhook_text[$this->webhook_selected]["icon"]; ;
|
||||
$this->webhook_placeholder = self::$webhook_text[$this->webhook_selected]["placeholder"];
|
||||
$this->webhook_link = self::$webhook_text[$this->webhook_selected]["link"];
|
||||
@@ -77,7 +86,7 @@ class SlackSettingsForm extends Component
|
||||
|
||||
public function testWebhook(){
|
||||
|
||||
$slack = new Client([
|
||||
$webhook = new Client([
|
||||
'base_url' => e($this->webhook_endpoint),
|
||||
'defaults' => [
|
||||
'exceptions' => false,
|
||||
@@ -87,36 +96,39 @@ class SlackSettingsForm extends Component
|
||||
$payload = json_encode(
|
||||
[
|
||||
'channel' => e($this->webhook_channel),
|
||||
'text' => trans('general.slack_test_msg'),
|
||||
'text' => trans('general.webhook_test_msg'),
|
||||
'username' => e($this->webhook_botname),
|
||||
'icon_emoji' => ':heart:',
|
||||
]);
|
||||
|
||||
try {
|
||||
$slack->post($this->webhook_endpoint, ['body' => $payload]);
|
||||
$webhook->post($this->webhook_endpoint, ['body' => $payload]);
|
||||
$this->isDisabled='';
|
||||
return session()->flash('success' , 'Your Slack Integration works!');
|
||||
return session()->flash('success' , 'Your'.$this->webhook_name.' Integration works!');
|
||||
|
||||
} catch (\Exception $e) {
|
||||
$this->isDisabled= 'disabled';
|
||||
return session()->flash('error' , trans('admin/settings/message.slack.error', ['error_message' => $e->getMessage()]));
|
||||
return session()->flash('error' , trans('admin/settings/message.webhook.error', ['error_message' => $e->getMessage()]));
|
||||
}
|
||||
|
||||
//}
|
||||
return session()->flash('message' , trans('admin/settings/message.slack.error_misc'));
|
||||
return session()->flash('message' , trans('admin/settings/message.webhook.error_misc'));
|
||||
|
||||
|
||||
|
||||
}
|
||||
public function submit()
|
||||
{
|
||||
$this->validate($this->rules);
|
||||
if($this->webhook_selected != 'General') {
|
||||
$this->validate($this->rules);
|
||||
}
|
||||
|
||||
$this->setting->webhook_selected = $this->webhwebhook_selected;
|
||||
$this->setting->webhook_selected = $this->webhook_selected;
|
||||
$this->setting->webhook_endpoint = $this->webhook_endpoint;
|
||||
$this->setting->webhook_channel = $this->webhook_channel;
|
||||
$this->setting->webhook_botname = $this->webhook_botname;
|
||||
|
||||
|
||||
$this->setting->save();
|
||||
|
||||
session()->flash('save',trans('admin/settings/message.update.success'));
|
||||
|
||||
Reference in New Issue
Block a user